---
title: table
description: Declare one table of a luna's schema, with its columns, primary key, indexes, and a search, spatial or graph index.
related:
  - /data/define-tables
  - /sdk/db
  - /data/change-a-schema
---

`table` declares one table of a luna type's `schema`. Every luna of the type has its own copy of the table.

```ts luna/lunas/list.ts
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?)

```ts
import { table } from "luna";
```

| Prop | Type | Default | Description |
| - | - | - | - |
| `columns` | `Record<string, Ty>` | - | The columns, by name, each a t builder. At least one. |
| `options?` | `TableOptions` | `{}` | The primary key, indexes and special indexes of the table. |

Returns a `TableDef`. In handlers the table is `ctx.db.<name>`, a [`Table`](/sdk/db) or, with `singleRow`, a `SingleRow`.

### TableOptions

| Prop | Type | Default | Description |
| - | - | - | - |
| `primaryKey?` | `string[]` | - | The columns that identify a row, which get() and upsert() use. A t.id() column joins it at the front. |
| `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. |
| `singleRow?` | `boolean` | `false` | The table holds at most one row, read with get() and written with set(). |
| `search?` | `{ columns: string[]; tokenizer?: "unicode61" \| "porter" \| "trigram"; indexOnly?: string[] }` | - | A full-text index over these columns, queried with search(). |
| `spatial?` | `{ x: string; y: string; geo?: boolean }` | - | A spatial index over two number columns, queried with within() and nearby(). |
| `graph?` | `{ from: string; to: string }` | - | Each row is an edge from one node to another, walked with traverse() and path(). |

### An entry of indexes

| Prop | Type | Default | Description |
| - | - | - | - |
| `columns` | `string[]` | - | The columns, in order. |
| `name?` | `string` | `"<TABLE>_<COLUMN>_<COLUMN>"` | The index's name, unique among the luna type's tables and indexes. |
| `unique?` | `boolean` | `false` | No two rows may hold the same values in these columns. |

### search

| Prop | Type | Default | Description |
| - | - | - | - |
| `columns` | `string[]` | - | The text columns to index: t.string(), t.enum() or t.json() columns. |
| `tokenizer?` | `"unicode61" \| "porter" \| "trigram"` | `"unicode61"` | unicode61 matches words, porter matches English stems so running finds run, trigram matches parts of words in any language. |
| `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. |

### spatial

| Prop | Type | Default | Description |
| - | - | - | - |
| `x` | `string` | - | A t.int(), t.float() or t.timestamp() column. |
| `y` | `string` | - | Another number column. |
| `geo?` | `boolean` | `false` | x is longitude and y latitude in degrees, and distances are in metres. |

### graph

| Prop | Type | Default | Description |
| - | - | - | - |
| `from` | `string` | - | The column holding the node an edge starts at. |
| `to` | `string` | - | The column holding the node it ends at. |

## 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](/data/change-a-schema).

## Examples

### Keys, indexes and one row of settings

```ts luna/lunas/board.ts
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

```ts luna/lunas/user.ts
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 })),
  }),
});
```
