🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more
Socket
Book a DemoInstallSign in
Socket

node-sql-parser

Package Overview
Dependencies
Maintainers
1
Versions
179
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-sql-parser

simple node sql parser

Source
npmnpm
Version
1.2.0
Version published
Weekly downloads
451K
15.77%
Maintainers
1
Weekly downloads
 
Created
Source

Nodejs SQL Parser

Build Status Codacy Badge Coverage Status Dependencies Known Vulnerabilities

npm version NPM downloads

issues

TypeScript definitions on DefinitelyTyped license

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList, columnList and convert it back to SQL.

:star: Features

  • support multiple sql statement seperate by semicolon
  • support select, delete, update and insert type
  • output the table and column list that the sql visited with the corresponding authority
  • support typescript

:tada: Install

npm install node-sql-parser --save

or

yarn add node-sql-parser

Install the following type module for typescript usage

npm install @types/node-sql-parser --save-dev

or

yarn add @types/node-sql-parser --dev

:rocket: Usage

Create AST for SQL statement

const { Parser } = require('node-sql-parser');
const parser = new Parser();
const ast = parser.astify('SELECT * FROM t');

console.log(ast);
  • ast for SELECT * FROM t
{
  "tableList": [
    "select::null::t"
  ],
  "columnList": [
    "select::null::(.*)"
  ],
  "ast": {
    "with": null,
    "type": "select",
    "options": null,
    "distinct": null,
    "columns": "*",
    "from": [
      {
        "db": null,
        "table": "t",
        "as": null
      }
    ],
    "where": null,
    "groupby": null,
    "having": null,
    "orderby": null,
    "limit": null
  }
}

Convert AST back to SQL

const { Parser } = require('node-sql-parser');
const parser = new Parser()
const ast = parser.astify('SELECT * FROM t');
const sql = parse.sqlify(ast);

console.log(sql); // SELECT * FROM `t`

TableList, ColumnList, Ast

const { Parser } = require('node-sql-parser');
const parser = new Parser()
const { tableList, columnList, ast } = parser.parse('SELECT * FROM t');

Get the SQL visited tables

  • get the table list that the sql visited
  • the format is {type}::{dbName}::{tableName} // type could be select, update, delete or insert
const { Parser } = require('node-sql-parser');
const parser = new Parser();
const tableList = parser.tableList('SELECT * FROM t');

console.log(tableList); // ["select::null::t"]

Get the SQL visited columns

  • get the column list that the sql visited
  • the format is {type}::{tableName}::{columnName} // type could be select, update, delete or insert
  • for select *, delete and insert into tableName values() without specified columns, the .* column authority regex is required
const { Parser } = require('node-sql-parser');
const parser = new Parser();
const columnList = parser.columnList('SELECT t.id FROM t');

console.log(columnList); // ["select::t::id"]

Check the SQL with Authority List

  • check table authority
  • whiteListCheck function check on table mode by default
const { Parser } = require('node-sql-parser');
const parser = new Parser();
const sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)'
const whiteTableList = ['(select|update)::(.*)::(a|b)'] // array that contain multiple authorities
parser.whiteListCheck(sql, whiteTableList, 'table') // if check failed, an error would be thrown with relevant error message, if passed it would return undefined
  • check column authority
const { Parser } = require('node-sql-parser');
const parser = new Parser();
const sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)'
const whiteColumnList = ['select::null::name', 'update::a::id'] // array that contain multiple authorities
parser.whiteListCheck(sql, whiteColumnList, 'column') // if check failed, an error would be thrown with relevant error message, if passed it would return undefined

:kissing_heart: Acknowledgement

This project is based on the SQL parser extracted from flora-sql-parser module.

License

MIT

Keywords

sql

FAQs

Package last updated on 11 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts