Registry / database / json-sql-builder

json-sql-builder

JSON →
library1.0.26jsnpmunverified

Build SQL queries from JSON objects with a MongoDB-like syntax. Current version 1.0.26 is in active development with support for ANSI-SQL, MySQL, and PostgreSQL dialects. The library generates parameterized SQL strings and value arrays, supporting SELECT, INSERT, UPDATE, DELETE, CREATE, JOINs, subqueries, and PostgreSQL JSON helpers. Key differentiator: enables dynamic query construction from JSON configurations, similar to Mongo query language.

npm install json-sql-builder
INSTALL
IMPORT
SIG · JSON-SQL-BUILDER
J
json-sql-builder
databasejavascriptv1.0.26
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.

SQLBuilder
const SQLBuilder = require('json-sql-builder');
import SQLBuilder from 'json-sql-builder';
The package is CJS-only; ESM imports will not work. Use require().
SQLBuilder instance
new SQLBuilder('mysql');
new SQLBuilder('SQL');
Dialect must be one of 'mysql', 'postgresql', or omitted for ANSI. Case-insensitive but best practice is lowercase.
build method
sqlbuilder.build($select: { $from: 'table' });
sqlbuilder.build({ select: { from: 'table' } });
Operators must be prefixed with $. Use $select, $from, $where, etc.

Demonstrates building a SELECT query with column aliases, aggregation, WHERE conditions, and GROUP BY using the MySQL dialect.

const SQLBuilder = require('json-sql-builder'); const sqlbuilder = new SQLBuilder('mysql'); const result = sqlbuilder.build({ $select: { $columns: ['id', 'name', { total: { $sum: 'amount' } }], $from: 'transactions', $where: { date: { $gte: '2020-01-01' }, status: { $in: ['pending', 'completed'] } }, $groupBy: ['status'] } }); console.log(result.sql); console.log(result.values);
Debug
Known issues
gotchaAll operators must be prefixed with a dollar sign ($). Forgetting the $ prefix will cause the operator to be interpreted as a literal column name.
fix
Ensure all operators like $select, $from, $where, $sum, $in, $gte, $column are written with a preceding $.
affects: >=1.0.0
gotchaThe package uses CJS modules only; attempting to import with ESM syntax (import) will fail.
fix
Use require('json-sql-builder') instead of import.
affects: >=1.0.0
breakingIn version 1.0.14, the CREATE INDEX syntax changed and may not be backward compatible with earlier versions.
fix
Update your query format to match the new CREATE INDEX syntax documented in the release notes.
affects: <1.0.14
deprecatedThe $create operator used to support CREATE TABLE in older versions; as of 1.0.16, only CREATE VIEW is documented.
fix
Review the documentation for CREATE VIEW usage; CREATE TABLE may not be supported.
affects: <1.0.16
Errors
Common errors & fixes
SyntaxError: Unexpected token '?'
Using parameterized queries with PostgreSQL but the query string contains ? placeholders not supported by all dialects.
fix
Use the correct dialect (e.g., 'postgresql') or refer to the docs for dialect-specific placeholder syntax.
TypeError: sqlbuilder.build is not a function
The package was imported incorrectly (e.g., using default import instead of require).
fix
Use const SQLBuilder = require('json-sql-builder'); and create an instance.
Error: Unsupported operator: select (did you mean $select?)
Forgetting the dollar sign prefix on operators.
fix
Prefix all operators with $, e.g., $select, $from, $where.
Upgrade
Version history
1.0.26latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
json-sql-builder — npm install json-sql-builder · libregistry