Skip to content

Commit 1747a8c

Browse files
authored
Merge pull request #41 from launchql/feat/readmes
readmes and consistent interfaces
2 parents d77f43c + 743d004 commit 1747a8c

File tree

14 files changed

+147
-135
lines changed

14 files changed

+147
-135
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,21 @@ The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding
9797
Here's how you can use the deparser in your TypeScript code, using [`@pgsql/utils`](https://github.com/launchql/pgsql-parser/tree/main/packages/utils) to create an AST for `deparse`:
9898

9999
```ts
100-
import ast from '@pgsql/utils';
101-
import { deparse } from 'pgsql-deparser';
100+
import * as t from '@pgsql/utils';
101+
import { RangeVar, SelectStmt } from '@pgsql/types';
102+
import { deparseSync as deparse } from 'pgsql-deparser';
102103

103104
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
104-
const stmt = ast.selectStmt({
105+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
105106
targetList: [
106-
ast.resTarget({
107-
val: ast.columnRef({
108-
fields: [ast.aStar()]
107+
t.nodes.resTarget({
108+
val: t.nodes.columnRef({
109+
fields: [t.nodes.aStar()]
109110
})
110111
})
111112
],
112113
fromClause: [
113-
ast.rangeVar({
114+
t.nodes.rangeVar({
114115
relname: 'some_table',
115116
inh: true,
116117
relpersistence: 'p'
@@ -120,11 +121,11 @@ const stmt = ast.selectStmt({
120121
op: 'SETOP_NONE'
121122
});
122123

123-
// Modify the AST if needed
124-
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
124+
// Modify the AST if needed
125+
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';
125126

126127
// Deparse the modified AST back to a SQL string
127-
console.log(deparse(stmts));
128+
console.log(deparse(stmt));
128129

129130
// Output: SELECT * FROM another_table
130131
```

packages/deparser/README.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
<a href="https://www.npmjs.com/package/pgsql-deparser"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/pgsql-parser?filename=packages%2Fdeparser%2Fpackage.json"/></a>
1515
</p>
1616

17-
`pgsql-deparser` is a streamlined tool designed to convert PostgreSQL Abstract Syntax Trees (AST) back into SQL queries. It is a companion module to [`pgsql-parser`](https://github.com/launchql/pgsql-parser), which is capable of both parsing SQL queries into ASTs and deparsing these ASTs back into SQL. However, unlike `pgsql-parser`, which incorporates the full PostgreSQL parser, `pgsql-deparser` focuses solely on the deparser functionality. This makes it an excellent choice for scenarios where only AST-to-SQL conversion is needed, avoiding the extra overhead associated with the full parsing capabilities.
17+
`pgsql-deparser` is the lightning-fast, pure TypeScript solution for converting PostgreSQL ASTs back into SQL queries. Perfect companion to [`pgsql-parser`](https://github.com/launchql/pgsql-parser), this focused tool delivers SQL generation without any native dependencies or WebAssembly overhead.
1818

1919
## Installation
2020

2121
```sh
2222
npm install pgsql-deparser
2323
```
2424

25-
## Key Features
25+
## Features
2626

27-
- **Lightweight and Pure TypeScript**: Built entirely in TypeScript, `pgsql-deparser` is lightweight and doesn't rely on native dependencies, facilitating easier integration and deployment across various environments.
28-
- **Focused Functionality**: By concentrating on decompiling ASTs to SQL, `pgsql-deparser` offers a specialized, efficient solution for those who need to generate SQL statements from ASTs without the full parsing mechanism.
29-
- **Compatibility**: Ideal for use cases where the AST is already obtained from another source or process, allowing for seamless SQL generation from AST representations.
27+
***Pure TypeScript Performance** – Zero dependencies, no WASM, no compilation - just blazing fast SQL generation
28+
* 🪶 **Ultra Lightweight** – Minimal footprint with laser-focused functionality for AST-to-SQL conversion only
29+
* 🧪 **Battle-Tested Reliability** – Validated against 23,000+ SQL statements ensuring production-grade stability
30+
* 🌍 **Universal Compatibility** – Runs anywhere JavaScript does - browsers, Node.js, edge functions, you name it
3031

3132
## Deparser Example
3233

@@ -35,20 +36,21 @@ The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding
3536
Here's how you can use the deparser in your TypeScript code, using [`@pgsql/utils`](https://github.com/launchql/pgsql-parser/tree/main/packages/utils) to create an AST for `deparse`:
3637

3738
```ts
38-
import ast, { SelectStmt } from '@pgsql/utils';
39-
import { deparse } from 'pgsql-deparser';
39+
import * as t from '@pgsql/utils';
40+
import { RangeVar, SelectStmt } from '@pgsql/types';
41+
import { deparseSync as deparse } from 'pgsql-deparser';
4042

4143
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
42-
const stmt: SelectStmt = ast.selectStmt({
44+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
4345
targetList: [
44-
ast.resTarget({
45-
val: ast.columnRef({
46-
fields: [ast.aStar()]
46+
t.nodes.resTarget({
47+
val: t.nodes.columnRef({
48+
fields: [t.nodes.aStar()]
4749
})
4850
})
4951
],
5052
fromClause: [
51-
ast.rangeVar({
53+
t.nodes.rangeVar({
5254
relname: 'some_table',
5355
inh: true,
5456
relpersistence: 'p'
@@ -58,11 +60,11 @@ const stmt: SelectStmt = ast.selectStmt({
5860
op: 'SETOP_NONE'
5961
});
6062

61-
// Modify the AST if needed
62-
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
63+
// Modify the AST if needed
64+
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';
6365

6466
// Deparse the modified AST back to a SQL string
65-
console.log(deparse(stmts));
67+
console.log(deparse(stmt));
6668

6769
// Output: SELECT * FROM another_table
6870
```
@@ -75,43 +77,41 @@ console.log(deparse(stmts));
7577

7678
As of PG 13, PG majors versions maintained will have a matching dedicated major npm version. Only the latest Postgres stable release receives active updates.
7779

78-
Our latest is built with `13-latest` branch from libpg_query
80+
Our latest is built with `17-latest` branch from libpg_query
7981

80-
| PostgreSQL Major Version | libpg_query | Status | npm
82+
| PostgreSQL Major Version | libpg_query | Status | npm tag |
8183
|--------------------------|-------------|---------------------|---------|
82-
| 13 | 13-latest | Active development | `latest`
84+
| 17 | 17-latest | Active Development | `latest` |
85+
| 16 | (n/a) | Not supported |
86+
| 15 | (n/a) | Not supported |
87+
| 14 | (n/a) | Not supported |
88+
| 13 | 13-latest | Only Critical Fixes | `13.16.0` |
8389
| 12 | (n/a) | Not supported |
8490
| 11 | (n/a) | Not supported |
8591
| 10 | 10-latest | Not supported | `@1.3.1` ([tree](https://github.com/launchql/pgsql-parser/tree/39b7b1adc8914253226e286a48105785219a81ca)) |
8692

93+
## Credits
8794

88-
## Related
89-
90-
* [pgsql-parser](https://github.com/launchql/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
91-
* [pgsql-deparser](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
92-
* [pgsql-enums](https://github.com/launchql/pgsql-parser/tree/main/packages/pgsql-enums): 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`
93-
* [@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.
94-
* [@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.
95-
* [@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.
96-
* [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.
97-
* [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.
98-
99-
Thanks [@lfittl](https://github.com/lfittl) for building the core `libpg_query` suite:
100-
101-
* [libpg_query](https://github.com/pganalyze/libpg_query)
102-
* [pg_query](https://github.com/lfittl/pg_query)
103-
* [pg_query.go](https://github.com/lfittl/pg_query.go)
95+
Built on the excellent work of several contributors:
10496

105-
## Credits
97+
* **[Dan Lynch](https://github.com/pyramation)** — official maintainer since 2018 and architect of the current implementation
98+
* **[Lukas Fittl](https://github.com/lfittl)** for [libpg_query](https://github.com/pganalyze/libpg_query) — the core PostgreSQL parser that powers this project
99+
* **[Greg Richardson](https://github.com/gregnr)** for AST guidance and pushing the transition to WASM for better interoperability
100+
* **[Ethan Resnick](https://github.com/ethanresnick)** for the original Node.js N-API bindings
101+
* **[Zac McCormick](https://github.com/zhm)** for the foundational [node-pg-query-native](https://github.com/zhm/node-pg-query-native) parser
106102

107-
Thanks to [@zhm](https://github.com/zhm) for the OG parser that started it all:
103+
## Related
108104

109-
* [pg-query-parser](https://github.com/zhm/pg-query-parser)
110-
* [pg-query-native](https://github.com/zhm/node-pg-query-native)
111-
* [Original LICENSE](https://github.com/zhm/pg-query-parser/blob/master/LICENSE.md)
105+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
106+
* [pgsql-deparser](https://www.npmjs.com/package/pgsql-deparser): A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement `pgsql-parser`.
107+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
108+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): Provides TypeScript enum definitions for PostgreSQL constants, enabling type-safe usage of PostgreSQL enums and constants in your applications.
109+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/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.
110+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
111+
* [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.
112112

113113
## Disclaimer
114114

115-
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED AS IS, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
115+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
116116

117117
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

packages/deparser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
"database"
4747
],
4848
"devDependencies": {
49-
"libpg-query": "17.3.3"
49+
"libpg-query": "17.5.2"
5050
},
5151
"dependencies": {
52-
"@pgsql/types": "^17.5.3"
52+
"@pgsql/types": "^17.6.1"
5353
}
5454
}

packages/deparser/src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import { Deparser, DeparserOptions } from "./deparser";
22

3-
const deparse = Deparser.deparse;
4-
export { deparse, Deparser, DeparserOptions };
3+
const deparseMethod = Deparser.deparse;
4+
5+
// Export the original sync version as deparseSync
6+
export const deparseSync = deparseMethod;
7+
8+
// Create an async wrapper for deparse
9+
export const deparse = async (...args: Parameters<typeof deparseMethod>): Promise<ReturnType<typeof deparseMethod>> => {
10+
return deparseMethod(...args);
11+
};
12+
13+
export { Deparser, DeparserOptions };

packages/deparser/test-utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { parse } from 'libpg-query';
2-
import { deparse } from '../src';
2+
import { deparseSync as deparse } from '../src';
33
import { cleanTree } from '../src/utils';
44
import { readFileSync } from 'fs';
55
import * as path from 'path';
66
import { expect } from '@jest/globals';
7-
import {diff} from 'jest-diff'
7+
import { diff } from 'jest-diff'
88

99
type ParseErrorType =
1010
| 'PARSE_FAILED'

0 commit comments

Comments
 (0)