@pgsql/utils 13.4.0 → 13.5.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.
- package/README.md +34 -7
- package/main/asts.js +2096 -0
- package/main/index.js +5 -0
- package/module/asts.js +2094 -0
- package/module/index.js +2 -0
- package/package.json +6 -3
- package/src/asts.ts +2100 -0
- package/src/index.ts +3 -1
- package/src/utils.ts +1 -1
- package/types/asts.d.ts +240 -0
- package/types/index.d.ts +2 -0
- package/types/utils.d.ts +2 -2
package/README.md
CHANGED
|
@@ -14,13 +14,15 @@
|
|
|
14
14
|
<a href="https://www.npmjs.com/package/@pgsql/utils"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Futils%2Fpackage.json"/></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
`@pgsql/utils` is a companion utility library for `@pgsql/types`,
|
|
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
19
|
## Features
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
21
|
+
- **AST Node Creation**: Simplifies the process of constructing PostgreSQL AST nodes, allowing for easy assembly of SQL queries or statements programmatically.
|
|
22
|
+
- **Type-safe Enum Conversion**: Convert between string and integer representations of PostgreSQL AST enum values.
|
|
23
|
+
- **Comprehensive Coverage**: Supports all enum types and node types defined in the PostgreSQL AST.
|
|
24
|
+
- **Seamless Integration**: Designed to be used alongside the `@pgsql/types` package for a complete AST handling solution.
|
|
25
|
+
|
|
24
26
|
|
|
25
27
|
## Installation
|
|
26
28
|
|
|
@@ -32,11 +34,36 @@ npm install @pgsql/utils
|
|
|
32
34
|
|
|
33
35
|
## Usage
|
|
34
36
|
|
|
37
|
+
### AST Node Creation
|
|
38
|
+
|
|
39
|
+
With the AST helper methods, creating complex SQL ASTs becomes straightforward and intuitive.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { CreateStmt, ColumnDef } from '@pgsql/types';
|
|
43
|
+
import ast from '@pgsql/utils';
|
|
44
|
+
|
|
45
|
+
const newColumn: ColumnDef = ast.columnDef({
|
|
46
|
+
colname: 'id',
|
|
47
|
+
typeName: ast.typeName({
|
|
48
|
+
names: [ast.string({ str: 'int4' })]
|
|
49
|
+
})
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const createStmt: CreateStmt = ast.createStmt({
|
|
53
|
+
relation: ast.rangeVar({
|
|
54
|
+
relname: 'new_table'
|
|
55
|
+
}),
|
|
56
|
+
tableElts: [newColumn]
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Enum Value Conversion
|
|
61
|
+
|
|
35
62
|
`@pgsql/utils` provides the `getEnumValue` function to convert between the string and integer representations of enum values.
|
|
36
63
|
|
|
37
64
|
Here are a couple of examples demonstrating how to use `@pgsql/utils` in real applications:
|
|
38
65
|
|
|
39
|
-
|
|
66
|
+
#### Example 1: Converting Enum Name to Integer
|
|
40
67
|
|
|
41
68
|
Suppose you are working with the A_Expr_Kind enum and you have the name of an enum value. You can get its integer representation like this:
|
|
42
69
|
|
|
@@ -49,7 +76,7 @@ const enumValue = getEnumValue('A_Expr_Kind', enumName);
|
|
|
49
76
|
console.log(enumValue); // Outputs the integer value corresponding to 'AEXPR_OP'
|
|
50
77
|
```
|
|
51
78
|
|
|
52
|
-
|
|
79
|
+
#### Example 2: Converting Integer to Enum Name
|
|
53
80
|
|
|
54
81
|
```ts
|
|
55
82
|
import { getEnumValue } from '@pgsql/utils';
|
|
@@ -67,7 +94,7 @@ console.log(enumName); // Outputs 'SORTBY_ASC' if 1 corresponds to 'SORTBY_ASC'
|
|
|
67
94
|
* [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`
|
|
68
95
|
* [@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.
|
|
69
96
|
* [@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.
|
|
70
|
-
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A utility library for
|
|
97
|
+
* [@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.
|
|
71
98
|
* [pg-proto-parser](https://github.com/launchql/pg-proto-parser): A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
72
99
|
* [libpg-query](https://github.com/launchql/libpg-query-node): The real PostgreSQL parser exposed for Node.js, used primarily in `pgsql-parser` for parsing and deparsing SQL queries.
|
|
73
100
|
|