Skip to content

Quick start

import connect, {
withTransaction,
upsertMutation,
classifyPgError,
} from '@smplcty/db';
// 1. A pool from DATABASE_URL, with a server-side guardrail.
const pool = connect(undefined, { statement_timeout: 30_000 });
// 2. A bulk, non-destructive upsert (UPDATE then INSERT-where-not-exists).
const { update, insert } = upsertMutation(
'widgets',
[
['id', 'int', true], // key
['name', 'text'],
],
{ bulk: true },
);
// 3. Run both halves in one transaction with the same JSONB parameter.
const rows = JSON.stringify([
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
]);
try {
await withTransaction(pool, async (client) => {
await client.query(update, [rows]);
await client.query(insert, [rows]);
});
} catch (err) {
const { httpStatus, message } = classifyPgError(err);
console.error(httpStatus, message);
}

From here, see each helper: