Skip to main content

Query Builder

Surrealize provides you with the most commonly used statements for working with your database. Currently, the following statements are fully or partially supported:

StatementStatusDescription
SELECT⚠️Select data from the database
CREATECreate a new record
UPDATEUpdate an existing record
UPSERTUpsert a record
RELATE🚫Relate records
DELETEDelete records

✅ = fully supported
⚠️ = partially supported
🚫 = not supported

Basic usage

To use a statement, you first need to create a Statements instance.

import { Surrealize, q } from "surrealize";

const surrealize = new Surrealize();

// create statements using the `q` object
const query = q.select().from("users").toQuery();

Typed usage

You can also create typed statements which provides you better type safety.

To create typed statements, you need to initialize a new Statements instance with a schema.

import { Statements } from "surrealize";
import { z } from "zod";

/**
* We use `zod` here to define a schema
* but you can use any schema library you like.
*/
const someSchema = z.object({
id: z.string(),
name: z.string(),
});

const statements = new Statements({
schema: someSchema,
});

// now you have typed statements.
const query = statements
.select()
.from("users")
// 👇 auto completes the fields
.where(eq("name", "John"))
.toQuery();