Skip to content

DB

Singleton class that initializes and provides access to a configured pg-promise database instance.

DB is the compatibility default instance: internally it holds one Database built by createDb(), and republishes its handles on the static DB.db / DB.pgp fields. It remains the simplest entry point for a single-database application.

Applications that need more than one database in a process — an admin database plus one or more cells — should call createDb() directly instead. DB connects exactly one.

Import:

js
import { DB, db, pgp } from 'pg-schemata';

DB.init(connection, repositories, logger?, options?)

Initializes the database singleton. Call once at application startup.

ParameterTypeRequiredDescription
connectionstring | objectYespg-promise connection string or config object
repositoriesRecord<string, Function>YesMap of repository names to model constructors
loggerobjectNoLogger instance passed to each repository (default null)
optionsobjectNoConfiguration options
options.auditActorResolver() => string | nullNoCallback returning the current actor ID for audit fields

Returns: DB class (for chaining)

Throws: Error if connection or repositories are invalid

js
import { DB } from 'pg-schemata';
import { Users } from './models/Users.js';

DB.init(process.env.DATABASE_URL, { users: Users });

With audit resolver:

js
DB.init(process.env.DATABASE_URL, { users: Users }, console, {
  auditActorResolver: () => als.getStore()?.userId ?? null,
});

Calling init() multiple times is safe — subsequent calls are complete no-ops: the database is not recreated, and an auditActorResolver passed to a later call is ignored.

DB.close()

Closes the default instance's pool and resets the singleton so a later DB.init() starts cleanly. It clears DB.db, DB.pgp, and — only when a default instance existed — the audit resolver registered through DB.init(). Closing an uninitialized singleton leaves a resolver set by setAuditActorResolver() alone.

Returns: Promise<void>

js
await DB.close();

Safe before initialization, and safe to call repeatedly or concurrently. Use it instead of pgp.end(), which destroys every pg-promise pool in the process — including handles owned by createDb() instances.

DB.db and DB.pgp remain writable fields in 3.x, so existing code that assigns them keeps working. Reassignment is discouraged: DB.close() is the supported way to reset the singleton.

db()

Returns the initialized pg-promise database instance.

Returns: IDatabase<any> — the pg-promise database object with attached repositories

js
import { db } from 'pg-schemata';

const user = await db().users.findById(id);

pgp()

Returns the pg-promise library instance.

Returns: IMain — the pg-promise root library

js
import { pgp } from 'pg-schemata';

const formatted = pgp().as.format('WHERE id = $1', [id]);

A lightweight Postgres-first ORM layer.