Registry / database / pg-chain

pg-chain

JSON →
library1.1.3jsnpmunverified

pg-chain is a chainable SQL query builder for PostgreSQL in Node.js, using tagged template literals to construct SQL statements. Version 1.1.3 is the current stable release, with no frequent update cadence documented. It provides methods like SELECT, INSERT_INTO, UPDATE, DELETE_FROM, WITH_RECURSIVE, etc., that return chainable objects. Differentiators include no external dependencies, TypeScript type definitions included, and support for both .toSql() output and direct use with the pg library via .text and .values properties. It is designed for simplicity and composability, allowing reusable conditionals for pagination.

npm install pg-chain
INSTALL
IMPORT
SIG · PG-CHAIN
P
pg-chain
databasejavascriptv1.1.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.

SELECT
import { SELECT } from 'pg-chain'
const SELECT = require('pg-chain').SELECT
ESM-only. CommonJS require is not supported; use dynamic import if needed.
INSERT_INTO
import { INSERT_INTO } from 'pg-chain'
import { INSERT } from 'pg-chain'
INSERT is not exported; the correct symbol is INSERT_INTO.
UPDATE
import { UPDATE } from 'pg-chain'
import UPDATE from 'pg-chain'
Named export, not default export.
DELETE_FROM
import { DELETE_FROM } from 'pg-chain'
import { DELETE } from 'pg-chain'
DELETE_FROM is the correct symbol; DELETE is not exported.
WITH_RECURSIVE
import { WITH_RECURSIVE } from 'pg-chain'
import { WITH } from 'pg-chain'
WITH_RECURSIVE is the exported symbol; WITH alone is not available.

Demonstrates basic SELECT, INSERT, UPDATE, and DELETE queries using pg-chain's tagged template syntax.

import { SELECT, INSERT_INTO, UPDATE, DELETE_FROM } from 'pg-chain'; // SELECT example const chain = SELECT`id, name`.FROM`users`.WHERE`id = ${10}`; const [sql, params] = chain.toSql(); console.log(sql); // "SELECT id, name FROM users WHERE id = $1" console.log(params); // [10] // INSERT example const insertChain = INSERT_INTO`users (name, status)`.VALUES('Alice', 'active').RETURNING`id`; const [insertSql, insertParams] = insertChain.toSql(); console.log(insertSql); // "INSERT INTO users (name, status) VALUES ($1, $2) RETURNING id" console.log(insertParams); // ['Alice', 'active'] // UPDATE example const updateChain = UPDATE`users`.SET`name = ${'Alice'}`.WHERE`id = ${1}`; const [updateSql, updateParams] = updateChain.toSql(); console.log(updateSql); // "UPDATE users SET name = $1 WHERE id = $2" console.log(updateParams); // ['Alice', 1] // DELETE example const deleteChain = DELETE_FROM`users`.WHERE`id = ${1}`; const [deleteSql, deleteParams] = deleteChain.toSql(); console.log(deleteSql); // "DELETE FROM users WHERE id = $1" console.log(deleteParams); // [1]
Debug
Known issues
gotchaMethods like AS, EXISTS must not be called with tagged templates when parentheses are needed; they expect a regular function call.
fix
Use EXISTS(SELECT...) instead of EXISTS`SELECT...`.
affects: >=1.0.0
gotchaINSERT does not exist; use INSERT_INTO.
fix
Import { INSERT_INTO } from 'pg-chain' instead of { INSERT }.
affects: >=1.0.0
gotchaDELETE does not exist; use DELETE_FROM.
fix
Import { DELETE_FROM } from 'pg-chain' instead of { DELETE }.
affects: >=1.0.0
gotchaWITH_RECURSIVE is exported, but WITH alone is not available.
fix
Use WITH_RECURSIVE for recursive CTEs; for non-recursive CTEs, use WITH_RECURSIVE without RECURSIVE in SQL? Actually, WITH_RECURSIVE always adds RECURSIVE. To get a plain WITH, you must manually construct or use a workaround.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'pg-chain' or its corresponding type declarations.
Missing installation or wrong import style.
fix
Run `npm install pg-chain` and use ESM import: `import { SELECT } from 'pg-chain'`.
INSERT is not exported from 'pg-chain'.
Trying to import INSERT instead of INSERT_INTO.
fix
Use `import { INSERT_INTO } from 'pg-chain'`.
DELETE is not exported from 'pg-chain'.
Trying to import DELETE instead of DELETE_FROM.
fix
Use `import { DELETE_FROM } from 'pg-chain'`.
Cannot read properties of undefined (reading 'toSql')
Calling toSql on a non-chainable result, e.g., using tagged template on a method that doesn't return a chain.
fix
Ensure the method supports chaining; for example, AS and EXISTS return different objects, not chainable methods.
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
pg-chain — npm install pg-chain · libregistry