Node.js SQL Parser is a versatile library designed to parse various SQL statements into a structured Abstract Syntax Tree (AST) and then reconstruct the SQL from that AST. It supports a comprehensive range of SQL dialects, including MySQL, PostgreSQL, SQLite, Athena, BigQuery, Snowflake, and TransactSQL, among others. Currently at version 5.4.0, the package maintains an active development pace with frequent minor and patch updates, consistently adding support for new SQL syntax features and improving existing dialect compatibility. Key differentiators include its extensive multi-dialect support, the ability to extract visited table and column lists with associated authority, and its dual functionality for both SQL parsing and generation. It offers both Node.js module and browser UMD bundles, providing flexibility across different environments.
npm install node-sql-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the SQL parser, parse a sample SQL query into an AST, extract table and column lists, and then reconstruct the SQL from the AST. It highlights the basic workflow for SQL analysis and manipulation.
Always pass the appropriate dialect option to the `Parser` constructor, e.g., `new Parser({ dialect: 'postgresql' })`. Refer to the documentation for a list of supported dialects.For browser-based applications, consider using dialect-specific UMD bundles if your application only requires a subset of SQL parsing capabilities to reduce overall page load size.
When processing the output of `parser.astify()`, be prepared to handle an array of AST objects, even if you anticipate a single statement. Iterate through the array or access the first element if you are certain only one statement is present.
For CommonJS, use `const { Parser } = require('node-sql-parser');`. For ESM/TypeScript, use `import { Parser } from 'node-sql-parser';`. In a browser environment using UMD, ensure the correct script is loaded and access `new NodeSQLParser.Parser()`.Review the documentation for a list of supported SQL dialects and ensure the dialect string passed to the options object (e.g., `{ dialect: 'postgresql' }`) is spelled correctly and is indeed supported.First, verify the SQL query for correctness against the specified database's syntax. Second, ensure you have correctly set the `dialect` option in the `Parser` constructor to match your SQL. If the SQL is valid for the dialect but still errors, it might be a limitation of the parser itself; check the GitHub issues for similar reports.
No dependency data recorded yet.