This document describes the various code generation methods in the proto-parser package, explaining what each method generates and how they're used in the parsing pipeline.
The proto-parser package processes protobuf definitions and generates TypeScript code for working with PostgreSQL AST nodes. The main entry point is the ProtoStore class in src/store.ts, which orchestrates the code generation process.
- Purpose: Generates import statements for types from a specified source module
- Output:
import { Type1, Type2 } from './source'or with suffix:import { Type1 as Type1Suffix } from './source' - Used in: Importing types into other generated files
- Purpose: Creates factory functions for instantiating AST nodes that return the type directly
- Output: An object with camelCase methods for each type
- Example:
export default { selectStmt: (_p?: SelectStmt) => SelectStmt, insertStmt: (_p?: InsertStmt) => InsertStmt }
- Used in:
asts.tsfile generation (whenutils.astHelpers.enabledis true)
- Purpose: Creates factory functions that return AST nodes wrapped in type-specific objects
- Output: An object with camelCase methods that return wrapped types
- Example:
export default { selectStmt: (_p?: SelectStmt) => { SelectStmt: SelectStmt }, insertStmt: (_p?: InsertStmt) => { InsertStmt: InsertStmt } }
- Used in:
wrapped-asts.tsfile generation (whenutils.wrappedAstHelpers.enabledis true)
- Purpose: Creates a union type of all AST node types
- Output:
- Simple:
export type Node = SelectStmt | InsertStmt | ... - Wrapped:
export type Node = { SelectStmt: SelectStmt } | { InsertStmt: InsertStmt } | ...
- Simple:
- Used in:
types.tsfile generation
- Purpose: Converts protobuf Type definitions to TypeScript interfaces
- Output:
export interface SelectStmt { targetList?: Node[]; fromClause?: Node[]; // ... other fields }
- Used in:
types.tsfile generation
- Purpose: Creates import specifiers for types from configured source
- Output:
import { SelectStmt, InsertStmt, ... } from './types' - Used in:
ast-helpers.tsto import type definitions
- Purpose: Converts protobuf enums to TypeScript enum declarations
- Output:
export enum MyEnum { VALUE1 = 0, VALUE2 = 1 }
- Used in:
enums.tsfile generation (when not using union types)
- Purpose: Converts protobuf enums to TypeScript union types
- Output:
export type MyEnum = 'VALUE1' | 'VALUE2' | ... - Used in:
enums.tsfile generation (whenenumsAsTypeUnionis true)
- Purpose: Generates import statements for enums
- Output:
import { Enum1, Enum2 } from './enums' - Used in: Importing enums into
types.ts
- Purpose: Creates bidirectional enum converter function
- Output:
export const getEnumValue = (enumType: EnumType, key: string | number) => { // Returns number for string input, string for number input }
- Used in:
utils/enums.ts(bidirectional mode)
- Purpose: Creates function to convert enum strings to numbers
- Output:
export const getEnumInt = (enumType: EnumType, key: string): number => { ... } - Used in:
utils/enums-to-int.ts(unidirectional mode)
- Purpose: Creates function to convert enum numbers to strings
- Output:
export const getEnumString = (enumType: EnumType, key: number): string => { ... } - Used in:
utils/enums-to-str.ts(unidirectional mode)
- Purpose: Creates nested object with individual enum converters
- Output:
export const enumToIntMap = { MyEnum: (key: string) => number, OtherEnum: (key: string) => number }
- Used in:
utils/enums-to-int.ts(nested object format)
- Purpose: Creates nested object for number-to-string conversion
- Output:
export const enumToStringMap = { MyEnum: (value: number) => string, OtherEnum: (value: number) => string }
- Used in:
utils/enums-to-str.ts(nested object format)
- Purpose: Creates JSON mapping from enum values to names
- Output:
{ "MyEnum": { "0": "VALUE1", "1": "VALUE2" } } - Used in:
writeEnumMaps()for generating enum mapping files
- Purpose: Creates JSON mapping from enum names to values
- Output:
{ "MyEnum": { "VALUE1": 0, "VALUE2": 1 } } - Used in:
writeEnumMaps()for generating enum mapping files
- Purpose: Maps protobuf primitive types to TypeScript types
- Mappings:
string→stringdouble,float,int32, etc. →numberint64,uint64, etc. →bigintbytes→Uint8Arraybool→boolean- Custom types → Type reference
- Purpose: Main entry point for type resolution
- Logic: Uses
getTSTypefor primitives, creates type references for custom types
-
Parse Phase (
ProtoStore._parse)- Processes protobuf definitions
- Collects types, enums, and services
- Handles enum value transformations (e.g., removing UNDEFINED at 0)
-
Write Phase (
ProtoStore.write)writeEnumMaps(): Generates JSON or TypeScript enum mapping fileswriteTypes(): Generates TypeScript interfaces for all typeswriteEnums(): Generates enum declarations or union typeswriteUtilsEnums(): Generates enum utility functionswriteAstHelpers(): Generates AST node factory functionswriteRuntimeSchema(): Generates runtime schema information
The generation behavior is controlled by options in PgProtoStoreOptions:
types.enabled: Enable type generationtypes.optionalFields: Make all fields optionaltypes.wrappedNodeTypeExport: Use wrapped object format for Node unionenums.enabled: Enable enum generationenums.enumsAsTypeUnion: Generate union types instead of enumsenums.removeUndefinedAt0: Remove UNDEFINED enum value at position 0utils.enums.unidirectional: Generate separate to-int and to-string functionsutils.enums.outputFormat: Use 'nestedObjects' for better type safetyutils.astHelpers.enabled: Generate AST helper methods inasts.tsutils.wrappedAstHelpers.enabled: Generate wrapped AST helper methods inwrapped-asts.ts
Based on configuration, the following files are typically generated:
types.ts: TypeScript interfaces for all AST node typesenums.ts: Enum declarations or union typesutils/enums.ts: Bidirectional enum converter (or separate files for unidirectional)asts.ts: AST helper methods that return type instances directlywrapped-asts.ts: AST helper methods that return{ TypeName: instance }utils/ast-helpers.ts: Factory functions for creating AST nodesenums-map-to-int.json/ts: Enum to integer mappingsenums-map-to-str.json/ts: Integer to enum string mappings