{"library":"pgsql-parser","title":"PostgreSQL Query Parser","description":"pgsql-parser is a JavaScript/TypeScript library designed for parsing PostgreSQL SQL queries into an Abstract Syntax Tree (AST) and deparsing ASTs back into SQL. It leverages the actual PostgreSQL C parser, compiled to WebAssembly, to ensure 100% compatibility with PostgreSQL's syntax. The current stable version, as specified, is 17.9.15. The project is part of a broader monorepo that includes related packages like `@pgsql/parser` for multi-version parsing, `@pgsql/deparser` for AST-to-SQL conversion only, and `@pgsql/types` for comprehensive TypeScript definitions of AST nodes. It offers symmetric operations, meaning an AST generated from SQL can be perfectly converted back to the original SQL, and is extensively tested for reliability. This library is ideal for tools requiring deep SQL analysis, modification, or code generation, providing a robust and type-safe foundation for working with PostgreSQL at the AST level. Its release cadence follows the PostgreSQL versioning and related ecosystem packages are frequently updated.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pgsql-parser"],"cli":null},"imports":["import { parse } from 'pgsql-parser';","import { deparse } from 'pgsql-parser';","import type { ParseResult } from 'pgsql-parser';","import { Parser } from '@pgsql/parser';\nconst parser = new Parser({ version: 17 });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parse, deparse } from 'pgsql-parser';\nimport type { SelectStmt } from '@pgsql/types';\n\nasync function main() {\n  const sqlQuery = \"SELECT id, name FROM users WHERE status = 'active' ORDER BY name ASC;\";\n\n  try {\n    // Parse the SQL query into an Abstract Syntax Tree (AST)\n    const ast = await parse(sqlQuery);\n    console.log('Original AST:', JSON.stringify(ast, null, 2));\n\n    // Example: Modify the AST (e.g., change the table name)\n    // The AST structure can be complex and depends on the PostgreSQL version.\n    // Assuming a simple SelectStmt structure for demonstration.\n    if (ast && ast.stmts && ast.stmts.length > 0) {\n      const selectStmt = ast.stmts[0]?.stmt?.SelectStmt as SelectStmt | undefined;\n      if (selectStmt && selectStmt.fromClause && selectStmt.fromClause.length > 0) {\n        const rangeVar = selectStmt.fromClause[0]?.RangeVar;\n        if (rangeVar) {\n          rangeVar.relname = 'customers'; // Change 'users' to 'customers'\n          console.log('\\nModified AST:', JSON.stringify(ast, null, 2));\n\n          // Deparse the modified AST back into a SQL string\n          const modifiedSql = await deparse(ast);\n          console.log('\\nModified SQL:', modifiedSql);\n        }\n      }\n    }\n\n  } catch (error) {\n    if (error instanceof Error) {\n      console.error('Parsing failed:', error.message);\n    } else {\n      console.error('An unknown error occurred:', error);\n    }\n  }\n}\n\nmain();","lang":"typescript","description":"Demonstrates how to parse a PostgreSQL SQL query into an AST, programmatically modify the AST (e.g., change a table name), and then deparse the modified AST back into a SQL string using `pgsql-parser`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}