Gathering detailed insights and metrics for node-sql-parser-openobserve
Gathering detailed insights and metrics for node-sql-parser-openobserve
Gathering detailed insights and metrics for node-sql-parser-openobserve
Gathering detailed insights and metrics for node-sql-parser-openobserve
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-openobserve
Typescript
Module System
Min. Node Version
Node Version
NPM Version
PEG.js (67.99%)
JavaScript (29.55%)
TypeScript (2.46%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
1 Stars
2,877 Commits
1 Watchers
2 Branches
1 Contributors
Updated on Oct 02, 2024
Latest Version
0.1.2
Package Id
node-sql-parser-openobserve@0.1.2
Unpacked Size
219.17 kB
Size
45.18 kB
File Count
45
NPM Version
10.8.2
Node Version
21.7.3
Published on
Oct 01, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
0
Compared to previous week
Last Month
0%
0
Compared to previous month
Last Year
0%
0
Compared to previous year
2
33
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
No vulnerabilities found.
No security vulnerabilities found.