Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

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";
PropType
t.string(opts?)(opts?: { maxLength?: number }) => Ty<string>

Text, at most maxLength characters when given.

Type(opts?: { maxLength?: number }) => Ty<string>
t.int()() => Ty<number>

A whole number.

Type() => Ty<number>
t.float()() => Ty<number>

Any number.

Type() => Ty<number>
t.bool()() => Ty<boolean>

true or false.

Type() => Ty<boolean>
t.timestamp()() => Ty<number>

Milliseconds since the Unix epoch, as a whole number.

Type() => Ty<number>
t.uuid()() => Ty<string>

A UUID, checked as one.

Type() => Ty<string>
t.bytes()() => Ty<string>

Bytes, as base64 text in arguments and results.

Type() => Ty<string>
t.json<V>()<V = unknown>() => Ty<V>

Any JSON value, typed as V in TypeScript and never checked.

Type<V = unknown>() => Ty<V>
t.enum(values)(values: readonly string[]) => Ty<one of values>

One of the strings listed.

Type(values: readonly string[]) => Ty<one of values>
t.array(items)(items: Ty<I>) => Ty<I[]>

A list whose items each match items.

Type(items: Ty<I>) => Ty<I[]>
t.object(fields)(fields: S) => Ty<InferObject<S>>

An object with these fields and no others.

Type(fields: S) => Ty<InferObject<S>>
t.id()() => Ty<number>

A column holding an integer primary key, given on insert.

Type() => 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.

Type(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().

Type(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.

Type() => RequestArgs

Modifiers

Each builder’s result has these methods. Each returns a new type and leaves the original as it was.

PropType
optional()() => Ty<V | null>

The value may be null, or missing from an object or the arguments.

Type() => Ty<V | null>
default(value)(value: V) => Ty<V>

Columns only: the value a row gets when an insert leaves the column out.

Type(value: V) => Ty<V>
unique()() => Ty<V>

Columns only: no two rows may hold the same value.

Type() => 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.

Type(text: string) => Ty<V>

Options of t.vector

PropType
dimsnumber

How many numbers each vector holds, from 1 to 8192.

Typenumber
metric?"cosine" | "l2" | "l1"

How nearest() measures distance.

Type"cosine" | "l2" | "l1"
Default"cosine"
index?boolean

Keeps an approximate index for large tables. Below 10,000 rows, searches stay exact.

Typeboolean
Defaultfalse

Options of t.blobRef

PropType
bucketstring

The bucket, by the name the luna type's blobs gives it.

Typestring
onDelete?"restrict" | "cascade" | "setNull"

Deleting the file fails, deletes the row, or sets the column to null. setNull needs an optional column.

Type"restrict" | "cascade" | "setNull"
Default"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 a Uint8Array, and reads back as base64 text, as arguments carry it. A t.vector() column takes a Float32Array or an array of numbers and reads back as a Float32Array.
  • An argument or result of t.int() or t.timestamp() with a fraction fails the check. Date.now() is a whole number.
  • maxLength counts characters, not bytes.
  • t.id() adds its column to the front of the table’s primary key. A vector column cannot be unique() or have a default().

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");
}),

Was this page helpful?