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);
Errors
Common errors & fixes
TypeError: PostgresConnection.initialize is not a function
Using CommonJS require() instead of ESM import.
fixUse 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.
fixRun '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).
fixEnsure column types match the supported PostgreSQL types exactly (e.g., 'INTEGER', 'TEXT', 'TIMESTAMP WITHOUT TIME ZONE').
Audit
Dependencies
pgrequiredUnderlying PostgreSQL driver for Node.js, required for connection and query execution.
reflect-metadataoptionalUsed for reflection support in TypeScript decorators (if used).