The builder writes the same queries as Rindle SQL, as typed TypeScript. Results are typed from your schema, and a misspelled table or column is a type error.
import {
createSchema, createWasmStore, gt, number, string, boolean, table,
} from "@rindle/wasm";
const issue = table("issue")
.columns({
id: number(),
title: string(),
priority: number(),
closed: boolean(),
})
.primaryKey("id");
const comment = table("comment")
.columns({ id: number(), issueId: number(), body: string() })
.primaryKey("id");
const schema = createSchema({ tables: [issue, comment] });
const store = await createWasmStore(schema);
const view = store.query.issue
.where.closed(false)
.where.priority(gt(3))
.orderBy("id", "desc")
.limit(50)
.sub(
"comments",
comment,
{ parent: ["id"], child: ["issueId"] },
(c) => c.orderBy("id", "desc").limit(10),
)
.materialize();
view.subscribe((rows) => console.log(rows));The same query in SQL:
SELECT * FROM issue
WHERE closed = 0 AND priority > 3
ORDER BY id DESC LIMIT 50
WITH comments OF comment ON issueId = id (ORDER BY id DESC LIMIT 10)Schema
table(name).columns({ … }).primaryKey(…cols) |
A table. |
string(), number(), boolean(), int64(), json<T>() |
Column types. Add .nullable() for a column that can be NULL. |
createSchema({ tables }) |
The schema. |
rel(parent, child, { parentCol: "childCol" }) |
A relationship, to reuse in sub, countAs and exists. Group them with defineRelationships({ … }). |
int64() is an exact 64-bit integer (BIGINT), and its value is always a bigint: 5n, never
5, however small. Rows, literals, cursors and mutator arguments all carry it exactly, so two ids
one apart past 2^53 are two rows in order.
const account = table("account").columns({ id: int64(), name: string() }).primaryKey("id");
store.query.account.where.id(gt(9007199254740993n)).orderBy("id", "asc");Two things about bigint itself will catch you:
JSON.stringifythrows on one. Rindle’s own requests carry it for you; convert it yourself (String(row.id)) before you put a row in your own JSON.bigint + numberthrows. Writerow.total + 1n, notrow.total + 1.
A write takes a bigint, or a number that is an exact integer. A number() column takes a
bigint only if it fits a double. Anything that would have to round is refused, in the browser
and on the server alike. A countAs is a number.
Building a Query
store.query.<table> starts a query. Every method returns a new query.
| Method | SQL |
|---|---|
.where(condition) |
WHERE. Calls are combined with AND. |
.where.<column>(value) |
WHERE column = value, or pass an operator: .where.priority(gt(3)) |
.orderBy(column, "asc" | "desc") |
ORDER BY |
.limit(n) |
LIMIT |
.start(row, { inclusive }) |
WHERE (cols) > (…): the page after row. { inclusive: true } includes row (>=). |
.select(...columns) |
A column list. Also takes JSON paths. |
.one() |
LIMIT 1. The view’s data is a row or null. |
.sub(name, table, correlation, build?) |
WITH name OF table ON … |
.countAs(name, table, correlation, build?) |
A live count of related rows on each row |
.count(), .groupBy(column), .having(…) |
Aggregates |
.materialize() |
Starts the live query. |
.ast() |
The query as JSON. |
A correlation is { parent: [...], child: [...] }: the parent columns, and the child columns
they match. A relationship from rel can stand in for the table and correlation.
Conditions
| Function | SQL |
|---|---|
eq, ne, gt, ge, lt, le |
=, !=, >, >=, <, <= |
is, isNot, isNull, isNotNull |
IS, IS NOT, IS NULL, IS NOT NULL |
like, notLike, ilike, notIlike |
LIKE, NOT LIKE, ILIKE, NOT ILIKE |
inList, notInList |
IN, NOT IN |
and(...), or(...) |
AND, OR |
exists(table, correlation, build?), notExists(…) |
EXISTS, NOT EXISTS |
import { exists, or } from "@rindle/wasm";
store.query.issue.where(
or(
issue.priority(gt(8)),
exists(comment, { parent: ["id"], child: ["issueId"] }),
),
);A table doubles as a condition factory: issue.priority(gt(8)) is the condition
priority > 8.
JSON Paths
jsonPath(column, ...path) names a value inside a json() column. A string step is an object
key and a number is an array index. Use it in select, where and orderBy:
import { jsonPath } from "@rindle/client";
store.query.issue
.select("id", jsonPath("body", "labels"))
.where(jsonPath("body", "status"), "open")
.orderBy(jsonPath("body", "priority"), "desc");Selecting a path and not its column returns a partial document, { id, body: { labels: [...] } },
and the server sends only that part. This is how to read a small piece of a large document.
The row’s type makes every part of body optional.
where(path, value)takes a value or an operator such asgt(3). Insideor()andand()usepathCond(path, value).- A comparison is type-strict:
"5"does not match the number5. A JSONnull, an array or an object at the path compares as no value. - A row whose document is SQL
NULLcomes back withbody: {}.
Looking Inside a Document
These operators test a json() column, or a path into one, as Postgres’s jsonb operators do:
| Function | SQL | True when |
|---|---|---|
contains(value) |
@> |
the document has every key and array element of value |
containedBy(value) |
<@ |
every part of the document is in value |
hasKey(key) |
? |
key is a key of the object, or a string in the array |
hasAnyKey(keys), hasAllKeys(keys) |
?|, ?& |
any, or all, of keys are |
import { contains, jsonPath } from "@rindle/client";
// On the whole document: the client syncs the document.
store.query.issue.where.body(contains({ labels: ["urgent"] }));
// On a path: the client syncs only that value.
store.query.issue.where(jsonPath("body", "labels"), contains(["urgent"]));Arrays match as sets: order and repeats do not matter.
On the server, contains finds its rows in an inverted index
where the table has one. Without it, and for the other three always, the test reads every row of
the table, so it is slow on a large one.
Nothing declares a path. A query can name any path, and the engine reads its value out of the document as a row enters the query, on the server and in the browser alike.
Views
materialize() returns a view:
data |
The current rows. |
subscribe(listener) |
Calls listener(rows) now and after every change. Returns an unsubscribe function. |
destroy() |
Stops the query. |