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.
- Rust
- C#
- TypeScript
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,
}Use the [SpacetimeDB.Table] attribute on a partial struct or partial class:
[SpacetimeDB.Table(Name = "people", Public = true)]
public partial struct People
{
[SpacetimeDB.PrimaryKey]
[SpacetimeDB.AutoInc]
public uint Id;
[SpacetimeDB.Index.BTree]
public string Name;
[SpacetimeDB.Unique]
public string Email;
}The partial modifier is required to allow code generation.
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.
- Rust
- C#
- TypeScript
#[spacetimedb::table(name = user, public)]
pub struct User { /* ... */ }
#[spacetimedb::table(name = secret)]
pub struct Secret { /* ... */ }[SpacetimeDB.Table(Name = "user", Public = true)]
public partial struct User { /* ... */ }
[SpacetimeDB.Table(Name = "secret", Public = false)]
public partial struct Secret { /* ... */ }const publicTable = table({ name: 'user', public: true }, { /* ... */ });
const privateTable = table({ name: 'secret', public: false }, { /* ... */ });Next Steps
- Column Types and Constraints - Define table structure with types, unique columns, primary keys, and auto-increment
- Indexes - Speed up queries with single and multi-column indexes
- Access Permissions - Query and modify tables from reducers, views, and clients
- Learn about Scheduling tables
- Learn about Performance Best Practices