A super simple query builder for surreal.db
npm:
npm install suormyarn:
yarn add suormimport { suorm } from "suorm";
const userTable = suorm.define.table
.name("user")
.schemafull()
.addField((f) =>
f.name("email").type("string").assert("$value != NONE && is::email($value)")
)
.addIndex((i) => i.name("email_index").unique().columns("email")).build;
/**
* userTable: DEFINE TABLE user SCHEMAFULL;DEFINE FIELD email ON TABLE user TYPE string ASSERT $value != NONE && is::email($value);DEFINE INDEX email_index ON TABLE user COLUMNS email UNIQUE;
*/import { suorm } from "suorm";
const emailData = suorm
.select("email")
.as("address")
.from("user")
.where("email != NONE")
.limit(10)
.start(5).build;
/**
* emailData: SELECT email AS address FROM user WHERE email != NONE LIMIT 10 START 5;
*/- suorm -
new QueryBuilder() - QueryBuilder
- TableBuilder
- FieldBuilder
- IndexBuilder
- SelectBuilder
// Common
const {
suorm,
QueryBuilder,
TableBuilder,
FieldBuilder,
IndexBuilder,
SelectBuilder
} = require("suorm");
// TypeScript / ESM
import {
suorm,
QueryBuilder,
TableBuilder,
FieldBuilder,
IndexBuilder,
SelectBuilder
} from "suorm";Returns:
- table: TableBuilder
- field: FieldBuilder
- index: IndexBuilder
Example:
// Table
suorm.define.table.name("user").schemafull().build;
// Field
suorm.define.field.name("email").type("string").assert("$value != NON").build;
// Index
suorm.define.index.name("email_index").unique().columns("email").build;Returns:
- Instance of SelectBuilder
Example:
// SELECT * FROM user;
suorm.select("*").from("user").build;
// SELECT email,username FROM user;
suorm.select("email", "username").from("user").build;Example:
table.name("user");Example:
table.schemafull();
// or schemaless (default)
table.schemafull(false);Example:
table.drop();Example:
table.addField((f) => f.name("email").type("string"));
// or
table.addField(new FieldBuilder().name("email").type("string"));Example:
table.addIndex((i) => i.name("email_index").columns("email"));
// or
table.addField(new IndexBuilder().name("email_index").columns("email"));Example:
// String Output
table.build;Example:
field.name("email");Example:
field.tableName("user");Example:
field.type("bool");Description:
- Rule 1:
val/orgets converted into UPPERCASE and an operator if is one ofDefaultValues, e.g.field.value("none")=NONE(not"none") - Rule 2: If
val/orstarts with$(dollar sign) it won't be a string anymore, e.g.field.value("$value")=$value(not"$value") - Rule 3: If
val/oris an number/boolean/bigint it won't be converted into an string and it will be in UPPERCASE, e.g.field.value(true)=TRUE(not"true") andfield.value(69)=69(not"69") - Rule 4: If
val/oris an valid JS object, it will be automatically converted into an json string using JSON.stringify, e.g.field.value({ foo: "bar" })='{"foo":"bar"}'
If ignoreDefaultValues is set to true:
- Rule 1 and Rule 2 wont apply anymore, e.g.
field.value("none", null, true)="none"(notNONE) andfield.value("$value", null, true)="$value"(not$value) - Rule 3 won't apply anymore too, e.g.
field.value(true)="true"(notTRUE) andfield.value(69)="69"(not69)
Example:
field.value("[email protected]"); // VALUE "[email protected]"
field.value("$value", "none"); // VALUE $value OR NONE
field.value("$value", "none", true); // VALUE $value OR "none"Example:
field.assert("$value != NONE");Example:
// String Output
field.build;Example:
index.name("email_index");Example:
index.unique();Example:
index.columns("email");
// or
index.columns("foo", "bar");Example:
// String Output
index.build;Example:
const select = new SelectBuilder(["email", "username"]);Example:
// SELECT foobar FROM user
select.from("user");Example:
select.where("email IS NOT NONE");
// or
select.where("email != NONE");Example:
// SELECT email AS address...
select.as("address");Example:
// SELECT * FROM user SPLIT email;
select.split("email");
// SELECT * FROM user SPLIT email,username;
select.split("email", "username");Example:
// SELECT * FROM user GROUP BY email;
select.group("email");
// SELECT * FROM user GROUP BY email,username;
select.group("email", "username");Example:
// SELECT * FROM user LIMIT 50;
select.limit(50);Example:
// SELECT * FROM user TIMEOUT 5s;
select.timeout("5s");Example:
// SELECT * FROM user LIMIT 50 START 10;
select.start(10);Example:
// SELECT * FROM user PARALLEL;
select.parallel();Example:
// String Output
select.build;"string" | "number" | "object" | "any" | "array" | "bool" | "datetime" | "decimal" | "duration" | "float" | "int" | "record"
"NONE" | "NULL" | "TRUE" | "FALSE" | "none" | "null" | "true" | "false"