A TypeScript Postgres SQL syntax parser that produces a typed AST (Abstract Syntax Tree) covering common PostgreSQL syntaxes. Current stable version is 10.5.10, released frequently with CI. Key differentiators: works in both Node.js and browser, provides typed ASTs for TypeScript users, includes AST visitors/mappers for traversal and modification, and supports converting AST back to SQL. It does not support PL/pgSQL and may not cover all edge cases. Built primarily for pg-mem, an in-memory PostgreSQL emulator.
npm install trader-pgsql-ast-parserNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates parsing multiple SQL statements, parsing a single statement, converting AST back to SQL, and traversing the AST with a visitor to collect table names.
Use a specialized PL/pgSQL parser or avoid parsing procedural code. The parser focuses on SQL DML/DDL statements.
Update imports to top-level: import { parse } from 'pgsql-ast-parser' instead of 'pgsql-ast-parser/lib/parser'.Replace any calls to parseOld with parse() or parseFirst().
Only use the public types exported in the package's type definitions (Statement, etc.). Avoid importing from internal paths like 'pgsql-ast-parser/lib/ast-types'.
Consider deep-cloning the AST before modification using structuredClone or a library like lodash.cloneDeep.
Change import to top-level: import { parse } from 'pgsql-ast-parser'Use toSql.statement(ast) instead of toSql(ast).
Review SQL for unsupported syntax. Ensure only standard SQL DML/DDL is passed.
Change to: import type { Statement } from 'pgsql-ast-parser'Use parse() to handle multiple statements, or split the string and call parseFirst on each part.