{"id":13459458,"url":"https://github.com/taozhi8833998/node-sql-parser","last_synced_at":"2025-03-24T16:32:00.037Z","repository":{"id":37550350,"uuid":"162260782","full_name":"taozhi8833998/node-sql-parser","owner":"taozhi8833998","description":"Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL","archived":false,"fork":false,"pushed_at":"2025-03-20T01:09:27.000Z","size":15653,"stargazers_count":862,"open_issues_count":37,"forks_count":192,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-20T01:40:31.947Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://taozhi8833998.github.io/node-sql-parser/","language":"PEG.js","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taozhi8833998.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["taozhi8833998"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://www.paypal.me/taozhi8833998"]}},"created_at":"2018-12-18T09:09:06.000Z","updated_at":"2025-03-20T00:46:10.000Z","dependencies_parsed_at":"2022-07-14T08:18:12.638Z","dependency_job_id":"cfd9c248-4126-4e1e-abc5-23afa85fa949","html_url":"https://github.com/taozhi8833998/node-sql-parser","commit_stats":{"total_commits":1120,"total_committers":35,"mean_commits":32.0,"dds":0.4267857142857143,"last_synced_commit":"8dd67f8da48620d0b87a393514bc99bc96ca6a49"},"previous_names":[],"tags_count":90,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taozhi8833998%2Fnode-sql-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taozhi8833998%2Fnode-sql-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taozhi8833998%2Fnode-sql-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taozhi8833998%2Fnode-sql-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taozhi8833998","download_url":"https://codeload.github.com/taozhi8833998/node-sql-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245308759,"owners_count":20594302,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T09:01:23.219Z","updated_at":"2025-03-24T16:31:55.026Z","avatar_url":"https://github.com/taozhi8833998.png","language":"PEG.js","readme":"# Nodejs SQL Parser\n\n[![Build Status](https://travis-ci.org/taozhi8833998/node-sql-parser.svg?branch=master)](https://travis-ci.org/taozhi8833998/node-sql-parser)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/d923c9f2853f44f295c383d9943b56cc)](https://www.codacy.com/manual/taozhi8833998/node-sql-parser?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=taozhi8833998/node-sql-parser\u0026amp;utm_campaign=Badge_Grade)\n[![Coverage Status](https://img.shields.io/coveralls/github/taozhi8833998/node-sql-parser/master.svg)](https://coveralls.io/github/taozhi8833998/node-sql-parser?branch=master)\n[![Dependencies](https://img.shields.io/david/taozhi8833998/node-sql-parser.svg)](https://img.shields.io/david/taozhi8833998/node-sql-parser)\n[![Known Vulnerabilities](https://snyk.io/test/github/taozhi8833998/node-sql-parser/badge.svg?targetFile=package.json)](https://snyk.io/test/github/taozhi8833998/node-sql-parser?targetFile=package.json)\n[![](https://img.shields.io/badge/Powered%20by-ganjiang-brightgreen.svg)](https://github.com/taozhi8833998/node-sql-parser)\n\n[![npm version](https://badge.fury.io/js/node-sql-parser.svg)](https://badge.fury.io/js/node-sql-parser)\n[![NPM downloads](http://img.shields.io/npm/dm/node-sql-parser.svg?style=flat-square)](http://www.npmtrends.com/node-sql-parser)\n\n[![](https://img.shields.io/gitter/room/taozhi8833998/node-sql-parser.svg)](https://gitter.im/node-sql-parser/community)\n[![issues](https://img.shields.io/github/issues/taozhi8833998/node-sql-parser.svg)](https://github.com/taozhi8833998/node-sql-parser/issues)\n\n[![TypeScript definitions on DefinitelyTyped](http://definitelytyped.org/badges/standard.svg)](http://definitelytyped.org)\n[![license](https://img.shields.io/npm/l/node-sql-parser)](https://github.com/taozhi8833998/node-sql-parser/blob/master/LICENSE)\n\n**Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList, columnList and convert it back to SQL.**\n\n## :star: Features\n\n- support multiple sql statement seperate by semicolon\n- support select, delete, update and insert type\n- support drop, truncate and rename command\n- output the table and column list that the sql visited with the corresponding authority\n- support various databases engine\n\n## :tada: Install\n\n### From [npmjs](https://www.npmjs.org/)\n\n```bash\nnpm install node-sql-parser --save\n\nor\n\nyarn add node-sql-parser\n```\n\n### From [GitHub Package Registry](https://npm.pkg.github.com/)\n\n```bash\nnpm install @taozhi8833998/node-sql-parser --registry=https://npm.pkg.github.com/\n```\n\n### From Browser\n\nImport the JS file in your page:\n\n```javascript\n// support all database parser, but file size is about 750K\n\u003cscript src=\"https://unpkg.com/node-sql-parser/umd/index.umd.js\"\u003e\u003c/script\u003e\n\n// or you can import specified database parser only, it's about 150K\n\n\u003cscript src=\"https://unpkg.com/node-sql-parser/umd/mysql.umd.js\"\u003e\u003c/script\u003e\n\n\u003cscript src=\"https://unpkg.com/node-sql-parser/umd/postgresql.umd.js\"\u003e\u003c/script\u003e\n```\n- `NodeSQLParser` object is on `window`\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\" \u003e\n  \u003chead\u003e\n    \u003ctitle\u003enode-sql-parser\u003c/title\u003e\n    \u003cmeta charset=\"utf-8\" /\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cp\u003e\u003cem\u003eCheck console to see the output\u003c/em\u003e\u003c/p\u003e\n    \u003cscript src=\"https://unpkg.com/node-sql-parser/umd/mysql.umd.js\"\u003e\u003c/script\u003e\n    \u003cscript\u003e\n      window.onload = function () {\n        // Example parser\n        const parser = new NodeSQLParser.Parser()\n        const ast = parser.astify(\"select id, name from students where age \u003c 18\")\n        console.log(ast)\n        const sql = parser.sqlify(ast)\n        console.log(sql)\n      }\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## :rocket: Usage\n\n### Supported Database SQL Syntax\n\n- Athena\n- BigQuery\n- DB2\n- Hive\n- MariaDB\n- MySQL\n- PostgresQL\n- Redshift\n- Sqlite\n- TransactSQL\n- [FlinkSQL](https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/sql/)\n- Snowflake(alpha)\n- [Noql](https://noql.synatic.dev/)\n- New issue could be made for other new database.\n\n### Create AST for SQL statement\n\n```javascript\n// import Parser for all databases\nconst { Parser } = require('node-sql-parser');\nconst parser = new Parser();\nconst ast = parser.astify('SELECT * FROM t'); // mysql sql grammer parsed by default\n\nconsole.log(ast);\n```\n\n- `ast` for `SELECT * FROM t`\n\n```json\n{\n  \"with\": null,\n  \"type\": \"select\",\n  \"options\": null,\n  \"distinct\": null,\n  \"columns\": \"*\",\n  \"from\": [\n    {\n      \"db\": null,\n      \"table\": \"t\",\n      \"as\": null\n    }\n  ],\n  \"where\": null,\n  \"groupby\": null,\n  \"having\": null,\n  \"orderby\": null,\n  \"limit\": null\n}\n```\n\n#### Get node location in the AST\n```javascript\nconst { Parser } = require('node-sql-parser');\nconst parser = new Parser();\nconst ast = parser.astify('SELECT * FROM t', { parseOptions: { includeLocations: true } });\n\nconsole.log(ast);\n```\n\n- `ast` for `SELECT * FROM t` with the `loc` property indicating locations and ranges\n\n```json\n{\n  \"with\": null,\n  \"type\": \"select\",\n  \"options\": null,\n  \"distinct\": null,\n  \"columns\": [\n    {\n      \"expr\": {\n        \"type\": \"column_ref\",\n        \"table\": null,\n        \"column\": \"*\"\n      },\n      \"as\": null,\n      \"loc\": {\n        \"start\": {\n          \"offset\": 7,\n          \"line\": 1,\n          \"column\": 8\n        },\n        \"end\": {\n          \"offset\": 8,\n          \"line\": 1,\n          \"column\": 9\n        }\n      }\n    }\n  ],\n  \"into\": {\n    \"position\": null\n  },\n  \"from\": [\n    {\n      \"db\": null,\n      \"table\": \"t\",\n      \"as\": null,\n      \"loc\": {\n        \"start\": {\n          \"offset\": 14,\n          \"line\": 1,\n          \"column\": 15\n        },\n        \"end\": {\n          \"offset\": 15,\n          \"line\": 1,\n          \"column\": 16\n        }\n      }\n    }\n  ],\n  \"where\": null,\n  \"groupby\": null,\n  \"having\": null,\n  \"orderby\": null,\n  \"limit\": null,\n  \"locking_read\": null,\n  \"window\": null,\n  \"loc\": {\n    \"start\": {\n      \"offset\": 0,\n      \"line\": 1,\n      \"column\": 1\n    },\n    \"end\": {\n      \"offset\": 15,\n      \"line\": 1,\n      \"column\": 16\n    }\n  }\n}\n```\n\n\n### Convert AST back to SQL\n\n```javascript\nconst opt = {\n  database: 'MySQL' // MySQL is the default database\n}\n// import mysql parser only\nconst { Parser } = require('node-sql-parser');\nconst parser = new Parser()\n// opt is optional\nconst ast = parser.astify('SELECT * FROM t', opt);\nconst sql = parser.sqlify(ast, opt);\n\nconsole.log(sql); // SELECT * FROM `t`\n```\n\n### Parse specified Database\nThere two ways to parser the specified database.\n\nimport Parser from the specified database path `node-sql-parser/build/{database}`\n\n```javascript\n// import transactsql parser only\nconst { Parser } = require('node-sql-parser/build/transactsql')\nconst parser = new Parser()\nconst sql = `SELECT id FROM test AS result`\nconst ast = parser.astify(sql)\nconsole.log(parser.sqlify(ast)) // SELECT [id] FROM [test] AS [result]\n```\nOR you can pass a options object to the parser, and specify the database property.\n\n```javascript\nconst opt = {\n  database: 'Postgresql'\n}\n// import all databases parser\nconst { Parser } = require('node-sql-parser')\nconst parser = new Parser()\n// pass the opt config to the corresponding methods\nconst ast = parser.astify('SELECT * FROM t', opt)\nconst sql = parser.sqlify(ast, opt)\nconsole.log(sql); // SELECT * FROM \"t\"\n```\n\n### Get TableList, ColumnList, Ast by `parse` function\n\n```javascript\nconst opt = {\n  database: 'MariaDB' // MySQL is the default database\n}\nconst { Parser } = require('node-sql-parser/build/mariadb');\nconst parser = new Parser()\n// opt is optional\nconst { tableList, columnList, ast } = parser.parse('SELECT * FROM t', opt);\n```\n\n### Get the SQL visited tables\n\n-  get the table list that the sql visited\n-  the format is **{type}::{dbName}::{tableName}** // type could be select, update, delete or insert\n\n```javascript\nconst opt = {\n  database: 'MySQL'\n}\nconst { Parser } = require('node-sql-parser/build/mysql');\nconst parser = new Parser();\n// opt is optional\nconst tableList = parser.tableList('SELECT * FROM t', opt);\n\nconsole.log(tableList); // [\"select::null::t\"]\n```\n- if the table name is prefixed with database name, the table name will be parsed as **dbName::tableName**\n- if the table name is prefixed with database and schema name, the table name will be parsed as **dbName.schemaName::tableName**\n- if the table name is prefixed with server name in TransactSQL, the table name will be parsed as **serverName.dbName.schemaName::tableName**\n\n### Get the SQL visited columns\n\n- get the column list that the sql visited\n- the format is **{type}::{tableName}::{columnName}** // type could be select, update, delete or insert\n- for `select *`, `delete` and `insert into tableName values()` without specified columns, the `.*` column authority regex is required\n\n```javascript\nconst opt = {\n  database: 'MySQL'\n}\nconst { Parser } = require('node-sql-parser/build/mysql');\nconst parser = new Parser();\n// opt is optional\nconst columnList = parser.columnList('SELECT t.id FROM t', opt);\n\nconsole.log(columnList); // [\"select::t::id\"]\n```\n\n### Check the SQL with Authority List\n\n- check table authority\n- `whiteListCheck` function check on `table` mode and `MySQL` database by default\n\n```javascript\nconst { Parser } = require('node-sql-parser');\nconst parser = new Parser();\nconst sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)'\nconst whiteTableList = ['(select|update)::(.*)::(a|b)'] // array that contain multiple authorities\nconst opt = {\n  database: 'MySQL',\n  type: 'table',\n}\n// opt is optional\nparser.whiteListCheck(sql, whiteTableList, opt) // if check failed, an error would be thrown with relevant error message, if passed it would return undefined\n```\n\n- check column authority\n\n```javascript\nconst { Parser } = require('node-sql-parser');\nconst parser = new Parser();\nconst sql = 'UPDATE a SET id = 1 WHERE name IN (SELECT name FROM b)'\nconst whiteColumnList = ['select::null::name', 'update::a::id'] // array that contain multiple authorities\nconst opt = {\n  database: 'MySQL',\n  type: 'column',\n}\n// opt is optional\nparser.whiteListCheck(sql, whiteColumnList, opt) // if check failed, an error would be thrown with relevant error message, if passed it would return undefined\n```\n\n## :kissing_heart: Acknowledgement\n\nThis project is inspired by the SQL parser [flora-sql-parser](https://github.com/godmodelabs/flora-sql-parser) module.\n\n## License\n\n[Apache-2.0](LICENSE)\n\n## Buy me a Coffee\n\nIf you like my project, **Star** in the corresponding project right corner. Your support is my biggest encouragement! ^_^\n\nYou can also scan the qr code below or open paypal link to donate to Author.\n\n### Paypal\n\nDonate money by [paypal](https://www.paypal.me/taozhi8833998/5) to my account [taozhi8833998@163.com](taozhi8833998@163.com)\n\n### AliPay(支付宝)\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://github.com/taozhi8833998/node-sql-parser/blob/master/img/alipay.jpg\" width=\"300\" hight=\"300\"\u003e\n\u003c/p\u003e\n\n\n### Wechat(微信)\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://github.com/taozhi8833998/node-sql-parser/blob/master/img/wechat_pay.jpg\" width=\"300\" hight=\"300\"\u003e\n\u003c/p\u003e\n\n### Explain\n\nIf you have made a donation, you can leave your name and email in the issue, your name will be written to the donation list.\n\n\n## [Donation list](https://github.com/taozhi8833998/node-sql-parser/blob/master/DONATIONLIST.md)\n\n## Star History\n\n\u003ca href=\"https://star-history.com/#taozhi8833998/node-sql-parser\u0026Date\"\u003e\n  \u003cpicture\u003e\n    \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://api.star-history.com/svg?repos=taozhi8833998/node-sql-parser\u0026type=Date\" /\u003e\n    \u003cimg alt=\"Star History Chart\" src=\"https://api.star-history.com/svg?repos=taozhi8833998/node-sql-parser\u0026type=Date\" /\u003e\n  \u003c/picture\u003e\n\u003c/a\u003e\n","funding_links":["https://github.com/sponsors/taozhi8833998","https://www.paypal.me/taozhi8833998","https://www.paypal.me/taozhi8833998/5"],"categories":["PEG.js","SQL"],"sub_categories":["PHP - Docblock Parser"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaozhi8833998%2Fnode-sql-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaozhi8833998%2Fnode-sql-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaozhi8833998%2Fnode-sql-parser/lists"}