Basic Database Interface
A database interface is a TypeScript type where each key represents a table name and each value is an interface describing that table’s columns:ColumnType<SelectType, InsertType, UpdateType>
TheColumnType type allows you to specify different types for select, insert, and update operations on a single column.
Examples
Read-only column (like database-generated timestamps):Generated<T>
A shortcut for database-generated columns that are optional in inserts and updates:Usage
- SELECT: Returns
string(foruser_id) orDate(forcreated_at) - INSERT: Optional (
string | undefinedorDate | undefined) - UPDATE: Same as select type
GeneratedAlways<T>
For columns that are always database-generated and cannot be inserted or updated:GENERATED ALWAYS AS IDENTITY columns:
JSONColumnType<SelectType, InsertType, UpdateType>
For JSON columns that are inserted/updated as stringified JSON:Example
Selectable<R>, Insertable<R>, Updateable<R>
These utility types extract the appropriate type for each operation:Complete Example
Here’s a complete database interface showing various column types:Best Practices
Export
Selectable, Insertable, and Updateable type variants from your table definitions for use throughout your application.