|
| 1 | +# @pgsql/parser |
| 2 | + |
| 3 | +<p align="center" width="100%"> |
| 4 | + <img src="https://github.com/launchql/libpg-query-node/assets/545047/5fd420cc-cdc6-4211-9b0f-0eca8321ba72" alt="hyperweb.io" width="100"> |
| 5 | +</p> |
| 6 | + |
| 7 | +<p align="center" width="100%"> |
| 8 | + <a href="https://github.com/launchql/libpg-query/blob/main/LICENSE-MIT"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a> |
| 9 | + <a href="https://www.npmjs.com/package/libpg-query"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/libpg-query-node?filename=versions%2F17%2Fpackage.json"/></a><br /> |
| 10 | + <a href="https://github.com/launchql/libpg-query-node/actions/workflows/ci.yml"><img height="20" src="https://github.com/launchql/libpg-query-node/actions/workflows/ci.yml/badge.svg" /></a> |
| 11 | + <a href="https://github.com/launchql/libpg-query-node/actions/workflows/ci.yml"><img height="20" src="https://img.shields.io/badge/macOS-available-333333?logo=apple&logoColor=white" /></a> |
| 12 | + <a href="https://github.com/launchql/libpg-query-node/actions/workflows/ci.yml"><img height="20" src="https://img.shields.io/badge/Windows-available-333333?logo=windows&logoColor=white" /></a> |
| 13 | + <a href="https://github.com/launchql/libpg-query-node/actions/workflows/ci.yml"><img height="20" src="https://img.shields.io/badge/Linux-available-333333?logo=linux&logoColor=white" /></a> |
| 14 | +</p> |
| 15 | + |
| 16 | +Multi-version PostgreSQL parser with dynamic version selection. This package provides a unified interface to parse PostgreSQL queries using different parser versions (15, 16, 17). |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +npm install @pgsql/parser |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Dynamic Version Selection |
| 27 | + |
| 28 | +```javascript |
| 29 | +import { parse, PgParser } from '@pgsql/parser'; |
| 30 | + |
| 31 | +// Parse with default version (17) |
| 32 | +const result = await parse('SELECT 1+1 as sum'); |
| 33 | +console.log(result); |
| 34 | +// { version: 17, result: { version: 170004, stmts: [...] } } |
| 35 | + |
| 36 | +// Parse with specific version |
| 37 | +const result15 = await parse('SELECT 1+1 as sum', 15); |
| 38 | +console.log(result15); |
| 39 | +// { version: 15, result: { version: 150007, stmts: [...] } } |
| 40 | + |
| 41 | +// Using PgParser class |
| 42 | +const parser = new PgParser(16); |
| 43 | +const result16 = await parser.parse('SELECT * FROM users'); |
| 44 | +``` |
| 45 | + |
| 46 | +### Static Version Imports |
| 47 | + |
| 48 | +For better tree-shaking and when you know which version you need: |
| 49 | + |
| 50 | +```javascript |
| 51 | +// Import specific version |
| 52 | +import * as pg17 from '@pgsql/parser/v17'; |
| 53 | + |
| 54 | +await pg17.loadModule(); |
| 55 | +const result = await pg17.parse('SELECT 1'); |
| 56 | +console.log(result); |
| 57 | +// { version: 170004, stmts: [...] } |
| 58 | +``` |
| 59 | + |
| 60 | +### Error Handling |
| 61 | + |
| 62 | +The parser returns errors in a consistent format: |
| 63 | + |
| 64 | +```javascript |
| 65 | +const result = await parse('INVALID SQL'); |
| 66 | +if (result.error) { |
| 67 | + console.error(result.error); |
| 68 | + // { type: 'syntax', message: 'syntax error at or near "INVALID"', position: 0 } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## API |
| 73 | + |
| 74 | +### `parse(query: string, version?: 15 | 16 | 17): Promise<ParseResult>` |
| 75 | + |
| 76 | +Parse a SQL query with the specified PostgreSQL version. |
| 77 | + |
| 78 | +- `query`: The SQL query string to parse |
| 79 | +- `version`: PostgreSQL version (15, 16, or 17). Defaults to 17. |
| 80 | + |
| 81 | +Returns a promise that resolves to: |
| 82 | +- On success: `{ version: number, result: AST }` |
| 83 | +- On error: `{ version: number, error: { type: string, message: string, position: number } }` |
| 84 | + |
| 85 | +### `PgParser` |
| 86 | + |
| 87 | +Class for creating a parser instance with a specific version. |
| 88 | + |
| 89 | +```javascript |
| 90 | +const parser = new PgParser(version); |
| 91 | +await parser.parse(query); |
| 92 | +parser.parseSync(query); // Only available after first parse() |
| 93 | +``` |
| 94 | + |
| 95 | +## Version Exports |
| 96 | + |
| 97 | +- `@pgsql/parser/v15` - PostgreSQL 15 parser |
| 98 | +- `@pgsql/parser/v16` - PostgreSQL 16 parser |
| 99 | +- `@pgsql/parser/v17` - PostgreSQL 17 parser |
| 100 | + |
| 101 | +Each version export provides: |
| 102 | +- `loadModule()`: Initialize the WASM module |
| 103 | +- `parse(query)`: Parse a query (async) |
| 104 | +- `parseSync(query)`: Parse a query (sync, requires loadModule first) |
| 105 | + |
| 106 | +## Credits |
| 107 | + |
| 108 | +Built on the excellent work of several contributors: |
| 109 | + |
| 110 | +* **[Dan Lynch](https://github.com/pyramation)** — official maintainer since 2018 and architect of the current implementation |
| 111 | +* **[Lukas Fittl](https://github.com/lfittl)** for [libpg_query](https://github.com/pganalyze/libpg_query) — the core PostgreSQL parser that powers this project |
| 112 | +* **[Greg Richardson](https://github.com/gregnr)** for AST guidance and pushing the transition to WASM for better interoperability |
| 113 | +* **[Ethan Resnick](https://github.com/ethanresnick)** for the original Node.js N-API bindings |
| 114 | +* **[Zac McCormick](https://github.com/zhm)** for the foundational [node-pg-query-native](https://github.com/zhm/node-pg-query-native) parser |
| 115 | + |
| 116 | +## Related |
| 117 | + |
| 118 | +* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration. |
| 119 | +* [pgsql-deparser](https://www.npmjs.com/package/pgsql-deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`. |
| 120 | +* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs. |
| 121 | +* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): Provides TypeScript enum definitions for PostgreSQL constants, enabling type-safe usage of PostgreSQL enums and constants in your applications. |
| 122 | +* [@pgsql/utils](https://www.npmjs.com/package/@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. |
| 123 | +* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums. |
| 124 | +* [libpg-query](https://github.com/launchql/libpg-query-node): The real PostgreSQL parser exposed for Node.js, used primarily in `pgsql-parser` for parsing and deparsing SQL queries. |
| 125 | + |
| 126 | +## Disclaimer |
| 127 | + |
| 128 | +AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND. |
| 129 | + |
| 130 | +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. |
0 commit comments