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

@airdraft/db-adapter-postgres

v0.1.2

Published

Airdraft PostgreSQL database adapter via pg

Readme

@airdraft/db-adapter-postgres

PostgreSQL storage adapter for Airdraft via pg.

Use this adapter to store Airdraft content in a PostgreSQL database — suitable for self-hosted deployments, managed cloud databases (Neon, Supabase, RDS, PlanetScale Postgres, etc.), and multi-tenant setups where multiple Airdraft projects share a single connection pool.


Installation

npm install @airdraft/db-adapter @airdraft/db-adapter-postgres pg
npm install -D @types/pg

Quick start

// airdraft.config.ts
import { defineConfig } from '@airdraft/core'
import { PostgresAdapter } from '@airdraft/db-adapter-postgres'

export default defineConfig({
  adapter: new PostgresAdapter({
    connectionString: process.env.DATABASE_URL!,
  }),
  // … collections, plugins
})

Add to .env.local:

DATABASE_URL=postgresql://user:pass@localhost:5432/mydb

Run the migration on first deploy (idempotent — safe to run on every deploy):

// instrumentation.ts  (Next.js server-start hook)
import airdraft from '@/airdraft.config'
import { BaseDatabaseAdapter } from '@airdraft/db-adapter'

export async function register() {
  if (airdraft.adapter instanceof BaseDatabaseAdapter) {
    await airdraft.adapter.migrate()
  }
}

Options

new PostgresAdapter(options)

| Option | Type | Default | Description | |---|---|---|---| | connectionString | string | — | PostgreSQL connection string (postgresql://…). | | pool | Pool | — | Provide an existing pg.Pool to share across adapters (e.g. in multi-tenant cloud). Mutually exclusive with connectionString. | | projectId | string | 'default' | Namespace for multi-tenant deployments. | | history | boolean | false | Mirror every write to airdraft_entry_history. | | cacheTtlMs | number | 0 | Per-entry read cache TTL. 0 disables caching. | | cacheMaxSize | number | 500 | Maximum entries in the LRU cache. |


Schema

airdraft_entries

| Column | Type | Notes | |---|---|---| | id | SERIAL PRIMARY KEY | | | project_id | TEXT NOT NULL | Multi-tenancy namespace | | collection | TEXT NOT NULL | | | slug | TEXT NOT NULL | | | sha | TEXT NOT NULL | SHA-256 of the serialized content | | data | JSONB NOT NULL | Entry data | | published | BOOLEAN | NULL = published (default), FALSE = draft | | created_at | TIMESTAMPTZ | | | updated_at | TIMESTAMPTZ | |

Unique constraint on (project_id, collection, slug). pg error code 23505ConflictError.

airdraft_entry_history (when history: true)

Same columns as airdraft_entries minus id/published, plus a SERIAL history id.


Features

  • JSONB storagedata is stored as native JSONB. Use (data->>'field')::numeric for numeric comparisons.
  • Optimistic concurrency — SHA checked on every write; ConflictError thrown on mismatch.
  • Efficient paginationCOUNT(*) OVER() window function returns total rows in a single query.
  • Field filtersqueryEntries() maps filter entries to data->>'field' = $n predicates. $contains maps to ILIKE.
  • Transactions — history snapshots written inside a BEGIN/COMMIT block.
  • Connection pooling — Uses a pg.Pool; pass pool to share across multiple adapter instances.
  • Atomic field opsincrement, set, push, pull with SHA re-computation. pull correctly returns [] (not NULL) when the last element is removed.

Multi-tenant cloud setup

When many projects share one Postgres cluster, pass a shared pool and set projectId per project:

import { Pool } from 'pg'
import { PostgresAdapter } from '@airdraft/db-adapter-postgres'

const pool = new Pool({ connectionString: process.env.DATABASE_URL })

// per-project adapter, e.g. in a Next.js route handler
const adapter = new PostgresAdapter({ pool, projectId: project.slug })

Testing

Runs the shared contract suite using @electric-sql/pglite — a WASM Postgres that needs no Docker or external service:

npm test

The test file (src/__tests__/contract.test.ts) creates an in-memory PGlite instance, wraps it in a pg.Pool-compatible shim, and runs two suites: one without history and one with history enabled.


License

MIT