Skip to content

Commit 58a9207

Browse files
authored
Merge pull request #197 from launchql/feat/trans-readme
feat: Add comprehensive documentation for @pgsql/transform package
2 parents d7f2d15 + 35aa7c9 commit 58a9207

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 2 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
@@ -232,4 +233,4 @@ console.log(await deparse(query));
232233

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

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.
236+
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+
## Transforming ASTs Between PG Versions
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)