{"library":"pg-proto-parser","title":"PostgreSQL Protocol Buffers Definition Parser","description":"The `pg-proto-parser` library is a TypeScript project designed to parse Protocol Buffers (`.proto`) definitions specifically for `pganalyze/libpg_query` PostgreSQL Abstract Syntax Tree (AST) structures. It does not parse raw SQL queries or the PostgreSQL wire protocol directly. Instead, its primary function is to generate TypeScript interfaces, utility functions, and JSON mappings for the enums and messages defined within these PostgreSQL-related protobuf schemas. This generated code facilitates the creation of type-safe AST nodes and simplifies enum value conversions, serving as a foundational tool for other PostgreSQL tooling like `launchql/pgsql-parser` for maintainable upgrades. The package is currently at version 1.30.5 and appears to be actively maintained, offering a crucial layer for developers working with PostgreSQL's internal query representation in a type-safe TypeScript environment.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pg-proto-parser"],"cli":null},"imports":["import { PgProtoParser } from 'pg-proto-parser';","import type { PgProtoParserOptions } from 'pg-proto-parser';","import type { A_Const } from 'pg-proto-parser/types';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { PgProtoParser } from 'pg-proto-parser';\nimport { readFileSync, mkdirSync, writeFileSync } from 'fs';\nimport { resolve } from 'path';\n\nconst protoContent = `\nsyntax = \"proto3\";\n\npackage pg_query;\n\nmessage Node {\n  int32 location = 1;\n}\n\nmessage A_Const {\n  Node node = 1;\n  oneof val {\n    int64 ival = 2; // integer\n    bool bval = 3; // boolean\n    string sval = 4; // string\n  }\n  int32 location = 5;\n}\n`;\n\n// Create a dummy proto file for parsing demonstration\nconst tempDir = resolve(__dirname, './temp-proto-output');\nmkdirSync(tempDir, { recursive: true });\nconst tempProtoFile = resolve(tempDir, 'dummy.proto');\nwriteFileSync(tempProtoFile, protoContent);\n\nasync function generatePgProtoTypes() {\n  try {\n    // In a real application, 'inFile' would point to the libpg_query proto definitions.\n    // For this example, we use our dummy proto file.\n    const parser = new PgProtoParser(tempProtoFile, {\n      outDir: tempDir,\n      enums: {\n        enumMap: { enabled: true, format: 'ts', toIntOutFile: 'enum-to-int.ts', toStrOutFile: 'enum-to-str.ts' }\n      }\n    });\n\n    await parser.write();\n\n    console.log(`Successfully generated types and utilities in: ${tempDir}`);\n    console.log('Check files like types.ts, enums.ts, utils.ts in the output directory.');\n\n    // Example of importing a generated type (after generation)\n    // const { A_Const } = await import(resolve(tempDir, 'types.ts'));\n    // const myConst: A_Const = { node: { location: 0 }, ival: '123' }; // Example usage\n\n  } catch (error) {\n    console.error('Error generating types:', error);\n  }\n}\n\ngeneratePgProtoTypes();\n","lang":"typescript","description":"This quickstart demonstrates how to initialize `PgProtoParser` with a dummy protobuf schema, configure output options, and generate corresponding TypeScript interfaces, enums, and utility files into a specified directory.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}