Skip to content

Commit 2f612e5

Browse files
committed
readmes
1 parent 7ffcccf commit 2f612e5

File tree

3 files changed

+128
-165
lines changed

3 files changed

+128
-165
lines changed

README.md

Lines changed: 47 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# PostgreSQL AST Tools
1+
# pgsql-parser
22

33
<p align="center" width="100%">
44
<img height="120" src="https://github.com/launchql/pgsql-parser/assets/545047/6440fa7d-918b-4a3b-8d1b-755d85de8bea" />
55
</p>
66

7+
78
<p align="center" width="100%">
89
<a href="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml">
910
<img height="20" src="https://github.com/launchql/pgsql-parser/actions/workflows/run-tests.yaml/badge.svg" />
@@ -13,6 +14,9 @@
1314
<a href="https://github.com/launchql/pgsql-parser/blob/main/LICENSE-MIT"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
1415
</p>
1516

17+
## PostgreSQL AST Tools
18+
19+
1620
A comprehensive monorepo for PostgreSQL Abstract Syntax Tree (AST) parsing, manipulation, and code generation. This collection of packages provides everything you need to work with PostgreSQL at the AST level, from parsing SQL queries to generating type-safe TypeScript definitions.
1721

1822
## 📦 Packages Overview
@@ -56,6 +60,7 @@ import { parse } from 'pgsql-parser';
5660

5761
const ast = await parse('SELECT * FROM users WHERE id = 1');
5862
console.log(JSON.stringify(ast, null, 2));
63+
// {"version":170004,"stmts":[{"stmt":{"SelectStmt":{"targetList":[{"ResTarget": ... ,"op":"SETOP_NONE"}}}]}
5964
```
6065

6166
#### Convert AST back to SQL
@@ -68,19 +73,20 @@ console.log(sql); // SELECT * FROM users WHERE id = 1
6873

6974
#### Build AST Programmatically
7075
```typescript
71-
import ast from '@pgsql/utils';
76+
import * as t from '@pgsql/utils';
77+
import { RangeVar, SelectStmt } from '@pgsql/types';
7278

73-
const selectStmt = ast.selectStmt({
79+
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
7480
targetList: [
75-
ast.resTarget({
76-
val: ast.columnRef({
77-
fields: [ast.aStar()]
81+
t.nodes.resTarget({
82+
val: t.nodes.columnRef({
83+
fields: [t.nodes.aStar()]
7884
})
7985
})
8086
],
8187
fromClause: [
82-
ast.rangeVar({
83-
relname: 'users',
88+
t.nodes.rangeVar({
89+
relname: 'some_table',
8490
inh: true,
8591
relpersistence: 'p'
8692
})
@@ -102,22 +108,6 @@ pgsql deparse ast.json
102108
pgsql proto-gen --inFile pg_query.proto --outDir out --types --enums
103109
```
104110

105-
## 🏗️ Architecture
106-
107-
This monorepo is organized to provide modular, focused tools that work together seamlessly:
108-
109-
```
110-
pgsql-parser/
111-
├── packages/
112-
│ ├── parser/ # Core parser with WebAssembly
113-
│ ├── deparser/ # Pure TypeScript deparser
114-
│ ├── pgsql-cli/ # Unified CLI tool
115-
│ ├── utils/ # AST construction utilities
116-
│ ├── proto-parser/ # Protobuf code generation
117-
│ └── transform/ # (Not production-ready yet)
118-
└── ...
119-
```
120-
121111
### Package Relationships
122112

123113
- **pgsql-parser** provides full parsing and deparsing capabilities using the actual PostgreSQL parser
@@ -148,28 +138,20 @@ cd packages/parser
148138
npm run build
149139
```
150140

151-
## 📚 Documentation
141+
## Documentation
152142

153143
Each package has its own detailed README:
154144

155-
- [pgsql-parser Documentation](./packages/parser/README.md)
156-
- [pgsql-deparser Documentation](./packages/deparser/README.md)
157-
- [@pgsql/cli Documentation](./packages/pgsql-cli/README.md)
158-
- [@pgsql/utils Documentation](./packages/utils/README.md)
159-
- [pg-proto-parser Documentation](./packages/proto-parser/README.md)
145+
- [`pgsql-parser`](./packages/parser/README.md)
146+
- [`pgsql-deparser`](./packages/deparser/README.md)
147+
- [`@pgsql/cli`](./packages/pgsql-cli/README.md)
148+
- [`@pgsql/utils`](./packages/utils/README.md)
149+
- [`pg-proto-parser`](./packages/proto-parser/README.md)
160150

161-
## 🎯 Use Cases
162-
163-
- **SQL Query Analysis**: Parse queries to understand their structure and components
164-
- **Query Transformation**: Modify queries programmatically at the AST level
165-
- **SQL Generation**: Build complex queries programmatically with type safety
166-
- **Code Generation**: Generate TypeScript types from PostgreSQL schemas
167-
- **Query Validation**: Validate SQL syntax using the real PostgreSQL parser
168-
- **Database Tooling**: Build developer tools that understand PostgreSQL deeply
169-
170-
## 💡 Examples
151+
## Examples
171152

172153
### Transform a Query
154+
173155
```typescript
174156
import { parse } from 'pgsql-parser';
175157
import { deparse } from 'pgsql-deparser';
@@ -186,38 +168,39 @@ console.log(newSql); // SELECT * FROM customers WHERE active = TRUE
186168
```
187169

188170
### Build a Query Programmatically
171+
189172
```typescript
190173
import ast from '@pgsql/utils';
191-
import { deparse } from 'pgsql-deparser';
174+
import { deparse as deparseSync } from 'pgsql-deparser';
192175

193-
const query = ast.selectStmt({
176+
const query: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
194177
targetList: [
195-
ast.resTarget({
196-
val: ast.columnRef({
197-
fields: [ast.string({ str: 'name' })]
178+
t.nodes.resTarget({
179+
val: t.nodes.columnRef({
180+
fields: [t.nodes.string({ sval: 'name' })]
198181
})
199182
}),
200-
ast.resTarget({
201-
val: ast.columnRef({
202-
fields: [ast.string({ str: 'email' })]
183+
t.nodes.resTarget({
184+
val: t.nodes.columnRef({
185+
fields: [t.nodes.string({ sval: 'email' })]
203186
})
204187
})
205188
],
206189
fromClause: [
207-
ast.rangeVar({
190+
t.nodes.rangeVar({
208191
relname: 'users',
209192
inh: true,
210193
relpersistence: 'p'
211194
})
212195
],
213-
whereClause: ast.aExpr({
196+
whereClause: t.nodes.aExpr({
214197
kind: 'AEXPR_OP',
215-
name: [ast.string({ str: '>' })],
216-
lexpr: ast.columnRef({
217-
fields: [ast.string({ str: 'age' })]
198+
name: [t.nodes.string({ sval: '>' })],
199+
lexpr: t.nodes.columnRef({
200+
fields: [t.nodes.string({ sval: 'age' })]
218201
}),
219-
rexpr: ast.aConst({
220-
val: ast.integer({ ival: 18 })
202+
rexpr: t.nodes.aConst({
203+
ival: t.ast.integer({ ival: 18 })
221204
})
222205
}),
223206
limitOption: 'LIMIT_OPTION_DEFAULT',
@@ -228,46 +211,17 @@ console.log(deparse(query));
228211
// SELECT name, email FROM users WHERE age > 18
229212
```
230213

231-
## 🤝 Contributing
232-
233-
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
234-
235-
### Development Workflow
236-
1. Fork the repository
237-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
238-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
239-
4. Push to the branch (`git push origin feature/amazing-feature`)
240-
5. Open a Pull Request
241-
242-
## 📄 License
243-
244-
This project is licensed under the MIT License - see the [LICENSE](LICENSE-MIT) file for details.
245-
246-
## 🙏 Credits
247-
248-
Built on the excellent work of several contributors:
249-
250-
* **[Dan Lynch](https://github.com/pyramation)** — official maintainer since 2018 and architect of the current implementation
251-
* **[Lukas Fittl](https://github.com/lfittl)** for [libpg_query](https://github.com/pganalyze/libpg_query) — the core PostgreSQL parser that powers this project
252-
* **[Greg Richardson](https://github.com/gregnr)** for AST guidance and pushing the transition to WASM for better interoperability
253-
* **[Ethan Resnick](https://github.com/ethanresnick)** for the original Node.js N-API bindings
254-
* **[Zac McCormick](https://github.com/zhm)** for the foundational [node-pg-query-native](https://github.com/zhm/node-pg-query-native) parser
255-
256-
## 🔗 Related Projects
257-
258-
### Core Packages (in this monorepo)
259-
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): The real PostgreSQL parser for Node.js
260-
* [pgsql-deparser](https://www.npmjs.com/package/pgsql-deparser): Lightning-fast SQL generation from AST
261-
* [@pgsql/cli](https://www.npmjs.com/package/@pgsql/cli): Unified CLI for PostgreSQL AST operations
262-
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): Type-safe AST construction utilities
263-
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): PostgreSQL protobuf parser and code generator
264-
265-
214+
## Related
266215

267-
### External Dependencies
268-
* [libpg-query](https://github.com/launchql/libpg-query-node): The PostgreSQL parser exposed for Node.js
216+
* [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.
217+
* [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`.
218+
* [@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.
219+
* [@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.
220+
* [@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.
221+
* [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.
222+
* [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.
269223

270-
## ⚖️ Disclaimer
224+
## Disclaimer
271225

272226
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
273227

0 commit comments

Comments
 (0)