@reasonable-stack/orm
v0.4.0
Published
A reasonably good ORM for SQL databases.
Maintainers
Readme
@reasonable-stack/orm
A reasonably good ORM for SQL databases on TypeScript.
It's a type-safe query builder for Bun, with adapters for SQLite and PostgreSQL.
import { nowUTC } from '@reasonable-stack/types';
import { from, into, table, t } from '@reasonable-stack/orm';
import * as sqlite from '@reasonable-stack/orm/bun-sqlite';
import { create } from '@reasonable-stack/orm/experimental-migrations';
import { randomUUIDv7 } from 'bun';
const users = table('users', {
id: t.uuid().default(randomUUIDv7).primaryKey(),
name: t.string(),
email: t.string({ caseInsensitive: true }).unique(),
createdAt: t.timestamp().default(nowUTC),
});
const db = sqlite.connect('./db.sqlite');
await create.table(users, { ifNotExists: true }).run(db);
await into(users)
.values({ name: 'Jane Doe', email: '[email protected]' })
.insert(db);
const jane = await from(users)
.where(users.email, '=', '[email protected]')
.select({ id: users.id, name: users.name })
.one(db);
// → { id: string, name: string } | null- Columns, operators and values are type-checked and autocompleted.
- Values are validated before any SQL runs.
- No singletons: you always pass the database explicitly.
Contents
Install
bun add @reasonable-stack/ormRequires Bun 1.4.4 or later, and TypeScript 6.0 or later for the types.
Timestamps are read as DateTimes, and dates as Dates, of
@reasonable-stack/types,
which is installed along with it.
Connecting
import * as sqlite from '@reasonable-stack/orm/bun-sqlite';
import * as pg from '@reasonable-stack/orm/bun-postgres';
const db = sqlite.connect(':memory:');
const db = pg.connect('postgres://user:pass@localhost:5432/app');
// log every statement
const db = sqlite.connect(':memory:', { loggers: [console] });
await db.close();A logger is anything with debug(sql, params) and error(sql, params, error)
methods.
Tables
import { nowUTC } from '@reasonable-stack/types';
import { table, t, type Infer } from '@reasonable-stack/orm';
import { randomUUIDv7 } from 'bun';
const tasks = table('tasks', {
id: t.uuid().default(randomUUIDv7).primaryKey(),
parentId: t.uuid().nullable(),
status: t.enum(['scheduled', 'running', 'succeeded', 'failed']),
attempts: t.integer({ unsigned: true }).default(0),
maxAttempts: t.integer({ unsigned: true }).default(3),
payload: t.embeds.one({
kind: t.string(),
params: t.record().default({}),
}),
events: t.embeds.many({
kind: t.string(),
timestamp: t.timestamp().default(nowUTC),
}).default([]),
scheduledTo: t.timestamp().default(nowUTC),
});
const users = table('users', {
id: t.uuid().default(randomUUIDv7).primaryKey(),
name: t.string({ length: 36 }),
email: t.string({ caseInsensitive: true }).unique(),
logins: t.integer({ unsigned: true }).default(0),
createdAt: t.timestamp().default(nowUTC),
lastLoggedInAt: t.timestamp().nullable(),
});
type Task = Infer<typeof tasks>; // a row
type TaskInsert = Infer<typeof tasks, 'insert'>; // what `.values()` takes
type TaskUpdate = Infer<typeof tasks, 'update'>; // what `set` takesKeys are camelCase in TypeScript and snake_case in the database
(scheduledTo → scheduled_to).
| builder | TS type | postgres | sqlite |
| :----------------------------------------- | :------------------------ | :------------------- | :-------------------- |
| t.bigint({ unsigned?, autoIncrement? }) | bigint | BIGINT | INTEGER |
| t.binary() | Uint8Array | BYTEA | BLOB |
| t.boolean() | boolean | BOOLEAN | INTEGER |
| t.custom({ ... }) | the schema's output | its primitive's | its primitive's |
| t.date() | Date | DATE | TEXT |
| t.decimal({ precision, scale }) | Decimal | NUMERIC(p, s) | NUMERIC |
| t.enum([...values]) | union of values | VARCHAR(n) | TEXT |
| t.float() | number | DOUBLE PRECISION | REAL |
| t.integer({ unsigned?, autoIncrement? }) | number | BIGINT | INTEGER |
| t.json(schema) | schema's output | JSONB | TEXT |
| t.record() | Record<string, unknown> | JSONB | TEXT |
| t.string({ length? }) | string | VARCHAR(length) | TEXT |
| t.string({ caseInsensitive: true }) | string | CITEXT | TEXT COLLATE NOCASE |
| t.text() | string | TEXT | TEXT |
| t.timestamp() | DateTime | TIMESTAMPTZ | INTEGER |
| t.uuid() | string | UUID | TEXT |
| t.vector({ dimensions }) | number[] | VECTOR(dimensions) | throws |
| t.embeds.one({ ... }) | an object | JSONB | TEXT |
| t.embeds.many({ ... }) | an array of objects | JSONB | TEXT |
| modifier | effect |
| :-------------- | :-------------------------------------------- |
| .primaryKey() | primary key |
| .nullable() | accepts null, optional on insert |
| .default(v) | value or function used when omitted on insert |
| .unique() | unique constraint |
| option | of | effect |
| :-------------------- | :--------------------------- | :--------------------------------- |
| unsigned: true | t.integer(), t.bigint() | rejects negatives |
| autoIncrement: true | t.integer(), t.bigint() | counted up by the database |
| length | t.string() | the most characters it holds (255) |
| caseInsensitive | t.string() | compares case-insensitively |
| precision, scale | t.decimal(), both required | digits in all, and after the point |
| dimensions | t.vector(), required | the numbers each vector has |
t.timestamp() values are DateTimes, which are always in UTC, to the
millisecond. They also take ISO 8601 strings with an offset
('2026-11-14T09:00:00-03:00'), which are converted to UTC and truncated to the
millisecond, and epoch milliseconds. Native Dates aren't taken. They're
stored in UTC, so the database's time zone never matters. t.date() values
are Dates of @reasonable-stack/types, and also take YYYY-MM-DD strings.
t.string() holds up to 255 characters unless given a length, and
t.enum()'s n is its longest value's length. caseInsensitive strings
compare case-insensitively. They need CREATE EXTENSION citext on postgres
(see migrations), where they're CITEXT, which has no length of
its own: the length is still checked before any SQL runs. t.vector() needs
the pgvector extension on postgres
(CREATE EXTENSION vector), and throws on sqlite, which has no vector type.
t.json() takes any Standard Schema (zod,
valibot, arktype, ...).
t.decimal() values are Decimals of @reasonable-stack/types, which are
exact. They also take numbers, bigints and strings ('12.3'), and are rounded
half up to the column's scale, so they read back the way the database keeps
them (12.30 at scale 2). Values with more digits before the point than
precision − scale are rejected. Sums and averages of decimals are decimals
too, and inc takes decimals or numbers. On sqlite they're numbers, so
they're exact up to a precision of 15; a wider column throws.
autoIncrement columns are counted up by the database, so inserts and
updates take no value of them. On sqlite, only the primary key can
autoincrement.
Custom types
t.custom() defines a type of your own, and is the only way to validate a
column with a schema of your own. You give it a primitive the database stores
it as, a Standard Schema that validates its
values, and the primitive's options, side by side. It returns a column
builder. Wrap it in a function to call it like the built-in types:
import { z } from 'zod';
const tc = {
email: () => t.custom({
primitive: 'STRING',
schema: z.email(),
caseInsensitive: true,
length: 255,
}),
};
const contacts = table('contacts', {
id: t.uuid().default(randomUUIDv7).primaryKey(),
email: tc.email().unique(),
backup: tc.email().nullable(),
});| primitive | options | schema's output |
| :----------------------------------------------------------------- | :------------------------------------------------------ | :----------------- |
| STRING | length?, caseInsensitive?, length defaults to 255 | string |
| INTEGER, BIGINT | unsigned?, autoIncrement? | number, bigint |
| DECIMAL | precision, scale, both required | Decimal |
| VECTOR | dimensions, required | number[] |
| BINARY BOOLEAN DATE FLOAT JSON TEXT TIMESTAMP UUID | none | the primitive's |
The schema replaces the primitive's own, so it alone validates values: the
options only shape the column, e.g. a STRING's length is its VARCHAR's.
Its output has to be what the primitive stores (a DateTime for a
TIMESTAMP, a Decimal for a DECIMAL), which TypeScript checks. Options
are checked when t.custom() is called, so a typo throws as soon as a table
uses the type.
Migrations
[!WARNING] Migrations are experimental, hence their entry point's name: their API may change in any release.
import { create, createMigrator, exec } from '@reasonable-stack/orm/experimental-migrations';
const migrate = createMigrator({
'0001': async (db) => {
if (db.dialect.name === 'postgres') await exec('CREATE EXTENSION IF NOT EXISTS citext').run(db);
if (db.dialect.name === 'postgres') await exec('CREATE EXTENSION IF NOT EXISTS vector').run(db);
await create.table(users).run(db);
},
'0002': async (db) => {
await create.table(tasks).run(db);
await create.index('tasks_status_scheduled_to_idx', {
table: 'tasks',
columns: ['status', ['scheduled_to', 'DESC']],
}).run(db);
},
});
await migrate(db);Migrations run in the order of their keys, each in its own transaction, and
only once: applied ones are tracked in the schema_migrations table. A
failing migration is rolled back, and the ones after it don't run.
create.table() creates a table the way its definition says it is today. Any
other change takes the name of a table or column the way the database spells
it (snake_case), verbatim: a migration describes the schema as it was when it
ran, which today's definitions may no longer match.
import { t } from '@reasonable-stack/orm';
import { alter, create, createMigrator, drop, exec, file, run } from '@reasonable-stack/orm/experimental-migrations';
const migrate = createMigrator({
// ...
'0003': async (db) => {
await alter.table('tasks')
.addColumn('created_by', t.uuid().nullable())
.addColumn('priority', t.integer().default(0), { backfill: 0 })
.renameColumn('scheduled_to', 'run_at')
.dropColumn('events')
.run(db);
},
'0004': async (db) => {
await alter.table('tasks').rename('jobs').run(db);
await create.index('jobs_status_run_at_idx', {
table: 'jobs',
columns: ['status', 'run_at'],
where: `"status" = 'scheduled'`,
ifNotExists: true,
}).run(db);
},
'0005': run([
drop.index('tasks_status_scheduled_to_idx', { ifExists: true }),
drop.table('legacy_jobs', { ifExists: true }),
]),
'0006': run([
file('migrations/0006.sql'),
exec('UPDATE "users" SET "logins" = 0 WHERE "logins" IS NULL'),
]),
});| runnable | what it does |
| :-------------------------------------------- | :-------------------------------------------------- |
| create.table(table, { ifNotExists? }) | creates a table from its definition |
| create.index(name, { table, columns, ... }) | creates an index, see below |
| alter.table(name) | changes a table, see below |
| drop.index(name, { ifExists? }) | drops an index |
| drop.table(name, { ifExists? }) | drops a table |
| exec(sql, params?) | runs a single statement, and throws when given more |
| file(path) | runs every statement of a SQL file, one at a time |
Each of them runs with .run(db). run([...]) runs runnables and migration
functions one after the other, as a migration of its own.
create.index() takes the indexed columns as names, or [name, direction]
pairs, where the direction is 'ASC', 'DESC', 'ASC NULLS FIRST',
'ASC NULLS LAST', 'DESC NULLS FIRST' or 'DESC NULLS LAST'. sqlite takes
no NULLS FIRST or LAST in an index, since it always puts nulls first
ascending and last descending: it takes those two, and refuses the others.
unique makes it a unique index, where (raw SQL) a partial one, and
ifNotExists skips it when it's already there.
alter.table() applies its changes one after the other, in order. After
.rename(), the changes that follow apply to the table under its new name.
.addColumn() can't add a primary key, autoincrement or unique column (add a
unique index instead). Defaults are filled in on insert, never by the
database, so the rows already in the table get backfill: it's required for a
column that isn't nullable, and nullable ones are null unless given one.
A failing statement of a file rolls back the whole migration. A relative
path is resolved from the working directory. To resolve it from the
migration's own module instead, pass a URL:
new URL('./0006.sql', import.meta.url). exec also takes parameters, in
your database's placeholder syntax (? on sqlite, $1 on postgres).
Insert
import { into, parse } from '@reasonable-stack/orm';
const task: TaskInsert = { status: 'scheduled', payload: { kind: 'Prune' }, events: [] };
const inserted: Task[] = await into(tasks)
.values(task)
.values([task, task])
.insert(db);
await into(tasks).values(task).insert(db, { returning: ['id', tasks.status] }); // → Pick<Task, 'id' | 'status'>[]
await into(tasks).values(task).insert(db, { returning: false }); // → undefined
// validate untrusted input, filling in defaults
const row: TaskInsert = parse(tasks, JSON.parse(body));insert, update and delete all take returning. It defaults to true,
except for delete, which defaults to false:
| returning | result |
| :--------------------- | :---------- |
| true | full rows |
| [...keys or columns] | only those |
| false | undefined |
On conflict
const rows = [{ email: '[email protected]', name: 'Jane' }];
// skip rows that conflict
await into(users).values(rows).insert(db, {
conflict: { on: ['email'], do: 'nothing' },
});
// update the existing row
await into(users).values(rows).insert(db, {
conflict: {
on: ['email'],
do: {
set: { lastLoggedInAt: nowUTC() }, // a given value
inc: { logins: 1 }, // an increment
replace: ['name'], // the value being inserted
},
},
});
// throw, same as no `conflict` at all
await into(users).values(rows).insert(db, {
conflict: { on: ['email'], do: 'throw' },
});Select
A table is shaped like its columns: tasks.status references a column.
import { exp, from } from '@reasonable-stack/orm';
const due: Task[] = await from(tasks)
.where(tasks.status, '=', 'scheduled')
.where(tasks.scheduledTo, '<', nowUTC()) // multiple `where`s are ANDed
.orderBy([tasks.scheduledTo, exp.desc(tasks.attempts)])
.limit(10)
.offset(20)
.all(db);
const scheduled = from(tasks).where(tasks.status, '=', 'scheduled');
await scheduled.one(db); // → Task | null, the first row
await scheduled.exists(db); // → boolean
await scheduled.count(db); // → numberorderBy orders by an operand ascending, unless it's given in a direction:
| direction | SQL |
| :--------------------------- | :----------------- |
| exp.asc(operand) | ASC |
| exp.ascNullFirst(operand) | ASC NULLS FIRST |
| exp.ascNullLast(operand) | ASC NULLS LAST |
| exp.desc(operand) | DESC |
| exp.descNullFirst(operand) | DESC NULLS FIRST |
| exp.descNullLast(operand) | DESC NULLS LAST |
Where nulls go otherwise differs between databases: last when ascending on postgres, first on sqlite. Say where to put them, to read the same rows on both:
await from(users).orderBy([exp.descNullLast(users.lastLoggedInAt)]).all(db);| operator | value |
| :-------------------------------------------- | :------------------------------------------------------ |
| '=' '!=' '<' '<=' '>' '>=' | the column's type, or another column |
| 'IS DISTINCT FROM' 'IS NOT DISTINCT FROM' | like '!=' and '=', but null is a value too |
| 'IN' 'NOT IN' | a list of the column's type |
| 'BETWEEN' 'NOT BETWEEN' | a [low, high] pair, both included |
| 'LIKE' 'NOT LIKE' | a string pattern (text columns) |
| 'ILIKE' 'NOT ILIKE' | a case-insensitive string pattern (text columns) |
| 'IS' 'IS NOT' | null (nullable columns), true or false (booleans) |
const lastWeek = nowUTC().sub(days(7)); // both from @reasonable-stack/types
await from(tasks).where(tasks.status, 'IN', ['scheduled', 'running']).all(db);
await from(tasks).where(tasks.status, 'NOT IN', ['succeeded']).all(db);
await from(tasks).where(tasks.scheduledTo, 'BETWEEN', [lastWeek, nowUTC()]).all(db);
await from(tasks).where(tasks.attempts, 'NOT BETWEEN', [1, 3]).all(db);
await from(tasks).where(tasks.payload.kind, 'LIKE', 'Prune%').all(db);
await from(tasks).where(tasks.payload.kind, 'NOT ILIKE', '%test%').all(db);
await from(tasks).where(tasks.parentId, 'IS NOT', null).all(db);
await from(tasks).where(tasks.parentId, 'IS DISTINCT FROM', id).all(db); // rows without a parent tooLIKE is case-sensitive on postgres (except for caseInsensitive strings), and
case-insensitive for ASCII letters on sqlite. Use ILIKE to ignore case on
both.
Regular expressions
exp.match() tests text against a regular expression:
await from(tasks).where(exp.match(tasks.payload.kind, /^N/)).all(db);
await from(tasks).where(exp.match(tasks.payload.kind, /^n/i)).all(db);On postgres it's ~, or ~* with the i flag, and patterns are postgres'
own flavor of regular expressions. Flags without an equivalent there (m,
s, v, y) throw rather than being dropped, and g, d and u don't
change a match.
SQLite has no regular expressions of its own, so sqlite.connect adds them:
REGEXP, and regexp_like(text, pattern, flags) for a regular expression
with flags. Both match the way JavaScript does, every flag included, and can
be used in fragments too.
Picking columns
await from(tasks).select({ id: tasks.id, when: tasks.scheduledTo }).all(db);
// → { id: string, when: DateTime }[]
await from(tasks).select({ ...tasks, parent: tasks.parentId }).all(db);
// → (Task & { parent: string | null })[]
await from(tasks).select(tasks).all(db);
// → Task[], same as no select at all
await from(tasks).select(tasks.id).all(db);
// → string[], the values themselves
await from(tasks).select({ status: tasks.status }).distinct().all(db);
// → { status: Task['status'] }[], each status once
await from(tasks).select({ ...tasks.omit(['events']) }).all(db);
// → Omit<Task, 'events'>[]pick and omit narrow a table to some of its columns, to be spread or
selected whole (see joins). as, pick and omit are reserved,
so they can't be column names.
Comparing columns
await from(tasks).where(tasks.attempts, '<', tasks.maxAttempts).all(db);Coalesce
exp.coalesce() reads the first of its operands that isn't null. The rest
can be columns or plain values of the first one's type:
await from(users).select({ seen: exp.coalesce(users.lastLoggedInAt, users.createdAt) }).all(db);
// → { seen: DateTime }[]
await from(users).where(exp.coalesce(users.lastLoggedInAt, DateTime.parse(0)), '<', lastWeek).all(db);It reads null only when every one of its operands may.
AND, OR and NOT
await from(tasks)
.where(exp.or([
exp(tasks.status, '=', 'scheduled'),
exp.and([
exp(tasks.status, '=', 'running'),
exp(tasks.attempts, '>', 3),
]),
]))
.all(db);
await from(tasks).where(exp.not(exp.match(tasks.payload.kind, /^N/))).all(db);exp.not() negates any condition, fragments included. As in SQL, it reads
null (and so filters the row out) where the condition does.
Embedded fields
Fields of t.embeds.one() columns are referenced like columns:
await from(tasks)
.where(tasks.payload.kind, '=', 'Prune')
.select({ kind: tasks.payload.kind, params: tasks.payload.params })
.all(db);
// → { kind: string, params: Record<string, unknown> }[]Raw SQL fragments
import { f } from '@reasonable-stack/orm';
await from(tasks).where(f`upper(${tasks.payload.kind})`, '=', 'PRUNE').all(db);A fragment can also be a condition of its own, to use any operator your database has, e.g. postgres' JSON containment:
await from(tasks).where(f`${tasks.payload} @> ${{ kind: 'Prune' }}`).all(db);
await from(tasks)
.where(exp.or([
f`${tasks.payload} @> ${{ params: { days: 30 } }}`,
exp(tasks.status, '=', 'scheduled'),
]))
.all(db);Interpolated columns are rendered in place, and any other value is passed as a parameter.
A fragment reads as unknown, unless .as() gives it a column type. It's
then typed, decoded and compared like a column of that type, while its SQL
stays as written:
await from(tasks)
.select({
doubled: f`${tasks.attempts} * 2`.as(t.integer()),
retried: f`${tasks.attempts} > ${0}`.as(t.boolean()),
retries: f`nullif(${tasks.attempts}, ${0})`.as(t.integer().nullable()),
})
.all(db);
// → { doubled: number, retried: boolean, retries: number | null }[]
await from(tasks).where(f`${tasks.scheduledTo}`.as(t.timestamp()), '<', nowUTC()).all(db);Group by
await from(tasks)
.where(tasks.status, '!=', 'scheduled')
.groupBy(tasks.status)
.select({
status: tasks.status,
tasks: exp.count(tasks.id),
attempts: exp.sum(tasks.attempts),
average: exp.avg(tasks.attempts),
})
.all(db);
// → { status: Task['status'], tasks: number, attempts: number, average: number }[]groupBy takes a single operand, or a list of them:
.groupBy([tasks.payload.kind, tasks.status]).
| aggregate | reads |
| :--------------------------------- | :-------------------------------------------------------- |
| exp.count(x) | how many rows have x not null |
| exp.count.distinct(x) | how many distinct values of x there are |
| exp.sum(x) | the sum of x, 0 for no rows (numeric columns) |
| exp.sum.distinct(x) | the sum of the distinct values of x |
| exp.min(x) exp.max(x) | the lowest or highest value of x (not booleans or JSON) |
| exp.avg(x) | the average of x, as a float (numeric columns) |
| exp.collect(table \| projection) | the group's rows, as a list (see Joins) |
exp.min(), exp.max() and exp.avg() read null only without a groupBy
(there may be no rows to aggregate then), or when the values may be null, or
their table's row may be missing in an outer join.
Selecting aggregates next to plain (non-aggregated) columns takes a groupBy,
to say which rows each aggregates: postgres refuses the query otherwise, and
sqlite silently reads the columns of an arbitrary row. Without one, the query
throws when it runs:
await from(tasks).select({ status: tasks.status, tasks: exp.count(tasks.id) }).all(db);
// throws: "status" isn't aggregated, so the query needs a groupByAggregates on their own are fine, and aggregate every row.
having filters groups the way where filters rows:
await from(tasks)
.groupBy(tasks.payload.kind)
.having(exp.count(tasks.id), '>', 10)
.having(exp.max(tasks.scheduledTo), '<', lastWeek)
.select({ kind: tasks.payload.kind, latest: exp.max(tasks.scheduledTo) })
.all(db);
// → { kind: string, latest: DateTime }[]It also takes the keys of the projection, which read as whatever is selected
under them, both on their own and inside exp():
await from(tasks)
.groupBy([tasks.payload.kind, tasks.status])
.having('count', '>', 1)
.having(exp.or([exp('count', '<', 10), exp('attempts', '=', 0)]))
.select({
kind: tasks.payload.kind,
status: tasks.status,
count: exp.count(tasks.id),
attempts: exp.sum(tasks.attempts),
})
.all(db);
// → { kind: string, status: Task['status'], count: number, attempts: number }[]Keys work before or after .select(...). After it, only the projection's
keys type-check, and a .select(...) after them must have the keys they
named. A key the projection doesn't have throws when the query runs.
Joins
Alias a table to query it more than once:
const parent = tasks.as('parent');
const child = tasks.as('child');
await from(parent)
.leftJoin(child, exp(parent.id, '=', child.parentId))
.where(parent.parentId, 'IS', null)
.groupBy(parent.id)
.select({ ...parent, children: exp.count(child.id) })
.all(db);
// → (Task & { children: number })[]exp.collect() collects a table's rows in each group into a list, or
just the columns you pick:
await from(parent)
.leftJoin(child, exp(parent.id, '=', child.parentId))
.where(parent.parentId, 'IS', null)
.groupBy(parent.id) // aggregating next to the parent's columns needs a groupBy
.select({
...parent,
children: exp.collect(child),
summary: exp.collect({ id: child.id, kind: child.payload.kind }),
})
.all(db);
// → (Task & { children: Task[], summary: { id: string, kind: string }[] })[]A parent without children gets an empty list.
innerJoin only keeps rows with a match, leftJoin keeps every row of the
tables joined so far, and rightJoin every row of the joined table:
await from(parent).innerJoin(child, exp(parent.id, '=', child.parentId)).select({ childId: child.id }).all(db);
// → { childId: string }[]
await from(parent).leftJoin(child, exp(parent.id, '=', child.parentId)).select({ childId: child.id }).all(db);
// → { childId: string | null }[]
await from(child).rightJoin(parent, exp(parent.id, '=', child.parentId)).select({ childId: child.id }).all(db);
// → { childId: string | null }[]A join may leave a table's row missing: the left-joined table's where it has no match, and the rows of the tables joined before a right join (the queried table's included) where they have none. An inner join never does. Where a row may be missing, its columns are nullable, and so is the row itself when it's selected as a whole, under a key of its own:
await from(child)
.leftJoin(parent, exp(parent.id, '=', child.parentId))
.select({ ...child, parent })
.all(db);
// → (Task & { parent: Task | null })[]A missing row is told apart by its primary key (or any required column) being null. For a table without one, the row reads null when every one of its columns is.
Narrowed by pick or omit, a table selected as a whole reads only the
columns it kept, and is still null when its row is missing, even when the
primary key is left out:
await from(child)
.leftJoin(parent, exp(parent.id, '=', child.parentId))
.select({ ...child.omit(['parentId']), parent: parent.pick(['id', 'status']) })
.all(db);
// → (Omit<Task, 'parentId'> & { parent: Pick<Task, 'id' | 'status'> | null })[]A query only takes the tables it reads from, so referencing child before
joining it is a type error, and throws when the query is built.
Update
const updated: Pick<Task, 'id'>[] = await from(tasks)
.where(tasks.status, '=', 'scheduled')
.update(db, {
set: { status: 'running' },
inc: { attempts: 1 },
returning: ['id'], // optional, defaults to `true` (full rows)
});Delete
await from(tasks).where(tasks.status, '=', 'succeeded').delete(db); // → undefined
await from(tasks).where(tasks.status, '=', 'succeeded').delete(db, { returning: true }); // → Task[]update and delete refuse joins, select, distinct, groupBy, having,
orderBy, limit and offset, so they never touch more rows than the query
suggests.
Transactions
await db.transaction(async (tx) => {
await into(users).values({ email: '[email protected]', name: 'Jane' }).insert(tx);
await tx.transaction(async (savepoint) => {
// nested transactions use savepoints
});
});The transaction rolls back if the callback throws.
Validation
import { ValidationError } from '@reasonable-stack/orm';
const people = table('people', {
id: t.uuid().default(randomUUIDv7).primaryKey(),
name: t.string({ length: 5 }),
age: t.integer(),
});
try {
await into(people).values({ name: 'Jane Doe', age: 1.5 }).insert(db);
} catch (error) {
if (error instanceof ValidationError) {
error.message; // Failed validation:
// - name: must be at most 5 characters long
// - age: must be an integer
error.issues; // [{ message, path: ['name'] }, ...]
}
}