SQL Parser Mistic
raw JSON →The `sql-parser-mistic` package provides a lexer and parser for SQL syntax, specifically designed for JavaScript environments. Currently at version 1.2.3, it primarily focuses on parsing basic `SELECT` queries, supporting features like `WHERE`, `GROUP BY`, `ORDER BY`, `LIMIT`, `CASE/WHEN`, `REGEXP`, `ILIKE`, and `BETWEEN` clauses, as well as sub-selects, functions, types, and named parameters. It processes a SQL query string into a stream of tokens using its lexer, then constructs an Abstract Syntax Tree (AST) via its parser, represented by a `Select` object. This package differentiates itself by its lightweight, JavaScript-centric approach to SQL parsing, allowing programmatic manipulation and reformatting of SQL queries. Release cadence appears to be irregular, with recent minor updates addressing parsing fixes and feature additions within the 1.x line.
Common errors
error Syntax Error: unexpected TOKEN at line X column Y ↓
SELECT statement and uses only the clauses and functions known to be supported by sql-parser-mistic. error TypeError: Cannot read properties of undefined (reading 'tokenize') ↓
import { lexer, parser } from 'sql-parser-mistic';. If in a CommonJS environment, ensure the library is correctly transpiled or imported, typically as const { lexer, parser } = require('sql-parser-mistic');. Warnings
gotcha Limited SQL Dialect Support: The parser primarily supports `SELECT` statements and a subset of common clauses (WHERE, GROUP BY, ORDER BY, LIMIT, CASE/WHEN, REGEXP, BETWEEN, ILIKE). It does not support Data Definition Language (DDL) or Data Manipulation Language (DML) statements like INSERT, UPDATE, DELETE, CREATE TABLE, etc. ↓
gotcha Potential for Syntax Errors with Unsupported Features: While the library has added support for various features over time, complex or less common SQL constructs outside its tested scope may result in unexpected 'Syntax Error' messages. ↓
Install
npm install sql-parser-mistic yarn add sql-parser-mistic pnpm add sql-parser-mistic Imports
- lexer wrong
const lexer = require('sql-parser-mistic').lexer;correctimport { lexer } from 'sql-parser-mistic'; - parser wrong
const parser = require('sql-parser-mistic').parser;correctimport { parser } from 'sql-parser-mistic';
Quickstart
import { lexer, parser } from 'sql-parser-mistic';
const sqlQuery = `
SELECT
id,
name,
CASE
WHEN status = 'active' THEN 'Enabled'
WHEN status = 'inactive' THEN 'Disabled'
ELSE 'Unknown'
END AS display_status,
created_at
FROM
users
WHERE
age BETWEEN 18 AND 65
AND name ILIKE '%john%'
ORDER BY
created_at DESC
LIMIT 10 OFFSET 0;
`;
try {
// Step 1: Tokenize the SQL query into a stream of tokens
const tokens = lexer.tokenize(sqlQuery);
console.log("Tokens (first 5):", tokens.slice(0, 5), "...");
// Step 2: Parse the tokens into an Abstract Syntax Tree (AST)
const ast = parser.parse(tokens);
console.log("\nParsed AST (simplified properties):", {
type: ast.type,
from: ast.from.value,
where: ast.where ? 'present' : 'absent',
limit: ast.limit ? ast.limit.value : 'absent'
});
// Step 3: Convert the AST back to a formatted SQL string
const formattedSql = ast.toString();
console.log("\nFormatted SQL from AST:\n", formattedSql);
} catch (error) {
console.error("Error parsing SQL:", error.message);
}