Registry / database / pg-lightquery

pg-lightquery

JSON →
library0.4.5jsnpmunverified

pg-lightquery is a modern, type-safe PostgreSQL query builder for Node.js with TypeScript support (v0.4.5). It offers deferred execution, fluent CTE-based multi-table operations, smart operators (LIKE, IN, date ranges, JSON queries), complex joins, transaction builder, optional audit fields with lastChangedBy tracking, and minimal dependencies (only 4 core packages). Unlike heavier ORMs like Sequelize, it focuses on composable, security-first design with separate column validation and data projection. Suitable for Node.js applications requiring type-safe SQL generation with full control.

npm install pg-lightquery
INSTALL
IMPORT
SIG · PG-LIGHTQUERY
P
pg-lightquery
databasejavascriptv0.4.5
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.

PostgresConnection
import PostgresConnection from 'pg-lightquery';
const PostgresConnection = require('pg-lightquery'); // CJS not supported
Default export. Must be imported with default import in ESM. CJS require() will fail as package is ESM-only.
TableBase
import { TableBase } from 'pg-lightquery';
import TableBase from 'pg-lightquery'; // TableBase is not a default export
Named export. TypeScript definitions included.
EnhancedTableBase
import { EnhancedTableBase } from 'pg-lightquery';
import EnhancedTableBase from 'pg-lightquery'; // Must use named import
Named export for advanced table relationships. TypeScript types included.
ColumnDefinition
import type { ColumnDefinition } from 'pg-lightquery';
import { ColumnDefinition } from 'pg-lightquery'; // Type-only import preferred for runtime efficiency
TypeScript type export. Use 'import type' to avoid runtime inclusion.

Shows how to initialize a connection, define a schema with TypeScript types, create a table class using TableBase, and execute a type-safe insert operation.

import PostgresConnection, { TableBase, ColumnDefinition, TableDefinition } from 'pg-lightquery'; // Initialize connection PostgresConnection.initialize({ host: 'localhost', port: 5432, user: 'your_user', password: 'your_password', database: 'your_database', }); // Define schema const usersColumns = { id: { type: 'INTEGER', primaryKey: true, autoIncrement: true }, name: { type: 'TEXT', notNull: true }, email: { type: 'TEXT', unique: true }, } as const; type UsersSchema = { [K in keyof typeof usersColumns]: ColumnDefinition }; const usersTable: TableDefinition<UsersSchema> = { tableName: 'users', schema: { columns: usersColumns }, }; // Create table class class UsersTable extends TableBase<UsersSchema> { constructor() { super(usersTable); } insertUser(userData: { name: string; email: string }) { return this.insert({ allowedColumns: ['name', 'email'], options: { data: userData, returnField: 'id' }, }); } } // Use const users = new UsersTable(); const result = await users.insertUser({ name: 'Alice', email: 'alice@example.com' }); console.log('Inserted user id:', result.id);
Debug
Known issues
breakingESM-only: This package is ESM-only. Using require() will throw a runtime error.
fix
Use import syntax in an ESM context (set "type": "module" in package.json or use .mjs extension).
affects: >=0.4.0
deprecatedThe 'EnhancedTableBase' may be deprecated in future versions in favor of composition patterns.
fix
Consider using TableBase with manual relation handling for simpler use cases.
affects: 0.4.x
gotchaThe 'update' method requires a 'where' clause; omitting it will throw an error to prevent accidental updates.
fix
Always provide a 'where' object with at least one condition when calling update().
affects: >=0.4.0
gotchaThe 'insert' method's 'allowedColumns' is mandatory; passing columns not in the list will be silently ignored or throw, depending on configuration.
fix
Explicitly list all columns that can be inserted via allowedColumns in the insert options.
affects: >=0.4.0
Errors
Common errors & fixes
TypeError: PostgresConnection.initialize is not a function
Using CommonJS require() instead of ESM import.
fix
Use import PostgresConnection from 'pg-lightquery'; and ensure your project is configured for ESM.
ERR_MODULE_NOT_FOUND: Cannot find package 'pg-lightquery'
Package not installed or npm install failed.
fix
Run 'npm install pg-lightquery' in your project root.
TypeScript error: Type 'X' is not assignable to type 'ColumnDefinition'
Using incorrect types for column definitions (e.g., wrong type strings).
fix
Ensure column types match the supported PostgreSQL types exactly (e.g., 'INTEGER', 'TEXT', 'TIMESTAMP WITHOUT TIME ZONE').
Upgrade
Version history
0.4.5latest on npm
Audit
Dependencies
pgrequiredUnderlying PostgreSQL driver for Node.js, required for connection and query execution.
reflect-metadataoptionalUsed for reflection support in TypeScript decorators (if used).
Agent activity
4 hits · last 30 days
node
4
Resources
pg-lightquery — npm install pg-lightquery · libregistry