SQL Parser
The `sql-parser` package provides a JavaScript-based lexer and parser specifically designed for SQL syntax. As of version 0.5.0, it is primarily capable of tokenizing and parsing fairly basic `SELECT` queries, offering functionalities like `WHERE`, `GROUP BY`, `ORDER BY`, and `LIMIT` clause parsing. The lexer outputs tokens in a format compatible with JISON, which the parser then consumes to produce a `Select` object. A notable feature is the ability to reconstruct a well-formatted SQL string from the parsed object via its `.toString()` method. The project appears to be in a largely unmaintained state, with no indication of active development towards comprehensive SQL support or regular releases since its initial limited scope. Its implementation borrowed heavily from CoffeeScript project boilerplate, suggesting an older codebase that predates modern JavaScript module patterns and extensive TypeScript adoption.
Common errors
-
TypeError: lexer.tokenize is not a function
cause The `lexer` object was not correctly destructured or assigned from the `require('sql-parser')` call, or the package structure differs from expectation.fixEnsure you are using `const { lexer } = require('sql-parser');` or `const sqlParser = require('sql-parser'); const lexer = sqlParser.lexer;`. -
Error: Parse error on line X column Y: Unexpected token
cause The SQL query contains syntax or constructs not supported by this basic parser (e.g., complex joins, subqueries, DDL statements, specific SQL dialect features).fixSimplify the SQL query to only basic SELECT statements with supported clauses (WHERE, GROUP BY, ORDER BY, LIMIT) or use a more robust SQL parsing library if full SQL support is needed. -
SyntaxError: Named export 'lexer' not found. The requested module 'sql-parser' does not provide an export named 'lexer'
cause Attempting to use ESM `import` syntax (`import { lexer } from 'sql-parser';`) for a CommonJS-only package.fixChange the import statement to CommonJS `require()`: `const { lexer } = require('sql-parser');`.
Warnings
- breaking This package is only capable of parsing fairly basic SELECT queries. It does not support a full SQL grammar, including DDL (CREATE, ALTER, DROP), DML (INSERT, UPDATE, DELETE beyond basic SELECT queries), or complex SQL features (e.g., subqueries, joins beyond basic forms, stored procedures).
- gotcha The project appears to be abandoned or unmaintained. The last publish on npm was March 17, 2015, and the version is 0.5.0, suggesting no active development or updates for a significant period.
- gotcha This package is CommonJS (CJS) only. Attempting to import it using ECMAScript Modules (ESM) syntax (e.g., `import { lexer } from 'sql-parser';`) will result in module resolution errors in an ESM environment.
- gotcha The package does not ship with TypeScript type definitions, nor are external types available on DefinitelyTyped. This means developers using TypeScript will lose type safety when interacting with `sql-parser`.
Install
-
npm install sql-parser -
yarn add sql-parser -
pnpm add sql-parser
Imports
- lexer
import { lexer } from 'sql-parser';const { lexer } = require('sql-parser'); - parser
import parser from 'sql-parser';
const { parser } = require('sql-parser'); - Lexer
import { Lexer } from 'sql-parser';const Lexer = require('sql-parser').Lexer;
Quickstart
const { lexer, parser } = require('sql-parser');
const sqlQuery = "select id, name from users where age > 18 order by name asc limit 10";
console.log('Original SQL:', sqlQuery);
// Tokenize the SQL query
const tokens = lexer.tokenize(sqlQuery);
console.log('Tokens:', JSON.stringify(tokens, null, 2));
// Parse the tokens into a Select object (AST-like structure)
try {
const selectObject = parser.parse(tokens);
console.log('Parsed Object (simplified):', selectObject);
// Convert the parsed object back to a formatted SQL string
const formattedSql = selectObject.toString();
console.log('Formatted SQL from object:\n', formattedSql);
} catch (e) {
console.error('Parsing error:', e.message);
}