Skip to content

Commit e242169

Browse files
committed
readmes and consistent interfaces
1 parent 5f1d7ec commit e242169

File tree

10 files changed

+68
-53
lines changed

10 files changed

+68
-53
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: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding
3535
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`:
3636

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

4142
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
42-
const stmt: SelectStmt = ast.selectStmt({
43+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
4344
targetList: [
44-
ast.resTarget({
45-
val: ast.columnRef({
46-
fields: [ast.aStar()]
45+
t.nodes.resTarget({
46+
val: t.nodes.columnRef({
47+
fields: [t.nodes.aStar()]
4748
})
4849
})
4950
],
5051
fromClause: [
51-
ast.rangeVar({
52+
t.nodes.rangeVar({
5253
relname: 'some_table',
5354
inh: true,
5455
relpersistence: 'p'
@@ -58,11 +59,11 @@ const stmt: SelectStmt = ast.selectStmt({
5859
op: 'SETOP_NONE'
5960
});
6061

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

6465
// Deparse the modified AST back to a SQL string
65-
console.log(deparse(stmts));
66+
console.log(deparse(stmt));
6667

6768
// Output: SELECT * FROM another_table
6869
```

packages/deparser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@
4949
"libpg-query": "17.3.3"
5050
},
5151
"dependencies": {
52-
"@pgsql/types": "^17.5.3"
52+
"@pgsql/types": "^17.6.0"
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/parser/README.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,18 @@ npm install pgsql-parser
3333

3434
The package exports both async and sync methods. Async methods handle initialization automatically, while sync methods require explicit initialization.
3535

36-
⚠️ We recommend using `@pgsql/deparser` instead of `deparse` from `pgsql-parser`. The deparser package is more complete, supports sub-expressions, and doesn't require the WebAssembly module, making it lighter and more flexible for most use cases. It will soon be deprecated, in a minor version bump.
36+
⚠️ If you don't need the parser functionality, consider using the TS-only (no WASM, zero dependencies) [`pgsql-deparser`](https://github.com/launchql/pgsql-parser/tree/main/packages/deparser) for a super fast, lightweight deparser. Battle-tested with 23,000+ SQL statements 🚀
3737

3838
### Async Methods (Recommended)
3939

4040
```typescript
41-
import { parse, deparse, parseFunction } from 'pgsql-parser';
41+
import { parse, deparse } from 'pgsql-parser';
4242

4343
// Parse SQL to AST
4444
const stmts = await parse('SELECT * FROM test_table');
4545

4646
// Deparse AST back to SQL
4747
const sql = await deparse(stmts);
48-
49-
// Parse PL/pgSQL functions
50-
const funcAst = await parseFunction(`
51-
CREATE FUNCTION get_count() RETURNS integer AS $$
52-
BEGIN
53-
RETURN (SELECT COUNT(*) FROM users);
54-
END;
55-
$$ LANGUAGE plpgsql;
56-
`);
5748
```
5849

5950
### Sync Methods
@@ -97,20 +88,21 @@ The `pgsql-deparser` module serializes ASTs to SQL in pure TypeScript, avoiding
9788
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`:
9889

9990
```ts
100-
import ast from '@pgsql/utils';
101-
import { deparse } from 'pgsql-deparser';
91+
import * as t from '@pgsql/utils';
92+
import { RangeVar, SelectStmt } from '@pgsql/types';
93+
import { deparseSync as deparse } from 'pgsql-deparser';
10294

10395
// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils
104-
const stmt = ast.selectStmt({
96+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
10597
targetList: [
106-
ast.resTarget({
107-
val: ast.columnRef({
108-
fields: [ast.aStar()]
98+
t.nodes.resTarget({
99+
val: t.nodes.columnRef({
100+
fields: [t.nodes.aStar()]
109101
})
110102
})
111103
],
112104
fromClause: [
113-
ast.rangeVar({
105+
t.nodes.rangeVar({
114106
relname: 'some_table',
115107
inh: true,
116108
relpersistence: 'p'
@@ -120,11 +112,11 @@ const stmt = ast.selectStmt({
120112
op: 'SETOP_NONE'
121113
});
122114

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

126118
// Deparse the modified AST back to a SQL string
127-
console.log(deparse(stmts));
119+
console.log(deparse(stmt));
128120

129121
// Output: SELECT * FROM another_table
130122
```

packages/parser/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
"database"
4343
],
4444
"dependencies": {
45-
"@pgsql/types": "^17.5.3",
46-
"libpg-query": "17.3.3",
45+
"@pgsql/types": "^17.6.0",
46+
"libpg-query": "17.5.0",
47+
"pgsql-deparser": "^17.6.2",
4748
"minimist": "^1.2.6"
4849
}
4950
}

packages/parser/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export {
22
parse as parse,
33
parseSync as parseSync,
4-
parsePlPgSQL as parseFunction,
5-
deparseSync as deparseSync,
6-
deparse as deparse,
74
loadModule as loadModule
85
} from 'libpg-query';
6+
7+
export { deparse, deparseSync } from 'pgsql-deparser';

packages/utils/__test__/utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as t from '../src';
22
import { RangeVar, SelectStmt } from '@pgsql/types';
3-
import { deparse } from 'pgsql-deparser';
3+
import { deparseSync as deparse } from 'pgsql-deparser';
44

55
it('simple SelectStmt', () => {
66
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
@@ -25,7 +25,7 @@ it('simple SelectStmt', () => {
2525
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';
2626

2727
expect(stmt).toMatchSnapshot();
28-
expect(deparse(stmt, {})).toMatchSnapshot();
28+
expect(deparse(stmt)).toMatchSnapshot();
2929
});
3030

3131
it('SelectStmt with WHERE clause', () => {

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"pgsql-deparser": "^17.6.2"
3636
},
3737
"dependencies": {
38-
"@pgsql/types": "^17.5.3",
38+
"@pgsql/types": "^17.6.0",
3939
"nested-obj": "0.0.1"
4040
},
4141
"keywords": []

yarn.lock

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,11 +1227,16 @@
12271227
node-addon-api "^3.2.1"
12281228
node-gyp-build "^4.3.0"
12291229

1230-
"@pgsql/types@^17.0.0", "@pgsql/types@^17.5.3":
1230+
"@pgsql/types@^17.0.0":
12311231
version "17.5.3"
12321232
resolved "https://registry.yarnpkg.com/@pgsql/types/-/types-17.5.3.tgz#b4a53ba71a1bf111de744d01fc49a4e1576f3e31"
12331233
integrity sha512-lRPdKcipoKLoovXw6tVwg9gdqV1fIvfjzqFDY1UFsJa8I+/yaVrGWCtmE+CeWU5e+imXd7X3fTWs8N8bK3R+AQ==
12341234

1235+
"@pgsql/types@^17.6.0":
1236+
version "17.6.0"
1237+
resolved "https://registry.yarnpkg.com/@pgsql/types/-/types-17.6.0.tgz#ea2511d8ebf24ee821968532184058d4ebf4ecaa"
1238+
integrity sha512-WGVIne1kFm3pIHiCkUWGbv7vlhxCRT+61UigsYejhmEsl4Iyf8d2CBnt9muhi571mfuwvtkeD0sxo5y8oj0ijQ==
1239+
12351240
"@pkgjs/parseargs@^0.11.0":
12361241
version "0.11.0"
12371242
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
@@ -4579,6 +4584,13 @@ [email protected]:
45794584
"@launchql/protobufjs" "7.2.6"
45804585
"@pgsql/types" "^17.0.0"
45814586

4587+
4588+
version "17.5.0"
4589+
resolved "https://registry.yarnpkg.com/libpg-query/-/libpg-query-17.5.0.tgz#1b60b950b5c8eae8dfed664a470c05f79f89634a"
4590+
integrity sha512-vFe0Lnneg/wIa5hsIIyJ2h1aExqLrn2qHVIC57bmyZE77Jk8G5S8qs7S94KYLJCk3i9TQZfkKTjViqJ+o6an4A==
4591+
dependencies:
4592+
"@pgsql/types" "^17.6.0"
4593+
45824594
lines-and-columns@^1.1.6:
45834595
version "1.2.4"
45844596
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"

0 commit comments

Comments
 (0)