Postgres
A Postgres driver for the `DB` protocol, backed by a connection pool.
@ozaco/db/impl/postgres@ozaco/db/impl/postgres provides PostgresDB, a concrete implementation of the DB protocol backed by a pooled Postgres client. Install it with a PostgresConfig — a connection URL, your SchemaDef, and an optional pool size — and the effect scope gains a QueryBuilder reachable through useDatabase().
On install the driver builds the Drizzle table objects from your schema, opens a connection pool (default max of 10), and runs applyMigrations against the postgres dialect. Because Postgres rejects a REFERENCES to a table that does not yet exist, migrations order table creation by foreign-key dependency. The pool is drained through the protocol’s close action.
Postgres-specific types are used during DDL generation — SERIAL for auto-increment primary keys, TIMESTAMP WITH TIME ZONE, BYTEA for blobs, VARCHAR(n) — and raw errors are classified into DbErrorCode tags (for example a unique-constraint violation becomes unique-violation).
Plugin
PostgresDB
The installable Postgres implementation of the `DB` protocol.
const PostgresDB: Plugin<QueryBuilder, [PostgresConfig], DBActions>
// DBActions:
PostgresDB.actions.close(): Future<void>Built from DB.implement({ name: 'postgres', version: '0.0.1', setup }). Its context is the QueryBuilder; its setup argument is a PostgresConfig. Pass it to install from @ozaco/std/plugin, then query through the returned builder or useDatabase().
Migrations run automatically during setup, and close ends the pool. transaction on the query builder maps to a real Postgres transaction, rolling back if the callback fails.
import { defineSchema } from '@ozaco/db'
import { PostgresDB } from '@ozaco/db/impl/postgres'
import { install } from '@ozaco/std/plugin'
function* main() {
const db = yield* install(PostgresDB, {
url: 'postgres://user:pass@localhost:5432/app',
schema: defineSchema({ users }),
max: 20,
})
return yield* db.from(users).where({ active: true }).all()
}Configuration
PostgresConfig
Install options: a connection URL, the schema, and an optional pool size.
interface PostgresConfig {
readonly url: string
readonly schema: SchemaDef
readonly max?: number
}url is a standard postgres:// connection string. schema is a SchemaDef from defineSchema, migrated on install. max caps the number of pooled connections and defaults to 10.
import { defineSchema } from '@ozaco/db'
import type { PostgresConfig } from '@ozaco/db/impl/postgres'
const config: PostgresConfig = {
url: 'postgres://user:pass@localhost:5432/app',
schema: defineSchema({ users }),
max: 20,
}