Skip to content

Commit 781299e

Browse files
committed
readme
1 parent 899250e commit 781299e

File tree

4 files changed

+117
-36
lines changed

4 files changed

+117
-36
lines changed

parser/README.md

Lines changed: 110 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,37 @@ npm install @pgsql/parser@lts
3030
### Dynamic Version Selection
3131

3232
```javascript
33-
import { parse, PgParser } from '@pgsql/parser';
33+
import Parser from '@pgsql/parser';
34+
// or: import { Parser } from '@pgsql/parser';
3435

35-
// Parse with default version (17)
36-
const result = await parse('SELECT 1+1 as sum');
36+
// Create parser with default version (17)
37+
const parser = new Parser();
38+
const result = await parser.parse('SELECT 1+1 as sum');
3739
console.log(result);
38-
// { version: 17, result: { version: 170004, stmts: [...] } }
40+
// { version: 170004, stmts: [...] }
3941

40-
// Parse with specific version
41-
const result15 = await parse('SELECT 1+1 as sum', 15);
42+
// Create parser with specific version
43+
const parser15 = new Parser({ version: 15 });
44+
const result15 = await parser15.parse('SELECT 1+1 as sum');
4245
console.log(result15);
43-
// { version: 15, result: { version: 150007, stmts: [...] } }
46+
// { version: 150007, stmts: [...] }
47+
48+
// Check supported versions
49+
import { isSupportedVersion, getSupportedVersions } from '@pgsql/parser';
50+
console.log(getSupportedVersions()); // [13, 14, 15, 16, 17]
51+
console.log(isSupportedVersion(15)); // true
52+
```
53+
54+
### CommonJS Usage
4455

45-
// Using PgParser class
46-
const parser = new PgParser(16);
47-
const result16 = await parser.parse('SELECT * FROM users');
56+
```javascript
57+
const { Parser, isSupportedVersion, getSupportedVersions } = require('@pgsql/parser');
58+
59+
async function parseSQL() {
60+
const parser = new Parser({ version: 16 });
61+
const result = await parser.parse('SELECT * FROM users');
62+
console.log(result);
63+
}
4864
```
4965

5066
### Static Version Imports
@@ -53,61 +69,122 @@ For better tree-shaking and when you know which version you need:
5369

5470
```javascript
5571
// Import specific version
56-
import * as pg17 from '@pgsql/parser/v17';
72+
import { parse } from '@pgsql/parser/v17';
5773

58-
await pg17.loadModule();
59-
const result = await pg17.parse('SELECT 1');
74+
const result = await parse('SELECT 1');
6075
console.log(result);
6176
// { version: 170004, stmts: [...] }
77+
78+
// Or access via the main module
79+
import { v17 } from '@pgsql/parser';
80+
const result2 = await v17.parse('SELECT 1');
6281
```
6382

6483
### Error Handling
6584

66-
The parser returns errors in a consistent format:
85+
The parser throws errors directly (not wrapped in result objects):
6786

6887
```javascript
69-
const result = await parse('INVALID SQL');
70-
if (result.error) {
71-
console.error(result.error);
72-
// { type: 'syntax', message: 'syntax error at or near "INVALID"', position: 0 }
88+
import Parser from '@pgsql/parser';
89+
90+
const parser = new Parser();
91+
try {
92+
const result = await parser.parse('INVALID SQL');
93+
} catch (error) {
94+
console.error(error.name); // 'SqlError'
95+
console.error(error.message); // 'syntax error at or near "INVALID"'
96+
console.error(error.sqlDetails); // { cursorPosition: 0, ... }
7397
}
7498
```
7599

76100
## API
77101

78-
### `parse(query: string, version?: 13 | 14 | 15 | 16 | 17): Promise<ParseResult>`
102+
### `Parser`
79103

80-
Parse a SQL query with the specified PostgreSQL version.
104+
The main parser class for parsing SQL with a specific PostgreSQL version.
81105

82-
- `query`: The SQL query string to parse
83-
- `version`: PostgreSQL version (13, 14, 15, 16, or 17). Defaults to 17.
106+
```typescript
107+
import Parser from '@pgsql/parser';
84108

85-
Returns a promise that resolves to:
86-
- On success: `{ version: number, result: AST }`
87-
- On error: `{ version: number, error: { type: string, message: string, position: number } }`
109+
const parser = new Parser(options?: { version?: 13 | 14 | 15 | 16 | 17 });
110+
```
111+
112+
#### Properties
113+
- `version`: The PostgreSQL version used by this parser instance
114+
- `ready`: A promise that resolves when the parser is fully loaded
88115

89-
### `PgParser`
116+
#### Methods
90117

91-
Class for creating a parser instance with a specific version.
118+
##### `parse(query: string): Promise<ParseResult>`
119+
Parse a SQL query asynchronously. Automatically loads the parser if needed.
92120

93121
```javascript
94-
const parser = new PgParser(version);
95-
const result = await parser.parse(query);
96-
const syncResult = parser.parseSync(query); // Only available after first parse()
122+
const result = await parser.parse('SELECT 1');
123+
// Returns: { version: 170004, stmts: [...] }
124+
```
125+
126+
##### `parseSync(query: string): ParseResult`
127+
Parse a SQL query synchronously. Requires the parser to be loaded first.
128+
129+
```javascript
130+
await parser.loadParser(); // or await parser.ready
131+
const result = parser.parseSync('SELECT 1');
132+
```
133+
134+
##### `loadParser(): Promise<void>`
135+
Explicitly load the parser. Usually not needed as `parse()` loads automatically.
136+
137+
### Utility Functions
138+
139+
##### `isSupportedVersion(version: number): boolean`
140+
Check if a PostgreSQL version is supported.
141+
142+
##### `getSupportedVersions(): number[]`
143+
Get an array of all supported PostgreSQL versions.
144+
145+
### Error Handling
146+
147+
All parsing errors throw `SqlError` with the following structure:
148+
149+
```typescript
150+
class SqlError extends Error {
151+
name: 'SqlError';
152+
message: string;
153+
sqlDetails?: {
154+
cursorPosition?: number;
155+
fileName?: string;
156+
functionName?: string;
157+
lineNumber?: number;
158+
};
159+
}
97160
```
98161

99162
## Version Exports
100163

164+
Each PostgreSQL version can be imported directly for better tree-shaking:
165+
101166
- `@pgsql/parser/v13` - PostgreSQL 13 parser
102167
- `@pgsql/parser/v14` - PostgreSQL 14 parser
103168
- `@pgsql/parser/v15` - PostgreSQL 15 parser
104169
- `@pgsql/parser/v16` - PostgreSQL 16 parser
105170
- `@pgsql/parser/v17` - PostgreSQL 17 parser
106171

107172
Each version export provides:
108-
- `loadModule()`: Initialize the WASM module
109-
- `parse(query)`: Parse a query (async)
110-
- `parseSync(query)`: Parse a query (sync, requires loadModule first)
173+
- `parse(query: string): Promise<ParseResult>` - Parse a query asynchronously
174+
- `parseSync(query: string): ParseResult` - Parse a query synchronously (auto-loads if needed)
175+
- `SqlError` - The error class for parsing errors
176+
- All TypeScript types for that PostgreSQL version
177+
178+
Example:
179+
```javascript
180+
import { parse, parseSync } from '@pgsql/parser/v17';
181+
182+
// Async parsing
183+
const result = await parse('SELECT 1');
184+
185+
// Sync parsing (auto-loads on first use)
186+
const result2 = parseSync('SELECT 2');
187+
```
111188

112189
## Build Configurations
113190

parser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pgsql/parser",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"author": "Dan Lynch <[email protected]>",
55
"description": "Multi-version PostgreSQL parser with dynamic version selection",
66
"main": "./wasm/index.cjs",

parser/templates/index.cjs.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class Parser {
4545
}
4646

4747
async parse(query) {
48-
await this.loadParser();
48+
if (!this.parser) {
49+
await this.loadParser();
50+
}
4951
try {
5052
return await this.parser.parse(query);
5153
} catch (error) {

parser/templates/index.js.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export class Parser {
4545
}
4646

4747
async parse(query) {
48-
await this.loadParser();
48+
if (!this.parser) {
49+
await this.loadParser();
50+
}
4951
try {
5052
return await this.parser.parse(query);
5153
} catch (error) {

0 commit comments

Comments
 (0)