PostgreSQL Adapter
$pnpm add @datrix/adapter-postgres
Requires pg (node-postgres) as a peer dependency.
Requirements
- PostgreSQL 12+ — the adapter uses
json_agg(),row_to_json(), andLATERALjoins for relation population. - Native foreign key constraints are fully supported and automatically managed by migrations.
Configuration
import { PostgresAdapter } from "@datrix/adapter-postgres"
new PostgresAdapter({
host: "localhost", // database server hostname
port: 5432, // database server port
database: "mydb", // database name
user: "postgres", // login user
password: "secret", // login password
// SSL — pass true to enable with default settings,
// or an object for certificate-based auth
ssl: {
rejectUnauthorized: true,
ca: "...", // CA certificate (PEM)
cert: "...", // client certificate (PEM)
key: "...", // client private key (PEM)
},
// Connection pool
max: 10, // maximum number of connections in the pool
min: 2, // minimum number of idle connections to maintain
// Timeouts (milliseconds)
connectionTimeoutMillis: 5000, // time to wait for a new connection
idleTimeoutMillis: 30000, // time a connection can sit idle before being closed
applicationName: "my-app", // shown in pg_stat_activity
})
Populate strategies
Three strategies are selected automatically based on query depth and complexity:
- JSON Aggregation — default for single-level relations. Uses
json_agg()androw_to_json()in a single query — no extra round-trips. - LATERAL Joins — used when populate options include
limit,offset,where, ororderBy. Generates aLEFT JOIN LATERAL (...)subquery per relation. - Batched IN Queries — fallback for deep nesting (depth > 1) or high cardinality. Collects parent IDs and issues targeted
WHERE id = ANY($1)queries, stitching results in Node.js memory.
Migration
All migration operations map directly to native PostgreSQL DDL (CREATE TABLE, ALTER TABLE, CREATE INDEX, etc.). PostgreSQL supports transactional DDL, so migrations are fully rollback-safe — structural changes are reverted automatically if a migration fails.
Known limitations
- No partial or expression indexes. Only simple field indexes with an optional unique constraint.
- Auto-increment IDs are not gap-free. Failed inserts do not reclaim IDs.
json_aggon empty sets returnsnull, not[]. The adapter normalizes this to[]automatically.
Field type mapping
| Datrix type | PostgreSQL column |
|---|---|
string | TEXT (or VARCHAR(n) when maxLength is set) |
number | DOUBLE PRECISION (or INTEGER for foreign keys) |
boolean | BOOLEAN |
date | TIMESTAMP WITH TIME ZONE |
json | JSONB |
array | JSONB |
enum | VARCHAR |
file | TEXT |
relation | INTEGER (foreign key) |