Registry / database / json-sql-builder2

json-sql-builder2

JSON →
library1.0.24jsnpmunverified

json-sql-builder2 v1.0.24 is a query builder that translates JSON data structures into SQL queries. It supports multiple SQL dialects including MySQL, MariaDB, PostgreSQL, SQLite, Oracle, and Microsoft SQL Server. Its key differentiator is the ability to define queries entirely in JSON, making dynamic query construction easier and more readable compared to traditional string concatenation. It automatically manages parameterized queries to prevent SQL injection. The library is actively maintained with frequent releases.

npm install json-sql-builder2
INSTALL
IMPORT
SIG · JSON-SQL-BUILDER2
J
json-sql-builder2
databasejavascriptv1.0.24
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
const SQLBuilder = require('json-sql-builder2');
import SQLBuilder from 'json-sql-builder2';
The package is CommonJS-only. No default ESM export is provided.
SQLBuilder instance methods ($select, $insert, etc.)
var sql = new SQLBuilder('MySQL'); sql.$select({ ... });
SQLBuilder.$select({...});
Methods are instance methods; do not call them statically.
SQLBuilder.CALLEE
const { CALLEE } = require('json-sql-builder2');
const CALLEE = require('json-sql-builder2').CALLEE;
CALLEE is exported as a named property; both patterns work but destructuring is preferred.

Creates a SQLBuilder instance for MySQL, builds a SELECT query with aggregation and WHERE clause using JSON, and logs the generated SQL and parameterized values.

const SQLBuilder = require('json-sql-builder2'); // Create instance for MySQL var sql = new SQLBuilder('MySQL'); // Build a SELECT query using JSON var result = sql.$select({ job_title: true, total_salary: { $sum: 'salary' }, $from: 'employees', $where: { department: 'Engineering', salary: { $gte: 50000 } }, $groupBy: 'department' }); console.log(result.sql); console.log(result.values); // Output: // SELECT `job_title`, SUM(`salary`) AS `total_salary` FROM `employees` WHERE `department` = ? AND `salary` >= ? GROUP BY `department` // ['Engineering', 50000]
Debug
Known issues
breakingThe default export changed from 'sql-builder' to 'json-sql-builder2' – old require('sql-builder') will fail.
fix
Use require('json-sql-builder2') instead.
affects: >=0.0.1 <1.0.0
deprecatedUsing $and/$or with empty array returns inconsistent SQL; avoid in new code.
fix
Use $where without empty logical operators, or ensure arrays have at least one condition.
affects: >=0.0.1
gotchaColumn names with special characters (e.g., hyphens) must be quoted explicitly; the builder does not auto-escape them.
fix
Use backtick-quoted keys or set the 'quoteIdentifiers' option to true.
affects: >=0.0.1
gotchaNested JSON objects in values are not automatically serialized; they will be treated as SQL placeholders.
fix
Use $json operator or stringify manually before passing.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Unknown dialect 'postgres'
Dialect string is case-sensitive; common misspelling 'postgres' instead of 'PostgreSQL'.
fix
Use new SQLBuilder('PostgreSQL') with exact casing.
Cannot find module 'json-sql-builder2'
Package not installed or installed under different name (e.g., via npm install json-sql-builder).
fix
Run: npm install json-sql-builder2
TypeError: sql.$select is not a function
Forgetting to instantiate with 'new' or calling static method on class.
fix
Use: var sql = new SQLBuilder('MySQL'); then sql.$select(...);
SyntaxError: Unexpected token '$' in JSON
Passing a JSON string instead of a JavaScript object (JSON does not allow '$' prefix).
fix
Pass a JavaScript object, not JSON.parse().
Upgrade
Version history
1.0.24latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
json-sql-builder2 — npm install json-sql-builder2 · libregistry