Registry / database / sql-query-generator

sql-query-generator

JSON →
library1.4.2jsnpmunverified

A schemaless SQL query builder for Node.js (v1.4.2) that generates parameterized SQL statements for PostgreSQL (default), MySQL, and MSSQL dialects. Unlike ORMs or schema-based builders, it works without any model definitions, making it ideal for dynamic queries. It supports SELECT, INSERT, UPDATE, DELETE operations with WHERE, ORDER BY, LIMIT/OFFSET, and JOINs. Includes TypeScript definitions. Release cadence is low; last release was March 2025.

npm install sql-query-generator
INSTALL
IMPORT
SIG · SQL-QUERY-GENERATO
S
sql-query-generator
databasejavascriptv1.4.2
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.

default
import sql from 'sql-query-generator'
const sql = require('sql-query-generator')
ESM import style; CJS require also works with default export.
Statement
import sql from 'sql-query-generator'
import { Statement } from 'sql-query-generator'
Statement is not exported; it's an internal class returned by methods.
TypeScript types
import sql from 'sql-query-generator'
import { SqlQueryGenerator } from 'sql-query-generator'
Types are bundled; no separate import needed. Use `import type { Statement } from 'sql-query-generator'` if needed.

Demonstrates basic CRUD operations (INSERT, SELECT, UPDATE, DELETE) with parameterized queries and dialect selection.

import sql from 'sql-query-generator'; sql.use('postgres'); // or 'mysql', 'mssql' // Insert const insertStmt = sql.insert('users', { name: 'John', email: 'john@example.com' }); console.log(insertStmt.text); // INSERT INTO users (name, email) VALUES ($1, $2) console.log(insertStmt.values); // ['John', 'john@example.com'] // Select with where const selectStmt = sql.select('users', ['id', 'name']).where({ email: 'john@example.com' }); console.log(selectStmt.text); // SELECT id, name FROM users WHERE email = $1 // Update const updateStmt = sql.update('users', { name: 'Jane' }).where({ id: 1 }); console.log(updateStmt.text); // UPDATE users SET name = $1 WHERE id = $2 // Delete const deleteStmt = sql.deletes('users').where({ id: 1 }); console.log(deleteStmt.text); // DELETE FROM users WHERE id = $1
Debug
Known issues
gotchaThe delete method is named `deletes`, not `delete`.
fix
Use sql.deletes('table') instead of sql.delete('table').
affects: >=0.0.0
gotchaThe `use` method must be called before any query methods to set the dialect; default is postgres.
fix
Call sql.use('mysql') or sql.use('mssql') after import.
affects: >=0.0.0
gotchaAll parameter placeholders use $1, $2, etc., even for MySQL and MSSQL dialects.
fix
The library does not convert to ? or @p1; you must handle parameterization externally if needed.
affects: >=0.0.0
gotchaThe `select` method's second argument can be a string, array, or '*' but does not support raw SQL expressions.
fix
Pass columns as array of strings or comma-separated string. Do not include functions like COUNT(*).
affects: >=0.0.0
gotchaThe `where` method's logical operator defaults to 'AND'; use the third argument to set 'OR'.
fix
sql.where({a:1, b:2}, '=', 'OR') produces (a = $1 OR b = $2).
affects: >=0.0.0
Errors
Common errors & fixes
sql.delete is not a function
The delete method is named `deletes` to avoid conflict with JavaScript's `delete` keyword.
fix
Replace sql.delete('table') with sql.deletes('table').
Cannot find module 'sql-query-generator'
Package not installed or missing from dependencies.
fix
Run npm install sql-query-generator in your project root.
Cannot read properties of null (reading 'text')
Calling .text on a statement that was not generated correctly (e.g., missing table name in select).
fix
Ensure all required arguments are provided. Example: sql.select('table', '*').text
TypeError: sql.use is not a function
Using a version that does not support the `use` method (possibly very old).
fix
Upgrade to version 1.x by running npm install sql-query-generator@latest.
Upgrade
Version history
1.4.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Meta
1
OpenAI (training)
1
Resources
sql-query-generator — npm install sql-query-generator · libregistry