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 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 a streamlined tool designed to convert PostgreSQL Abstract Syntax Trees (AST) back into SQL queries. It is a companion module to [`pgsql-parser`](https://github.com/launchql/pgsql-parser), which is capable of both parsing SQL queries into ASTs and deparsing these ASTs back into SQL. However, unlike `pgsql-parser`, which incorporates the full PostgreSQL parser, `pgsql-deparser` focuses solely on the deparser functionality. This makes it an excellent choice for scenarios where only AST-to-SQL conversion is needed, avoiding the extra overhead associated with the full parsing capabilities.
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
- ## Key Features
25
+ ## Features
26
26
 
27
- - **Lightweight and Pure TypeScript**: Built entirely in TypeScript, `pgsql-deparser` is lightweight and doesn't rely on native dependencies, facilitating easier integration and deployment across various environments.
28
- - **Focused Functionality**: By concentrating on decompiling ASTs to SQL, `pgsql-deparser` offers a specialized, efficient solution for those who need to generate SQL statements from ASTs without the full parsing mechanism.
29
- - **Compatibility**: Ideal for use cases where the AST is already obtained from another source or process, allowing for seamless SQL generation from AST representations.
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 ast, { SelectStmt } from '@pgsql/utils';
39
- import { deparse } from 'pgsql-deparser';
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 = ast.selectStmt({
44
+ const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
43
45
  targetList: [
44
- ast.resTarget({
45
- val: ast.columnRef({
46
- fields: [ast.aStar()]
46
+ t.nodes.resTarget({
47
+ val: t.nodes.columnRef({
48
+ fields: [t.nodes.aStar()]
47
49
  })
48
50
  })
49
51
  ],
50
52
  fromClause: [
51
- ast.rangeVar({
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(stmts));
67
+ console.log(deparse(stmt));
66
68
 
67
69
  // Output: SELECT * FROM another_table
68
70
  ```
69
71
 
70
- ## Why Use `pgsql-deparser`?
72
+ ### Latest Version (PostgreSQL 17)
71
73
 
72
- `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.
74
+ ```sh
75
+ npm install pgsql-deparser
76
+ ```
77
+
78
+ ### Version-Specific Packages (PostgreSQL 13-16)
73
79
 
74
- ## Versions
80
+ While we highly recommend using PG17, for PostgreSQL versions 13-16, use the version-specific packages:
75
81
 
76
- 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.
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
- Our latest is built with `13-latest` branch from libpg_query
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
- | PostgreSQL Major Version | libpg_query | Status | npm
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
- ## Related
98
+ ```ts
99
+ import { deparseSync as deparse } from 'pgsql-deparser';
89
100
 
90
- * [pgsql-parser](https://github.com/launchql/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
91
- * [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
92
- * [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/pgsql-enums): A utility package offering easy access to PostgreSQL enumeration types in JSON format, aiding in string and integer conversions of enums used within ASTs to compliment `pgsql-parser`
93
- * [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes.
94
- * [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
95
- * [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/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.
96
- * [pg-proto-parser](https://github.com/launchql/pg-proto-parser): A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
97
- * [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.
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
- Thanks [@lfittl](https://github.com/lfittl) for building the core `libpg_query` suite:
136
+ ## Why Use `pgsql-deparser`?
100
137
 
101
- * [libpg_query](https://github.com/pganalyze/libpg_query)
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
- Thanks to [@zhm](https://github.com/zhm) for the OG parser that started it all:
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
- * [pg-query-parser](https://github.com/zhm/pg-query-parser)
110
- * [pg-query-native](https://github.com/zhm/node-pg-query-native)
111
- * [Original LICENSE](https://github.com/zhm/pg-query-parser/blob/master/LICENSE.md)
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 AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
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": "SEE LICENSE IN LICENSE",
8
- "main": "main/index.js",
9
- "module": "module/index.js",
10
- "typings": "types/index.d.ts",
11
- "directories": {
12
- "lib": "src",
13
- "test": "__tests__"
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
- "publishConfig": {
32
- "access": "public"
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.10.0",
86
- "dotty": "^0.1.0",
87
- "pgsql-enums": "^13.11.0"
88
- },
89
- "gitHead": "85346892e652a57e570562fdac7a768545113619"
90
- }
39
+ "@pgsql/types": "^13.11.1"
40
+ }
41
+ }