Registry / database / sql-where-parser

sql-where-parser

JSON →
library2.2.1jsnpmunverified

A library for parsing the WHERE clause of an SQL-like string into an abstract syntax tree (AST), with support for converting to MongoDB queries, arrays for HTML rendering, or custom structures via an evaluator function. Current stable version is 2.2.1, updated in 2021. Lightweight, with no dependencies, and supports unary, binary, and ternary operators. Differentiators include built-in AST, array, and MongoDB output without additional converters, and an evaluator callback for custom transformations.

npm install sql-where-parser
INSTALL
IMPORT
SIG · SQL-WHERE-PARSER
S
sql-where-parser
databasejavascriptv2.2.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.

SqlWhereParser
import SqlWhereParser from 'sql-where-parser';
const SqlWhereParser = require('sql-where-parser');
Default ESM import. CommonJS require is also supported (v1/v2).
SqlWhereParser
const SqlWhereParser = require('sql-where-parser');
import { SqlWhereParser } from 'sql-where-parser';
Named import is incorrect; use default import.
SqlWhereParser.Operator
SqlWhereParser.Operator
Operator
Operator is a static property on the class, not a named export.

Parses SQL WHERE clause into AST and converts to MongoDB query format.

import SqlWhereParser from 'sql-where-parser'; const parser = new SqlWhereParser(); const sql = 'name = "Shaun Persad" AND age >= 27'; const ast = parser.parse(sql); console.log(JSON.stringify(ast, null, 2)); /* Output: { "AND": [ { "=": ["name", "Shaun Persad"] }, { ">=": ["age", 27] } ] } */ // Convert to MongoDB query const mongoQuery = parser.parse(sql, (operatorValue, operands) => { switch (operatorValue) { case '=': return { [operands[0]]: operands[1] }; case 'AND': return { $and: operands }; case '>=': return { [operands[0]]: { $gte: operands[1] } }; default: return parser.defaultEvaluator(operatorValue, operands); } }); console.log(JSON.stringify(mongoQuery, null, 2));
Debug
Known issues
gotchaParser does not support SQL functions like COUNT(), UPPER(), etc. Only basic operators.
fix
Implement custom evaluator for functions or preprocess the string.
affects: >=2.0.0
gotchaString values must be double-quoted; single quotes are not recognized.
fix
Use double quotes around string literals in SQL input.
affects: >=2.0.0
gotchaNumber parsing does not support decimal numbers with leading zeros (e.g., 0.5) – they may be treated as strings.
fix
Ensure decimal numbers are written without unnecessary leading zeros.
affects: >=2.0.0
deprecatedThe toArray() method returns an array with Operator instances; direct comparison with string operators requires .valueOf() or use in toHtml.
fix
Use SqlWhereParser.Operator type check or .valueOf() to get operator string.
affects: >=2.0.0
Errors
Common errors & fixes
SqlWhereParser is not a constructor
Named import instead of default import.
fix
Use `import SqlWhereParser from 'sql-where-parser'`.
Cannot read properties of undefined (reading 'parse')
Missing import or incorrect path.
fix
Ensure package is installed: `npm install sql-where-parser` and import correctly.
Unexpected token: operator '='
SQL string uses single quotes or unquoted strings.
fix
Use double quotes for string literals in the WHERE clause.
Upgrade
Version history
2.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
Meta
2
OpenAI (training)
1
Resources
sql-where-parser — npm install sql-where-parser · libregistry