|
8 | 8 | <a href="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml"> |
9 | 9 | <img height="20" src="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml/badge.svg" /> |
10 | 10 | </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> --> |
13 | 11 | <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> |
15 | 13 | </p> |
16 | 14 |
|
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. |
18 | 117 |
|
19 | 118 | ## Related |
20 | 119 |
|
|
31 | 130 |
|
32 | 131 | AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND. |
33 | 132 |
|
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