A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.npmjs.com/package/@pgsql/parser below:

@pgsql/parser - npm


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@lts
Dynamic 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 });
parse(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:

Each version export provides:

Example:

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:

Built on the excellent work of several contributors:

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