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

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";
PropType
columnsRecord<string, Ty>

The columns, by name, each a t builder. At least one.

TypeRecord<string, Ty>
options?TableOptions

The primary key, indexes and special indexes of the table.

TypeTableOptions
Default{}

Returns a TableDef. In handlers the table is ctx.db.<name>, a Table or, with singleRow, a SingleRow.

TableOptions

PropType
primaryKey?string[]

The columns that identify a row, which get() and upsert() use. A t.id() column joins it at the front.

Typestring[]
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.

Type{ name?: string; columns: string[]; unique?: boolean }[]
singleRow?boolean

The table holds at most one row, read with get() and written with set().

Typeboolean
Defaultfalse
search?{ columns: string[]; tokenizer?: "unicode61" | "porter" | "trigram"; indexOnly?: string[] }

A full-text index over these columns, queried with search().

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

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

Type{ from: string; to: string }

An entry of indexes

PropType
columnsstring[]

The columns, in order.

Typestring[]
name?string

The index's name, unique among the luna type's tables and indexes.

Typestring
Default"<TABLE>_<COLUMN>_<COLUMN>"
unique?boolean

No two rows may hold the same values in these columns.

Typeboolean
Defaultfalse
PropType
columnsstring[]

The text columns to index: t.string(), t.enum() or t.json() columns.

Typestring[]
tokenizer?"unicode61" | "porter" | "trigram"

unicode61 matches words, porter matches English stems so running finds run, trigram matches parts of words in any language.

Type"unicode61" | "porter" | "trigram"
Default"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.

Typestring[]

spatial

PropType
xstring

A t.int(), t.float() or t.timestamp() column.

Typestring
ystring

Another number column.

Typestring
geo?boolean

x is longitude and y latitude in degrees, and distances are in metres.

Typeboolean
Defaultfalse

graph

PropType
fromstring

The column holding the node an edge starts at.

Typestring
tostring

The column holding the node it ends at.

Typestring

Good to know

  • A table’s name cannot start with _luna_ or sqlite_, 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 singleRow table has no primary key and no t.id() column.
  • On a table with no t.id() column and no primaryKey, get() and upsert() throw.
  • A t.blobRef() column must name a bucket declared in the luna type’s blobs.
  • 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 })),
  }),
});

Was this page helpful?