23232 . [ Installation] ( #installation )
24243 . [ 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
5455With 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
109138const schema = {
110139 " tableName" : " users" ,
111140 " columns" : [
@@ -118,31 +147,36 @@ const schema = {
118147
119148// Construct the CREATE TABLE statement
120149const 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
137169const sql = deparse (createStmt , {});
170+
138171console .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
0 commit comments