Registry / database / sql-json-generator

sql-json-generator

JSON →
library0.15.1jsnpmunverified

sql-json-generator (v0.15.1) is a Node.js module that generates SQL queries (SELECT, INSERT, UPDATE, DELETE) from JSON objects, loosely based on MongoDB query syntax. It supports MySQL features like backtick escaping, aggregate functions, joins, subqueries via $raw, and SqlCalcFoundRows. The library is designed to simplify building reusable JSON query definitions for REST APIs with MySQL backends. It is not ESM-native and relies on CommonJS. Updates are infrequent; the last release was in 2021. Compared to alternatives like knex.js or sequelize, it is more lightweight but lacks ORM features and broader database support.

npm install sql-json-generator
INSTALL
IMPORT
SIG · SQL-JSON-GENERATOR
S
sql-json-generator
databasejavascriptv0.15.1
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 SQLGenerator = require('sql-json-generator'); const sqlGenerator = new SQLGenerator();
import SQLGenerator from 'sql-json-generator';
Package is CommonJS only; ES import may fail or require bundler handling.
constructor
const sqlGenerator = new SQLGenerator();
const sqlGenerator = new SqlJsonGenerator();
Constructor name is SQLGenerator, case-sensitive.
select
sqlGenerator.select({...})
new SQLGenerator().SELECT({...})
Method names are lowercase: select, insert, update, delete.

Shows basic SELECT query generation with fields, WHERE condition, ordering, and limit.

const SQLGenerator = require('sql-json-generator'); const sqlGenerator = new SQLGenerator(); const queryData = { $from: 'users', $fields: ['id', 'name', 'email'], $where: [{ status: 'active' }], $order: { name: 1 }, $limit: 10 }; const sql = sqlGenerator.select(queryData); console.log(sql); // SELECT `id`, `name`, `email` FROM `users` WHERE `users`.`status` = 'active' ORDER BY `users`.`name` ASC LIMIT 10
Debug
Known issues
deprecated$sqlCalcFoundRows is legacy syntax; use $from: { table: 'table', sqlCalcFoundRows: true } instead.
fix
Use $from object with sqlCalcFoundRows property.
affects: <=0.14.0
gotchaThe library does not escape column names for aliases; providing an alias like 'table.column' may break.
fix
Use explicit $field objects with $as for aliasing.
affects: >=0.1.0
gotcha$where clauses are always wrapped in parentheses; multiple conditions may produce unexpected grouping.
fix
Use $and/$or explicit arrays to control logic grouping.
affects: >=0.1.0
breakingRemoved support for Node.js <8 in v0.12.0.
fix
Use Node.js 8 or later.
affects: >=0.12.0
Errors
Common errors & fixes
TypeError: SQLGenerator is not a constructor
Attempting to import using ES module syntax without proper transpilation.
fix
Use CommonJS require: const SQLGenerator = require('sql-json-generator');
sqlGenerator.select is not a function
Calling select() before instantiating with 'new'.
fix
Instantiate first: const sqlGenerator = new SQLGenerator();
ReferenceError: $from is not defined
Passing a raw object instead of a defined JSON structure.
fix
Ensure $from is a string or object within queryData.
Upgrade
Version history
0.15.1latest on npm
Audit
Dependencies
mysqloptionalOptional peer dependency for MySQL-specific escaping and features like SqlCalcFoundRows.
Agent activity
17 hits · last 30 days
node
14
Meta
1
OpenAI (training)
1
Resources
sql-json-generator — npm install sql-json-generator · libregistry