Registry / database / mongo-sql

mongo-sql

JSON →
library6.2.0jsnpmunverified

Mongo-sql is a Node.js library that converts JSON-style query definitions into SQL strings, with a focus on PostgreSQL syntax. As of version 6.2.0 (stable, with irregular releases), it supports SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, JOINs, subqueries, and more. Unlike chain-style SQL builders, mongo-sql uses plain JavaScript objects to represent queries, making them easy to manipulate programmatically. It generates parameterized queries with `$N` placeholders to prevent SQL injection. Designed for extensibility and semantic clarity, it is ideal for developers who want a declarative, data-driven approach to SQL generation.

database
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.

The package uses CommonJS; ESM import may fail or require default interop.

const mongoSql = require('mongo-sql');

Both are valid in CommonJS, but the destructured import is more idiomatic.

const { sql } = require('mongo-sql');

Helpers are exported as a property of the main module, not as a submodule.

const { helpers } = require('mongo-sql');

Shows how to build a SELECT query with an OR condition using mongo-sql's JSON syntax, then execute the SQL generation to get parameterized query string and values.

const { sql } = require('mongo-sql'); const query = { type: 'select', table: 'users', where: { $or: { id: 5, name: 'Bob' } } }; const result = sql(query); console.log(result.toString()); // SELECT "users".* FROM "users" WHERE "users"."id" = $1 OR "users"."name" = $2 console.log(result.values); // [5, 'Bob']
Debug
Known footguns
breakingIn v6.0.0, the API changed from `require('mongo-sql').sql()` to `require('mongo-sql')()` (default export is now the sql function). Also removed legacy helper methods.
deprecatedThe `mongo-sql` package is deprecated in favor of its successor `mosql`. Future development will occur under the new package name.
gotchaWhen using `returning` clause in INSERT/UPDATE/DELETE, the column names must be quoted correctly. The library auto-quotes but may produce incorrect syntax for expressions.
gotchaThe `$or` operator only supports an object with multiple keys; it does not accept an array of conditions. Using an array will throw an error.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources