Skip to content

@storyshelf/db-postgres

View as Markdown

@storyshelf/db-postgres connects StoryShelf to Postgres through postgres.js and Drizzle ORM. It implements the same DatabaseAdapter contract as SQLite (including its presets), so the rest of the application does not change when switching databases.

Terminal window
npm i @storyshelf/db-postgres

JSR JSR Score

import { createPostgresDatabase } from "@storyshelf/db-postgres";
const database = createPostgresDatabase({
url: process.env.DATABASE_URL!,
});

url (or connectionString) is required unless a preconfigured client is injected. Optional postgres.js tuning is forwarded via ssl, prepare, max, idleTimeout, and connectTimeout. Migrations run inside the adapter's lifecycle.setup — await them via app.lifecycle.setup() after creating the router, and close via app.lifecycle.teardown() on shutdown. The adapter implements DatabaseAdapter: insert, update, get, remove, list, count, and all, plus metadata/lifecycle from the shared Adapter base.

// With explicit pooling and TLS
const database = createPostgresDatabase({
url: process.env.DATABASE_URL!,
ssl: true,
prepare: true,
max: 10,
idleTimeout: 30,
connectTimeout: 10,
});

All providers speak the standard Postgres wire protocol. Pick the connection string and options for your host.

ProviderConnection string shapeTLSPooling notes
Self-hostedpostgres://user:pass@localhost:5432/shelfssl: false or omit for plain TCP; set ssl: true if the server enforces TLSDefault max: 10; tune max / idleTimeout for your host
AWS RDSpostgres://user:pass@<rds-endpoint>:5432/db?sslmode=requiressl: true for default CA; or ssl: { ca: rdsCaBundle } for a custom RDS CA bundleIAM auth via injected client with a refreshed token (see below); prepare: true (default) is fine
GCP Cloud SQLpostgres://user:pass@<cloudsql-ip>:5432/dbssl: { ca, cert, key } for mutual TLS (Cloud SQL server + client certs)Use Cloud SQL Connector or inject a client with IAM credentials; mutual TLS required when not using the proxy
Supabasepostgres://postgres.<ref>:<pass>@<pooler-host>:6543/postgres?pgbouncer=true (pooler) or :5432/postgres (direct)ssl: true (Supabase enforces TLS)PgBouncer (port 6543, transaction mode) requires prepare: false; direct connections on 5432 can leave prepare: true
Neonpostgres://user:pass@<neon-host>/db?sslmode=requiressl: trueServerless pooler — keep max low (e.g. max: 5); pooled endpoints via PgBouncer need prepare: false
Azure Database for PostgreSQLpostgres://user:pass@<server>.postgres.database.azure.com:5432/db?sslmode=requiressl: trueMicrosoft Entra ID (IAM) via injected client with an access token; otherwise password auth works with default pooling

When client is supplied, all other connection options are ignored and the caller owns the client lifecycle. Use this for IAM token refresh or pre-configured TLS:

import postgres from "postgres";
import { createPostgresDatabase } from "@storyshelf/db-postgres";
// Example: IAM — fetch a fresh token before connecting (RDS, Azure Entra ID, etc.)
const client = postgres(process.env.DATABASE_URL!, {
ssl: true,
// password is the IAM token; refresh by recreating the client on rotation
});
const database = createPostgresDatabase({ client });

This is also the hook for hermetic tests and for custom CA bundles that are loaded from disk or a secrets manager.

Choose Postgres when you need a managed relational database or already run Postgres for your team. It is a drop-in replacement for SQLite and its presets; only the database construction changes. Pair it with local storage for single-node deployments or S3-compatible storage when instances do not share a filesystem.