@pgsql/utils 13.10.0 → 13.12.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
@@ -19,10 +19,11 @@
19
19
  # Table of Contents
20
20
 
21
21
  1. [@pgsql/utils](#pgsql-utils)
22
- - [Key Features](#key-features)
22
+ - [Features](#features)
23
23
  2. [Installation](#installation)
24
24
  3. [Usage](#usage)
25
25
  - [AST Node Creation](#ast-node-creation)
26
+ - [JSON AST](#json-ast)
26
27
  - [Select Statement](#select-statement)
27
28
  - [Creating Table Schemas Dynamically](#creating-table-schemas-dynamically)
28
29
  - [Enum Value Conversion](#enum-value-conversion)
@@ -53,6 +54,34 @@ npm install @pgsql/utils
53
54
 
54
55
  With the AST helper methods, creating complex SQL ASTs becomes straightforward and intuitive.
55
56
 
57
+ #### JSON AST
58
+
59
+ Explore the PostgreSQL Abstract Syntax Tree (AST) as JSON objects with ease using `@pgsql/utils`. Below is an example of how you can generate a JSON AST using TypeScript:
60
+
61
+ ```ts
62
+ import ast from '@pgsql/utils';
63
+ const selectStmt = ast.selectStmt({
64
+ targetList: [
65
+ ast.resTarget({
66
+ val: ast.columnRef({
67
+ fields: [ast.aStar()]
68
+ })
69
+ })
70
+ ],
71
+ fromClause: [
72
+ ast.rangeVar({
73
+ relname: 'some_amazing_table',
74
+ inh: true,
75
+ relpersistence: 'p'
76
+ })
77
+ ],
78
+ limitOption: 'LIMIT_OPTION_DEFAULT',
79
+ op: 'SETOP_NONE'
80
+ });
81
+ console.log(selectStmt);
82
+ // Output: { "SelectStmt": { "targetList": [ { "ResTarget": { "val": { "ColumnRef": { "fields": [ { "A_Star": {} } ] } } } } ], "fromClause": [ { "RangeVar": { "relname": "some_amazing_table", "inh": true, "relpersistence": "p" } } ], "limitOption": "LIMIT_OPTION_DEFAULT", "op": "SETOP_NONE" } }
83
+ ```
84
+
56
85
  #### Select Statement
57
86
 
58
87
  ```ts
@@ -105,7 +134,7 @@ deparse(createStmt, {});
105
134
  #### Creating Table Schemas Dynamically
106
135
 
107
136
  ```ts
108
- // Example JSON with schema
137
+ // Example JSON schema
109
138
  const schema = {
110
139
  "tableName": "users",
111
140
  "columns": [
@@ -118,7 +147,11 @@ const schema = {
118
147
 
119
148
  // Construct the CREATE TABLE statement
120
149
  const createStmt = ast.createStmt({
121
- relation: ast.rangeVar({ relname: schema.tableName }),
150
+ relation: ast.rangeVar({
151
+ relname: schema.tableName,
152
+ inh: true,
153
+ relpersistence: 'p'
154
+ }).RangeVar as RangeVar, // special case due to PG AST
122
155
  tableElts: schema.columns.map(column => ast.columnDef({
123
156
  colname: column.name,
124
157
  typeName: ast.typeName({
@@ -126,23 +159,24 @@ const createStmt = ast.createStmt({
126
159
  }),
127
160
  constraints: column.constraints?.map(constraint =>
128
161
  ast.constraint({
129
- contype: constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL",
130
- keys: [ast.string({ str: column.name })]
162
+ contype: constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL"
131
163
  })
132
164
  )
133
165
  }))
134
166
  });
135
167
 
136
- // Assuming `deparse` function converts AST to SQL string
168
+ // `deparse` function converts AST to SQL string
137
169
  const sql = deparse(createStmt, {});
170
+
138
171
  console.log(sql);
139
- // CREATE TABLE (
140
- // id int PRIMARY KEY ( id ),
141
- // username string,
142
- // email string UNIQUE ( email ),
143
- // created_at timestamp NOT NULL ( created_at )
144
- // )
172
+ // OUTPUT:
145
173
 
174
+ // CREATE TABLE users (
175
+ // id int PRIMARY KEY,
176
+ // username text,
177
+ // email text UNIQUE,
178
+ // created_at timestamp NOT NULL
179
+ // )
146
180
  ```
147
181
 
148
182
  ### Enum Value Conversion
@@ -179,7 +213,7 @@ console.log(enumName); // Outputs 'SORTBY_ASC' if 1 corresponds to 'SORTBY_ASC'
179
213
 
180
214
  * [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.
181
215
  * [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`.
182
- * [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums-json): 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`
216
+ * [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`
183
217
  * [@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.
184
218
  * [@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.
185
219
  * [@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.
package/main/asts.js CHANGED
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  /**
7
- * This file was automatically generated by pg-proto-parser@1.20.0.
7
+ * This file was automatically generated by pg-proto-parser@1.23.0.
8
8
  * DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
9
9
  * and run the pg-proto-parser generate command to regenerate this file.
10
10
  */
package/module/asts.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file was automatically generated by pg-proto-parser@1.20.0.
2
+ * This file was automatically generated by pg-proto-parser@1.23.0.
3
3
  * DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
4
4
  * and run the pg-proto-parser generate command to regenerate this file.
5
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgsql/utils",
3
- "version": "13.10.0",
3
+ "version": "13.12.0",
4
4
  "description": "PostgreSQL AST utils for pgsql-parser",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/launchql/pgsql-parser",
@@ -66,7 +66,7 @@
66
66
  "esprima": "4.0.1",
67
67
  "jest": "^29.5.0",
68
68
  "pg-proto-parser": "^1.20.0",
69
- "pgsql-deparser": "^13.14.0",
69
+ "pgsql-deparser": "^13.16.0",
70
70
  "prettier": "^2.8.7",
71
71
  "rimraf": "5.0.5",
72
72
  "ts-jest": "^29.1.0",
@@ -74,8 +74,8 @@
74
74
  "typescript": "^5.0.4"
75
75
  },
76
76
  "dependencies": {
77
- "@pgsql/types": "^13.8.0",
77
+ "@pgsql/types": "^13.10.0",
78
78
  "nested-obj": "0.0.1"
79
79
  },
80
- "gitHead": "1358b34422c45c2111dc0b1764155a720ab7fd73"
80
+ "gitHead": "85346892e652a57e570562fdac7a768545113619"
81
81
  }
package/src/asts.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file was automatically generated by pg-proto-parser@1.20.0.
2
+ * This file was automatically generated by pg-proto-parser@1.23.0.
3
3
  * DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
4
4
  * and run the pg-proto-parser generate command to regenerate this file.
5
5
  */
package/src/wrapped.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file was automatically generated by pg-proto-parser@1.20.0.
2
+ * This file was automatically generated by pg-proto-parser@1.23.0.
3
3
  * DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
4
4
  * and run the pg-proto-parser generate command to regenerate this file.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This file was automatically generated by pg-proto-parser@1.20.0.
2
+ * This file was automatically generated by pg-proto-parser@1.23.0.
3
3
  * DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
4
4
  * and run the pg-proto-parser generate command to regenerate this file.
5
5
  */