Skip to content

Commit 7aef660

Browse files
feat: Add comprehensive documentation for @pgsql/transform package
- Add transform package to main README packages table - Document core use case: single source of truth deparser - Explain multi-version AST transformation (PG13→PG17) - Highlight key limitation: only PG17-parseable SQL supported - Add comprehensive usage examples and architecture overview - Fix packages/transform/README.md with correct package documentation - Include installation, quick start, and integration examples Co-Authored-By: Dan Lynch <[email protected]>
1 parent a47f5d4 commit 7aef660

File tree

2 files changed

+226
-6
lines changed

2 files changed

+226
-6
lines changed

README.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ await deparse(stmt);
132132
| [**@pgsql/cli**](./packages/pgsql-cli) | Unified CLI for all PostgreSQL AST operations | • Parse SQL to AST<br>• Deparse AST to SQL<br>• Generate TypeScript from protobuf<br>• Single tool for all operations |
133133
| [**@pgsql/utils**](./packages/utils) | Type-safe AST node creation utilities | • Programmatic AST construction<br>• Runtime Schema<br>• Seamless integration with types |
134134
| [**pg-proto-parser**](./packages/proto-parser) | PostgreSQL protobuf parser and code generator | • Generate TypeScript interfaces from protobuf<br>• Create enum mappings and utilities<br>• AST helper generation |
135+
| [**@pgsql/transform**](./packages/transform) | Multi-version PostgreSQL AST transformer | • Transform ASTs between PostgreSQL versions (13→17)<br>• Single source of truth deparser pipeline<br>• Backward compatibility for legacy SQL |
135136

136137

137138
## 🛠️ Development
@@ -217,6 +218,126 @@ console.log(await deparse(query));
217218
// SELECT name, email FROM users WHERE age > 18
218219
```
219220

221+
## AST Transformation Between PostgreSQL Versions
222+
223+
The `@pgsql/transform` package enables transformation of PostgreSQL ASTs between different PostgreSQL versions, providing a crucial component for building a single source of truth deparser that works with legacy SQL code.
224+
225+
### Core Use Case
226+
227+
The transform package serves as the backbone for maintaining backward compatibility in the deparser pipeline. It allows you to:
228+
229+
- **Transform legacy ASTs**: Convert ASTs from older PostgreSQL versions (13, 14, 15, 16) to the latest version (17)
230+
- **Build unified deparsers**: Create a single deparser that can handle SQL from multiple PostgreSQL versions
231+
- **Maintain compatibility**: Support legacy codebases while leveraging the latest PostgreSQL parser features
232+
233+
### Key Limitation
234+
235+
The transform package only supports ASTs that derive from SQL parseable by PostgreSQL 17. This means:
236+
237+
-**Supported**: SQL code from PG13-16 that is still valid in PG17
238+
-**Not supported**: Deprecated PG13 syntax that was removed in later versions
239+
-**Not supported**: SQL that cannot be parsed by the PG17 parser
240+
241+
This limitation is by design - it ensures that all transformed ASTs can be reliably deparsed using the latest PostgreSQL grammar.
242+
243+
### Installation
244+
245+
```bash
246+
npm install @pgsql/transform
247+
```
248+
249+
### Basic Usage
250+
251+
#### Multi-Version Transformer
252+
253+
```typescript
254+
import { ASTTransformer } from '@pgsql/transform';
255+
256+
const transformer = new ASTTransformer();
257+
258+
// Transform from any version to PG17
259+
const pg17Ast = transformer.transformToLatest(pg13Ast, 13);
260+
261+
// Transform between specific versions
262+
const pg15Ast = transformer.transform(pg13Ast, 13, 15);
263+
264+
// Convenience methods for common transformations
265+
const pg17FromPg13 = transformer.transform13To17(pg13Ast);
266+
const pg17FromPg14 = transformer.transform14To17(pg14Ast);
267+
```
268+
269+
#### Direct Transformers
270+
271+
For better performance when you know the source and target versions:
272+
273+
```typescript
274+
import {
275+
PG13ToPG17Transformer,
276+
PG14ToPG17Transformer,
277+
PG15ToPG17Transformer,
278+
PG16ToPG17Transformer
279+
} from '@pgsql/transform';
280+
281+
// Direct transformation from PG13 to PG17
282+
const pg13to17 = new PG13ToPG17Transformer();
283+
const pg17Ast = pg13to17.transform(pg13Ast);
284+
285+
// Works with both individual nodes and ParseResult objects
286+
const transformedParseResult = pg13to17.transform(pg13ParseResult);
287+
```
288+
289+
#### Integration with Parser and Deparser
290+
291+
```typescript
292+
import { parse } from '@pgsql/parser'; // Multi-version parser
293+
import { deparse } from 'pgsql-deparser';
294+
import { PG13ToPG17Transformer } from '@pgsql/transform';
295+
296+
// Parse with older PostgreSQL version
297+
const pg13Ast = await parse('SELECT * FROM users', { version: 13 });
298+
299+
// Transform to latest version for deparser
300+
const transformer = new PG13ToPG17Transformer();
301+
const pg17Ast = transformer.transform(pg13Ast);
302+
303+
// Deparse using latest grammar
304+
const sql = await deparse(pg17Ast);
305+
console.log(sql); // SELECT * FROM users
306+
```
307+
308+
### Transformation Chain
309+
310+
The package supports both incremental and direct transformations:
311+
312+
**Incremental Chain**: PG13 → PG14 → PG15 → PG16 → PG17
313+
- Each step handles version-specific changes
314+
- Useful for debugging transformation issues
315+
- Allows intermediate version targeting
316+
317+
**Direct Transformers**: PG13 → PG17, PG14 → PG17, etc.
318+
- Optimized single-step transformations
319+
- Better performance for common use cases
320+
- Internally chains the incremental transformers
321+
322+
### Version Support
323+
324+
| Source Version | Target Versions | Transformer Class |
325+
|----------------|-----------------|-------------------|
326+
| PostgreSQL 13 | 14, 15, 16, 17 | `V13ToV14Transformer`, `PG13ToPG17Transformer` |
327+
| PostgreSQL 14 | 15, 16, 17 | `V14ToV15Transformer`, `PG14ToPG17Transformer` |
328+
| PostgreSQL 15 | 16, 17 | `V15ToV16Transformer`, `PG15ToPG17Transformer` |
329+
| PostgreSQL 16 | 17 | `V16ToV17Transformer`, `PG16ToPG17Transformer` |
330+
331+
### Architecture
332+
333+
The transform package is designed to work seamlessly with the broader pgsql-parser ecosystem:
334+
335+
1. **Parse** legacy SQL using version-specific parsers
336+
2. **Transform** the resulting AST to the latest version
337+
3. **Deparse** using the unified, latest-version deparser
338+
339+
This architecture enables a single source of truth for SQL generation while maintaining support for legacy codebases.
340+
220341
## Related
221342

222343
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
@@ -232,4 +353,4 @@ console.log(await deparse(query));
232353

233354
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
234355

235-
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
356+
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

packages/transform/README.md

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,112 @@
88
<a href="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml">
99
<img height="20" src="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml/badge.svg" />
1010
</a>
11-
<!-- <a href="https://www.npmjs.com/package/@pgsql/types"><img height="20" src="https://img.shields.io/npm/dt/@pgsql/types"></a> -->
12-
<!-- <a href="https://www.npmjs.com/package/@pgsql/types"><img height="20" src="https://img.shields.io/npm/dw/@pgsql/types"/></a> -->
1311
<a href="https://github.com/launchql/pgsql-parser/blob/main/LICENSE-MIT"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
14-
<a href="https://www.npmjs.com/package/@pgsql/types"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Ftypes%2Fpackage.json"/></a>
12+
<a href="https://www.npmjs.com/package/@pgsql/transform"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Ftransform%2Fpackage.json"/></a>
1513
</p>
1614

17-
`@pgsql/types` is a TypeScript library providing type definitions for PostgreSQL AST nodes, primarily used in conjunction with [`pgsql-parser`](https://github.com/launchql/pgsql-parser). It offers a comprehensive and type-safe way to interact with the AST nodes generated by PostgreSQL query parsing.
15+
`@pgsql/transform` is a TypeScript library for transforming PostgreSQL ASTs between different PostgreSQL versions. It serves as a crucial component for building a single source of truth deparser that can handle SQL from multiple PostgreSQL versions while maintaining backward compatibility.
16+
17+
## 🎯 Core Purpose
18+
19+
The transform package enables you to:
20+
21+
- **Transform legacy ASTs**: Convert ASTs from PostgreSQL versions 13-16 to version 17
22+
- **Build unified deparsers**: Create a single deparser pipeline that works with multiple PostgreSQL versions
23+
- **Maintain backward compatibility**: Support legacy codebases while leveraging the latest PostgreSQL features
24+
25+
## ⚠️ Key Limitation
26+
27+
This package only supports ASTs derived from SQL that is parseable by PostgreSQL 17. This means:
28+
29+
-**Supported**: SQL from PG13-16 that remains valid in PG17
30+
-**Not supported**: Deprecated syntax from older versions that was removed
31+
-**Not supported**: SQL that cannot be parsed by the PG17 parser
32+
33+
This design ensures all transformed ASTs can be reliably deparsed using the latest PostgreSQL grammar.
34+
35+
## 📦 Installation
36+
37+
```bash
38+
npm install @pgsql/transform
39+
```
40+
41+
## 🚀 Quick Start
42+
43+
### Multi-Version Transformer
44+
45+
```typescript
46+
import { ASTTransformer } from '@pgsql/transform';
47+
48+
const transformer = new ASTTransformer();
49+
50+
// Transform any version to PG17
51+
const pg17Ast = transformer.transformToLatest(pg13Ast, 13);
52+
53+
// Transform between specific versions
54+
const pg15Ast = transformer.transform(pg13Ast, 13, 15);
55+
56+
// Convenience methods
57+
const result = transformer.transform13To17(pg13Ast);
58+
```
59+
60+
### Direct Transformers
61+
62+
For better performance when you know source and target versions:
63+
64+
```typescript
65+
import { PG13ToPG17Transformer } from '@pgsql/transform';
66+
67+
const transformer = new PG13ToPG17Transformer();
68+
const pg17Ast = transformer.transform(pg13Ast);
69+
```
70+
71+
### Integration Example
72+
73+
```typescript
74+
import { parse } from '@pgsql/parser';
75+
import { deparse } from 'pgsql-deparser';
76+
import { PG13ToPG17Transformer } from '@pgsql/transform';
77+
78+
// Parse with older version
79+
const pg13Ast = await parse('SELECT * FROM users', { version: 13 });
80+
81+
// Transform to latest
82+
const transformer = new PG13ToPG17Transformer();
83+
const pg17Ast = transformer.transform(pg13Ast);
84+
85+
// Deparse with latest grammar
86+
const sql = await deparse(pg17Ast);
87+
```
88+
89+
## 🔄 Transformation Chain
90+
91+
**Incremental**: PG13 → PG14 → PG15 → PG16 → PG17
92+
- Step-by-step version upgrades
93+
- Useful for debugging transformation issues
94+
95+
**Direct**: PG13 → PG17, PG14 → PG17, etc.
96+
- Single-step transformations
97+
- Optimized for performance
98+
99+
## 📋 Supported Transformations
100+
101+
| From | To | Transformer |
102+
|------|----|-----------|
103+
| PG13 | PG14, PG15, PG16, PG17 | `V13ToV14Transformer`, `PG13ToPG17Transformer` |
104+
| PG14 | PG15, PG16, PG17 | `V14ToV15Transformer`, `PG14ToPG17Transformer` |
105+
| PG15 | PG16, PG17 | `V15ToV16Transformer`, `PG15ToPG17Transformer` |
106+
| PG16 | PG17 | `V16ToV17Transformer`, `PG16ToPG17Transformer` |
107+
108+
## 🏗️ Architecture
109+
110+
The transform package fits into the broader pgsql-parser ecosystem:
111+
112+
1. **Parse** legacy SQL with version-specific parsers
113+
2. **Transform** ASTs to the latest version
114+
3. **Deparse** using the unified, latest-version deparser
115+
116+
This enables a single source of truth for SQL generation while supporting legacy codebases.
18117

19118
## Related
20119

@@ -31,4 +130,4 @@
31130

32131
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
33132

34-
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
133+
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

0 commit comments

Comments
 (0)