pgsql-ast-parser is a JavaScript and TypeScript library designed for parsing PostgreSQL SQL syntax into a typed Abstract Syntax Tree (AST), and then facilitating its modification or conversion back to SQL. It is currently at version 12.0.2 and appears to have an active release cadence, with major versions indicating significant structural changes to the AST. Key differentiators include its ability to run in both Node.js and browser environments, its robust TypeScript typing which is strongly recommended for usage, and its foundational role as the underlying parser for `pg-mem`, an in-memory PostgreSQL database emulator. While it covers most common PostgreSQL syntaxes, it explicitly states it does not support PL/pgSQL or some obscure syntaxes, requiring users to test specific complex queries.
npm install pgsql-ast-parserVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to parse a SQL statement and then use an `astVisitor` to traverse the Abstract Syntax Tree, collecting all referenced table names and counting the number of joins.
Update code that accesses `ALTER TABLE` AST nodes to use the `changes` array property instead of `change`.
Refactor code that inspects `INSERT` statement ASTs to account for the new unified `insert` property, rather than separate `values` or `select` properties.
Avoid parsing PL/pgSQL blocks directly. For complex or unusual SQL, test parsing capabilities thoroughly and consider simplifying the input SQL or contributing support for missing syntax.
Utilize TypeScript in your project to leverage the provided type definitions for the AST, which will greatly improve developer experience and reduce type-related bugs when working with the parsed SQL structures.
Review the SQL query for typos or unsupported features (like PL/pgSQL). Simplify the query or test specific parts to isolate the problematic syntax. If it's standard PostgreSQL syntax, consider opening a bug report.
Consult the library's TypeScript definitions and AST documentation for the specific `pgsql-ast-parser` version you are using. Ensure your code aligns with the current AST structure, especially after major version upgrades.
Change your import statements from `const pkg = require('pgsql-ast-parser');` to `import * as pkg from 'pgsql-ast-parser';` or `import { parse } from 'pgsql-ast-parser';`.No dependency data recorded yet.