@blumbo/db
v0.1.2
Published
Blumbo platform: Postgres connection and migration plumbing, with the cold-start retry.
Readme
@blumbo/db
Postgres plumbing shared by Blumbo apps that store anything: connect, apply migrations, and the retry that makes a cold compute or a still-booting container a wait rather than a failed boot.
Owns no schema. Each app has its own database and its own credentials.
Install
npm install @blumbo/db drizzle-ormdrizzle-orm is a peer dependency on purpose. Your app defines its own
schema with it, and two copies in one tree means two sets of types that do not
match each other.
Use
import { applyMigrations, connect } from '@blumbo/db'
import * as schema from './schema.ts'
// No default connection string, deliberately: an app that silently connects
// somewhere is worse than one that refuses to start.
const connection = connect(schema, { variable: 'DATABASE_URL' })
// Takes the connection itself, not the options again. Committed SQL, applied
// at boot, after waiting out a cold or still-starting database. Generate
// migrations, never `push` — the files in `drizzle/` are what runs and what
// gets reviewed.
await applyMigrations(connection, './drizzle')
const { db, close } = connectionapplyMigrations retries only the errors that mean "not up yet"
(ECONNREFUSED, ENOTFOUND, ETIMEDOUT, ECONNRESET, and Postgres 57P03),
backing off to a deadline. Anything else fails immediately, because a bad
password should not look like a slow start.
Boot-time migration is safe only at one machine. Nothing here coordinates between processes, so two instances booting the same image is two migrators racing. Keep the app at one machine, or move migrations to a release command.
