You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This skill documents the code generation pipelines in pgsql-parser: protobuf-based TypeScript generation, type inference from SQL fixtures, and keyword list generation from PostgreSQL source.
4
+
5
+
## Overview
6
+
7
+
Several packages generate TypeScript code from external sources rather than being hand-written. These generated files should **not** be edited by hand — instead, re-run the generation scripts after changing inputs.
|`@pgsql/traverse`|`npm run build:proto`| Visitor-pattern traversal utilities |
17
+
|`@pgsql/transform`|`npm run build:proto`| Multi-version AST transformer utilities |
18
+
|`pg-ast`|`npm run build:proto`| Low-level AST type helpers |
19
+
20
+
Each package has a `scripts/pg-proto-parser.ts` that configures `PgProtoParser` with package-specific options (which features to enable, output paths, type sources).
21
+
22
+
**When to re-run:** After updating `__fixtures__/proto/17-latest.proto` (e.g., when upgrading to a new PostgreSQL version).
23
+
24
+
```bash
25
+
# Re-generate for a specific package
26
+
cd packages/utils && npm run build:proto
27
+
28
+
# Or build all (build:proto runs as part of build)
29
+
pnpm run build
30
+
```
31
+
32
+
Note: `build:proto` is called automatically as part of `npm run build` in these packages, so a full `pnpm run build` from root covers everything.
33
+
34
+
### Proto-Parser Test Utils
35
+
36
+
The `pg-proto-parser` package also has its own generation script:
37
+
38
+
```bash
39
+
cd packages/proto-parser && npm run generate:test-utils
40
+
```
41
+
42
+
This generates test utility functions from a `13-latest.proto` fixture into `test-utils/utils/`.
43
+
44
+
## 2. Type Inference & Narrowed Type Generation (`pgsql-types`)
45
+
46
+
The `pgsql-types` package has a two-step pipeline that discovers actual AST usage patterns from SQL fixtures and generates narrowed TypeScript types:
47
+
48
+
### Step 1: Infer field metadata
49
+
50
+
```bash
51
+
cd packages/pgsql-types && npm run infer
52
+
```
53
+
54
+
Runs `scripts/infer-field-metadata.ts`:
55
+
- Reads all `.sql` files from `__fixtures__/kitchen-sink/` and `__fixtures__/postgres/`
56
+
- Parses each statement and walks the AST
57
+
- For every `Node`-typed field, records which concrete node tags actually appear
58
+
- Writes `src/field-metadata.json` with nullable/tag/array info per field
59
+
60
+
### Step 2: Generate narrowed types
61
+
62
+
```bash
63
+
cd packages/pgsql-types && npm run generate
64
+
```
65
+
66
+
Runs `scripts/generate-types.ts`:
67
+
- Reads `src/field-metadata.json` (must run `infer` first)
68
+
- Generates `src/types.ts` with narrowed union types instead of generic `Node`
**When to re-run:** When upgrading to a new PostgreSQL version that adds/removes/reclassifies keywords.
87
+
88
+
**Requires:** A local checkout of the PostgreSQL source code to provide the `kwlist.h` file. The script will prompt for the path interactively if not provided as an argument.
cd packages/deparser && ts-node scripts/generate-version-deparsers.ts
94
+
```
95
+
96
+
Generates `versions/{13,14,15,16}/src/index.ts` files that wire up version-specific AST transformers (e.g., `PG13ToPG17Transformer`) to the main v17 deparser. This allows deparsing ASTs from older PostgreSQL versions.
97
+
98
+
**When to re-run:** When adding support for a new PostgreSQL version or changing the transformer class names.
99
+
100
+
## Quick Reference
101
+
102
+
| Workflow | Command | Input | Output |
103
+
|----------|---------|-------|--------|
104
+
| Proto codegen (all) |`pnpm run build`|`__fixtures__/proto/17-latest.proto`| Generated TS in each package's `src/`|
105
+
| Proto codegen (one pkg) |`cd packages/<pkg> && npm run build:proto`| Same | Same |
106
+
| Type inference |`cd packages/pgsql-types && npm run infer`|`__fixtures__/kitchen-sink/**/*.sql`|`src/field-metadata.json`|
107
+
| Type generation |`cd packages/pgsql-types && npm run generate`|`src/field-metadata.json`|`src/types.ts`|
|`copy`|`makage assets`| Copy non-TS assets to `dist/`|
65
+
|`lint`|`eslint . --fix`| ESLint with auto-fix |
66
+
|`test`|`jest`| Run Jest tests |
67
+
|`test:watch`|`jest --watch`| Run Jest in watch mode |
68
+
|`prepublishOnly`|`npm run build`| Ensure build before publish |
69
+
70
+
## Package-Specific Scripts
71
+
72
+
### Fixture & Testing Scripts (see testing-fixtures skill)
73
+
74
+
| Package | Script | Description |
75
+
|---------|--------|-------------|
76
+
|`deparser`|`npm run kitchen-sink`| Regenerate fixtures + test files (most common command) |
77
+
|`deparser`|`npm run fixtures`| Regenerate `generated.json` only |
78
+
|`deparser`|`npm run fixtures:kitchen-sink`| Regenerate test files only |
79
+
|`deparser`|`npm run fixtures:ast`| Generate AST JSON fixtures |
80
+
|`deparser`|`npm run fixtures:sql`| Generate SQL fixtures via native deparse |
81
+
|`deparser`|`npm run fixtures:upstream-diff`| Compare upstream (libpg-query) vs our deparser output |
82
+
|`plpgsql-deparser`|`npm run fixtures`| Extract PL/pgSQL fixtures |
83
+
|`transform`|`npm run kitchen-sink`| Generate transform kitchen-sink tests |
84
+
|`transform`|`npm run test:ast`| AST round-trip validation |
85
+
|`parser`|`npm run test:ast`| AST round-trip validation |
86
+
87
+
### Code Generation Scripts (see code-generation skill)
88
+
89
+
| Package | Script | Description |
90
+
|---------|--------|-------------|
91
+
|`utils`|`npm run build:proto`| Generate AST helpers from protobuf |
92
+
|`traverse`|`npm run build:proto`| Generate traversal utilities from protobuf |
93
+
|`transform`|`npm run build:proto`| Generate transformer utilities from protobuf |
94
+
|`pg-ast`|`npm run build:proto`| Generate AST types from protobuf |
95
+
|`pgsql-types`|`npm run infer`| Infer field metadata from SQL fixtures |
96
+
|`pgsql-types`|`npm run generate`| Generate narrowed types from metadata |
97
+
|`proto-parser`|`npm run generate:test-utils`| Generate test utilities from protobuf |
98
+
|`quotes`|`npm run keywords`| Generate keyword list from PostgreSQL `kwlist.h`|
99
+
100
+
### Version Management
101
+
102
+
| Package | Script | Description |
103
+
|---------|--------|-------------|
104
+
|`parser`|`npm run prepare-versions`| Generate version-specific sub-packages from `config/versions.json`|
105
+
| (root) |`npm run bump-versions`| Interactive CLI to bump `pgsql-parser` / `pgsql-deparser` versions per PG version |
106
+
107
+
Version configuration lives in `config/versions.json` — maps PG versions (13-17) to their `libpg-query`, `pgsql-parser`, `pgsql-deparser`, and `@pgsql/types` versions plus npm dist-tags.
108
+
109
+
### CLI Development
110
+
111
+
| Package | Script | Description |
112
+
|---------|--------|-------------|
113
+
|`pgsql-cli`|`npm run dev`| Run CLI in dev mode via `ts-node src/index`|
51
114
52
115
## Code Conventions
53
116
54
117
- TypeScript throughout, compiled to both CJS and ESM
55
118
-`@pgsql/types` provides all AST node types — use them for type safety
56
119
-`@pgsql/quotes` handles SQL identifier quoting — use `QuoteUtils` methods
57
120
- Test files go in `__tests__/` within each package
58
-
- Fixture SQL files go in `__fixtures__/kitchen-sink/` (see skills for details)
121
+
- Fixture SQL files go in `__fixtures__/kitchen-sink/` (see testing-fixtures skill)
122
+
- Generated files (marked `DO NOT EDIT BY HAND`) should be regenerated via scripts, not edited manually
0 commit comments