{"id":43972,"library":"sql-where-parser","title":"sql-where-parser","description":"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.","status":"active","version":"2.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/shaunpersad/sql-where-parser","tags":["javascript","SQL","parser","WHERE","AST","abstract","syntax","tree","convert"],"install":[{"cmd":"npm install sql-where-parser","lang":"bash","label":"npm"},{"cmd":"yarn add sql-where-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add sql-where-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default ESM import. CommonJS require is also supported (v1/v2).","wrong":"const SqlWhereParser = require('sql-where-parser');","symbol":"SqlWhereParser","correct":"import SqlWhereParser from 'sql-where-parser';"},{"note":"Named import is incorrect; use default import.","wrong":"import { SqlWhereParser } from 'sql-where-parser';","symbol":"SqlWhereParser","correct":"const SqlWhereParser = require('sql-where-parser');"},{"note":"Operator is a static property on the class, not a named export.","wrong":"Operator","symbol":"SqlWhereParser.Operator","correct":"SqlWhereParser.Operator"}],"quickstart":{"code":"import SqlWhereParser from 'sql-where-parser';\n\nconst parser = new SqlWhereParser();\nconst sql = 'name = \"Shaun Persad\" AND age >= 27';\nconst ast = parser.parse(sql);\nconsole.log(JSON.stringify(ast, null, 2));\n/*\nOutput:\n{\n  \"AND\": [\n    { \"=\": [\"name\", \"Shaun Persad\"] },\n    { \">=\": [\"age\", 27] }\n  ]\n}\n*/\n\n// Convert to MongoDB query\nconst mongoQuery = parser.parse(sql, (operatorValue, operands) => {\n  switch (operatorValue) {\n    case '=': return { [operands[0]]: operands[1] };\n    case 'AND': return { $and: operands };\n    case '>=': return { [operands[0]]: { $gte: operands[1] } };\n    default: return parser.defaultEvaluator(operatorValue, operands);\n  }\n});\nconsole.log(JSON.stringify(mongoQuery, null, 2));","lang":"typescript","description":"Parses SQL WHERE clause into AST and converts to MongoDB query format."},"warnings":[{"fix":"Implement custom evaluator for functions or preprocess the string.","message":"Parser does not support SQL functions like COUNT(), UPPER(), etc. Only basic operators.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use double quotes around string literals in SQL input.","message":"String values must be double-quoted; single quotes are not recognized.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Ensure decimal numbers are written without unnecessary leading zeros.","message":"Number parsing does not support decimal numbers with leading zeros (e.g., 0.5) – they may be treated as strings.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use SqlWhereParser.Operator type check or .valueOf() to get operator string.","message":"The toArray() method returns an array with Operator instances; direct comparison with string operators requires .valueOf() or use in toHtml.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"search_vec":"'2.2.1':46 '2021':49 'abstract':20,86 'addit':72 'array':31,67 'ast':23,66,85 'binari':57 'built':64 'built-in':63 'callback':77 'claus':11 'convert':27,73,89 'current':42 'custom':36,79 'depend':53 'differenti':61 'evalu':40,76 'function':41 'html':33 'includ':62 'javascript':81 'librari':6 'lightweight':50 'like':16 'mongodb':29,69 'oper':60 'output':70 'pars':8 'parser':4,83 'queri':30 'render':34 'sql':2,15,82 'sql-like':14 'sql-where-pars':1 'stabl':43 'string':17 'structur':37 'support':25,55 'syntax':21,87 'ternari':59 'transform':80 'tree':22,88 'unari':56 'updat':47 'version':44 'via':38 'without':71","created_at":"2026-06-05T17:02:21.746870+00:00","updated_at":"2026-06-05T17:02:21.746870+00:00","problems":[{"fix":"Use `import SqlWhereParser from 'sql-where-parser'`.","cause":"Named import instead of default import.","error":"SqlWhereParser is not a constructor"},{"fix":"Ensure package is installed: `npm install sql-where-parser` and import correctly.","cause":"Missing import or incorrect path.","error":"Cannot read properties of undefined (reading 'parse')"},{"fix":"Use double quotes for string literals in the WHERE clause.","cause":"SQL string uses single quotes or unquoted strings.","error":"Unexpected token: operator '='"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/shaunpersad/sql-where-parser#readme","github":"https://github.com/shaunpersad/sql-where-parser","docs":null,"changelog":null,"pypi":null,"npm":"sql-where-parser","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}