8000 new API · launchql/libpg-query-node@408ee1d · GitHub
[go: up one dir, main page]

Skip to content

Commit 408ee1d

Browse files
committed
new API
1 parent a8a4bf0 commit 408ee1d

File tree

10 files changed

+49
-49
lines changed

10 files changed

+49
-49
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@ npm install libpg-query
3939

4040
## Usage
4141

42-
### `parseQuery(sql: string): Promise<ParseResult[]>`
42+
### `parse(query: string): Promise<ParseResult>`
4343

4444
Parses the SQL and returns a Promise for the parse tree. May reject with a parse error.
4545

4646
```typescript
47-
import { parseQuery } from 'libpg-query';
47+
import { parse } from 'libpg-query';
4848

49-
const result = await parseQuery('SELECT * FROM users WHERE active = true');
50-
// Returns: ParseResult[] - array of parsed query objects
49+
const result = await parse('SELECT * FROM users WHERE active = true');
50+
// Returns: ParseResult - parsed query object
5151
```
5252

53-
### `parseQuerySync(sql: string): ParseResult[]`
53+
### `parseSync(query: string): ParseResult`
5454

5555
Synchronous version that returns the parse tree directly. May throw a parse error.
5656

5757
```typescript
58-
import { parseQuerySync } from 'libpg-query';
58+
import { parseSync } from 'libpg-query';
5959

60-
const result = parseQuerySync('SELECT * FROM users WHERE active = true');
61-
// Returns: ParseResult[] - array of parsed query objects
60+
const result = parseSync('SELECT * FROM users WHERE active = true');
61+
// Returns: ParseResult - parsed query object
6262
```
6363

6464
### `parsePlPgSQL(funcsSql: string): Promise<ParseResult>`
@@ -94,10 +94,10 @@ const result = parsePlPgSQLSync(functionSql);
9494
Converts a parse tree back to SQL string. Returns a Promise for the SQL string.
9595

9696
```typescript
97-
import { parseQuery, deparse } from 'libpg-query';
97+
import { parse, deparse } from 'libpg-query';
9898

99-
const parseTree = await parseQuery('SELECT * FROM users WHERE active = true');
100-
const sql = await deparse(parseTree[0]);
99+
const parseTree = await parse('SELECT * FROM users WHERE active = true');
100+
const sql = await deparse(parseTree);
101101
// Returns: string - reconstructed SQL query
102102
```
103103

@@ -106,10 +106,10 @@ const sql = await deparse(parseTree[0]);
106106
Synchronous version that converts a parse tree back to SQL string directly.
107107

108108
```typescript
109-
import { parseQuerySync, deparseSync } from 'libpg-query';
109+
import { parseSync, deparseSync } from 'libpg-query';
110110

111-
const parseTree = parseQuerySync('SELECT * FROM users WHERE active = true');
112-
const sql = deparseSync(parseTree[0]);
111+
const parseTree = parseSync('SELECT * FROM users WHERE active = true');
112+
const sql = deparseSync(parseTree);
113113
// Returns: string - reconstructed SQL query
114114
```
115115

@@ -166,37 +166,37 @@ The library provides both async and sync methods. Async methods handle initializ
166166
Async methods handle initialization automatically and are always safe to use:
167167

168168
```typescript
169-
import { parseQuery, deparse } from 'libpg-query';
169+
import { parse, deparse } from 'libpg-query';
170170

171171
// These handle initialization automatically
172-
const result = await parseQuery('SELECT * FROM users');
173-
const sql = await deparse(result[0]);
172+
const result = await parse('SELECT * FROM users');
173+
const sql = await deparse(result);
174174
```
175175

176176
#### Sync Methods
177177

178178
Sync methods require explicit initialization using `loadModule()`:
179179

180180
```typescript
181-
import { loadModule, parseQuerySync } from 'libpg-query';
181+
import { loadModule, parseSync } from 'libpg-query';
182182

183183
// Initialize first
184184
await loadModule();
185185

186186
// Now safe to use sync methods
187-
const result = parseQuerySync('SELECT * FROM users');
187+
const result = parseSync('SELECT * FROM users');
188188
```
189189

190190
### `loadModule(): Promise<void>`
191191

192192
Explicitly initializes the WASM module. Required before using any sync methods.
193193

194194
```typescript
195-
import { loadModule, parseQuerySync } from 'libpg-query';
195+
import { loadModule, parseSync } from 'libpg-query';
196196

197197
// Initialize before using sync methods
198198
await loadModule();
199-
const result = parseQuerySync('SELECT * FROM users');
199+
const result = parseSync('SELECT * FROM users');
200200
```
201201

202202
Note: We recommend using async methods as they handle initialization automatically. Use sync methods only when necessary, and always call `loadModule()` first.

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function ptrToString(ptr: number): string {
5656
return wasmModule.UTF8ToString(ptr);
5757
}
5858

59-
export const parseQuery = awaitInit(async (query: string): Promise<ParseResult> => {
59+
export const parse = awaitInit(async (query: string): Promise<ParseResult> => {
6060
const queryPtr = stringToPtr(query);
6161
let resultPtr = 0;
6262

@@ -170,7 +170,7 @@ export const normalize = awaitInit(async (query: string): Promise<string> => {
170170
});
171171

172172
// Sync versions
173-
export function parseQuerySync(query: string): ParseResult {
173+
export function parseSync(query: string): ParseResult {
174174
if (!wasmModule) {
175175
throw new Error('WASM module not initialized. Call loadModule() first.');
176176
}

test/deparsing.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ const { expect } = require("chai");
33

44
describe("Query Deparsing", () => {
55
before(async () => {
6-
await query.parseQuery("SELECT 1");
6+
await query.parse("SELECT 1");
77
});
88

99
describe("Sync Deparsing", () => {
1010
it("should deparse a simple query", () => {
1111
const sql = 'SELECT * FROM users';
12-
const parseTree = query.parseQuerySync(sql);
12+
const parseTree = query.parseSync(sql);
1313
const deparsed = query.deparseSync(parseTree);
1414
expect(deparsed).to.equal(sql);
1515
});
1616

1717
it("should deparse a complex query", () => {
1818
const sql = 'SELECT a, b, c FROM t1 JOIN t2 ON t1.id = t2.id WHERE t1.x > 10';
19-
const parseTree = query.parseQuerySync(sql);
19+
const parseTree = query.parseSync(sql);
2020
const deparsed = query.deparseSync(parseTree);
2121
expect(deparsed).to.equal(sql);
2222
});
@@ -29,7 +29,7 @@ describe("Query Deparsing", () => {
2929
describe("Async Deparsing", () => {
3030
it("should return a promise resolving to same result", async () => {
3131
const sql = 'SELECT * FROM users';
32-
const parseTree = await query.parseQuery(sql);
32+
const parseTree = await query.parse(sql);
3333
const deparsed = await query.deparse(parseTree);
3434
expect(deparsed).to.equal(sql);
3535
});
@@ -47,15 +47,15 @@ describe("Query Deparsing", () => {
4747
describe("Round-trip parsing and deparsing", () => {
4848
it("should maintain query semantics through round-trip", async () => {
4949
const sql = 'SELECT a, b, c FROM t1 JOIN t2 ON t1.id = t2.id WHERE t1.x > 10';
50-
const parseTree = await query.parseQuery(sql);
50+
const parseTree = await query.parse(sql);
5151
const deparsed = await query.deparse(parseTree);
5252
expect(deparsed).to.equal(sql);
5353
});
5454
});
5555

5656
it('should deparse a parse tree', async () => {
5757
const sql = 'SELECT * FROM users';
58-
const parseTree = await query.parseQuery(sql);
58+
const parseTree = await query.parse(sql);
5959
const deparsed = await query.deparse(parseTree);
6060
expect(deparsed).to.equal(sql);
6161
});

test/fingerprint.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { expect } = require("chai");
33

44
describe("Query Fingerprinting", () => {
55
before(async () => {
6-
await query.parseQuery("SELECT 1");
6+
await query.parse("SELECT 1");
77
});
88

99
describe("Sync Fingerprinting", () => {

test/normalize.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { expect } = require("chai");
33

44
describe("Query Normalization", () => {
55
before(async () => {
6-
await query.parseQuery("SELECT 1");
6+
await query.parse("SELECT 1");
77
});
88

99
describe("Sync Normalization", () => {

test/parsing.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ function removeLocationProperties(obj) {
2424

2525
describe("Query Parsing", () => {
2626
before(async () => {
27-
await query.parseQuery("SELECT 1");
27+
await query.parse("SELECT 1");
2828
});
2929

3030
describe("Sync Parsing", () => {
3131
it("should return a single-item parse result for common queries", () => {
3232
const queries = ["select 1", "select null", "select ''", "select a, b"];
33-
const results = queries.map(query.parseQuerySync);
33+
const results = queries.map(query.parseSync);
3434
results.forEach((res) => {
3535
expect(res.stmts).to.have.lengthOf(1);
3636
});
@@ -46,30 +46,30 @@ describe("Query Parsing", () => {
4646
});
4747

4848
it("should support parsing multiple queries", () => {
49-
const res = query.parseQuerySync("select 1; select null;");
49+
const res = query.parseSync("select 1; select null;");
5050
expect(res.stmts.map(removeLocationProperties)).to.deep.eq([
51-
...query.parseQuerySync("select 1;").stmts.map(removeLocationProperties),
52-
...query.parseQuerySync("select null;").stmts.map(removeLocationProperties),
51+
...query.parseSync("select 1;").stmts.map(removeLocationProperties),
52+
...query.parseSync("select null;").stmts.map(removeLocationProperties),
5353
]);
5454
});
5555

5656
it("should not parse a bogus query", () => {
57-
expect(() => query.parseQuerySync("NOT A QUERY")).to.throw(Error);
57+
expect(() => query.parseSync("NOT A QUERY")).to.throw(Error);
5858
});
5959
});
6060

6161
describe("Async parsing", () => {
6262
it("should return a promise resolving to same result", async () => {
6363
const testQuery = "select * from john;";
64-
const resPromise = query.parseQuery(testQuery);
64+
const resPromise = query.parse(testQuery);
6565
const res = await resPromise;
6666

6767
expect(resPromise).to.be.instanceof(Promise);
68-
expect(res).to.deep.eq(query.parseQuerySync(testQuery));
68+
expect(res).to.deep.eq(query.parseSync(testQuery));
6969
});
7070

7171
it("should reject on bogus queries", async () => {
72-
return query.parseQuery("NOT A QUERY").then(
72+
return query.parse("NOT A QUERY").then(
7373
() => {
7474
throw new Error("should have rejected");
7575
},

test/plpgsql.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { expect } = require("chai");
33

44
describe("PL/pgSQL Parsing", () => {
55
before(async () => {
6-
await query.parseQuery("SELECT 1");
6+
await query.parse("SELECT 1");
77
});
88

99
describe("Sync PL/pgSQL Parsing", () => {

wasm/index.cjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1717
return (mod && mod.__esModule) ? mod : { "default": mod };
1818
};
1919
Object.defineProperty(exports, "__esModule", { value: true });
20-
exports.normalize = exports.fingerprint = exports.parsePlPgSQL = exports.deparse = exports.parseQuery = void 0;
20+
exports.normalize = exports.fingerprint = exports.parsePlPgSQL = exports.deparse = exports.parse = void 0;
2121
exports.loadModule = loadModule;
22-
exports.parseQuerySync = parseQuerySync;
22+
exports.parseSync = parseSync;
2323
exports.deparseSync = deparseSync;
2424
exports.parsePlPgSQLSync = parsePlPgSQLSync;
2525
exports.fingerprintSync = fingerprintSync;
@@ -59,7 +59,7 @@ function stringToPtr(str) {
5959
function ptrToString(ptr) {
6060
return wasmModule.UTF8ToString(ptr);
6161
}
62-
exports.parseQuery = awaitInit(async (query) => {
62+
exports.parse = awaitInit(async (query) => {
6363
const queryPtr = stringToPtr(query);
6464
let resultPtr = 0;
6565
try {
@@ -156,7 +156,7 @@ exports.normalize = awaitInit(async (query) => {
156156
}
157157
});
158158
// Sync versions
159-
function parseQuerySync(query) {
159+
function parseSync(query) {
160160
if (!wasmModule) {
161161
throw new Error('WASM module not initialized. Call loadModule() first.');
162162
}

wasm/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ParseResult } from "@pgsql/types";
22
export * from "@pgsql/types";
33
export declare function loadModule(): Promise<void>;
4-
export declare const parseQuery: (query: string) => Promise<ParseResult>;
4+
export declare const parse: (query: string) => Promise<ParseResult>;
55
export declare const deparse: (parseTree: ParseResult) => Promise<string>;
66
export declare const parsePlPgSQL: (query: string) => Promise<ParseResult>;
77
export declare const fingerprint: (query: string) => Promise<string>;
88
export declare const normalize: (query: string) => Promise<string>;
9-
export declare function parseQuerySync(query: string): ParseResult;
9+
export declare function parseSync(query: string): ParseResult;
1010
export declare function deparseSync(parseTree: ParseResult): string;
1111
export declare function parsePlPgSQLSync(query: string): ParseResult;
1212
export declare function fingerprintSync(query: string): string;

wasm/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function stringToPtr(str) {
3333
function ptrToString(ptr) {
3434
return wasmModule.UTF8ToString(ptr);
3535
}
36-
export const parseQuery = awaitInit(async (query) => {
36+
export const parse = awaitInit(async (query) => {
3737
const queryPtr = stringToPtr(query);
3838
let resultPtr = 0;
3939
try {
@@ -130,7 +130,7 @@ export const normalize = awaitInit(async (query) => {
130130
}
131131
});
132132
// Sync versions
133-
export function parseQuerySync(query) {
133+
export function parseSync(query) {
134134
if (!wasmModule) {
135135
throw new Error('WASM module not initialized. Call loadModule() first.');
136136
}

0 commit comments

Comments
 (0)
0