Registry / database / json-sql

json-sql

JSON →
library0.5.0jsnpmunverified

A Node.js library that translates MongoDB-style query objects into SQL queries. Version 0.5.0 is the current stable release, though development activity appears low. It is not a database driver; it only generates SQL strings and parameter values for use with any SQL driver (e.g., node-postgres). Supports SELECT, INSERT, UPDATE, DELETE, JOINs, nested conditions, and parameterized queries with automatic variable naming. Differentiators include a simple JSON-based query syntax, no DSL, and zero runtime dependencies.

npm install json-sql
INSTALL
IMPORT
SIG · JSON-SQL
J
json-sql
databasejavascriptv0.5.0
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 (factory function)
import jsonSql from 'json-sql'; const sql = jsonSql();
const sql = require('json-sql'); (missing call to factory)
The module exports a factory function. In CJS, you must call require('json-sql')() or require('json-sql').builder(). The factory can accept options like `valuesPrefix`.
builder
import { builder } from 'json-sql'; const sql = builder();
import { build } from 'json-sql';
The named export is `builder`, not `build`. The method on the returned instance is `build`.
build method result
const result = jsonSql().build({type: 'select', table: 'users'}); result.query; result.values;
const { query, values } = jsonSql().build(...); (incorrect destructuring, missing parentheses)
`build()` returns an object with `.query` (string) and `.values` (object).

Basic SELECT query with parameterized condition using json-sql.

var jsonSql = require('json-sql')(); var sql = jsonSql.build({ type: 'select', table: 'users', fields: ['name', 'age'], condition: {name: 'Max', id: 6} }); console.log(sql.query); // select name, age from users where name = $p1 and id = 6 console.log(sql.values); // { p1: 'Max' }
Debug
Known issues
gotchaThe factory function returns a new instance each call; state is not shared.
fix
Call require('json-sql')() once and reuse the instance, or use the builder export.
affects: >=0.0.0
gotchaThe `values` prefix defaults to 'p' (e.g., $p1). To change it, pass `valuesPrefix` option to the factory.
fix
const sql = require('json-sql')({ valuesPrefix: 'param' });
affects: >=0.0.0
gotchaConditions with operators (e.g., $gt, $lt) must be objects; plain values are treated as equality.
fix
Use { age: { $gt: 18 } } instead of { age: 18 } for greater-than.
affects: >=0.0.0
deprecatedThe library has not seen updates since 2016; no TypeScript definitions or ES modules support.
fix
Consider alternatives like squel.js or knex.js for active maintenance.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: json_sql_1.default is not a function
Using default import from TypeScript/ESM when the module is CJS with no default export.
fix
Use `import jsonSql from 'json-sql'; const sql = jsonSql();` with `esModuleInterop` enabled, or use `const jsonSql = require('json-sql')();`.
Cannot read property 'build' of undefined
Calling `require('json-sql').build()` instead of `require('json-sql')().build()`.
fix
First invoke the factory: `const jsonSql = require('json-sql')();` then call `jsonSql.build(...)`.
Error: Unknown type 'delete'
Using 'delete' as type instead of 'remove'.
fix
Use `type: 'remove'` for delete operations.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources