npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@datrix/adapter-postgres

v0.2.0

Published

PostgreSQL adapter for Datrix framework

Readme

Datrix PostgreSQL Adapter

PostgreSQL adapter for the Datrix framework. Provides full CRUD, relation population, migration support, and native referential integrity enforcement.

Installation

pnpm add @datrix/adapter-postgres

Requires pg (node-postgres) driver as a peer dependency.

Configuration

import { PostgresAdapter } from "@datrix/adapter-postgres";

const adapter = new PostgresAdapter({
  host: "localhost",
  port: 5432,
  user: "datrix",
  password: "datrix",
  database: "myapp",
  max: 10,
  min: 2,
  connectionTimeoutMillis: 5000,
  idleTimeoutMillis: 30000,
  applicationName: "myapp",
  // Optional
  ssl: { rejectUnauthorized: false },
});

Requirements

  • PostgreSQL 12+ — The adapter uses json_agg(), row_to_json(), and LATERAL joins for efficient relation population.
  • Native foreign key constraints are fully supported and automatically managed by the framework migrations.

Architecture

src/
├── adapter.ts                  # Main adapter logic & connection pool handling
├── query-translator.ts         # Translates Datrix QueryObjects into raw SQL
├── pg-client.ts                # Pool/PoolClient wrapper with debug logging and error mapping
├── types.ts                    # PostgreSQL-specific type mappings and query types
├── test-utils.ts               # Test database setup helpers
├── index.ts                    # Public package exports
└── populate/
    ├── index.ts
    ├── populator.ts            # Strategy selection and batched recursive fetching
    ├── aggregation-builder.ts  # Generates json_agg() / row_to_json() subqueries
    ├── join-builder.ts         # Dynamic JOIN string constructor
    └── result-processor.ts     # JSON field parsing and final data formatting

Populate Strategies

Three strategies are employed dynamically based on query depth and complexity:

  • JSON Aggregation — Default for single-level relations. Uses json_agg() and row_to_json() in a single efficient query. Groups by primary key and produces fully populated JSON in the database—no extra round-trips.

  • LATERAL Joins — Used when populate options include limit, offset, where, or orderBy. Generates a LEFT JOIN LATERAL (...) subquery per relation, allowing per-relation constraints while remaining within a single SQL query.

  • 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. Supports recursive nested population.

Migration

Migration operations map directly to native PostgreSQL DDL commands (CREATE TABLE, ALTER TABLE, CREATE INDEX, etc.). Since PostgreSQL supports transactional DDL, migrations are fully rollback-safe — all structural changes can be reverted if a migration fails partway.

Known Limitations

  • No partial or expression indexes. Only simple field indexes with an optional unique constraint.
  • Auto-increment IDs are not gap-free. Counter increments are atomic but failed inserts do not reclaim IDs.
  • json_agg on empty sets returns null, not an empty array []. The ResultProcessor handles this and normalizes the value to [].

Testing

# PostgreSQL (default port 5432)
ADAPTER=postgres pnpm test

Docker setup for test database:

docker run -d --name postgres-test \
  -e POSTGRES_USER=datrix_test \
  -e POSTGRES_PASSWORD=datrix_test \
  -e POSTGRES_DB=datrix_test \
  -p 5432:5432 \
  postgres:16