1.0.5 • Published 2 years ago
suorm v1.0.5
SUORM - Surreal.db ORM
A super simple query builder for surreal.db
Installation
npm:
npm install suormyarn:
yarn add suormExample
Define Table
import { 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;
*/Select From
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;
*/⚠️ Warning
Not stable yet
A lot of stuff is not implemented yet
No assertion builder
No permission builder
Doesn't support GEO types
Selection
ORDERandFETCHare not implemented yet
Usage
Exports
- suorm -
new QueryBuilder() - QueryBuilder
- TableBuilder
- FieldBuilder
- IndexBuilder
- SelectBuilder
Example:
// Common
const {
suorm,
QueryBuilder,
TableBuilder,
FieldBuilder,
IndexBuilder,
SelectBuilder
} = require("suorm");
// TypeScript / ESM
import {
suorm,
QueryBuilder,
TableBuilder,
FieldBuilder,
IndexBuilder,
SelectBuilder
} from "suorm";Classes
QueryBuilder
get define(): Object;
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;select(...projection: string[]): SelectBuilder;
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;TableBuilder
name(name: string): this;
Example:
table.name("user");schemafull(opt: boolean = true): this;
Example:
table.schemafull();
// or schemaless (default)
table.schemafull(false);drop(opt: boolean = true): this;
Example:
table.drop();addField(callback: FieldBuilder | ((data: FieldBuilder) => FieldBuilder)): this;
Example:
table.addField((f) => f.name("email").type("string"));
// or
table.addField(new FieldBuilder().name("email").type("string"));addIndex(callback: IndexBuilder | ((data: IndexBuilder): this;
Example:
table.addIndex((i) => i.name("email_index").columns("email"));
// or
table.addField(new IndexBuilder().name("email_index").columns("email"));get build(): string;
Example:
// String Output
table.build;FieldBuilder
name(name: string): this;
Example:
field.name("email");tableName(name: string): this;
Example:
field.tableName("user");type(type: FieldDataTypes): this;
Example:
field.type("bool");value(val: string | number, or?: string | number, ignoreDefaultValues: boolean = false): this;
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("foobar@example.com"); // VALUE "foobar@example.com"
field.value("$value", "none"); // VALUE $value OR NONE
field.value("$value", "none", true); // VALUE $value OR "none"assert(exp: string): this;
Example:
field.assert("$value != NONE");get build(): string;
Example:
// String Output
field.build;IndexBuilder
name(name: string): this;
Example:
index.name("email_index");unique(opt: boolean = true): this;
Example:
index.unique();columns(...cols: string[]): this;
Example:
index.columns("email");
// or
index.columns("foo", "bar");get build(): string;
Example:
// String Output
index.build;SelectBuilder
constructor(projection: string[])
Example:
const select = new SelectBuilder(["email", "username"]);from(name: string): this;
Example:
// SELECT foobar FROM user
select.from("user");where(con: string): this;
Example:
select.where("email IS NOT NONE");
// or
select.where("email != NONE");as(con: string): this;
Example:
// SELECT email AS address...
select.as("address");split(...field: string[]): this;
Example:
// SELECT * FROM user SPLIT email;
select.split("email");
// SELECT * FROM user SPLIT email,username;
select.split("email", "username");group(...field: string[]): this;
Example:
// SELECT * FROM user GROUP BY email;
select.group("email");
// SELECT * FROM user GROUP BY email,username;
select.group("email", "username");limit(lim: number): this;
Example:
// SELECT * FROM user LIMIT 50;
select.limit(50);timeout(time: string): this;
Example:
// SELECT * FROM user TIMEOUT 5s;
select.timeout("5s");start(lim: number): this;
Example:
// SELECT * FROM user LIMIT 50 START 10;
select.start(10);parallel(opt: boolean = true): this;
Example:
// SELECT * FROM user PARALLEL;
select.parallel();get build(): string;
Example:
// String Output
select.build;Types
FieldDataTypes:
"string" | "number" | "object" | "any" | "array" | "bool" | "datetime" | "decimal" | "duration" | "float" | "int" | "record"
DefaultValues
"NONE" | "NULL" | "TRUE" | "FALSE" | "none" | "null" | "true" | "false"