@pgsql/parser
v16.1.0
Published
The real PostgreSQL query parser
Downloads
1,996
Maintainers
Readme
@pgsql/parser
NOTE: This version of @pgsql/parser
only contains the parsing functionality, optimized for users who do not require deparsing capabilities. This version is specifically tailored for PostgreSQL 16.
The real PostgreSQL parser for Node.js, @pgsql/parser
offers the ability to parse SQL statements into ASTs using the actual PostgreSQL parser. It enables users to parse SQL queries into AST format for further manipulation and analysis. Deparsing functionality is (yet) not included in this package.
Installation
npm install @pgsql/parser
Key Features
- True PostgreSQL Parsing: Utilizes the real PostgreSQL source code for accurate parsing.
- Symmetric Parsing and Deparsing: Convert SQL to AST and back, enabling query manipulation.
- AST Manipulation: Easily modify parts of a SQL statement through the AST.
Parser Example
Rewrite part of a SQL query:
import { parse } from '@pgsql/parser';
const stmts = parse('SELECT * FROM test_table');
CLI
npm install -g @pgsql/parser
usage
pgsql-parser <sqlfile>
Documentation
parser.parse(sql)
Parses the query and returns a parse object.
Parameters
| parameter | type | description |
| -------------------- | ------------------ | --------------------------------------------------------- |
| query
| String | SQL query |
Returns an object in the format:
{ query: <query|Object>,
error: { message: <message|String>,
fileName: <fileName|String>,
lineNumber: <line|Number>,
cursorPosition: <cursor|Number> }
parser.deparse(query)
Deparses the query tree and returns a formatted SQL statement. This function takes as input a query AST
in the same format as the query
property of on the result of the parse
method. This is the primary
functionality of this module.
Parameters
| parameter | type | description |
| -------------------- | ------------------ | --------------------------------------------------------- |
| query
| Object | Query tree obtained from parse
|
Returns a normalized formatted SQL string.
Versions
As of PG 13, PG majors versions maintained will have a matching dedicated major npm version. Only the latest Postgres stable release receives active updates.
Our latest is built with 13-latest
branch from libpg_query
| PostgreSQL Major Version | libpg_query | Status | npm
|--------------------------|-------------|---------------------|---------|
| 16 | 16-latest | Active development | latest
| 15 | (n/a) | Not supported |
| 14 | (n/a) | Not supported |
| 13 | 13-latest | use pgsql-parser
|
| 12 | (n/a) | Not supported |
| 11 | (n/a) | Not supported |
| 10 | 10-latest | Not supported | @1.3.1
(tree) |
Related
- pgsql-parser: The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
- pgsql-deparser: A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement
pgsql-parser
. - pgsql-enums: A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment
pgsql-parser
- @pgsql/enums: Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
- @pgsql/types: Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
- @pgsql/utils: A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.
- pg-proto-parser: A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
- libpg-query: The real PostgreSQL parser exposed for Node.js, used primarily in
pgsql-parser
for parsing and deparsing SQL queries.
Thanks @lfittl for building the core libpg_query
suite:
Credits
Thanks to @zhm for the OG parser that started it all:
Disclaimer
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.