Skip to content

Commit 1d88e4c

Browse files
committed
add JSON AST in docs
1 parent 6a1aa9d commit 1d88e4c

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

packages/utils/README.md

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
2. [Installation](#installation)
2424
3. [Usage](#usage)
2525
- [AST Node Creation](#ast-node-creation)
26+
- [JSON AST](#json-ast)
2627
- [Select Statement](#select-statement)
2728
- [Creating Table Schemas Dynamically](#creating-table-schemas-dynamically)
2829
- [Enum Value Conversion](#enum-value-conversion)
@@ -53,6 +54,34 @@ npm install @pgsql/utils
5354

5455
With the AST helper methods, creating complex SQL ASTs becomes straightforward and intuitive.
5556

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+
5685
#### Select Statement
5786

5887
```ts
@@ -105,7 +134,7 @@ deparse(createStmt, {});
105134
#### Creating Table Schemas Dynamically
106135

107136
```ts
108-
// Example JSON with schema
137+
// Example JSON schema
109138
const schema = {
110139
"tableName": "users",
111140
"columns": [
@@ -118,31 +147,36 @@ const schema = {
118147

119148
// Construct the CREATE TABLE statement
120149
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
122155
tableElts: schema.columns.map(column => ast.columnDef({
123156
colname: column.name,
124157
typeName: ast.typeName({
125158
names: [ast.string({ str: column.type })]
126159
}),
127160
constraints: column.constraints?.map(constraint =>
128161
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"
131163
})
132164
)
133165
}))
134166
});
135167

136-
// Assuming `deparse` function converts AST to SQL string
168+
// `deparse` function converts AST to SQL string
137169
const sql = deparse(createStmt, {});
170+
138171
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:
145173

174+
// CREATE TABLE users (
175+
// id int PRIMARY KEY,
176+
// username text,
177+
// email text UNIQUE,
178+
// created_at timestamp NOT NULL
179+
// )
146180
```
147181

148182
### Enum Value Conversion

packages/utils/__test__/__snapshots__/utils.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`dynamic creation of tables 1`] = `
4-
"CREATE TABLE (
5-
id int PRIMARY KEY ( id ),
6-
username string,
7-
email string UNIQUE ( email ),
8-
created_at timestamp NOT NULL ( created_at )
4+
"CREATE TABLE users (
5+
id int PRIMARY KEY,
6+
username text,
7+
email text UNIQUE,
8+
created_at timestamp NOT NULL
99
)"
1010
`;
1111

packages/utils/__test__/utils.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as u from '../src';
2-
import ast, { SelectStmt } from '../src';
2+
import ast, { SelectStmt, RangeVar } from '../src';
33
import { deparse } from 'pgsql-deparser';
44

55
it('getEnumValue snapshots', () => {
@@ -83,30 +83,33 @@ it('dynamic creation of tables', () => {
8383
"tableName": "users",
8484
"columns": [
8585
{ "name": "id", "type": "int", "constraints": ["PRIMARY KEY"] },
86-
{ "name": "username", "type": "string" },
87-
{ "name": "email", "type": "string", "constraints": ["UNIQUE"] },
86+
{ "name": "username", "type": "text" },
87+
{ "name": "email", "type": "text", "constraints": ["UNIQUE"] },
8888
{ "name": "created_at", "type": "timestamp", "constraints": ["NOT NULL"] }
8989
]
9090
};
9191

9292
// Construct the CREATE TABLE statement
9393
const createStmt = ast.createStmt({
94-
relation: ast.rangeVar({ relname: schema.tableName }),
94+
relation: ast.rangeVar({
95+
relname: schema.tableName,
96+
inh: true,
97+
relpersistence: 'p'
98+
}).RangeVar as RangeVar, // special case due to PG AST
9599
tableElts: schema.columns.map(column => ast.columnDef({
96100
colname: column.name,
97101
typeName: ast.typeName({
98102
names: [ast.string({ str: column.type })]
99103
}),
100104
constraints: column.constraints?.map(constraint =>
101105
ast.constraint({
102-
contype: constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL",
103-
keys: [ast.string({ str: column.name })]
106+
contype: constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL"
104107
})
105108
)
106109
}))
107110
});
108111

109-
// Assuming `deparse` function converts AST to SQL string
112+
// `deparse` function converts AST to SQL string
110113
const sql = deparse(createStmt, {});
111114
expect(sql).toMatchSnapshot();
112115
})

0 commit comments

Comments
 (0)