pg-prisma package

Prisma Next (8.x) and Postgres building blocks for @imqueue services.

Two kinds of thing live here, and they are used at different times.

Query middlewares rewrite the statement before it is lowered to SQL: stamp turns deletes into deletedAt stamps, hides stamped rows and records who created, updated or deleted a row; accessScope narrows every read to the records the caller is allowed to see; and audit writes a trail of every write to a table you nominate. dataLayer builds all three from an emitted contract in one call and is the entry point for the ordinary case — the individual factories are there to compose something it does not cover.

Installers and tools run once at startup or by hand rather than per query: installArchiving moves aged rows into a mirror archive schema on a pg_cron schedule, installChangeTriggers makes Postgres NOTIFY on every row change, and prettifySql/silently/isSqlLogSuppressed are query-logging helpers.

The per-model configuration is **derived from contract.json** by deriveDataLayer rather than generated: Prisma Next has no custom-generator protocol and needs none, since the contract already names every model, field and physical column. Nothing is written to disk and nothing can go stale against the schema. Access levels are the one thing that cannot be derived — Prisma Next has no schema annotation to carry them — so they are declared where dataLayer is called.

The middlewares commute: stamp merges what were two order-dependent Prisma 7 extensions, so there is no ordering left for a caller to get wrong.

Functions

Function

Description

accessScope(input)

Build the middleware restricting statements to the records a caller may see.

audit(input)

Build the middleware recording every write to the tables it is given.

dataLayer(options)

Build the whole data layer from an emitted contract, in one call.

dataPool(config, parsers)

A connection pool whose arrays of enums and JSON columns can be read.

deriveDataLayer(input)

Derive the data-layer config from an emitted contract.

emitAll(input)

Emit every generated file for a contract.

emitEnums(input)

Emit the enum constants for a contract.

emitImports(names, map)

Emit the import statements for the runtimes a file uses.

emitModels(input)

emitRpcTypes(input)

Emit the RPC query, input and argument classes for a contract.

hasDatabaseDefault(models, columnsOf)

Whether the database fills a column in when an insert leaves it out.

installArchiving(options)

Install the row-archiving machinery: a mirror archive schema, its settings table, the sweep function, and — best effort — a pg_cron schedule to run it.

installChangeTriggers(client, input)

Install Postgres triggers that NOTIFY on every row change in the given tables.

isoDates(input)

Read database timestamps back as canonical ISO 8601 instants.

isSqlLogSuppressed()

Whether SQL logging is currently suppressed.

join(parts, separator)

Several fragments, one after another.

namespaceOf(contract, namespace, omit)

parseImportMap(spec)

Read an import map from its generator-option spelling.

prettifySql(sql)

Format a one-line SQL string so it is readable in a log.

queryLog(input)

Build the middleware that logs each statement and how long it took.

quoted(value)

Quote a @property type string.

raw(text)

Text spliced in as written, binding nothing.

repositoriesFor(db, input)

Build the CRUD façade the RPC surface delegates to.

scopePredicate(qualifier, levels, resolvers)

Compose the scope predicate for one table, or null when nothing constrains it.

silently(fn)

Run fn with SQL logging suppressed.

sql(strings, values)

SQL as a tagged template, with everything interpolated bound.

sqlRunner(pool)

A query and a transaction that agree about which connection to use.

stamp(input)

Build the middleware that stamps authorship and turns deletes into stamps.

toOrdering(orderBy)

Turn a wire ordering into the callbacks .orderBy() takes.

toPredicate(relations, model, where)

Turn a wire filter into the predicate callback .where() takes.

toProjection(relations, model, select)

Split a wire projection into its scalar and relation halves.

toQuery(fragment)

A fragment as the pair query takes.

transactionFor(db, options)

Repositories bound to a transaction.

typeOf(field, enums, listEnum)

The TypeScript spelling of a field, and the @property type string.

withoutChangeNotify(client, fn, setting)

Run fn in a transaction whose row changes notify nobody.

withTransaction(pool, fn)

Run fn inside a transaction on one connection.

Interfaces

Interface

Description

AccessScopeOptions

Everything accessScope() needs to build its middleware.

ArchivableModel

One watched table to seed into the archive settings table.

AuditColumns

Column names within the audit table.

AuditConfig

Where the trail goes and what its columns are called.

AuditOptions

Everything audit() needs to build its middleware.

BulkCount

How many rows a bulk write affected.

ChangeTriggerConfig

Which tables notify, and under which Postgres object names.

ContractJson

The slice of an emitted contract.json this derivation reads.

DataLayer

What dataLayer() hands back.

DataLayerAudit

The audit half of DataLayerOptions, omitted to record nothing.

DataLayerOptions

Everything dataLayer() needs.

DerivedDataLayer

The config the middlewares consume, keyed by physical table.

DeriveFields

Field names to look for, where they are not the defaults.

DeriveOptions

Everything deriveDataLayer() needs.

EmitAllOptions

Everything emitAll() needs.

EmitContract

The slice of an emitted contract.json the emitter reads.

EmitModelsOptions

Everything emitModels() needs.

EmitRpcOptions

Everything emitRpcTypes() needs.

EnumDef

Field

FilterOps

The comparison operators a caller may send for one field.

InstallArchiveOptions

Everything installArchiving() needs.

IsoDateOptions

Everything isoDates() needs.

Model

Page

A page of entities, and the total when one was asked for.

PageOptions

Paging and whether to count.

Projection

A projection split into what select takes and what include takes.

QueryLogger

Where a query log line goes.

QueryLogOptions

Everything queryLog() needs.

Relation

RelationInfo

One relation, as the query translator needs it.

Repository

The CRUD surface exposed for one model.

RepositoryOptions

Everything repositoriesFor() needs.

RuntimeModule

A module the generated code imports from, and what it takes from it.

SqlConnection

A connection held for the length of a transaction.

SqlExecutor

The minimum this package needs of a Postgres connection.

SqlFragment

A statement and the values bound into it.

SqlPool

An executor that can hand out a dedicated connection.

SqlRunner

What sqlRunner() returns.

StampColumns

Columns a model is stamped with, by physical column name.

StampOptions

Everything stamp() needs to build its middleware.

Transactional

A client that can run work inside one transaction.

TypeParsers

What pg-types offers, of which only these two are wanted.

Variables

Variable

Description

AuditAction

The three write actions the trail records.

CHANGE_NOTIFY_CHANNEL

Default Postgres NOTIFY channel the change triggers emit on.

CHANGE_NOTIFY_FUNCTION_NAME

Default name of the trigger's plpgsql notify function.

CHANGE_NOTIFY_SUPPRESS_SETTING

Setting the trigger reads to decide whether to stay quiet.

CHANGE_NOTIFY_TRIGGER_NAME

Default name of the per-table change trigger.

EMPTY

A fragment that contributes nothing, for the empty case.

RUNTIME

Where each runtime lives by default.

TIMESTAMP_CODECS

Codecs whose values arrive as Postgres' own timestamp text.

Type Aliases

Type Alias

Description

AccessScopeResolver

Resolves the current request's value for one access level: - undefined — the level does not constrain this request (skip it), - null — active but valueless, so deny (match nothing), - a string — match rows where a scope column equals it, - an array — match rows where a scope column is IN it (empty denies).

ArchiveClient

The raw-SQL surface this installer needs.

Direction

Sort direction.

EnumNames

Member names per enum, where the label is not the name.

Fields

The object a predicate callback is handed.

ImportMap

Original specifier to the specifier to emit in its place.

OrderBy

An ordering as it arrives over the wire: { createdAt: 'desc' }.

RawClient

A RawExecutor that can also open a transaction.

RawExecutor

The raw-SQL surface used to install triggers. A pg.Pool satisfies it.

RelationMap

Relations per model, by field name.

RowOf

A model as a row that was read whole.

RuntimeName

A runtime the generated code can import from.

ScopeTables

Scope columns per physical table, per access level.

Select

A projection as it arrives over the wire: { id: true, user: { … } }.

StampTables

Stamp columns per physical table.

StorageTables

Storage columns per physical table.

ValidationRules

Zod suffixes per model per field, as the schema's @validate declared.

Where

A filter as it arrives over the wire.

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.