SQL Parsing Utility
raw JSON →`sql-parse` is an unmaintained JavaScript utility for parsing basic SQL queries, with its last known stable version being 0.1.5, published in June 2015. It was designed to provide a simple programmatic interface for tokenizing and parsing SQL strings, extracting components like `SELECT` statements and `WHERE` clauses, including initial support for `OR` operators. Due to its age and lack of maintenance for nearly a decade, it is considered abandoned. Modern SQL dialects, advanced query features, and contemporary JavaScript module systems (ESM) are not supported. Developers seeking robust, actively maintained SQL parsing solutions for diverse database systems (e.g., MySQL, PostgreSQL, BigQuery, Snowflake, FlinkSQL) should consider alternatives like `node-sql-parser`, `dt-sql-parser`, or `@casual-simulation/sql-parser`. Its release cadence was sporadic and ceased many years ago. It lacks official TypeScript types and has no external runtime dependencies.
Common errors
error SyntaxError: Cannot use import statement outside a module ↓
const { parse } = require('sql-parse');. error TypeError: (0, _sqlParse.parse) is not a function ↓
require syntax: const { parse } = require('sql-parse');. Verify that sql-parse is installed and the path is correct. error Parse error at line X, column Y: Unexpected token Z ↓
Warnings
breaking The `sql-parse` package is abandoned and has not been updated since June 2015. It no longer receives bug fixes, security patches, or feature enhancements. Continued use in production environments is highly discouraged due to significant security risks and compatibility issues with modern JavaScript runtimes and evolving SQL standards. ↓
gotcha This package is strictly CommonJS (`require`) and does not support ES Modules (`import/export`). Attempting to use `import` syntax will result in module resolution errors. ↓
gotcha The parser has limited support for SQL dialects and modern SQL syntax. It was designed for basic `SELECT` statements and early SQL standards. Complex queries, specific database dialect features (e.g., PostgreSQL JSON functions, MySQL `WITH` clauses), or advanced DDL/DML operations are likely to cause parse errors or unexpected results. ↓
gotcha The `sql-parse` package does not ship with TypeScript declaration files (`.d.ts`). Using it in a TypeScript project will result in implicitly typed `any` or require manual type definitions, reducing type safety. ↓
Install
npm install sql-parse yarn add sql-parse pnpm add sql-parse Imports
- parse wrong
import { parse } from 'sql-parse';correctconst { parse } = require('sql-parse'); - Full module import (for completeness, though `parse` is usually destructured)
const sqlParse = require('sql-parse');
Quickstart
const { parse } = require('sql-parse');
const simpleSelect = 'SELECT id, name FROM users WHERE active = true OR age > 30;';
const insertStatement = 'INSERT INTO products (name, price) VALUES ("Laptop", 1200.00);';
const updateStatement = 'UPDATE orders SET status = "shipped" WHERE id = 101;';
try {
const resultSelect = parse(simpleSelect);
console.log('Parsed SELECT:', JSON.stringify(resultSelect, null, 2));
// Note: The specific structure of `result` depends entirely on the library's implementation.
// Given its age, it might be a simple object or array of tokens/nodes.
// This example assumes a structure that might expose `statements` or similar.
if (resultSelect && Array.isArray(resultSelect.statements) && resultSelect.statements.length > 0) {
console.log('First statement type:', resultSelect.statements[0].type); // e.g., 'SELECT'
} else {
console.log('Could not determine statement type for SELECT.');
}
// Trying other statement types might fail or yield unexpected results
// as the README primarily focuses on SELECT queries.
// const resultInsert = parse(insertStatement);
// console.log('Parsed INSERT:', JSON.stringify(resultInsert, null, 2));
} catch (error) {
console.error('SQL Parsing Error:', error.message);
console.warn('This package is very old and may not support modern or complex SQL syntax.');
}