The `@pgsql/parser` package is a core component within a comprehensive monorepo for PostgreSQL Abstract Syntax Tree (AST) parsing, manipulation, and code generation. It provides a robust, multi-version PostgreSQL parser capable of converting SQL and PL/pgSQL queries into hydrated ASTs, supporting PostgreSQL versions 13 through 17. The current stable version for `@pgsql/parser` is approximately 17.0.4, with active development and frequent updates across the monorepo's packages, such as `@pgsql/deparser` (around 0.7.3). Key differentiators include its direct integration with `libpg-query` (the actual PostgreSQL parser exposed for Node.js), offering high fidelity to PostgreSQL's native parsing logic. It also ships with extensive TypeScript type definitions (`@pgsql/types`), utilities for programmatic AST construction (`@pgsql/utils`), and traversal tools (`@pgsql/traverse`), making it suitable for advanced static analysis, query transformation, and code generation tasks. This library aims to provide a complete toolkit for working with PostgreSQL at the AST level.
npm install plpgsql-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing a SQL query into an AST, inspecting the AST (specifically a SELECT statement's target list), and then deparsing the AST back into a SQL string. Includes error handling for invalid SQL and shows a PL/pgSQL function parsing.
Migrate to `@pgsql/parser@^17.0.0` and update import paths and API calls according to the latest documentation within the `constructive-io/pgsql-parser` monorepo. Install specific sub-packages, e.g., `npm install @pgsql/parser @pgsql/deparser @pgsql/types`.
Always explicitly import from the scoped packages, e.g., `import { parse } from '@pgsql/parser';`.Always consult the changelog of `@pgsql/parser` and related packages (`@pgsql/deparser`, `@pgsql/types`) when upgrading major versions. Thoroughly test existing parsing and transformation logic after an upgrade.
Utilize helper packages like `@pgsql/utils` for programmatic AST construction and `@pgsql/traverse` for safer traversal and modification via a visitor pattern. Always validate modified ASTs by deparsing and testing the resulting SQL.
Install the package: `npm install @pgsql/parser` or `yarn add @pgsql/parser`.
Carefully review the SQL/PL/pgSQL string for typos, missing punctuation, incorrect keywords, or dialect-specific syntax not compatible with standard PostgreSQL. Ensure the `@pgsql/parser` version supports the target PostgreSQL version's syntax.
For ESM (`type: 'module'` in package.json or `.mjs` files): `import { parse } from '@pgsql/parser';`. For CommonJS (`type: 'commonjs'` or `.cjs` files): `const { parse } = require('@pgsql/parser');`.