pgsql-deparser 13.19.0 → 13.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -16
- package/deparser/deparser.d.ts +37 -28
- package/deparser/deparser.js +1413 -948
- package/deparser/index.d.ts +3 -1
- package/deparser/index.js +5 -1
- package/deparser/utils/sql-formatter.d.ts +8 -0
- package/deparser/utils/sql-formatter.js +104 -1
- package/esm/deparser/deparser.js +1396 -931
- package/esm/deparser/index.js +3 -1
- package/esm/deparser/utils/sql-formatter.js +103 -1
- package/esm/index.js +101 -7
- package/esm/v13-to-v14.js +76 -29
- package/esm/v13-to-v18-direct.js +88 -0
- package/esm/v14-to-v15.js +34 -0
- package/esm/v15-to-v16.js +16 -5
- package/esm/v16-to-v17.js +50 -1
- package/esm/v17-to-v18.js +209 -0
- package/index.d.ts +18 -4
- package/index.js +104 -6
- package/package.json +7 -6
- package/v13-to-v14.d.ts +6 -0
- package/v13-to-v14.js +76 -29
- package/v13-to-v18-direct.d.ts +25 -0
- package/v13-to-v18-direct.js +92 -0
- package/v14-to-v15.d.ts +1 -0
- package/v14-to-v15.js +34 -0
- package/v15-to-v16.js +16 -5
- package/v16-to-v17.d.ts +3 -0
- package/v16-to-v17.js +50 -1
- package/v17-to-v18.d.ts +55 -0
- package/v17-to-v18.js +213 -0
package/README.md
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
# pgsql-deparser
|
|
2
2
|
|
|
3
3
|
<p align="center" width="100%">
|
|
4
|
-
<img height="
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
<p align="center" width="100%">
|
|
8
|
-
<a href="https://github.com/
|
|
9
|
-
<img height="20" src="https://github.com/
|
|
8
|
+
<a href="https://github.com/constructive-io/pgsql-parser/actions/workflows/run-tests.yaml">
|
|
9
|
+
<img height="20" src="https://github.com/constructive-io/pgsql-parser/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
10
|
</a>
|
|
11
11
|
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/npm/dt/pgsql-deparser"></a>
|
|
12
12
|
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/npm/dw/pgsql-deparser"/></a>
|
|
13
|
-
<a href="https://github.com/
|
|
14
|
-
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/github/package-json/v/
|
|
13
|
+
<a href="https://github.com/constructive-io/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-deparser"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/pgsql-parser?filename=packages%2Fdeparser%2Fpackage.json"/></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
`pgsql-deparser` is the lightning-fast, pure TypeScript solution for converting PostgreSQL ASTs back into SQL queries. Perfect companion to [`pgsql-parser`](https://github.com/
|
|
17
|
+
`pgsql-deparser` is the lightning-fast, pure TypeScript solution for converting PostgreSQL ASTs back into SQL queries. Perfect companion to [`pgsql-parser`](https://github.com/constructive-io/pgsql-parser), this focused tool delivers SQL generation without any native dependencies or WebAssembly overhead.
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -33,7 +33,7 @@ npm install pgsql-deparser
|
|
|
33
33
|
|
|
34
34
|
The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding the full parser's native dependencies. It's useful when only SQL string conversion from ASTs is needed, and is written in pure TypeScript for easy cross-environment deployment.
|
|
35
35
|
|
|
36
|
-
Here's how you can use the deparser in your TypeScript code, using [`@pgsql/utils`](https://github.com/
|
|
36
|
+
Here's how you can use the deparser in your TypeScript code, using [`@pgsql/utils`](https://github.com/constructive-io/pgsql-parser/tree/main/packages/utils) to create an AST for `deparse`:
|
|
37
37
|
|
|
38
38
|
```ts
|
|
39
39
|
import * as t from '@pgsql/utils';
|
|
@@ -69,27 +69,29 @@ console.log(deparse(stmt));
|
|
|
69
69
|
// Output: SELECT * FROM another_table
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
-
### Latest Version (PostgreSQL
|
|
72
|
+
### Latest Version (PostgreSQL 18)
|
|
73
73
|
|
|
74
74
|
```sh
|
|
75
75
|
npm install pgsql-deparser
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
-
### Version-Specific Packages (PostgreSQL 13-
|
|
78
|
+
### Version-Specific Packages (PostgreSQL 13-17)
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
We recommend PG18. PG17 remains fully supported; for PostgreSQL versions 13-17, use the version-specific packages:
|
|
81
81
|
|
|
82
82
|
```sh
|
|
83
83
|
npm install pgsql-deparser@pg13 # PostgreSQL 13
|
|
84
84
|
npm install pgsql-deparser@pg14 # PostgreSQL 14
|
|
85
85
|
npm install pgsql-deparser@pg15 # PostgreSQL 15
|
|
86
86
|
npm install pgsql-deparser@pg16 # PostgreSQL 16
|
|
87
|
+
npm install pgsql-deparser@pg17 # PostgreSQL 17
|
|
87
88
|
```
|
|
88
89
|
|
|
89
90
|
**Version Status:**
|
|
90
|
-
- **
|
|
91
|
-
- **
|
|
92
|
-
- **
|
|
91
|
+
- **PG18**: 🚀 Recommended (stable + modern AST)
|
|
92
|
+
- **PG17**: Stable (modern AST)
|
|
93
|
+
- **PG14-16**: Legacy (modern AST, not actively developed)
|
|
94
|
+
- **PG13**: Legacy (legacy AST format)
|
|
93
95
|
|
|
94
96
|
## Options
|
|
95
97
|
|
|
@@ -131,7 +133,7 @@ const sql2 = deparse(selectAst, { pretty: true });
|
|
|
131
133
|
// active = true;
|
|
132
134
|
```
|
|
133
135
|
|
|
134
|
-
For complete documentation and advanced options, see [DEPARSER_USAGE.md](
|
|
136
|
+
For complete documentation and advanced options, see [DEPARSER_USAGE.md](https://github.com/constructive-io/pgsql-parser/tree/main/packages/deparser/DEPARSER_USAGE.md).
|
|
135
137
|
|
|
136
138
|
## Why Use `pgsql-deparser`?
|
|
137
139
|
|
|
@@ -147,8 +149,13 @@ Built on the excellent work of several contributors:
|
|
|
147
149
|
* **[Ethan Resnick](https://github.com/ethanresnick)** for the original Node.js N-API bindings
|
|
148
150
|
* **[Zac McCormick](https://github.com/zhm)** for the foundational [node-pg-query-native](https://github.com/zhm/node-pg-query-native) parser
|
|
149
151
|
|
|
152
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
153
|
+
|
|
150
154
|
## Related
|
|
151
155
|
|
|
156
|
+
* [pgpm](https://pgpm.dev): A Postgres Package Manager that brings modular development to PostgreSQL with reusable packages, deterministic migrations, recursive dependency resolution, and tag-aware versioning.
|
|
157
|
+
* [pgsql-test](https://www.npmjs.com/package/pgsql-test): Instant, isolated PostgreSQL databases for each test with automatic transaction rollbacks, context switching, and clean seeding for fast, reliable database testing.
|
|
158
|
+
* [pgsql-seed](https://www.npmjs.com/package/pgsql-seed): PostgreSQL seeding utilities for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
152
159
|
* [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.
|
|
153
160
|
* [pgsql-deparser](https://www.npmjs.com/package/pgsql-deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
|
|
154
161
|
* [@pgsql/parser](https://www.npmjs.com/package/@pgsql/parser): Multi-version PostgreSQL parser with dynamic version selection at runtime, supporting PostgreSQL 15, 16, and 17 in a single package.
|
|
@@ -157,10 +164,10 @@ Built on the excellent work of several contributors:
|
|
|
157
164
|
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.
|
|
158
165
|
* [@pgsql/traverse](https://www.npmjs.com/package/@pgsql/traverse): PostgreSQL AST traversal utilities for pgsql-parser, providing a visitor pattern for traversing PostgreSQL Abstract Syntax Tree nodes, similar to Babel's traverse functionality but specifically designed for PostgreSQL AST structures.
|
|
159
166
|
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
160
|
-
* [libpg-query](https://github.com/
|
|
167
|
+
* [libpg-query](https://github.com/constructive-io/libpg-query-node): The real PostgreSQL parser exposed for Node.js, used primarily in `pgsql-parser` for parsing and deparsing SQL queries.
|
|
161
168
|
|
|
162
169
|
## Disclaimer
|
|
163
170
|
|
|
164
171
|
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
165
172
|
|
|
166
|
-
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.
|
|
173
|
+
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.
|
package/deparser/deparser.d.ts
CHANGED
|
@@ -10,31 +10,6 @@ export interface DeparserOptions {
|
|
|
10
10
|
functionDelimiterFallback?: string;
|
|
11
11
|
pretty?: boolean;
|
|
12
12
|
}
|
|
13
|
-
/**
|
|
14
|
-
* Deparser - Converts PostgreSQL AST nodes back to SQL strings
|
|
15
|
-
*
|
|
16
|
-
* Entry Points:
|
|
17
|
-
* 1. ParseResult (from libpg-query) - The complete parse result
|
|
18
|
-
* Structure: { version: number, stmts: RawStmt[] }
|
|
19
|
-
* Note: stmts is "repeated RawStmt" in protobuf, so array contains RawStmt
|
|
20
|
-
* objects inline (not wrapped as { RawStmt: ... } nodes)
|
|
21
|
-
* Example: { version: 170004, stmts: [{ stmt: {...}, stmt_len: 32 }] }
|
|
22
|
-
*
|
|
23
|
-
* 2. Wrapped ParseResult - When explicitly wrapped as a Node
|
|
24
|
-
* Structure: { ParseResult: { version: number, stmts: RawStmt[] } }
|
|
25
|
-
*
|
|
26
|
-
* 3. Wrapped RawStmt - When explicitly wrapped as a Node
|
|
27
|
-
* Structure: { RawStmt: { stmt: Node, stmt_len?: number } }
|
|
28
|
-
*
|
|
29
|
-
* 4. Array of Nodes - Multiple statements to deparse
|
|
30
|
-
* Can be: Node[] (e.g., SelectStmt, InsertStmt, etc.)
|
|
31
|
-
*
|
|
32
|
-
* 5. Single Node - Individual statement node
|
|
33
|
-
* Example: { SelectStmt: {...} }, { InsertStmt: {...} }, etc.
|
|
34
|
-
*
|
|
35
|
-
* The deparser automatically detects bare ParseResult objects for backward
|
|
36
|
-
* compatibility and wraps them internally for consistent processing.
|
|
37
|
-
*/
|
|
38
13
|
export declare class Deparser implements DeparserVisitor {
|
|
39
14
|
private tree;
|
|
40
15
|
private options;
|
|
@@ -58,6 +33,11 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
58
33
|
* @returns The delimiter to use
|
|
59
34
|
*/
|
|
60
35
|
private getFunctionDelimiter;
|
|
36
|
+
/**
|
|
37
|
+
* Maps ObjectType enum values to their corresponding SQL keywords
|
|
38
|
+
* Used by AlterOwnerStmt, AlterObjectSchemaStmt, and other statements that need object type keywords
|
|
39
|
+
*/
|
|
40
|
+
private getObjectTypeKeyword;
|
|
61
41
|
deparse(node: any, context?: DeparserContext): string | null;
|
|
62
42
|
visit(node: any, context?: DeparserContext): string;
|
|
63
43
|
getNodeType(node: any): string;
|
|
@@ -74,12 +54,18 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
74
54
|
visitBetweenRange(rexpr: any, context: DeparserContext): string;
|
|
75
55
|
InsertStmt(node: any, context: DeparserContext): string;
|
|
76
56
|
UpdateStmt(node: any, context: DeparserContext): string;
|
|
57
|
+
formatUpdateAssignments(targets: any[], context: DeparserContext): string;
|
|
77
58
|
DeleteStmt(node: any, context: DeparserContext): string;
|
|
78
59
|
WithClause(node: any, context: DeparserContext): string;
|
|
79
60
|
ResTarget(node: any, context: DeparserContext): string;
|
|
61
|
+
ATAlterConstraint(node: any, context: DeparserContext): string;
|
|
62
|
+
deparseKeyAttrs(attrs: any, withPeriod: boolean | undefined, context: DeparserContext): string;
|
|
63
|
+
ReturningClause(node: any, context: DeparserContext): string;
|
|
64
|
+
ReturningOption(node: any, context: DeparserContext): string;
|
|
80
65
|
deparseReturningList(list: any, context: DeparserContext): string;
|
|
81
66
|
BoolExpr(node: any, context: DeparserContext): string;
|
|
82
67
|
FuncCall(node: any, context: DeparserContext): string;
|
|
68
|
+
formatOverClause(over: any, context: DeparserContext): string;
|
|
83
69
|
FuncExpr(node: any, context: DeparserContext): string;
|
|
84
70
|
A_Const(node: any, context: DeparserContext): string;
|
|
85
71
|
ColumnRef(node: any, context: DeparserContext): string;
|
|
@@ -101,8 +87,6 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
101
87
|
CollateClause(node: any, context: DeparserContext): string;
|
|
102
88
|
BooleanTest(node: any, context: DeparserContext): string;
|
|
103
89
|
NullTest(node: any, context: DeparserContext): string;
|
|
104
|
-
private static readonly RESERVED_WORDS;
|
|
105
|
-
private static needsQuotes;
|
|
106
90
|
quoteIfNeeded(value: string): string;
|
|
107
91
|
preserveOperatorDefElemCase(defName: string): string;
|
|
108
92
|
String(node: any, context: DeparserContext): string;
|
|
@@ -118,10 +102,13 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
118
102
|
SubLink(node: any, context: DeparserContext): string;
|
|
119
103
|
CaseWhen(node: any, context: DeparserContext): string;
|
|
120
104
|
WindowDef(node: any, context: DeparserContext): string;
|
|
105
|
+
formatIdentityColumnOptions(def: any, context: DeparserContext): string;
|
|
106
|
+
needsBExprParens(node: any): boolean;
|
|
121
107
|
formatWindowFrame(node: any, context: DeparserContext): string | null;
|
|
122
108
|
SortBy(node: any, context: DeparserContext): string;
|
|
123
109
|
GroupingSet(node: any, context: DeparserContext): string;
|
|
124
110
|
CommonTableExpr(node: any, context: DeparserContext): string;
|
|
111
|
+
formatAexprConst(node: any, context: DeparserContext): string;
|
|
125
112
|
ParamRef(node: any, context: DeparserContext): string;
|
|
126
113
|
LockingClause(node: any, context: DeparserContext): string;
|
|
127
114
|
MinMaxExpr(node: any, context: DeparserContext): string;
|
|
@@ -172,6 +159,7 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
172
159
|
AlterTableSpaceOptionsStmt(node: any, context: DeparserContext): string;
|
|
173
160
|
CreateExtensionStmt(node: any, context: DeparserContext): string;
|
|
174
161
|
AlterExtensionStmt(node: any, context: DeparserContext): string;
|
|
162
|
+
AlterExtensionContentsStmt(node: any, context: DeparserContext): string;
|
|
175
163
|
CreateFdwStmt(node: any, context: DeparserContext): string;
|
|
176
164
|
SetOperationStmt(node: any, context: DeparserContext): string;
|
|
177
165
|
ReplicaIdentityStmt(node: any, context: DeparserContext): string;
|
|
@@ -186,6 +174,8 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
186
174
|
CheckPointStmt(node: any, context: DeparserContext): string;
|
|
187
175
|
LoadStmt(node: any, context: DeparserContext): string;
|
|
188
176
|
DiscardStmt(node: any, context: DeparserContext): string;
|
|
177
|
+
/** `schema.op` for an operator name list: schema parts quoted, the operator symbol as is. */
|
|
178
|
+
private qualifiedOperatorName;
|
|
189
179
|
CommentStmt(node: any, context: DeparserContext): string;
|
|
190
180
|
LockStmt(node: any, context: DeparserContext): string;
|
|
191
181
|
CreatePolicyStmt(node: any, context: DeparserContext): string;
|
|
@@ -245,6 +235,8 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
245
235
|
CreateOpFamilyStmt(node: any, context: DeparserContext): string;
|
|
246
236
|
AlterOpFamilyStmt(node: any, context: DeparserContext): string;
|
|
247
237
|
MergeStmt(node: any, context: DeparserContext): string;
|
|
238
|
+
MergeSupportFunc(node: any, context: DeparserContext): string;
|
|
239
|
+
MergeWhenClause(node: any, context: DeparserContext): string;
|
|
248
240
|
AlterTableMoveAllStmt(node: any, context: DeparserContext): string;
|
|
249
241
|
CreateSeqStmt(node: any, context: DeparserContext): string;
|
|
250
242
|
AlterSeqStmt(node: any, context: DeparserContext): string;
|
|
@@ -272,11 +264,29 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
272
264
|
OnConflictExpr(node: any, context: DeparserContext): string;
|
|
273
265
|
ScanToken(node: any, context: DeparserContext): string;
|
|
274
266
|
CreateOpClassItem(node: any, context: DeparserContext): string;
|
|
267
|
+
private quoteDefElemArg;
|
|
268
|
+
private formatOptionValue;
|
|
275
269
|
Var(node: any, context: DeparserContext): string;
|
|
276
270
|
TableFunc(node: any, context: DeparserContext): string;
|
|
277
271
|
RangeTableFunc(node: any, context: DeparserContext): string;
|
|
272
|
+
private formatXmlTableExpr;
|
|
278
273
|
RangeTableFuncCol(node: any, context: DeparserContext): string;
|
|
274
|
+
formatJsonFormat(format: any): string | null;
|
|
275
|
+
formatJsonOutput(output: any, context: DeparserContext): string | null;
|
|
276
|
+
formatJsonNullClause(absentOnNull: boolean | undefined): string;
|
|
277
|
+
formatJsonUniqueClause(unique: boolean | undefined): string | null;
|
|
278
|
+
JsonValueExpr(node: any, context: DeparserContext): string;
|
|
279
|
+
JsonKeyValue(node: any, context: DeparserContext): string;
|
|
280
|
+
JsonObjectConstructor(node: any, context: DeparserContext): string;
|
|
281
|
+
JsonArrayConstructor(node: any, context: DeparserContext): string;
|
|
279
282
|
JsonArrayQueryConstructor(node: any, context: DeparserContext): string;
|
|
283
|
+
formatJsonAggSuffix(ctor: any, context: DeparserContext): string;
|
|
284
|
+
JsonObjectAgg(node: any, context: DeparserContext): string;
|
|
285
|
+
JsonArrayAgg(node: any, context: DeparserContext): string;
|
|
286
|
+
JsonParseExpr(node: any, context: DeparserContext): string;
|
|
287
|
+
JsonScalarExpr(node: any, context: DeparserContext): string;
|
|
288
|
+
JsonSerializeExpr(node: any, context: DeparserContext): string;
|
|
289
|
+
JsonIsPredicate(node: any, context: DeparserContext): string;
|
|
280
290
|
RangeFunction(node: any, context: DeparserContext): string;
|
|
281
291
|
XmlExpr(node: any, context: DeparserContext): string;
|
|
282
292
|
schemaname(node: any, context: DeparserContext): string;
|
|
@@ -298,5 +308,4 @@ export declare class Deparser implements DeparserVisitor {
|
|
|
298
308
|
AlterObjectSchemaStmt(node: any, context: DeparserContext): string;
|
|
299
309
|
AlterRoleSetStmt(node: any, context: DeparserContext): string;
|
|
300
310
|
CreateForeignTableStmt(node: any, context: DeparserContext): string;
|
|
301
|
-
private containsMultilineStringLiteral;
|
|
302
311
|
}
|