Let's startPostgres
Install a driver, define the table, then push and connect.
Install
okmodel has no runtime dependencies. Each driver is an optional peer. Pick the one the runtime already has.
The default on Node and Bun. It connects to a Postgres server.
bun add okmodel postgresimport { connect } from "okmodel/pg/postgresjs";Define the table
table() stores the columns. t.identity().primaryKey() makes id the primary key.
import { table, t } from "okmodel/pg";
export const notes = table("notes", {
id: t.identity().primaryKey(),
title: t.text(),
});t.identity() is a bigint, generated always. Insert omits id. The default codec is a string. A key of more than one column is the primaryKey option. Keys.
Define the schema
schema() compiles the tables into one catalog. The module default-exports that result.
import { schema, table, t } from "okmodel/pg";
export const notes = table("notes", {
id: t.identity().primaryKey(),
title: t.text(),
});
export default schema({ tables: [notes] }); Define the config
Point schema at the file from the previous step. database is the URL push connects to.
import { defineConfig } from "okmodel/migrate";
export default defineConfig({
schema: "./schema.ts",
database: process.env.DATABASE_URL!,
});okm push loads okmodel.config.ts from the project directory. The file default-exports defineConfig. A file that does not is refused. A module that is not a schema() is refused: <path> must export a schema(). Other options are on Config.
Push
okm push connects and creates the tables. It does not write a SQL file.
bunx okm pushA protected target refuses push (OKM1850). No schema change prints no changes. bunx okm runs with Node. bun --bun okm uses the Bun runtime.
Connect
connect takes the direct URL and the schema. The process keeps db. Direct asserts the URL. Safe refuses an empty DATABASE_URL. The options are on Connect.
import { connect } from "okmodel/pg/postgresjs";
import schema from "./schema.ts";
export const db = connect(process.env.DATABASE_URL!, { schema }); 🎉 That's it!
That's it! You're now ready to use okmodel in your application. Continue to Basic Usage to learn how to insert, find, update, and delete.