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:
| Statement | Status | Description |
|---|---|---|
SELECT | ⚠️ | Select data from the database |
CREATE | ✅ | Create a new record |
UPDATE | ✅ | Update an existing record |
UPSERT | ✅ | Upsert a record |
RELATE | 🚫 | Relate records |
DELETE | ✅ | Delete 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();