Gathering detailed insights and metrics for @lucianbuzzo/node-sql-parser
Gathering detailed insights and metrics for @lucianbuzzo/node-sql-parser
Gathering detailed insights and metrics for @lucianbuzzo/node-sql-parser
Gathering detailed insights and metrics for @lucianbuzzo/node-sql-parser
npm install @lucianbuzzo/node-sql-parser
Typescript
Module System
Min. Node Version
Node Version
NPM Version
78.1
Supply Chain
96.5
Quality
75.3
Maintenance
100
Vulnerability
80.9
License
PEG.js (67.46%)
JavaScript (30.06%)
TypeScript (2.48%)
Total Downloads
10,060
Last Day
3
Last Week
133
Last Month
177
Last Year
750
834 Stars
3,014 Commits
185 Forks
13 Watching
6 Branches
39 Contributors
Minified
Minified + Gzipped
Latest Version
4.6.5
Package Id
@lucianbuzzo/node-sql-parser@4.6.5
Unpacked Size
42.53 MB
Size
5.28 MB
File Count
97
NPM Version
8.19.2
Node Version
16.18.0
Publised On
13 Feb 2023
Cumulative downloads
Total Downloads
Last day
-92.3%
3
Compared to previous day
Last week
224.4%
133
Compared to previous week
Last month
1,670%
177
Compared to previous month
Last year
-91.9%
750
Compared to previous year
1
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.
Reason
30 commit(s) and 6 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
8 existing vulnerabilities detected
Details
Reason
Found 2/14 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More