Skip to content
LogoLogo

Store.postgres

Postgres storage

Creates an atomic store from a Postgres client's query method.

Usage

store.ts
import { Store } from 'mppx'
import { Pool } from 'pg'
 
const db = new Pool({ connectionString: process.env.DATABASE_URL })
const store = Store.postgres(db) 
 
console.log(await Store.tryClaim(store, 'request:customer-42:1', Date.now() + 60_000))
true

Supports pg, Neon's query clients, @vercel/postgres, Supabase Postgres connections, and PGlite. For Postgres.js unsafe, use Store.sql.

Return type

type ReturnType = Store.AtomicStore
  • Provides get, put, delete, atomic update, and replay claims; see the AtomicStore contract.
  • Keep update callbacks synchronous and side-effect-free: conflicts rerun them.

Parameters

client

  • Type: Store.postgres.Client

Client exposing query(text: string, values: unknown[]): Promise<{ rows: Store.sql.Row[] } | Store.sql.Row[]>, including RETURNING rows.

options (optional)

  • Type: Store.sql.Options
PropertyTypeDefaultDescription
createTable (optional)booleantrueCreates the table on first use; set false for a migration-managed table.
keyPrefix (optional)stringNonePrefix for every backing key.
maxAttempts (optional)number32Maximum compare-and-swap attempts per update.
table (optional)string'mppx_store'Valid name or schema.name identifier.

See Store.sql for schema, migrations, serialization, and conflict errors.