feat: one install and one import per database; server dialects open their pool from a URL; nodeListener answers a throwing context; 0.5.0

This commit is contained in:
2026-11-03 13:06:04 +01:00
parent 1391c21c1f
commit e7637c3704
24 changed files with 194 additions and 227 deletions
+3 -9
View File
@@ -1,6 +1,6 @@
{
"name": "@crudy/restloom-postgres",
"version": "0.4.0",
"version": "0.5.0",
"type": "module",
"description": "Postgres dialect for restloom",
"keywords": ["restloom", "postgres", "postgresql", "rest", "api"],
@@ -18,18 +18,12 @@
"prepack": "npm run build"
},
"dependencies": {
"@crudy/restloom": "^0.4.0"
},
"peerDependencies": {
"pg": "^8.11.0"
},
"peerDependenciesMeta": {
"pg": { "optional": true }
"@crudy/restloom": "^0.5.0",
"pg": "^8.16.0"
},
"devDependencies": {
"@types/node": "^24.0.0",
"@types/pg": "^8.11.0",
"pg": "^8.16.0",
"typescript": "^5.9.0"
}
}
+12 -3
View File
@@ -1,10 +1,17 @@
import { SqlRenderer, type Dialect, type Tx } from '@crudy/restloom';
import type { Pool } from 'pg';
import pg, { type Pool, type PoolConfig } from 'pg';
import { mapError } from './errors.js';
import { introspect } from './introspect.js';
export * from '@crudy/restloom';
export { mapError } from './errors.js';
/** A pg Pool, or what opens one: a connection string or a pool config. */
export type PostgresConnection = Pool | PoolConfig | string;
const poolOf = (c: PostgresConnection): Pool =>
typeof c === 'string' ? new pg.Pool({ connectionString: c }) : typeof (c as Pool).connect === 'function' ? (c as Pool) : new pg.Pool(c as PoolConfig);
/** Postgres writes every primitive the core's defaults assume; subclass it to change one. */
export class PostgresRenderer extends SqlRenderer {}
@@ -13,11 +20,13 @@ export interface PostgresOptions {
}
/**
* The Postgres dialect over a `pg` Pool. Every request runs in one transaction
* The Postgres dialect over a `pg` Pool, given or opened from a connection
* string or config. Every request runs in one transaction
* with a local statement timeout, read-only for reads, so a `transaction` hook
* can set the role and session settings that row security reads.
*/
export function postgres(pool: Pool, options: PostgresOptions = {}): Dialect {
export function postgres(connection: PostgresConnection, options: PostgresOptions = {}): Dialect {
const pool = poolOf(connection);
return {
name: 'postgres',
capabilities: { returning: true, estimatedCount: true, rowSecurity: true, procedures: true },