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 #[spacetimedb::table] macro on a struct:

#[spacetimedb::table(name = people, public)]
pub struct People {
    #[primary_key]
    #[auto_inc]
    id: u32,
    #[index(btree)]
    name: String,
    #[unique]
    email: String,
}

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.
#[spacetimedb::table(name = user, public)]
pub struct User { /* ... */ }

#[spacetimedb::table(name = secret)]
pub struct Secret { /* ... */ }

Next Steps