{"id":17956,"library":"sql-parser-mistic","title":"SQL Parser Mistic","description":"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.","status":"active","version":"1.2.3","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/mistic100/sql-parser","tags":["javascript","sql","lexer","parser","ast"],"install":[{"cmd":"npm install sql-parser-mistic","lang":"bash","label":"npm"},{"cmd":"yarn add sql-parser-mistic","lang":"bash","label":"yarn"},{"cmd":"pnpm add sql-parser-mistic","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary `lexer` object for tokenizing SQL queries.","wrong":"const lexer = require('sql-parser-mistic').lexer;","symbol":"lexer","correct":"import { lexer } from 'sql-parser-mistic';"},{"note":"The primary `parser` object for converting tokens into an Abstract Syntax Tree (AST).","wrong":"const parser = require('sql-parser-mistic').parser;","symbol":"parser","correct":"import { parser } from 'sql-parser-mistic';"}],"quickstart":{"code":"import { lexer, parser } from 'sql-parser-mistic';\n\nconst sqlQuery = `\n  SELECT\n    id,\n    name,\n    CASE\n      WHEN status = 'active' THEN 'Enabled'\n      WHEN status = 'inactive' THEN 'Disabled'\n      ELSE 'Unknown'\n    END AS display_status,\n    created_at\n  FROM\n    users\n  WHERE\n    age BETWEEN 18 AND 65\n    AND name ILIKE '%john%'\n  ORDER BY\n    created_at DESC\n  LIMIT 10 OFFSET 0;\n`;\n\ntry {\n  // Step 1: Tokenize the SQL query into a stream of tokens\n  const tokens = lexer.tokenize(sqlQuery);\n  console.log(\"Tokens (first 5):\", tokens.slice(0, 5), \"...\");\n\n  // Step 2: Parse the tokens into an Abstract Syntax Tree (AST)\n  const ast = parser.parse(tokens);\n  console.log(\"\\nParsed AST (simplified properties):\", {\n    type: ast.type,\n    from: ast.from.value,\n    where: ast.where ? 'present' : 'absent',\n    limit: ast.limit ? ast.limit.value : 'absent'\n  });\n\n  // Step 3: Convert the AST back to a formatted SQL string\n  const formattedSql = ast.toString();\n  console.log(\"\\nFormatted SQL from AST:\\n\", formattedSql);\n\n} catch (error) {\n  console.error(\"Error parsing SQL:\", error.message);\n}","lang":"javascript","description":"Demonstrates tokenizing a SQL query, parsing it into an AST, and then converting the AST back into a formatted SQL string using recent features like CASE/WHEN, BETWEEN, and ILIKE."},"warnings":[{"fix":"Ensure that the SQL queries provided for parsing adhere strictly to the supported `SELECT` query syntax and features documented by the library. Avoid passing DDL/DML or highly complex, unsupported SQL structures.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Thoroughly test parsing capabilities with your specific SQL queries. If an error occurs, simplify the query or break it down into smaller, more basic components to identify the unsupported syntax. Refer to the project's specifications for examples of currently supported queries.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Review the SQL query for typos or unsupported SQL features. Ensure it is a valid `SELECT` statement and uses only the clauses and functions known to be supported by `sql-parser-mistic`.","cause":"The input SQL query contains syntax that is not recognized or supported by the parser, or it is malformed.","error":"Syntax Error: unexpected TOKEN at line X column Y"},{"fix":"If in an ES module environment, use `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');`.","cause":"The `lexer` object was not correctly imported or accessed. This commonly occurs when using CommonJS `require` syntax in an ESM module context, or vice-versa, or attempting to access an unexported property.","error":"TypeError: Cannot read properties of undefined (reading 'tokenize')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}