pg-query-string

raw JSON →
0.1.7 verified Sat May 09 auth: no javascript maintenance

pg-query-string is a small library (v0.1.7, stable) for building PostgreSQL query strings with parameterized values. It is a fork of squel, hardcoded to PostgreSQL, with type coercion and ES6 class-based schema/table constructs. Current version is 0.1.7, no recent updates. Key differentiator: produces both SQL text and parameter arrays for safe execution.

error TypeError: (0 , _pgQueryString.Table) is not a constructor
cause Using default import instead of named import.
fix
Change to: import { Table } from 'pg-query-string'
error SyntaxError: Cannot use import statement outside a module
cause Using ESM import in a CommonJS file without 'type': 'module'.
fix
Add 'type': 'module' to package.json or use dynamic import().
error Missing initializer in const declaration
cause Using const without assigning import result.
fix
Correct import: import { Schema } from 'pg-query-string'
gotcha Package is ESM-only; requires Node >= 13 or a bundler like webpack/esbuild.
fix Use import syntax or configure bundler to handle ESM.
deprecated The package has not been updated since 2019 and may have unpatched security issues.
fix Consider using 'pg' or 'knex' for more maintained query building.
gotcha Object literals for insert/update produce $1, $2, etc. in the order of object keys, which is non-deterministic in older JavaScript engines.
fix Use Map or arrays to guarantee order.
gotcha Schema and table names are quoted with double quotes, which are case-sensitive in PostgreSQL. Ensure lowercase names.
fix Use snake_case for schema/table names to avoid quoting issues.
npm install pg-query-string
yarn add pg-query-string
pnpm add pg-query-string

Creates a schema and table with typed columns, then builds an UPDATE query with parameterized values.

import { Schema, Table } from 'pg-query-string';

const schema = new Schema('my_schema');
const table = schema.table('users', {
  id: 'uuid',
  username: 'text',
  active: 'boolean'
});

const result = table.update(
  { active: true },
  { id: 1 }
);

console.log(result.text);
// UPDATE "my_schema".users SET active = $1::boolean WHERE (id = $2::uuid)
console.log(result.values);
// [ true, 1 ]