Database, ORM, and migrations
Query business tables with the typed builder and understand batch writes and schema changes.
Read business tables through app.database() and use the ORM builder for typed queries. Batch predetermined writes. Where a write depends on a prior read, make concurrency conditions and conflict handling explicit. Use numbered migrations for existing databases.
Read one record
app.database() opens the default database. The ORM builder exposes methods such as table, select, and where. Pass a table declared with schema.define to db.table to infer column types. With handwritten SQL, use the connection’s prepare and bind APIs.
import { orm } from "@platform/database/builder.ts";
const database = await app.database();
const db = orm(database);
const record = await db.table("config_records").select("key").where("key", key).first();
Configure extra data sources by name and open them with database.db(name). Node supports SQLite, Postgres, and MySQL. Workers uses D1. Shared queries follow the D1 syntax and execution contract.
Commit related writes
A batch takes statements you have already determined. If you need to read before choosing a write, account for concurrent changes. For example, make the update conditional on the record version and surface a conflict to the caller. Use paginate to batch a count and page query together.
Roll out a schema change
Each feature owns its table definitions, and the application assembles them. For existing databases, add a numbered TypeScript migration under database/migrations/. Test a fresh install and an upgrade with existing rows. Check defaults, indexes, constraint conflicts, and execution on an already migrated database. MySQL can implicitly commit DDL, so exercise failure and rollback on the target driver. Platform environments · Queues and jobs