Tables
Scaffold a new table file with:
alab table blog postThis creates schemas/blog/post.js where blog is the namespace and post is
the table name. The resulting database table is blog_post.
Basic Syntax
Section titled “Basic Syntax”Define tables using the table({...}) function:
export default table({ id: col.id(), title: col.title(), body: col.text(), published: col.flag(), author: col.belongs_to("auth.user"),});Every key becomes a column. The value is a column builder — either a
low-level type like col.string() or a
semantic type like col.title().
Column Modifiers
Section titled “Column Modifiers”Columns support chained modifiers to add constraints:
export default table({ id: col.id(), email: col.email().unique(), username: col.username(), bio: col.text().optional(), role: col.enum(["admin", "editor", "viewer"]).default("viewer"),});| Modifier | What it does |
|---|---|
.optional() | Allows NULL values |
.unique() | Adds a unique constraint |
.default(value) | Sets a default value |
Table Modifiers
Section titled “Table Modifiers”Chain modifiers on the table itself to add common functionality:
export default table({ id: col.id(), title: col.title(), body: col.body(),}) .timestamps() .soft_delete() .searchable(["title", "body"]);What each modifier produces
Section titled “What each modifier produces”| Modifier | What it adds |
|---|---|
.timestamps() | created_at and updated_at datetime columns (auto-managed) |
.auditable() | created_by and updated_by UUID columns |
.soft_delete() | Nullable deleted_at datetime column |
.sortable() | position integer column for manual ordering |
Metadata modifiers
Section titled “Metadata modifiers”These don’t add columns — they annotate the table in the OpenAPI x-db
extension for downstream consumers (generators, custom tooling).
| Modifier | What it does |
|---|---|
.sort_by(["col1", "col2"]) | Default ORDER BY columns for queries |
.searchable(["col1"]) | Marks columns for full-text search indexing |
.filterable(["col1"]) | Marks which columns can be filtered in queries |
Constraint modifiers
Section titled “Constraint modifiers”| Modifier | What it does |
|---|---|
.unique("col1", "col2") | Composite unique constraint across columns |
.index("col1", "col2") | Composite index across columns |
.many_to_many("other.table") | Many-to-many relationship (auto-creates a join table) |
.junction(...refs) | Marks explicit junction table for M2M (allows custom cols) |
Putting It Together
Section titled “Putting It Together”A realistic table definition combining columns, modifiers, and relationships:
export default table({ id: col.id(), title: col.title(), slug: col.slug(), body: col.body(), published: col.flag(), author: col.belongs_to("auth.user"),}) .timestamps() .soft_delete() .searchable(["title", "body"]) .index("author", "published");This produces:
- A
blog_posttable with the defined columns created_at,updated_at(from.timestamps())deleted_at(from.soft_delete())- A foreign key to
auth_user(fromcol.belongs_to()) - A composite index on
(author, published) - The slug column is automatically unique with validation (from
col.slug()) publisheddefaults tofalse(fromcol.flag())- Searchable and soft-delete metadata in the OpenAPI
x-dbextension