@pgsql/utils 13.9.0 → 13.10.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.
Files changed (2) hide show
  1. package/README.md +95 -9
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -16,6 +16,21 @@
16
16
 
17
17
  `@pgsql/utils` is a companion utility library for `@pgsql/types`, offering convenient functions to work with PostgreSQL Abstract Syntax Tree (AST) nodes and enums in a type-safe manner. This library facilitates the creation of AST nodes and simplifies the process of converting between enum names and their respective integer values, as defined in the PostgreSQL parser output.
18
18
 
19
+ # Table of Contents
20
+
21
+ 1. [@pgsql/utils](#pgsql-utils)
22
+ - [Key Features](#key-features)
23
+ 2. [Installation](#installation)
24
+ 3. [Usage](#usage)
25
+ - [AST Node Creation](#ast-node-creation)
26
+ - [Select Statement](#select-statement)
27
+ - [Creating Table Schemas Dynamically](#creating-table-schemas-dynamically)
28
+ - [Enum Value Conversion](#enum-value-conversion)
29
+ - [Example 1: Converting Enum Name to Integer](#example-1-converting-enum-name-to-integer)
30
+ - [Example 2: Converting Integer to Enum Name](#example-2-converting-integer-to-enum-name)
31
+ 4. [Related Projects](#related)
32
+ 5. [Disclaimer](#disclaimer)
33
+
19
34
  ## Features
20
35
 
21
36
  - **AST Node Creation**: Simplifies the process of constructing PostgreSQL AST nodes, allowing for easy assembly of SQL queries or statements programmatically.
@@ -38,25 +53,96 @@ npm install @pgsql/utils
38
53
 
39
54
  With the AST helper methods, creating complex SQL ASTs becomes straightforward and intuitive.
40
55
 
56
+ #### Select Statement
57
+
41
58
  ```ts
42
59
  import ast, { CreateStmt, ColumnDef } from '@pgsql/utils';
43
60
  import { deparse } from 'pgsql-deparser';
44
61
 
45
- const newColumn: ColumnDef = ast.columnDef({
46
- colname: 'id',
47
- typeName: ast.typeName({
48
- names: [ast.string({ str: 'int4' })]
62
+ const selectStmt: SelectStmt = ast.selectStmt({
63
+ targetList: [
64
+ ast.resTarget({
65
+ val: ast.columnRef({
66
+ fields: [ast.aStar()]
67
+ })
68
+ })
69
+ ],
70
+ fromClause: [
71
+ ast.rangeVar({
72
+ schemaname: 'myschema',
73
+ relname: 'mytable',
74
+ inh: true,
75
+ relpersistence: 'p'
76
+ })
77
+ ],
78
+ whereClause: ast.aExpr({
79
+ kind: 'AEXPR_OP',
80
+ name: [ast.string({ str: '=' })],
81
+ lexpr: ast.columnRef({
82
+ fields: [ast.string({ str: 'a' })]
83
+ }),
84
+ rexpr: ast.typeCast({
85
+ arg: ast.aConst({
86
+ val: ast.string({ str: 't' })
87
+ }),
88
+ typeName: ast.typeName({
89
+ names: [
90
+ ast.string({ str: 'pg_catalog' }),
91
+ ast.string({ str: 'bool' })
92
+ ],
93
+ typemod: -1
94
+ })
49
95
  })
96
+ }),
97
+ limitOption: 'LIMIT_OPTION_DEFAULT',
98
+ op: 'SETOP_NONE'
50
99
  });
51
100
 
52
- const createStmt: CreateStmt = ast.createStmt({
53
- relation: ast.rangeVar({
54
- relname: 'new_table'
101
+ deparse(createStmt, {});
102
+ // SELECT * FROM myschema.mytable WHERE a = TRUE
103
+ ```
104
+
105
+ #### Creating Table Schemas Dynamically
106
+
107
+ ```ts
108
+ // Example JSON with schema
109
+ const schema = {
110
+ "tableName": "users",
111
+ "columns": [
112
+ { "name": "id", "type": "int", "constraints": ["PRIMARY KEY"] },
113
+ { "name": "username", "type": "string" },
114
+ { "name": "email", "type": "string", "constraints": ["UNIQUE"] },
115
+ { "name": "created_at", "type": "timestamp", "constraints": ["NOT NULL"] }
116
+ ]
117
+ };
118
+
119
+ // Construct the CREATE TABLE statement
120
+ const createStmt = ast.createStmt({
121
+ relation: ast.rangeVar({ relname: schema.tableName }),
122
+ tableElts: schema.columns.map(column => ast.columnDef({
123
+ colname: column.name,
124
+ typeName: ast.typeName({
125
+ names: [ast.string({ str: column.type })]
55
126
  }),
56
- tableElts: [newColumn]
127
+ constraints: column.constraints?.map(constraint =>
128
+ ast.constraint({
129
+ contype: constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL",
130
+ keys: [ast.string({ str: column.name })]
131
+ })
132
+ )
133
+ }))
57
134
  });
58
135
 
59
- deparse(createStmt, {}) // SQL!
136
+ // Assuming `deparse` function converts AST to SQL string
137
+ const sql = deparse(createStmt, {});
138
+ 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
+ // )
145
+
60
146
  ```
61
147
 
62
148
  ### Enum Value Conversion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgsql/utils",
3
- "version": "13.9.0",
3
+ "version": "13.10.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.13.0",
69
+ "pgsql-deparser": "^13.14.0",
70
70
  "prettier": "^2.8.7",
71
71
  "rimraf": "5.0.5",
72
72
  "ts-jest": "^29.1.0",
@@ -77,5 +77,5 @@
77
77
  "@pgsql/types": "^13.8.0",
78
78
  "nested-obj": "0.0.1"
79
79
  },
80
- "gitHead": "112e54189b4b47ff1080d497704de8c4e94a4743"
80
+ "gitHead": "1358b34422c45c2111dc0b1764155a720ab7fd73"
81
81
  }