Registry / database / sql-ast

sql-ast

JSON →
library0.3.0jsnpmunverified

A MySQL parser that converts mysqldump output into an AST. Version 0.3.0 is the latest stable release, but the library is early-stage with limited feature coverage. It tokenizes SQL input into word, ident, punct, number, string, variable, and version tokens, and builds AST nodes for statements, actions, columns, indexes, rows, and more. Differentiated by its focus on mysqldump output and minimal dependencies. Development is infrequent; community contributions are welcome but not actively maintained.

npm install sql-ast
INSTALL
IMPORT
SIG · SQL-AST
S
sql-ast
databasejavascriptv0.3.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
const { parse } = require('sql-ast');
import { parse } from 'sql-ast';
Package does not provide ESM exports; CommonJS only.
Node
const { Node } = require('sql-ast');
const Node = require('sql-ast').NodeClass;
All exported classes are available as named exports from the CommonJS module.
Statement
const { Statement } = require('sql-ast');
import { Statement } from 'sql-ast';
CommonJS only; ESM imports will fail.

Parses a CREATE TABLE statement from a SQL string and logs the resulting AST array as JSON.

const { parse } = require('sql-ast'); const input = `CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL ) ENGINE=InnoDB;`; const ast = parse(input); console.log(JSON.stringify(ast, null, 2));
Debug
Known issues
breakingThe parse function returns an array of nodes, not a single root node.
fix
Iterate over the array returned by parse() to access individual Statement nodes.
affects: >=0.0.0
gotchaWhitespace and comments (except /*! */) are not tokenized; they are stripped during parsing.
fix
Do not rely on token positions for whitespace; use AST node positions for accurate source mapping.
affects: >=0.0.0
deprecatedFeature set is very limited; does not support many SQL dialects or complex statements.
fix
Consider using more mature SQL parsers like node-sql-parser or sql.js for broader SQL support.
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'sql-ast'
Package not installed or used in an ESM context without proper require.
fix
Install using `npm install sql-ast` and ensure CommonJS usage (require, not import).
TypeError: parse is not a function
Importing the default export instead of named export.
fix
Use `const { parse } = require('sql-ast');`
Upgrade
Version history
0.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
Meta
2
OpenAI (training)
1
Resources
sql-ast — npm install sql-ast · libregistry