Registry / database / sql-query-builder

sql-query-builder

JSON →
library0.3.2jsnpmunverified

Qb is a JavaScript SQL query builder for Node.js that generates SQL from JSON objects instead of method chaining. Current stable version is 0.3.2. It builds on brianc/node-sql and supports PostgreSQL, MySQL, and SQLite. Key differentiator: designed for BI applications where non-technical users send JSON specifications via an API. It enforces security by only allowing defined tables/columns, supports automatic joins based on model associations, and provides context-aware aliases. The library is minimal and has no active releases since 2017, suggesting maintenance status.

npm install sql-query-builder
INSTALL
IMPORT
SIG · SQL-QUERY-BUILDER
S
sql-query-builder
databasejavascriptv0.3.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.

Qb
import Qb from 'sql-query-builder'
const Qb = require('sql-query-builder').Qb
Default export from package; CommonJS require works with 'const Qb = require('sql-query-builder')'.
Qb
const Qb = require('sql-query-builder')
const { Qb } = require('sql-query-builder')
Only default export; named destructure fails.
Qb.prototype.query
const qb = new Qb(definitions, dialect); const sql = qb.query(spec);
const sql = Qb.query(spec);
Must instantiate Qb with definitions and dialect before calling query. Dialect is optional but defaults to 'postgres'.

Shows how to define tables, instantiate Qb, and generate a SELECT query with a WHERE clause.

const Qb = require('sql-query-builder'); // Define tables with columns and associations const definitions = { users: { id: {}, name: {}, posts: { association: 'hasMany', table: 'posts', key: 'user_id' } }, posts: { id: {}, title: {}, user_id: {} } }; const qb = new Qb(definitions, 'postgres'); const spec = { select: 'name', from: 'users', where: { field: 'id', match: { value: 1 } } }; const sql = qb.query(spec); console.log(sql); // SELECT "users"."name" AS "name" FROM "users" WHERE ("users"."id" = 1)
Debug
Known issues
gotchaThe package requires the 'sql' peer dependency (node-sql) which must be installed separately.
fix
npm install sql-query-builder sql --save
affects: >=0.1.0
gotchaIf you omit the dialect, it defaults to 'postgres', which may produce incompatible SQL for MySQL or SQLite.
fix
Always pass the dialect as second argument to the constructor, e.g. new Qb(definitions, 'mysql')
affects: >=0.1.0
gotchaTable and column names are automatically quoted (double quotes for postgres), which can cause issues with case-sensitive matching.
fix
Use consistent casing in your definitions and spec field names, or override via aliases.
affects: >=0.1.0
breakingThe API expects the definitions object structure to exactly match the library's expectations; invalid definitions cause silent failures.
fix
Ensure each table property has at least an empty object for its columns (e.g., 'id': {}).
affects: >=0.1.0
Errors
Common errors & fixes
Cannot find module 'sql'
Missing peer dependency 'sql' (node-sql).
fix
npm install sql
TypeError: Qb is not a constructor
Importing incorrectly, e.g., using named import or destructuring the result.
fix
const Qb = require('sql-query-builder'); then new Qb(...)
ReferenceError: dialect is not defined
Trying to use qb without constructing it, or calling query() on the class instead of instance.
fix
const qb = new Qb(definitions, 'postgres'); qb.query(spec);
Upgrade
Version history
0.3.2latest on npm
Audit
Dependencies
sqlrequiredCore dependency that provides the underlying SQL generation (node-sql).
Agent activity
18 hits · last 30 days
node
14
Meta
2
OpenAI (training)
1
Resources
sql-query-builder — npm install sql-query-builder · libregistry