pgsql-parser 17.5.1 โ 17.6.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 +34 -71
- package/esm/index.js +1 -1
- package/index.d.ts +1 -1
- package/index.js +1 -4
- package/package.json +5 -8
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
<a href="https://www.npmjs.com/package/pgsql-parser"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Fparser%2Fpackage.json"/></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
The real PostgreSQL parser for Node.js
|
|
17
|
+
The real PostgreSQL parser for Node.js. Built with the actual [PostgreSQL parser](https://github.com/pganalyze/libpg_query), `pgsql-parser` delivers true-to-spec SQL parsing and reconstruction. Transform SQL queries into ASTs, modify them programmatically, and convert them back to SQL with complete fidelity.
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -22,38 +22,30 @@ The real PostgreSQL parser for Node.js, `pgsql-parser` provides symmetric parsin
|
|
|
22
22
|
npm install pgsql-parser
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
##
|
|
25
|
+
## Features
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
- **
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
* ๐ **Symmetric Parsing & Deparsing** โ Parse SQL to AST and reconstruct it back to SQL with perfect round-trip accuracy
|
|
28
|
+
* ๐งช **Battle-Tested Reliability** โ Validated against 23,000+ SQL statements ensuring production-grade stability
|
|
29
|
+
* ๐ง **Direct from PostgreSQL** โ Uses the official Postgres C parser compiled to WebAssembly for 100% spec compliance
|
|
30
|
+
* ๐ **WebAssembly Powered:** - Cross-platform compatibility without native dependencies.
|
|
31
|
+
* ๐ ๏ธ **AST Manipulation:** - Easily modify parts of a SQL statement through the AST.
|
|
31
32
|
|
|
32
33
|
## API
|
|
33
34
|
|
|
34
35
|
The package exports both async and sync methods. Async methods handle initialization automatically, while sync methods require explicit initialization.
|
|
35
36
|
|
|
36
|
-
โ ๏ธ
|
|
37
|
+
โ ๏ธ If you don't need the parser functionality, consider using the TS-only (no WASM, zero dependencies) [`pgsql-deparser`](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser) for a super fast, lightweight deparser. Battle-tested with 23,000+ SQL statements ๐
|
|
37
38
|
|
|
38
39
|
### Async Methods (Recommended)
|
|
39
40
|
|
|
40
41
|
```typescript
|
|
41
|
-
import { parse, deparse
|
|
42
|
+
import { parse, deparse } from 'pgsql-parser';
|
|
42
43
|
|
|
43
44
|
// Parse SQL to AST
|
|
44
45
|
const stmts = await parse('SELECT * FROM test_table');
|
|
45
46
|
|
|
46
47
|
// Deparse AST back to SQL
|
|
47
48
|
const sql = await deparse(stmts);
|
|
48
|
-
|
|
49
|
-
// Parse PL/pgSQL functions
|
|
50
|
-
const funcAst = await parseFunction(`
|
|
51
|
-
CREATE FUNCTION get_count() RETURNS integer AS $$
|
|
52
|
-
BEGIN
|
|
53
|
-
RETURN (SELECT COUNT(*) FROM users);
|
|
54
|
-
END;
|
|
55
|
-
$$ LANGUAGE plpgsql;
|
|
56
|
-
`);
|
|
57
49
|
```
|
|
58
50
|
|
|
59
51
|
### Sync Methods
|
|
@@ -97,20 +89,21 @@ The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding
|
|
|
97
89
|
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`:
|
|
98
90
|
|
|
99
91
|
```ts
|
|
100
|
-
import
|
|
101
|
-
import {
|
|
92
|
+
import * as t from '@pgsql/utils';
|
|
93
|
+
import { RangeVar, SelectStmt } from '@pgsql/types';
|
|
94
|
+
import { deparseSync as deparse } from 'pgsql-deparser';
|
|
102
95
|
|
|
103
96
|
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
|
|
104
|
-
const stmt =
|
|
97
|
+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
|
|
105
98
|
targetList: [
|
|
106
|
-
|
|
107
|
-
val:
|
|
108
|
-
fields: [
|
|
99
|
+
t.nodes.resTarget({
|
|
100
|
+
val: t.nodes.columnRef({
|
|
101
|
+
fields: [t.nodes.aStar()]
|
|
109
102
|
})
|
|
110
103
|
})
|
|
111
104
|
],
|
|
112
105
|
fromClause: [
|
|
113
|
-
|
|
106
|
+
t.nodes.rangeVar({
|
|
114
107
|
relname: 'some_table',
|
|
115
108
|
inh: true,
|
|
116
109
|
relpersistence: 'p'
|
|
@@ -120,66 +113,36 @@ const stmt = ast.selectStmt({
|
|
|
120
113
|
op: 'SETOP_NONE'
|
|
121
114
|
});
|
|
122
115
|
|
|
123
|
-
// Modify the AST if needed
|
|
124
|
-
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
|
|
116
|
+
// Modify the AST if needed
|
|
117
|
+
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';
|
|
125
118
|
|
|
126
119
|
// Deparse the modified AST back to a SQL string
|
|
127
|
-
console.log(deparse(
|
|
120
|
+
console.log(deparse(stmt));
|
|
128
121
|
|
|
129
122
|
// Output: SELECT * FROM another_table
|
|
130
123
|
```
|
|
131
124
|
|
|
132
|
-
##
|
|
133
|
-
|
|
134
|
-
```
|
|
135
|
-
npm install -g pgsql-parser
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### usage
|
|
139
|
-
|
|
140
|
-
```sh
|
|
141
|
-
pgsql-parser <sqlfile>
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
## Versions
|
|
145
|
-
|
|
146
|
-
As of PG 13, PG majors versions maintained will have a matching dedicated major npm version. Only the latest Postgres stable release receives active updates.
|
|
147
|
-
|
|
148
|
-
Our latest is built with `17-latest` branch from libpg_query
|
|
125
|
+
## Credits
|
|
149
126
|
|
|
150
|
-
|
|
151
|
-
|--------------------------|-------------|---------------------|---------|
|
|
152
|
-
| 17 | 17-latest | Active Development | `latest` |
|
|
153
|
-
| 16 | (n/a) | Not supported |
|
|
154
|
-
| 15 | (n/a) | Not supported |
|
|
155
|
-
| 14 | (n/a) | Not supported |
|
|
156
|
-
| 13 | 13-latest | Only Critical Fixes | `13.16.0` |
|
|
157
|
-
| 12 | (n/a) | Not supported |
|
|
158
|
-
| 11 | (n/a) | Not supported |
|
|
159
|
-
| 10 | 10-latest | Not supported | `@1.3.1` ([tree](https://github.com/launchql/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |
|
|
127
|
+
Built on the excellent work of several contributors:
|
|
160
128
|
|
|
129
|
+
* **[Dan Lynch](https://github.com/pyramation)** โ official maintainer since 2018 and architect of the current implementation
|
|
130
|
+
* **[Lukas Fittl](https://github.com/lfittl)** for [libpg_query](https://github.com/pganalyze/libpg_query) โ the core PostgreSQL parser that powers this project
|
|
131
|
+
* **[Greg Richardson](https://github.com/gregnr)** for AST guidance and pushing the transition to WASM and multiple PG runtimes for better interoperability
|
|
132
|
+
* **[Ethan Resnick](https://github.com/ethanresnick)** for the original Node.js N-API bindings
|
|
133
|
+
* **[Zac McCormick](https://github.com/zhm)** for the foundational [node-pg-query-native](https://github.com/zhm/node-pg-query-native) parser
|
|
161
134
|
|
|
162
135
|
## Related
|
|
163
136
|
|
|
164
|
-
* [pgsql-parser](https://
|
|
165
|
-
* [pgsql-deparser](https://
|
|
166
|
-
* [pgsql
|
|
167
|
-
* [@pgsql/
|
|
168
|
-
* [@pgsql/
|
|
169
|
-
* [@pgsql/utils](https://
|
|
170
|
-
* [pg-proto-parser](https://
|
|
137
|
+
* [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.
|
|
138
|
+
* [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`.
|
|
139
|
+
* [@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.
|
|
140
|
+
* [@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.
|
|
141
|
+
* [@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.
|
|
142
|
+
* [@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.
|
|
143
|
+
* [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.
|
|
171
144
|
* [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.
|
|
172
145
|
|
|
173
|
-
## Credits
|
|
174
|
-
|
|
175
|
-
Thanks [@lfittl](https://github.com/lfittl) for building the core `libpg_query` suite:
|
|
176
|
-
|
|
177
|
-
* [libpg_query](https://github.com/pganalyze/libpg_query)
|
|
178
|
-
* [pg_query](https://github.com/lfittl/pg_query)
|
|
179
|
-
* [pg_query.go](https://github.com/lfittl/pg_query.go)
|
|
180
|
-
|
|
181
|
-
Thanks to [@zhm](https://github.com/zhm) for the [OG Parser](https://github.com/zhm/pg-query-parser/blob/master/LICENSE.md) that started it all.
|
|
182
|
-
|
|
183
146
|
## Disclaimer
|
|
184
147
|
|
|
185
148
|
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
package/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { parse as parse, parseSync as parseSync,
|
|
1
|
+
export { parse as parse, parseSync as parseSync, loadModule as loadModule } from 'libpg-query';
|
package/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { parse as parse, parseSync as parseSync,
|
|
1
|
+
export { parse as parse, parseSync as parseSync, loadModule as loadModule } from 'libpg-query';
|
package/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadModule = exports.
|
|
3
|
+
exports.loadModule = exports.parseSync = exports.parse = void 0;
|
|
4
4
|
var libpg_query_1 = require("libpg-query");
|
|
5
5
|
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return libpg_query_1.parse; } });
|
|
6
6
|
Object.defineProperty(exports, "parseSync", { enumerable: true, get: function () { return libpg_query_1.parseSync; } });
|
|
7
|
-
Object.defineProperty(exports, "parseFunction", { enumerable: true, get: function () { return libpg_query_1.parsePlPgSQL; } });
|
|
8
|
-
Object.defineProperty(exports, "deparseSync", { enumerable: true, get: function () { return libpg_query_1.deparseSync; } });
|
|
9
|
-
Object.defineProperty(exports, "deparse", { enumerable: true, get: function () { return libpg_query_1.deparse; } });
|
|
10
7
|
Object.defineProperty(exports, "loadModule", { enumerable: true, get: function () { return libpg_query_1.loadModule; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pgsql-parser",
|
|
3
|
-
"version": "17.
|
|
3
|
+
"version": "17.6.0",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "The real PostgreSQL query parser",
|
|
6
6
|
"main": "index.js",
|
|
@@ -19,9 +19,6 @@
|
|
|
19
19
|
"bugs": {
|
|
20
20
|
"url": "https://github.com/launchql/pgsql-parser/issues"
|
|
21
21
|
},
|
|
22
|
-
"bin": {
|
|
23
|
-
"pgsql-parser": "main/cli.js"
|
|
24
|
-
},
|
|
25
22
|
"scripts": {
|
|
26
23
|
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
|
|
27
24
|
"clean": "rimraf dist",
|
|
@@ -42,9 +39,9 @@
|
|
|
42
39
|
"database"
|
|
43
40
|
],
|
|
44
41
|
"dependencies": {
|
|
45
|
-
"@pgsql/types": "^17.
|
|
46
|
-
"libpg-query": "17.
|
|
47
|
-
"
|
|
42
|
+
"@pgsql/types": "^17.6.1",
|
|
43
|
+
"libpg-query": "17.5.2",
|
|
44
|
+
"pgsql-deparser": "^17.7.0"
|
|
48
45
|
},
|
|
49
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "e0f9b0579328f4206c315d499e0e2045d3b8f845"
|
|
50
47
|
}
|