Registry / database / pg-schemata

pg-schemata

JSON →
library1.3.3jsnpmunverified

A lightweight Postgres-first ORM layer built on pg-promise (v1.3.3) for Node.js 18+. Defines table schemas in code, auto-generates ColumnSets for efficient CRUD, rich WHERE modifiers (like $like, $ilike, $from, $to, $in), cursor-based pagination, multi-schema support, DTO validation via Zod, extensible class inheritance, and migration management. Alternatives like Sequelize or TypeORM offer heavier abstractions; pg-schemata stays close to SQL while reducing boilerplate.

npm install pg-schemata
INSTALL
IMPORT
SIG · PG-SCHEMATA
P
pg-schemata
databasejavascriptv1.3.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TableModel
import { TableModel } from 'pg-schemata'
const { TableModel } = require('pg-schemata')
ESM-only; CommonJS require not supported. TypeScript types included.
SchemaDefinition
import { SchemaDefinition } from 'pg-schemata'
import SchemaDefinition from 'pg-schemata'
Named export, not default. Used for TypeScript schema type.
MigrationManager
import { MigrationManager } from 'pg-schemata'
const MigrationManager = require('pg-schemata').MigrationManager
ESM-only; class for programmatic migrations.
DatabaseError
import { DatabaseError } from 'pg-schemata'
Exported error class for structured error handling.
pgSchemata default
import * as pgSchemata from 'pg-schemata'
import pgSchemata from 'pg-schemata'
No default export; use namespace import to access all exports.

Defines a table schema, creates a model extending TableModel, inserts a record, queries with WHERE modifiers, and demonstrates pagination and soft delete.

import pgp from 'pg-promise'; import { TableModel } from 'pg-schemata'; const db = pgp({ host: 'localhost', port: 5432, database: 'test', user: 'user', password: process.env.PGPASSWORD ?? '' }); const userSchema = { dbSchema: 'public', table: 'users', columns: [ { name: 'id', type: 'uuid', notNull: true }, { name: 'email', type: 'text', notNull: true }, { name: 'name', type: 'text', notNull: true }, ], constraints: { primaryKey: ['id'] }, }; class UserModel extends TableModel { constructor() { super(db, userSchema); } } const userModel = new UserModel(); async function main() { // Insert a user const newUser = await userModel.insert({ id: 'uuid-1', email: 'test@example.com', name: 'Test' }); console.log('Inserted:', newUser); // Find users with flexible WHERE const users = await userModel.findWhere({ email: { $eq: 'test@example.com' } }); // Cursor-based pagination const page = await userModel.findAllWithCursor({ limit: 10 }); // Soft delete (if softDelete enabled) await userModel.removeWhere({ id: 'uuid-1' }); } main().catch(console.error);
Debug
Known issues
gotchapg-schemata requires pg-promise as a peer dependency; failing to install it results in runtime errors.
fix
Install both packages: npm install pg-schemata pg-promise
affects: >=1.0.0
gotchaThe package is ESM-only and does not support CommonJS require(). Attempting to use require() throws an error.
fix
Use import syntax or dynamic import(). Ensure project is configured for ESM (type: 'module' in package.json).
affects: >=1.0.0
breakinghasAuditFields changed from boolean to object format for custom user field types in v1.2.0. Old boolean values may cause schema errors.
fix
Update hasAuditFields to object format if custom types are needed: { enabled: true, userFields: { type: 'uuid', nullable: true, default: null } }
affects: >=1.2.0
gotchaUsing constraint unique with NULLS NOT DISTINCT requires PostgreSQL 15+; older versions ignore the option.
fix
Ensure PostgreSQL 15+ if using nullsNotDistinct: true in unique constraints.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'pg-schemata'
pg-schemata not installed, or resolution issue in package.json/tsconfig
fix
Run npm install pg-schemata pg-promise and ensure both are in dependencies.
Must use import to load ES Module: require() of ES Module not supported
Using CommonJS require() with ESM-only package
fix
Switch to import syntax: import { TableModel } from 'pg-schemata'
pg-promise is not defined
pg-promise peer dependency not installed or not imported
fix
Install pg-promise: npm install pg-promise, then import it before using pg-schemata
Upgrade
Version history
1.3.3latest on npm
Audit
Dependencies
pg-promiserequiredCore database interaction and connection management
Agent activity
5 hits · last 30 days
node
4
Resources
pg-schemata — npm install pg-schemata · libregistry