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.
npm install mongo-sqlNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
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.
If migrating from v5, replace `var builder = require('mongo-sql'); builder.sql(query)` with `var mongoSql = require('mongo-sql'); mongoSql(query)`.Consider migrating to `mosql` (npm package) which has an identical API.
Use explicit string quotes for column names, e.g., `returning: ['"id"', '"createdAt"']` instead of `returning: ['id', 'createdAt']` if you need case-sensitive handling.
Use the object form: `{ $or: { cond1: val1, cond2: val2 } }` instead of `{ $or: [{ cond1: val1 }, { cond2: val2 }] }`.Use `const mongoSql = require('mongo-sql');` then call `mongoSql(query)`. For v5, use `require('mongo-sql').sql(query)`.Ensure `$or` is an object, not an array: `{ $or: { id: 5, name: 'Bob' } }`.Add a 'type' property with value 'select', 'insert', 'update', 'delete', etc.
Use lowercase column names or quote them: `returning: ['"MyColumn"']`.