{"library":"plpgsql-parser","title":"PostgreSQL SQL & PL/pgSQL AST Parser (@pgsql/parser)","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install plpgsql-parser"],"cli":null},"imports":["import { parse } from '@pgsql/parser';","import { deparse } from '@pgsql/deparser';","import type { SelectStmt } from '@pgsql/types';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parse } from '@pgsql/parser';\nimport { deparse } from '@pgsql/deparser';\nimport type { SelectStmt } from '@pgsql/types';\n\nasync function processSqlQuery(sql: string) {\n  try {\n    console.log(`Parsing SQL: ${sql}`);\n    // Parse the SQL query into an Abstract Syntax Tree (AST)\n    const ast = await parse(sql);\n    console.log('Parsed AST (partial):', JSON.stringify(ast.stmts[0]?.stmt, null, 2));\n\n    // Example: Accessing a specific type of statement and its properties\n    if (ast.stmts[0]?.stmt.SelectStmt) {\n      const selectStmt = ast.stmts[0].stmt.SelectStmt as SelectStmt;\n      console.log('Target list items:', selectStmt.targetList?.map(t => JSON.stringify(t.ResTarget?.val)));\n    }\n\n    // Convert the AST back to SQL (deparse)\n    const deparsedSql = await deparse(ast);\n    console.log(`Deparsed SQL: ${deparsedSql}`);\n\n  } catch (error: any) {\n    console.error('Error processing SQL:', error.message);\n    if (error.sqlDetails) {\n      console.error('SQL Details:', error.sqlDetails);\n    }\n  }\n}\n\n// Example SQL queries\nprocessSqlQuery('SELECT id, name FROM users WHERE status = $1 ORDER BY created_at DESC;');\nprocessSqlQuery('CREATE FUNCTION add(a INT, b INT) RETURNS INT LANGUAGE plpgsql AS $$ BEGIN RETURN a + b; END; $$;');\n// Intentionally malformed SQL to demonstrate error handling\n// processSqlQuery('SELECT * FROM users WHERE;');","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}