Skip to main content

Tables

Tables are the way to store data in SpacetimeDB. All data in SpacetimeDB is stored in memory for extremely low latency and high throughput access. SpacetimeDB also automatically persists all data to disk.

Defining Tables

Tables are defined in your module code with a name, columns, and optional configuration.

Use the table function to declare a new table:

import { table, t } from 'spacetimedb/server';

const people = table(
  { name: 'people', public: true },
  {
    id: t.u32().primaryKey().autoInc(),
    name: t.string().index('btree'),
    email: t.string().unique(),
  }
);

The first argument defines table options, and the second defines columns.

Table Visibility

Tables can be private (default) or public:

  • Private tables: Visible only to reducers and the database owner. Clients cannot access them.
  • Public tables: Exposed for client read access through subscriptions. Writes still occur only through reducers.
const publicTable = table({ name: 'user', public: true }, { /* ... */ });
const privateTable = table({ name: 'secret', public: false }, { /* ... */ });

Next Steps