pgsql-deparser 13.16.0 → 13.18.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 +94 -45
- package/package.json +26 -75
- package/src/deparser/deparser.ts +10474 -0
- package/src/deparser/index.ts +14 -0
- package/src/deparser/utils/list-utils.ts +27 -0
- package/src/deparser/utils/quote-utils.ts +86 -0
- package/src/deparser/utils/sql-formatter.ts +37 -0
- package/src/deparser/visitors/base.ts +145 -0
- package/src/index.ts +27 -3
- package/src/v13-to-v14.ts +2887 -0
- package/src/v13-to-v17-direct.ts +79 -0
- package/src/v14-to-v15.ts +1624 -0
- package/src/v15-to-v16.ts +3355 -0
- package/src/v16-to-v17.ts +1897 -0
- package/tsconfig.esm.json +8 -0
- package/tsconfig.json +26 -0
- package/LICENSE +0 -21
- package/main/deparser.js +0 -3495
- package/main/index.js +0 -10
- package/main/utils/index.js +0 -97
- package/module/deparser.js +0 -3492
- package/module/index.js +0 -3
- package/module/utils/index.js +0 -90
- package/src/deparser.ts +0 -4234
- package/src/utils/index.ts +0 -92
- package/types/deparser.d.ts +0 -119
- package/types/index.d.ts +0 -3
- package/types/utils/index.d.ts +0 -4
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Fdeparser%2Fpackage.json"/></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
`pgsql-deparser` is
|
|
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/launchql/pgsql-parser), this focused tool delivers SQL generation without any native dependencies or WebAssembly overhead.
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -22,11 +22,12 @@
|
|
|
22
22
|
npm install pgsql-deparser
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
##
|
|
25
|
+
## Features
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
- **
|
|
27
|
+
* ⚡ **Pure TypeScript Performance** – Zero runtime dependencies, no WASM, no compilation - just blazing fast SQL generation
|
|
28
|
+
* 🪶 **Ultra Lightweight** – Minimal footprint with laser-focused functionality for AST-to-SQL conversion only
|
|
29
|
+
* 🧪 **Battle-Tested Reliability** – Validated against 23,000+ SQL statements ensuring production-grade stability
|
|
30
|
+
* 🌍 **Universal Compatibility** – Runs anywhere JavaScript does - browsers, Node.js, edge functions, you name it
|
|
30
31
|
|
|
31
32
|
## Deparser Example
|
|
32
33
|
|
|
@@ -35,20 +36,21 @@ The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding
|
|
|
35
36
|
Here's how you can use the deparser in your TypeScript code, using [`@pgsql/utils`](https://github.com/launchql/pgsql-parser/tree/main/packages/utils) to create an AST for `deparse`:
|
|
36
37
|
|
|
37
38
|
```ts
|
|
38
|
-
import
|
|
39
|
-
import {
|
|
39
|
+
import * as t from '@pgsql/utils';
|
|
40
|
+
import { RangeVar, SelectStmt } from '@pgsql/types';
|
|
41
|
+
import { deparseSync as deparse } from 'pgsql-deparser';
|
|
40
42
|
|
|
41
43
|
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
|
|
42
|
-
const stmt: SelectStmt =
|
|
44
|
+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
|
|
43
45
|
targetList: [
|
|
44
|
-
|
|
45
|
-
val:
|
|
46
|
-
fields: [
|
|
46
|
+
t.nodes.resTarget({
|
|
47
|
+
val: t.nodes.columnRef({
|
|
48
|
+
fields: [t.nodes.aStar()]
|
|
47
49
|
})
|
|
48
50
|
})
|
|
49
51
|
],
|
|
50
52
|
fromClause: [
|
|
51
|
-
|
|
53
|
+
t.nodes.rangeVar({
|
|
52
54
|
relname: 'some_table',
|
|
53
55
|
inh: true,
|
|
54
56
|
relpersistence: 'p'
|
|
@@ -58,60 +60,107 @@ const stmt: SelectStmt = ast.selectStmt({
|
|
|
58
60
|
op: 'SETOP_NONE'
|
|
59
61
|
});
|
|
60
62
|
|
|
61
|
-
// Modify the AST if needed
|
|
62
|
-
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
|
|
63
|
+
// Modify the AST if needed
|
|
64
|
+
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';
|
|
63
65
|
|
|
64
66
|
// Deparse the modified AST back to a SQL string
|
|
65
|
-
console.log(deparse(
|
|
67
|
+
console.log(deparse(stmt));
|
|
66
68
|
|
|
67
69
|
// Output: SELECT * FROM another_table
|
|
68
70
|
```
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
### Latest Version (PostgreSQL 17)
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
```sh
|
|
75
|
+
npm install pgsql-deparser
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Version-Specific Packages (PostgreSQL 13-16)
|
|
73
79
|
|
|
74
|
-
|
|
80
|
+
While we highly recommend using PG17, for PostgreSQL versions 13-16, use the version-specific packages:
|
|
75
81
|
|
|
76
|
-
|
|
82
|
+
```sh
|
|
83
|
+
npm install pgsql-deparser@pg13 # PostgreSQL 13
|
|
84
|
+
npm install pgsql-deparser@pg14 # PostgreSQL 14
|
|
85
|
+
npm install pgsql-deparser@pg15 # PostgreSQL 15
|
|
86
|
+
npm install pgsql-deparser@pg16 # PostgreSQL 16
|
|
87
|
+
```
|
|
77
88
|
|
|
78
|
-
|
|
89
|
+
**Version Status:**
|
|
90
|
+
- **PG17**: 🚀 Recommended (stable + modern AST)
|
|
91
|
+
- **PG14-16**: ⚠️ Experimental (modern AST, hardening in progress)
|
|
92
|
+
- **PG13**: Stable (legacy AST format)
|
|
79
93
|
|
|
80
|
-
|
|
81
|
-
|--------------------------|-------------|---------------------|---------|
|
|
82
|
-
| 13 | 13-latest | Active development | `latest`
|
|
83
|
-
| 12 | (n/a) | Not supported |
|
|
84
|
-
| 11 | (n/a) | Not supported |
|
|
85
|
-
| 10 | 10-latest | Not supported | `@1.3.1` ([tree](https://github.com/launchql/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |
|
|
94
|
+
## Options
|
|
86
95
|
|
|
96
|
+
The deparser accepts optional configuration for formatting and output control:
|
|
87
97
|
|
|
88
|
-
|
|
98
|
+
```ts
|
|
99
|
+
import { deparseSync as deparse } from 'pgsql-deparser';
|
|
89
100
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
101
|
+
const options = {
|
|
102
|
+
pretty: true, // Enable pretty formatting (default: true)
|
|
103
|
+
newline: '\n', // Newline character (default: '\n')
|
|
104
|
+
tab: ' ', // Tab/indentation character (default: ' ')
|
|
105
|
+
semicolons: true // Add semicolons to statements (default: true)
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const sql = deparse(ast, options);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
| Option | Type | Default | Description |
|
|
112
|
+
|--------|------|---------|-------------|
|
|
113
|
+
| `pretty` | `boolean` | `true` | Enable pretty formatting with indentation and line breaks |
|
|
114
|
+
| `newline` | `string` | `'\n'` | Character(s) used for line breaks |
|
|
115
|
+
| `tab` | `string` | `' '` | Character(s) used for indentation |
|
|
116
|
+
| `semicolons` | `boolean` | `true` | Add semicolons to SQL statements |
|
|
117
|
+
|
|
118
|
+
**Pretty formatting example:**
|
|
119
|
+
```ts
|
|
120
|
+
// Without pretty formatting
|
|
121
|
+
const sql1 = deparse(selectAst, { pretty: false });
|
|
122
|
+
// "SELECT id, name FROM users WHERE active = true;"
|
|
123
|
+
|
|
124
|
+
// With pretty formatting
|
|
125
|
+
const sql2 = deparse(selectAst, { pretty: true });
|
|
126
|
+
// SELECT
|
|
127
|
+
// id,
|
|
128
|
+
// name
|
|
129
|
+
// FROM users
|
|
130
|
+
// WHERE
|
|
131
|
+
// active = true;
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
For complete documentation and advanced options, see [DEPARSER_USAGE.md](../../DEPARSER_USAGE.md).
|
|
98
135
|
|
|
99
|
-
|
|
136
|
+
## Why Use `pgsql-deparser`?
|
|
100
137
|
|
|
101
|
-
|
|
102
|
-
* [pg_query](https://github.com/lfittl/pg_query)
|
|
103
|
-
* [pg_query.go](https://github.com/lfittl/pg_query.go)
|
|
138
|
+
`pgsql-deparser` is particularly useful in development environments where native dependencies are problematic or in applications where only the deparser functionality is required. Its independence from the full `pgsql-parser` package allows for more focused and lightweight SQL generation tasks.
|
|
104
139
|
|
|
105
140
|
## Credits
|
|
106
141
|
|
|
107
|
-
|
|
142
|
+
Built on the excellent work of several contributors:
|
|
143
|
+
|
|
144
|
+
* **[Dan Lynch](https://github.com/pyramation)** — official maintainer since 2018 and architect of the current implementation
|
|
145
|
+
* **[Lukas Fittl](https://github.com/lfittl)** for [libpg_query](https://github.com/pganalyze/libpg_query) — the core PostgreSQL parser that powers this project
|
|
146
|
+
* **[Greg Richardson](https://github.com/gregnr)** for AST guidance and pushing the transition to WASM and multiple PG runtimes for better interoperability
|
|
147
|
+
* **[Ethan Resnick](https://github.com/ethanresnick)** for the original Node.js N-API bindings
|
|
148
|
+
* **[Zac McCormick](https://github.com/zhm)** for the foundational [node-pg-query-native](https://github.com/zhm/node-pg-query-native) parser
|
|
108
149
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
* [
|
|
150
|
+
## Related
|
|
151
|
+
|
|
152
|
+
* [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
|
+
* [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
|
+
* [@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.
|
|
155
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
|
|
156
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): Provides TypeScript enum definitions for PostgreSQL constants, enabling type-safe usage of PostgreSQL enums and constants in your applications.
|
|
157
|
+
* [@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
|
+
* [@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
|
+
* [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/launchql/libpg-query-node): The real PostgreSQL parser exposed for Node.js, used primarily in `pgsql-parser` for parsing and deparsing SQL queries.
|
|
112
161
|
|
|
113
162
|
## Disclaimer
|
|
114
163
|
|
|
115
|
-
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED
|
|
164
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
116
165
|
|
|
117
|
-
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.
|
|
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.
|
package/package.json
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "pgsql-deparser",
|
|
3
|
-
"version": "13.16.0",
|
|
4
|
-
"description": "PostgreSQL AST Deparser",
|
|
5
2
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
3
|
"homepage": "https://github.com/launchql/pgsql-parser",
|
|
7
|
-
"license": "
|
|
8
|
-
"main": "
|
|
9
|
-
"module": "
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "esm/index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"description": "PostgreSQL AST Deparser",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"copy": "copyfiles -f ../../../../LICENSE README.md package.json dist",
|
|
11
|
+
"clean": "rimraf dist",
|
|
12
|
+
"build": "npm run clean && tsc && tsc -p tsconfig.esm.json && npm run copy",
|
|
13
|
+
"publish:pkg": "npm publish --tag pg13"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public",
|
|
17
|
+
"directory": "dist"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/launchql/pgsql-parser"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/launchql/pgsql-parser/issues"
|
|
14
25
|
},
|
|
15
|
-
"files": [
|
|
16
|
-
"types",
|
|
17
|
-
"module",
|
|
18
|
-
"src",
|
|
19
|
-
"main"
|
|
20
|
-
],
|
|
21
26
|
"keywords": [
|
|
22
27
|
"sql",
|
|
23
28
|
"postgres",
|
|
@@ -28,63 +33,9 @@
|
|
|
28
33
|
"deparser",
|
|
29
34
|
"database"
|
|
30
35
|
],
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
},
|
|
34
|
-
"repository": {
|
|
35
|
-
"type": "git",
|
|
36
|
-
"url": "https://github.com/launchql/pgsql-parser"
|
|
37
|
-
},
|
|
38
|
-
"scripts": {
|
|
39
|
-
"build:main": "yarn tsc -p tsconfig.json --outDir main --module commonjs",
|
|
40
|
-
"build:module": "yarn tsc -p tsconfig.json --outDir module --module es2022",
|
|
41
|
-
"build": "npm run build:module && npm run build:main",
|
|
42
|
-
"buidl": "npm run build",
|
|
43
|
-
"buidl:clean": "npm run clean && npm run buidl",
|
|
44
|
-
"prepare": "npm run build",
|
|
45
|
-
"lint": "eslint .",
|
|
46
|
-
"format": "eslint . --fix",
|
|
47
|
-
"test": "jest",
|
|
48
|
-
"test:watch": "jest --watch",
|
|
49
|
-
"test:debug": "node --inspect node_modules/.bin/jest --runInBand"
|
|
50
|
-
},
|
|
51
|
-
"bugs": {
|
|
52
|
-
"url": "https://github.com/launchql/pgsql-parser/issues"
|
|
53
|
-
},
|
|
54
|
-
"jest": {
|
|
55
|
-
"preset": "ts-jest",
|
|
56
|
-
"testEnvironment": "node",
|
|
57
|
-
"transform": {
|
|
58
|
-
"^.+\\.ts?$": "ts-jest"
|
|
59
|
-
},
|
|
60
|
-
"transformIgnorePatterns": [
|
|
61
|
-
"<rootDir>/node_modules/"
|
|
62
|
-
],
|
|
63
|
-
"testPathIgnorePatterns": [
|
|
64
|
-
"main/",
|
|
65
|
-
"module/",
|
|
66
|
-
"types/"
|
|
67
|
-
]
|
|
68
|
-
},
|
|
69
|
-
"devDependencies": {
|
|
70
|
-
"@types/jest": "^29.5.0",
|
|
71
|
-
"eslint": "8.38.0",
|
|
72
|
-
"eslint-config-prettier": "^8.8.0",
|
|
73
|
-
"eslint-plugin-prettier": "^4.0.0",
|
|
74
|
-
"esprima": "4.0.1",
|
|
75
|
-
"glob": "8.0.3",
|
|
76
|
-
"jest": "^29.7.0",
|
|
77
|
-
"pgsql-parser": "^13.17.0",
|
|
78
|
-
"prettier": "^2.8.7",
|
|
79
|
-
"rimraf": "5.0.5",
|
|
80
|
-
"ts-jest": "^29.1.0",
|
|
81
|
-
"ts-node": "10.9.2",
|
|
82
|
-
"typescript": "^5.0.4"
|
|
83
|
-
},
|
|
36
|
+
"name": "pgsql-deparser",
|
|
37
|
+
"version": "13.18.0",
|
|
84
38
|
"dependencies": {
|
|
85
|
-
"@pgsql/types": "^13.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
},
|
|
89
|
-
"gitHead": "85346892e652a57e570562fdac7a768545113619"
|
|
90
|
-
}
|
|
39
|
+
"@pgsql/types": "^13.11.1"
|
|
40
|
+
}
|
|
41
|
+
}
|