Gathering detailed insights and metrics for node-sql-parser-fork
Gathering detailed insights and metrics for node-sql-parser-fork
Gathering detailed insights and metrics for node-sql-parser-fork
Gathering detailed insights and metrics for node-sql-parser-fork
Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
npm install node-sql-parser-fork
Typescript
Module System
Min. Node Version
Node Version
NPM Version
78.8
Supply Chain
95.1
Quality
91.6
Maintenance
100
Vulnerability
100
License
PEG.js (66.85%)
JavaScript (30.63%)
TypeScript (2.52%)
Total Downloads
2,599
Last Day
2
Last Week
12
Last Month
154
Last Year
1,787
Apache-2.0 License
3,130 Commits
1 Forks
2 Branches
1 Contributors
Updated on Jul 02, 2025
Minified
Minified + Gzipped
Latest Version
2.1.12
Package Id
node-sql-parser-fork@2.1.12
Unpacked Size
87.59 MB
Size
10.80 MB
File Count
134
NPM Version
8.19.4
Node Version
16.20.2
Published on
Jul 02, 2025
Cumulative downloads
Total Downloads
Last Day
0%
2
Compared to previous day
Last Week
-80.6%
12
Compared to previous week
Last Month
-11%
154
Compared to previous month
Last Year
120.1%
1,787
Compared to previous year
2
33
Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList, columnList and convert it back to SQL.
1npm install node-sql-parser --save 2 3or 4 5yarn add node-sql-parser
1npm install @taozhi8833998/node-sql-parser --registry=https://npm.pkg.github.com/
Import the JS file in your page:
1// support all database parser, but file size is about 750K 2<script src="https://unpkg.com/node-sql-parser/umd/index.umd.js"></script> 3 4// or you can import specified database parser only, it's about 150K 5 6<script src="https://unpkg.com/node-sql-parser/umd/mysql.umd.js"></script> 7 8<script src="https://unpkg.com/node-sql-parser/umd/postgresql.umd.js"></script>
NodeSQLParser
object is on window
1<!DOCTYPE html> 2<html lang="en" > 3 <head> 4 <title>node-sql-parser</title> 5 <meta charset="utf-8" /> 6 </head> 7 <body> 8 <p><em>Check console to see the output</em></p> 9 <script src="https://unpkg.com/node-sql-parser/umd/mysql.umd.js"></script> 10 <script> 11 window.onload = function () { 12 // Example parser 13 const parser = new NodeSQLParser.Parser() 14 const ast = parser.astify("select id, name from students where age < 18") 15 console.log(ast) 16 const sql = parser.sqlify(ast) 17 console.log(sql) 18 } 19 </script> 20 </body> 21</html>
1// import Parser for all databases 2const { Parser } = require('node-sql-parser'); 3const parser = new Parser(); 4const ast = parser.astify('SELECT * FROM t'); // mysql sql grammer parsed by default 5 6console.log(ast);
ast
for SELECT * FROM t
1{ 2 "with": null, 3 "type": "select", 4 "options": null, 5 "distinct": null, 6 "columns": "*", 7 "from": [ 8 { 9 "db": null, 10 "table": "t", 11 "as": null 12 } 13 ], 14 "where": null, 15 "groupby": null, 16 "having": null, 17 "orderby": null, 18 "limit": null 19}
1const { Parser } = require('node-sql-parser'); 2const parser = new Parser(); 3const ast = parser.astify('SELECT * FROM t', { parseOptions: { includeLocations: true } }); 4 5console.log(ast);
ast
for SELECT * FROM t
with the loc
property indicating locations and ranges1{ 2 "with": null, 3 "type": "select", 4 "options": null, 5 "distinct": null, 6 "columns": [ 7 { 8 "expr": { 9 "type": "column_ref", 10 "table": null, 11 "column": "*" 12 }, 13 "as": null, 14 "loc": { 15 "start": { 16 "offset": 7, 17 "line": 1, 18 "column": 8 19 }, 20 "end": { 21 "offset": 8, 22 "line": 1, 23 "column": 9 24 } 25 } 26 } 27 ], 28 "into": { 29 "position": null 30 }, 31 "from": [ 32 { 33 "db": null, 34 "table": "t", 35 "as": null, 36 "loc": { 37 "start": { 38 "offset": 14, 39 "line": 1, 40 "column": 15 41 }, 42 "end": { 43 "offset": 15, 44 "line": 1, 45 "column": 16 46 } 47 } 48 } 49 ], 50 "where": null, 51 "groupby": null, 52 "having": null, 53 "orderby": null, 54 "limit": null, 55 "locking_read": null, 56 "window": null, 57 "loc": { 58 "start": { 59 "offset": 0, 60 "line": 1, 61 "column": 1 62 }, 63 "end": { 64 "offset": 15, 65 "line": 1, 66 "column": 16 67 } 68 } 69}
1const opt = { 2 database: 'MySQL' // MySQL is the default database 3} 4// import mysql parser only 5const { Parser } = require('node-sql-parser'); 6const parser = new Parser() 7// opt is optional 8const ast = parser.astify('SELECT * FROM t', opt); 9const sql = parser.sqlify(ast, opt); 10 11console.log(sql); // SELECT * FROM `t`
There two ways to parser the specified database.
import Parser from the specified database path node-sql-parser/build/{database}
1// import transactsql parser only 2const { Parser } = require('node-sql-parser/build/transactsql') 3const parser = new Parser() 4const sql = `SELECT id FROM test AS result` 5const ast = parser.astify(sql) 6console.log(parser.sqlify(ast)) // SELECT [id] FROM [test] AS [result]
OR you can pass a options object to the parser, and specify the database property.
1const opt = { 2 database: 'Postgresql' 3} 4// import all databases parser 5const { Parser } = require('node-sql-parser') 6const parser = new Parser() 7// pass the opt config to the corresponding methods 8const ast = parser.astify('SELECT * FROM t', opt) 9const sql = parser.sqlify(ast, opt) 10console.log(sql); // SELECT * FROM "t"
parse
function1const opt = { 2 database: 'MariaDB' // MySQL is the default database 3} 4const { Parser } = require('node-sql-parser/build/mariadb'); 5const parser = new Parser() 6// opt is optional 7const { tableList, columnList, ast } = parser.parse('SELECT * FROM t', opt);
1const opt = { 2 database: 'MySQL' 3} 4const { Parser } = require('node-sql-parser/build/mysql'); 5const parser = new Parser(); 6// opt is optional 7const tableList = parser.tableList('SELECT * FROM t', opt); 8 9console.log(tableList); // ["select::null::t"]
select *
, delete
and insert into tableName values()
without specified columns, the .*
column authority regex is required1const opt = { 2 database: 'MySQL' 3} 4const { Parser } = require('node-sql-parser/build/mysql'); 5const parser = new Parser(); 6// opt is optional 7const columnList = parser.columnList('SELECT t.id FROM t', opt); 8 9console.log(columnList); // ["select::t::id"]
whiteListCheck
function check on table
mode and MySQL
database by default1const { Parser } = require('node-sql-parser'); 2const parser = new Parser(); 3const sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)' 4const whiteTableList = ['(select|update)::(.*)::(a|b)'] // array that contain multiple authorities 5const opt = { 6 database: 'MySQL', 7 type: 'table', 8} 9// opt is optional 10parser.whiteListCheck(sql, whiteTableList, opt) // if check failed, an error would be thrown with relevant error message, if passed it would return undefined
1const { Parser } = require('node-sql-parser'); 2const parser = new Parser(); 3const sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)' 4const whiteColumnList = ['select::null::name', 'update::a::id'] // array that contain multiple authorities 5const opt = { 6 database: 'MySQL', 7 type: 'column', 8} 9// opt is optional 10parser.whiteListCheck(sql, whiteColumnList, opt) // if check failed, an error would be thrown with relevant error message, if passed it would return undefined
This project is inspired by the SQL parser flora-sql-parser module.
If you like my project, Star in the corresponding project right corner. Your support is my biggest encouragement! ^_^
You can also scan the qr code below or open paypal link to donate to Author.
Donate money by paypal to my account taozhi8833998@163.com
If you have made a donation, you can leave your name and email in the issue, your name will be written to the donation list.
No vulnerabilities found.
No security vulnerabilities found.