Skip to content

TableModel

Full CRUD model that extends QueryModel. Adds write operations, validation, audit field population, and soft delete management.

Import:

js
import { TableModel } from 'pg-schemata';

Constructor

js
new TableModel(db, pgp, schema, logger?)

Same parameters as QueryModel, but requires schema.constraints.primaryKey to be defined.

Throws: SchemaDefinitionError if no primary key is defined

constraints.primaryKey must be an array of column names. It generates the PRIMARY KEY constraint and determines which columns the by-id methods target — findById, update, delete, bulkUpdate, reload, and the soft-delete helpers.

Those methods accept a scalar for single-column keys, resolved against the declared column whatever it is named, or an object carrying every column for composite keys:

js
await coupons.findById('SAVE10'); // primaryKey: ['code']
await memberships.findById({ tenant_id, user_id }); // composite

Passing a scalar to a composite-key model throws SchemaDefinitionError.

Changed in 3.0.0

These methods previously emitted WHERE id = $1 against a column literally named id, whatever primaryKey declared. See primary keys.

Inherited Methods

TableModel inherits all methods from QueryModel: findAll, findById, findWhere, findOneBy, findAfterCursor, countWhere, countAll, exists, findSoftDeleted, isSoftDeleted, exportToSpreadsheet, and all utility methods.

Write Methods

Every write method accepts a trailing options object with tx — a pg-promise task or transaction context to run on. Bulk methods run directly on the supplied context instead of opening their own transaction. See Transactions.

js
await db().tx(async t => {
  await db().users.insert(dto, { tx: t });
});

insert(dto, options?)

Inserts a single row after validation and sanitization.

ParameterTypeDescription
dtoobjectData to insert
options.txobjectpg-promise task/transaction to run on

Returns: Promise<Object> — the inserted row (RETURNING *) Throws: SchemaDefinitionError if validation fails or DTO is empty

update(id, dto, options?)

Updates a record by id. Only the columns dto carries are written; every column it omits keeps its current value.

ParameterTypeDescription
idstring | numberPrimary key value
dtoobjectColumns to write. Omitted columns are not modified
options.txobjectpg-promise task/transaction to run on

When audit fields are enabled, updated_at is set to CURRENT_TIMESTAMP and any value dto supplies for it is discarded. updated_by is honored if supplied, otherwise filled from the audit actor resolver.

An empty dto is accepted only when audit fields are enabled — the audit columns alone make a valid update. Without them there is nothing to write.

Returns: Promise<Object | null> — updated row, or null if not found Throws: SchemaDefinitionError if validation fails

delete(id)

Hard deletes a row by primary key.

ParameterTypeDescription
idstring | numberPrimary key value

Returns: Promise<number> — number of rows deleted

upsert(dto, conflictColumns, updateColumns?)

Inserts or updates on conflict.

ParameterTypeDescription
dtoobjectData to insert or update
conflictColumnsstring[]Columns that define the conflict
updateColumnsstring[]Columns to update on conflict (optional — defaults to all non-conflict columns)

Returns: Promise<Object> — the inserted or updated row

deleteWhere(where)

Hard deletes rows matching a WHERE clause.

ParameterTypeDescription
whereobject | Object[]Filter criteria

Returns: Promise<number> — number of rows deleted

updateWhere(where, updates, options?)

Updates rows matching a WHERE clause.

ParameterTypeDescription
whereobject | Object[]Conditions
updatesobjectFields to update
options.includeDeactivatedbooleanInclude soft-deleted rows (default false)

Returns: Promise<number> — number of rows updated

touch(id, updatedBy?, options?)

Advances updated_at, and sets updated_by when an actor is known. No data column is written.

ParameterTypeDescription
idstring | number | objectPrimary key — a scalar, or an object for composite keys
updatedBystringActor identifier (optional — uses resolver if omitted)
options.txobjectpg-promise task/transaction to run on

Requires audit fields. With no actor supplied and no resolver configured the timestamp still advances and updated_by is left as it was.

Returns: Promise<Object | null> — updated row, or null if no active row has that id Throws: SchemaDefinitionError if audit fields are not enabled

Bulk Methods

bulkInsert(records, returning?)

Inserts multiple rows in a transaction.

ParameterTypeDescription
recordsObject[]Rows to insert
returningstring[] | nullColumns to return (optional)

Returns: Promise<number | Object[]> — row count, or array of rows if returning specified

bulkUpdate(records, returning?)

Updates multiple rows by primary key in a transaction.

ParameterTypeDescription
recordsObject[]Each must include an id field
returningstring[] | nullColumns to return (optional)

Returns: Promise<Array> — array of row counts or row arrays

bulkUpsert(records, conflictColumns, updateColumns?, returning?)

Bulk insert-or-update in a transaction.

ParameterTypeDescription
recordsObject[]Rows to upsert
conflictColumnsstring[]Conflict columns
updateColumnsstring[]Columns to update (optional)
returningstring[] | nullColumns to return (optional)

Returns: Promise<number | Object[]>

Soft Delete Methods

removeWhere(where)

Soft deletes records by setting deactivated_at = NOW().

Returns: Promise<number> — number of rows updated Throws: SchemaDefinitionError if soft delete is not enabled (the non-standard status: 403 property was removed in 2.0.0 — catch SchemaDefinitionError and map to your own HTTP status)

restoreWhere(where)

Restores soft-deleted records by setting deactivated_at = NULL.

Returns: Promise<number>

purgeSoftDeleteWhere(where?)

Permanently deletes soft-deleted rows matching conditions.

Returns: Promise<Object> — pg-promise result

purgeSoftDeleteById(id, options?)

Permanently deletes a specific soft-deleted row. Only removes rows that are already deactivated.

ParameterTypeDescription
idPrimaryKeyScalar, or an object for composite keys
options.txobjectpg-promise task/transaction to run on

Returns: Promise<Object> — pg-promise result Throws: Error if soft delete is not enabled; SchemaDefinitionError if the key does not match the declared one

Import/Export

importFromSpreadsheet(filePath, sheetIndex?, callbackFn?, returning?)

Imports data from an Excel file into the table.

ParameterTypeDefaultDescription
filePathstringSource .xlsx file path
sheetIndexnumber0Sheet index (0-based)
callbackFn(row) => ObjectnullTransform function per row
returningstring[]nullColumns to return

Returns: Promise<{ inserted: number | Object[] }>

Schema Management

createTable()

Creates the table from the schema definition, including indexes.

Returns: Promise<void>

truncate()

Truncates the table and resets identity sequences.

Returns: Promise<void>

A lightweight Postgres-first ORM layer.