Skip to main content

Postgres adapter

The Postgres adapter introspects via pg_catalog system tables and produces SQL with $1, $2, ... positional parameters and double-quoted identifiers.

Setup

import pg from 'pg';
import { Biref, postgresAdapter } from '@biref/scanner';

const client = new pg.Client({ connectionString: 'postgres://localhost/mydb' });
await client.connect();

const biref = Biref.builder()
.withAdapter(postgresAdapter.create(client))
.build();

Client interface

postgresAdapter.create() accepts any object matching this structural type:

interface PostgresClient {
query<TRow>(
text: string,
params?: readonly unknown[],
): Promise<{ rows: TRow[] }>;
}

Both pg.Client and pg.Pool satisfy this shape. The SDK never imports pg.

Namespace model

Postgres uses schemas as namespaces. Default: public.

// Scan specific schemas
const model = await biref.scan({ namespaces: ['public', 'auth', 'billing'] });

// Scan everything (excludes pg_catalog, information_schema, pg_toast*, pg_temp_*)
const model = await biref.scan({ namespaces: 'all' });

URL schemes

postgres, postgresql

Type mapping

Postgres typeCategory
int2, int4, int8, serial, bigserialinteger
numeric, decimal, real, float4, float8, moneydecimal
text, varchar, char, citext, namestring
bool, booleanboolean
datedate
timestamp, timestamptztimestamp
time, timetztime
json, jsonbjson
uuiduuid
byteabinary
custom enum typesenum
any[]array (with recursive elementType)

Record parser

PostgresRecordParser extends DefaultRecordParser with:

  • int8 / bigint strings converted to JS bigint
  • json / jsonb strings parsed to objects
  • All other categories fall through to the default parser

Indexes

Supports: btree, hash, gin, gist, brin, spgist. Partial indexes are detected.

Constraints

Supports: unique, check, exclusion.