Multi-version PostgreSQL parser with dynamic version selection. This package provides a unified interface to parse PostgreSQL queries using different parser versions (13, 14, 15, 16, 17).
# Install latest (full build with all versions) npm install @pgsql/parser # Install LTS version (PostgreSQL 15-17 only) npm install @pgsql/parser@ltsDynamic Version Selection
import Parser from '@pgsql/parser'; // or: import { Parser } from '@pgsql/parser'; // Create parser with default version (17) const parser = new Parser(); const result = await parser.parse('SELECT 1+1 as sum'); console.log(result); // { version: 170004, stmts: [...] } // Create parser with specific version const parser15 = new Parser({ version: 15 }); const result15 = await parser15.parse('SELECT 1+1 as sum'); console.log(result15); // { version: 150007, stmts: [...] } // Check supported versions import { isSupportedVersion, getSupportedVersions } from '@pgsql/parser'; console.log(getSupportedVersions()); // [13, 14, 15, 16, 17] console.log(isSupportedVersion(15)); // true
const { Parser, isSupportedVersion, getSupportedVersions } = require('@pgsql/parser'); async function parseSQL() { const parser = new Parser({ version: 16 }); const result = await parser.parse('SELECT * FROM users'); console.log(result); }
For better tree-shaking and when you know which version you need:
// Import specific version import { parse } from '@pgsql/parser/v17'; const result = await parse('SELECT 1'); console.log(result); // { version: 170004, stmts: [...] } // Or access via the main module import { v17 } from '@pgsql/parser'; const result2 = await v17.parse('SELECT 1');
The parser throws errors directly (not wrapped in result objects):
import Parser from '@pgsql/parser'; const parser = new Parser(); try { const result = await parser.parse('INVALID SQL'); } catch (error) { console.error(error.name); // 'SqlError' console.error(error.message); // 'syntax error at or near "INVALID"' console.error(error.sqlDetails); // { cursorPosition: 0, ... } }
The main parser class for parsing SQL with a specific PostgreSQL version.
import Parser from '@pgsql/parser'; const parser = new Parser(options?: { version?: 13 | 14 | 15 | 16 | 17 });
version
: The PostgreSQL version used by this parser instanceready
: A promise that resolves when the parser is fully loadedparse(query: string): Promise<ParseResult>
Parse a SQL query asynchronously. Automatically loads the parser if needed.
const result = await parser.parse('SELECT 1'); // Returns: { version: 170004, stmts: [...] }
parseSync(query: string): ParseResult
Parse a SQL query synchronously. Requires the parser to be loaded first.
await parser.loadParser(); // or await parser.ready const result = parser.parseSync('SELECT 1');
loadParser(): Promise<void>
Explicitly load the parser. Usually not needed as parse()
loads automatically.
isSupportedVersion(version: number): boolean
Check if a PostgreSQL version is supported.
getSupportedVersions(): number[]
Get an array of all supported PostgreSQL versions.
All parsing errors throw SqlError
with the following structure:
class SqlError extends Error { name: 'SqlError'; message: string; sqlDetails?: { cursorPosition?: number; fileName?: string; functionName?: string; lineNumber?: number; }; }
Each PostgreSQL version can be imported directly for better tree-shaking:
@pgsql/parser/v13
- PostgreSQL 13 parser@pgsql/parser/v14
- PostgreSQL 14 parser@pgsql/parser/v15
- PostgreSQL 15 parser@pgsql/parser/v16
- PostgreSQL 16 parser@pgsql/parser/v17
- PostgreSQL 17 parserEach version export provides:
parse(query: string): Promise<ParseResult>
- Parse a query asynchronouslyparseSync(query: string): ParseResult
- Parse a query synchronously (auto-loads if needed)SqlError
- The error class for parsing errorsExample:
import { parse, parseSync } from '@pgsql/parser/v17'; // Async parsing const result = await parse('SELECT 1'); // Sync parsing (auto-loads on first use) const result2 = parseSync('SELECT 2');
This package supports different build configurations for different use cases:
When installing from npm, you can choose the appropriate build using tags:
npm install @pgsql/parser
- Full build with all versionsnpm install @pgsql/parser@lts
- LTS buildBuilt on the excellent work of several contributors:
pgsql-parser
.pgsql-parser
for parsing and deparsing SQL queries.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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4