t
The type builders for key fields, columns, handler arguments, results and pushes, their modifiers, and how each value travels as JSON.
t holds the type builders that describe key fields, table columns, handler arguments and results, pushes, events and stream entries.
import { defineLuna, t } from "luna";
export const Card = defineLuna({
name: "Card",
key: { board: t.string(), card: t.int() },
handlers: (h) => ({
move: h({ args: { to: t.enum(["todo", "doing", "done"]), note: t.string().optional() } }, (ctx, { to, note }) => {
console.log(ctx.key.card, to, note ?? "");
}),
}),
});
Reference
Builders
import { t, type Infer } from "luna";
t.string(opts?)(opts?: { maxLength?: number }) => Ty<string>
Text, at most maxLength characters when given.
(opts?: { maxLength?: number }) => Ty<string>t.int()() => Ty<number>
A whole number.
() => Ty<number>t.float()() => Ty<number>
Any number.
() => Ty<number>t.bool()() => Ty<boolean>
true or false.
() => Ty<boolean>t.timestamp()() => Ty<number>
Milliseconds since the Unix epoch, as a whole number.
() => Ty<number>t.uuid()() => Ty<string>
A UUID, checked as one.
() => Ty<string>t.bytes()() => Ty<string>
Bytes, as base64 text in arguments and results.
() => Ty<string>t.json<V>()<V = unknown>() => Ty<V>
Any JSON value, typed as V in TypeScript and never checked.
<V = unknown>() => Ty<V>t.enum(values)(values: readonly string[]) => Ty<one of values>
One of the strings listed.
(values: readonly string[]) => Ty<one of values>t.array(items)(items: Ty<I>) => Ty<I[]>
A list whose items each match items.
(items: Ty<I>) => Ty<I[]>t.object(fields)(fields: S) => Ty<InferObject<S>>
An object with these fields and no others.
(fields: S) => Ty<InferObject<S>>t.id()() => Ty<number>
A column holding an integer primary key, given on insert.
() => Ty<number>t.blobRef(bucket, opts?)(bucket: string, opts?: { onDelete?: "restrict" | "cascade" | "setNull" }) => Ty<string>
A column holding the path of a file in one of the luna's buckets, kept pointing at a file that exists.
(bucket: string, opts?: { onDelete?: "restrict" | "cascade" | "setNull" }) => Ty<string>t.vector(dims, opts?)(dims: number, opts?: { metric?: "cosine" | "l2" | "l1"; index?: boolean }) => Ty<Float32Array>
A column holding dims float32 numbers, such as an embedding, searched with nearest().
(dims: number, opts?: { metric?: "cosine" | "l2" | "l1"; index?: boolean }) => Ty<Float32Array>t.request()() => RequestArgs
As a handler's args only: the handler takes the raw HTTP request. See Handler spec.
() => RequestArgsModifiers
Each builder’s result has these methods. Each returns a new type and leaves the original as it was.
optional()() => Ty<V | null>
The value may be null, or missing from an object or the arguments.
() => Ty<V | null>default(value)(value: V) => Ty<V>
Columns only: the value a row gets when an insert leaves the column out.
(value: V) => Ty<V>unique()() => Ty<V>
Columns only: no two rows may hold the same value.
() => Ty<V>describe(text)(text: string) => Ty<V>
Says what a key field, argument or object field means. Kept with the app, and given no other meaning.
(text: string) => Ty<V>Options of t.vector
dimsnumber
How many numbers each vector holds, from 1 to 8192.
numbermetric?"cosine" | "l2" | "l1"
How nearest() measures distance.
"cosine" | "l2" | "l1""cosine"index?boolean
Keeps an approximate index for large tables. Below 10,000 rows, searches stay exact.
booleanfalseOptions of t.blobRef
bucketstring
The bucket, by the name the luna type's blobs gives it.
stringonDelete?"restrict" | "cascade" | "setNull"
Deleting the file fails, deletes the row, or sets the column to null. setNull needs an optional column.
"restrict" | "cascade" | "setNull""restrict"Infer
Infer<T> is the TypeScript type of a builder’s values.
const status = t.enum(["todo", "done"]);
type Status = Infer<typeof status>; // "todo" | "done"
Good to know
Each builder fits some places and travels as a fixed JSON shape.
| Builder | In TypeScript | As JSON | Key field | Column |
|---|---|---|---|---|
t.string() |
string |
string | Yes | Yes |
t.int() |
number |
integer | Yes | Yes |
t.float() |
number |
number | No | Yes |
t.bool() |
boolean |
true or false |
No | Yes |
t.timestamp() |
number |
integer | No | Yes |
t.uuid() |
string |
string | Yes | Yes |
t.bytes() |
string |
base64 string | Yes | Yes |
t.json() |
V |
any | No | Yes |
t.enum() |
one of the values | string | Yes | Yes |
t.array() |
array | array | No | No |
t.object() |
object | object | No | No |
t.id() |
number |
integer | No | Yes |
t.blobRef() |
string |
string | No | Yes |
t.vector() |
Float32Array |
array of numbers | No | Yes |
- A key field is required. An optional builder cannot be one.
- Arrays and objects cannot be columns. Store them in a
t.json()column. - A
t.bytes()column takes base64 text or aUint8Array, and reads back as base64 text, as arguments carry it. At.vector()column takes aFloat32Arrayor an array of numbers and reads back as aFloat32Array. - An argument or result of
t.int()ort.timestamp()with a fraction fails the check.Date.now()is a whole number. maxLengthcounts characters, not bytes.t.id()adds its column to the front of the table’s primary key. A vector column cannot beunique()or have adefault().
Examples
Reuse an object type
Declare the type once and use it in arguments, columns and TypeScript.
import { defineLuna, t, table, type Infer } from "luna";
const label = t.object({ name: t.string({ maxLength: 40 }), color: t.enum(["red", "green", "blue"]) });
export type Label = Infer<typeof label>;
export const Card = defineLuna({
name: "Card",
key: { board: t.string(), card: t.int() },
schema: { card: table({ title: t.string(), labels: t.json<Label[]>().default([]) }, { singleRow: true }) },
handlers: (h) => ({
edit: h({ args: { title: t.string(), labels: t.array(label) } }, (ctx, { title, labels }) => {
ctx.db.card.set({ title, labels });
}),
}),
});
Say what a field means
key: { board: t.string().describe("The board the card is on"), card: t.int() },
Accept an argument that may be left out
due: h({ args: { at: t.timestamp().optional() } }, (ctx, { at }) => {
console.log(at ?? "no due date");
}),