forked from launchql/libpg-query-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (40 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { getDefaultContext } from '@emnapi/runtime';
import PgQueryModule from './libpg-query.js';
let PgQuery;
const initPromise = PgQueryModule().then((module) => {
const binding = module.emnapiInit({
context: getDefaultContext(),
});
PgQuery = binding;
});
/**
* Function wrapper that waits for the WASM module to initialize
* before executing the function.
*/
function awaitInit(fn) {
return async (...args) => {
await initPromise;
return fn(...args);
};
}
export const parseQuery = awaitInit((query) => {
return new Promise(async (resolve, reject) => {
PgQuery.parseQueryAsync(query, (err, result) => {
err ? reject(err) : resolve(JSON.parse(result));
});
});
});
export const parsePlPgSQL = awaitInit((query) => {
return new Promise(async (resolve, reject) => {
PgQuery.parsePlPgSQLAsync(query, (err, result) => {
err ? reject(err) : resolve(JSON.parse(result));
});
});
});
export const fingerprint = awaitInit((query) => {
return new Promise(async (resolve, reject) => {
PgQuery.fingerprintAsync(query, (err, result) => {
err ? reject(err) : resolve(result);
});
});
});