Databases, tables and columns
Structure your data in databases, tables, columns and indexes, from the console or from the server.
Data lives in tables with typed columns, inside a database. A project can have several databases (for example, one per area of the product). You create the structure once, from the console (Database → Create database → Create table, Columns and Indexes tabs) or from the server, with @owbase/node and an API key.
import { Permission, Role, TablesDB, TablesDBIndexType } from '@owbase/node';import { client } from './client';
// The key needs the databases.write, tables.write and columns.read scopes.const db = new TablesDB(client);const table = { databaseId: 'store', tableId: 'orders' };
await db.create({ databaseId: 'store', name: 'Store' });await db.createTable({ ...table, name: 'Orders', // Signed-in users create orders; each row carries its own permissions (row security). permissions: [Permission.create(Role.users())], rowSecurity: true,});
await db.createVarcharColumn({ ...table, key: 'customer', size: 120, required: true });await db.createIntegerColumn({ ...table, key: 'total', required: true, min: 0 });await db.createEnumColumn({ ...table, key: 'status', elements: ['open', 'paid', 'shipped'], required: false, xdefault: 'open' });await db.createDatetimeColumn({ ...table, key: 'deliveryAt', required: false });
// Columns become ready in the background: the index can only use a column with status "available".async function waitForColumn(key: string): Promise<void> { for (let i = 0; i < 60; i++) { const column = await db.getColumn({ ...table, key }); if (String(column.status) === 'available') return; await new Promise((r) => setTimeout(r, 1000)); } throw new Error(`column ${key} was not ready in 60 s`);}await waitForColumn('status');await db.createIndex({ ...table, key: 'by_status', type: TablesDBIndexType.Key, columns: ['status'] });Column types
Section titled “Column types”| Type | Method (TablesDB.) |
Notes |
|---|---|---|
| Short text | createVarcharColumn |
with a maximum length (size); can have an index |
| Long text | createTextColumn, createMediumtextColumn, createLongtextColumn |
for large content |
| Integer | createIntegerColumn, createBigIntColumn |
optional min and max |
| Decimal | createFloatColumn |
optional min and max |
| Yes/no | createBooleanColumn |
|
| Date and time | createDatetimeColumn |
ISO 8601, in UTC |
| Closed list | createEnumColumn |
values in elements |
| Email, URL, IP | createEmailColumn, createUrlColumn, createIpColumn |
validated by the server |
| Relationship | createRelationshipColumn |
links to another table |
| Geography | createPointColumn, createLineColumn, createPolygonColumn |
coordinates |
Most columns accept required, xdefault (default value, only on a non-required column) and array: true (a list of values). The relationship column has no required. Text columns accept encrypt: true, which stores the value encrypted, but an encrypted column can’t be queried, and an index on it doesn’t help queries. An encrypted text column needs a size of at least 150.
Columns and indexes become ready in the background
Section titled “Columns and indexes become ready in the background”Creating a column or an index returns right away with status: "processing"; the server finishes shortly after and the status becomes available. Until then, the column doesn’t accept values or join an index: the example above waits with getColumn. If the status is failed, delete it and create it again.
Indexes
Section titled “Indexes”An index makes filtering and sorting fast on a large table, and it’s required for Query.search.
TablesDBIndexType. |
For |
|---|---|
Key |
filtering and sorting |
Unique |
guaranteeing a value isn’t repeated |
Fulltext |
text search (Query.search) |
Spatial |
geography columns |
An index can combine several columns (columns: ['status', 'total']), in the order your queries filter on them.
Next: create and read rows and define who has access.