table
Declare one table of a luna's schema, with its columns, primary key, indexes, and a search, spatial or graph index.
table declares one table of a luna type’s schema. Every luna of the type has its own copy of the table.
import { defineLuna, t, table } from "luna";
export const List = defineLuna({
name: "List",
key: { id: t.string() },
schema: {
items: table({ id: t.id(), text: t.string() }),
},
handlers: (h) => ({}),
});
Reference
table(columns, options?)
import { table } from "luna";
columnsRecord<string, Ty>
The columns, by name, each a t builder. At least one.
Record<string, Ty>options?TableOptions
The primary key, indexes and special indexes of the table.
TableOptions{}Returns a TableDef. In handlers the table is ctx.db.<name>, a Table or, with singleRow, a SingleRow.
TableOptions
primaryKey?string[]
The columns that identify a row, which get() and upsert() use. A t.id() column joins it at the front.
string[]indexes?{ name?: string; columns: string[]; unique?: boolean }[]
Indexes over one or more columns, to find and order rows faster. A unique index refuses two rows with the same values.
{ name?: string; columns: string[]; unique?: boolean }[]singleRow?boolean
The table holds at most one row, read with get() and written with set().
booleanfalsesearch?{ columns: string[]; tokenizer?: "unicode61" | "porter" | "trigram"; indexOnly?: string[] }
A full-text index over these columns, queried with search().
{ columns: string[]; tokenizer?: "unicode61" | "porter" | "trigram"; indexOnly?: string[] }spatial?{ x: string; y: string; geo?: boolean }
A spatial index over two number columns, queried with within() and nearby().
{ x: string; y: string; geo?: boolean }graph?{ from: string; to: string }
Each row is an edge from one node to another, walked with traverse() and path().
{ from: string; to: string }An entry of indexes
columnsstring[]
The columns, in order.
string[]name?string
The index's name, unique among the luna type's tables and indexes.
string"<TABLE>_<COLUMN>_<COLUMN>"unique?boolean
No two rows may hold the same values in these columns.
booleanfalsesearch
columnsstring[]
The text columns to index: t.string(), t.enum() or t.json() columns.
string[]tokenizer?"unicode61" | "porter" | "trigram"
unicode61 matches words, porter matches English stems so running finds run, trigram matches parts of words in any language.
"unicode61" | "porter" | "trigram""unicode61"indexOnly?string[]
Search columns kept only as words in the index. They read back as null, so write them on every insert and update. Each must be optional.
string[]spatial
xstring
A t.int(), t.float() or t.timestamp() column.
stringystring
Another number column.
stringgeo?boolean
x is longitude and y latitude in degrees, and distances are in metres.
booleanfalsegraph
fromstring
The column holding the node an edge starts at.
stringtostring
The column holding the node it ends at.
stringGood to know
- A table’s name cannot start with
_luna_orsqlite_, and a column’s cannot start with_luna_. Names of tables, columns and indexes are compared without regard to case. - A column cannot be an array or an object. Use
t.json(). - A
singleRowtable has no primary key and not.id()column. - On a table with no
t.id()column and noprimaryKey,get()andupsert()throw. - A
t.blobRef()column must name a bucket declared in the luna type’sblobs. - What a deploy does to existing lunas when tables change is in Change a schema.
Examples
Keys, indexes and one row of settings
import { defineLuna, t, table } from "luna";
export const Board = defineLuna({
name: "Board",
key: { id: t.string() },
schema: {
members: table({ user: t.string(), role: t.enum(["owner", "editor"]) }, { primaryKey: ["user"] }),
cards: table(
{ id: t.id(), column: t.string(), position: t.int(), slug: t.string(), text: t.string() },
{
indexes: [{ columns: ["column", "position"] }, { columns: ["slug"], unique: true }],
search: { columns: ["text"], tokenizer: "porter" },
},
),
settings: table({ title: t.string(), archived: t.bool().default(false) }, { singleRow: true }),
},
handlers: (h) => ({}),
});
Places on a map and a graph of follows
import { defineLuna, t, table } from "luna";
export const User = defineLuna({
name: "User",
key: { id: t.string() },
schema: {
places: table({ id: t.id(), name: t.string(), lng: t.float(), lat: t.float() }, { spatial: { x: "lng", y: "lat", geo: true } }),
follows: table({ follower: t.string(), followed: t.string() }, { primaryKey: ["follower", "followed"], graph: { from: "follower", to: "followed" } }),
},
handlers: (h) => ({
near: h({ args: { lng: t.float(), lat: t.float() }, query: true }, (ctx, { lng, lat }) =>
ctx.db.places.nearby({ x: lng, y: lat }, { k: 5, maxDistance: 2000 }),
),
reach: h({ args: { from: t.string() }, query: true }, (ctx, { from }) => ctx.db.follows.traverse(from, { depth: 2 })),
}),
});