@justscale/postgres
v0.1.3
Published
PostgreSQL adapter with repositories, migrations, and LISTEN/NOTIFY for JustScale
Downloads
348
Maintainers
Readme
@justscale/postgres
PostgreSQL adapter for JustScale — client, repositories, migrations, distributed locks via advisory locks, and a channel backend that fans messages out over LISTEN/NOTIFY.
Requires PostgreSQL 16+.
Install
pnpm add @justscale/postgres postgrespostgres (the postgres.js driver, v3.4.9+) is a peer dependency. This package ships a patch for [email protected]; install that exact minor or later. @electric-sql/pglite / @electric-sql/pglite-socket are optional peers used by the /dev and /testing subpath exports for in-process Postgres.
Usage
import JustScale, {
AbstractChannelBackend,
ChannelFeature,
bindService,
} from '@justscale/core';
import {
createPostgresClient,
createPostgresChannelBackend,
createPgModel,
createPgRepository,
PostgresLockFeature,
PostgresProcessFeature,
PostgresMigrationFeature,
} from '@justscale/postgres';
import { User } from './domain/user.js';
const connectionString = process.env.DATABASE_URL!;
const PgClient = createPostgresClient({ connectionString });
const PgChannel = createPostgresChannelBackend({ connectionString });
const PgUser = createPgModel(User, { table: 'users' });
const UserRepository = createPgRepository(PgUser);
const app = JustScale()
.add(PgClient)
.add(bindService(AbstractChannelBackend, PgChannel))
.add(ChannelFeature)
.add(PostgresLockFeature)
.add(PostgresProcessFeature)
.add(PostgresMigrationFeature)
.add(UserRepository)
.build();Services inject the abstract ModelRepository.of(User) token and stay storage-agnostic — the createPgRepository wiring stays in app.ts. PostgresProcessFeature binds the durable-process storage so createProcess handlers survive restarts.
What's included
- Client —
createPostgresClient(pooled),createRawPostgresClient;AbstractPostgresClientfor DI; implicit-transaction context viagetCurrentTransactionContext. - Models + repositories —
createPgModelmaps adefineModelclass to a table;createPgRepositoryproduces a DI-compatibleRepository<T>with typed queries, locking, and change streams viaModelChangeChannels. - Locks —
PostgresLockFeaturebacks@justscale/core/lockwith advisory locks. Strategies pick betweenpg_advisory_lockand a dedicated table; context tracking viawithLockContext/getCurrentLocks. - Channel backend —
createPostgresChannelBackendfans published channel messages out throughLISTEN/NOTIFYso every instance on the same database sees each publish exactly once. - Pub/Sub primitive —
createPostgresPubSubif you want rawLISTEN/NOTIFYoutside the channels abstraction. - Migrations —
PostgresMigrationFeatureadds themigrateCLI subset (run,status,pending,rollback). Dev-only commands (make,fresh,verify) live under@justscale/postgres/dev. - Durable iteration —
PgQueryIteratordrivesfor awaitloops inside durable processes with keyset pagination, so long-running jobs can resume mid-iterate.
Subpath exports
@justscale/postgres— production surface (client, repositories, migrations-prod, locks, channel, process storage).@justscale/postgres/dev— dev-only migration commands (make,fresh,verify, auto-sync tooling) that need a writable workspace.@justscale/postgres/testing—PostgresTestBundle, pglite-based containers, TRUNCATE helpers for fast test isolation.
Known limitations
Cluster coordinator migration not included. ClusterNode (multi-instance coordination) requires its own schema table. That migration is not yet shipped in this package — if you use @justscale/core/cluster with Postgres you will need to create the table manually. This will be resolved before 1.0.
Docs
https://justscale.sh/docs/postgres/overview · https://justscale.sh/docs/repositories/overview · https://justscale.sh/docs/fundamentals/locks · https://justscale.sh/docs/fundamentals/channels
