@pgsql/utils 13.10.0 → 13.10.1

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.
Files changed (2) hide show
  1. package/README.md +46 -12
  2. package/package.json +2 -2
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgsql/utils",
3
- "version": "13.10.0",
3
+ "version": "13.10.1",
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",
@@ -77,5 +77,5 @@
77
77
  "@pgsql/types": "^13.8.0",
78
78
  "nested-obj": "0.0.1"
79
79
  },
80
- "gitHead": "1358b34422c45c2111dc0b1764155a720ab7fd73"
80
+ "gitHead": "a4751a2635eaa353e45b0f8832ac77e80b2f114a"
81
81
  }