@primebrick/dal-pg
@primebrick/dal-pg is a shared Data Access Layer library for Primebrick v3.
It provides a type-driven, metadata-based Repository for PostgreSQL. Entities
are plain TypeScript classes decorated with @Entity, @Column, @Key,
@Unique, @AuditableField, @DeletableField, and @CloneField. The
Repository reads entity metadata at runtime to generate parameterized SQL.
What it gives you
- Type-safe entities — plain TS classes + decorators, no schema files to keep in sync.
- Metadata-driven SQL —
Repositoryreads entity metadata at runtime and emits parameterized SQL (RETURNING *on every write). - Query DSL — composable
field(),Filter,Sort,Join,Projectexpressions, type-checked at compile time. - Bulk operations —
addMany,upsertMany,updateManyusing a TEMP TABLE strategy (atomic, SQL-injection safe). - Soft delete — opt in via
@DeletableField; finders exclude soft-deleted rows by default. - Audit trail —
@AuditTrail()+AuditPortproduce field-level deltas fire-and-forget. - Optimistic locking — automatic for auditable entities; stable
ERR01/ERR02/ERR03codes. - Clone —
Repository.clone()copies a record by UUID, resetting audit/unique fields. - Streaming —
findAll({ stream: true })returns anAsyncIterablebacked by a pg cursor. - Dal gateway —
getDal()singleton owns the pool, registers type parsers (INT8→bigint,NUMERIC→number), and setssearch_path/statement_timeout/application_nameon every connection. - Framework-agnostic errors —
DalErrorcarries a stablecodestring; consumers map it to HTTP/NATS at their own boundary.
Architecture at a glance
The Dal gateway owns the connection pool and is the recommended entry point.
The Repository is the low-level engine — it accepts any Queryable (pool or
pooled client), which is how it participates in transactions via dal.withClient().
Hello World
A minimal end-to-end example: define an entity, bootstrap the Dal gateway, and perform one insert + one read.
Code
Where to go next
| If you want to… | Read |
|---|---|
| Get a service running end-to-end | Getting started |
| Understand the layers and design decisions | Architecture |
| Define entities with all decorators | Entities & decorators |
| Build type-safe queries | Query DSL |
| Use the Repository API (finders, writes, bulk, streaming) | Repository |
| Manage the pool, transactions, timeouts, shutdown | Connections & transactions |
| Track field-level changes | Audit trail |
| Prevent lost updates | Optimistic locking |
| Copy records by UUID | Clone |
| See what changed per release | Changelog |
| Look up a specific symbol | API reference |
Next steps
- Getting started — install, configure, and run your first query.
- Architecture — how the layers fit together.
Last modified on