The real PostgreSQL parser for Node.js, powered by WebAssembly (WASM) for true cross-platform compatibility.
A WASM-based PostgreSQL query parser that provides the same functionality as the native PostgreSQL parser without requiring native compilation or platform-specific binaries. Primarily used for the node.js parser and deparser pgsql-parser.
npm install libpg-query
import { parseQuery, parseQuerySync } from 'libpg-query';
// Async usage (recommended)
const result = await parseQuery('SELECT * FROM users WHERE id = $1');
console.log(result);
// Sync usage
const syncResult = parseQuerySync('SELECT * FROM users WHERE id = $1');
console.log(syncResult);
const { parseQuery, parseQuerySync } = require('libpg-query');
parseQuery('SELECT * FROM users WHERE id = $1').then(console.log);
This package uses a WASM-only build system for true cross-platform compatibility without native compilation dependencies.
- Node.js (version 16 or higher recommended)
- Docker (for WASM compilation using Emscripten)
- yarn or npm
-
Install dependencies:
npm install # or yarn install
-
Build WASM artifacts:
npm run wasm:build # or yarn wasm:build
-
Clean WASM build (if needed):
npm run wasm:clean # or yarn wasm:clean
-
Rebuild WASM artifacts from scratch:
npm run wasm:clean && npm run wasm:build # or yarn wasm:clean && yarn wasm:build
The WASM build process:
- Uses Docker with Emscripten SDK for compilation
- Compiles C wrapper code to WebAssembly
- Generates
wasm/libpg-query.js
andwasm/libpg-query.wasm
files - No native compilation or node-gyp dependencies required
npm test
# or
yarn test
- WASM artifacts must be built before running tests
- If tests fail with "fetch failed" errors, rebuild WASM artifacts:
npm run wasm:clean && npm run wasm:build && npm test
Parses the SQL and returns a Promise for the parse tree. May reject with a parse error.
import { parseQuery } from 'libpg-query';
const result = await parseQuery('SELECT * FROM users WHERE active = true');
// Returns: ParseResult[] - array of parsed query objects
Synchronous version that returns the parse tree directly. May throw a parse error.
import { parseQuerySync } from 'libpg-query';
const result = parseQuerySync('SELECT * FROM users WHERE active = true');
// Returns: ParseResult[] - array of parsed query objects
Parses the contents of a PL/pgSQL function from a CREATE FUNCTION
declaration. Returns a Promise for the parse tree.
import { parsePlPgSQL } from 'libpg-query';
const functionSql = `
CREATE FUNCTION get_user_count() RETURNS integer AS $$
BEGIN
RETURN (SELECT COUNT(*) FROM users);
END;
$$ LANGUAGE plpgsql;
`;
const result = await parsePlPgSQL(functionSql);
Synchronous version of PL/pgSQL parsing.
import { parsePlPgSQLSync } from 'libpg-query';
const result = parsePlPgSQLSync(functionSql);
interface ParseResult {
version: number;
stmts: Statement[];
}
interface Statement {
stmt_type: string;
stmt_len: number;
stmt_location: number;
query: string;
}
Note: The return value is an array, as multiple queries may be provided in a single string (semicolon-delimited, as PostgreSQL expects).
Our latest is built with 17-latest
branch from libpg_query
PG Major Version | libpg_query | Branch | npm |
---|---|---|---|
17 | 17-latest | 17-latest |
[email protected] |
16 | 16-latest | 16-latest |
[email protected] |
15 | 15-latest | 15-latest |
[email protected] |
14 | 14-latest | 14-latest |
[email protected] |
13 | 13-latest | 13-latest |
[email protected] |
12 | (n/a) | ||
11 | (n/a) | ||
10 | 10-latest | @1.3.1 (tree) |
"fetch failed" errors during tests:
- This indicates stale or missing WASM artifacts
- Solution:
npm run wasm:clean && npm run wasm:build
"WASM module not initialized" errors:
- Ensure you call an async method first to initialize the WASM module
- Or use the async versions of methods which handle initialization automatically
Docker permission errors:
- Ensure Docker is running and accessible
- On Linux, you may need to add your user to the docker group
The build process generates these files:
wasm/libpg-query.js
- Emscripten-generated JavaScript loaderwasm/libpg-query.wasm
- WebAssembly binarywasm/index.js
- ES module exportswasm/index.cjs
- CommonJS exports with sync wrappers
This is based on the output of libpg_query. This wraps the static library output and links it into a node module for use in js.
All credit for the hard problems goes to Lukas Fittl.
Additional thanks for the original Node.js integration work by Ethan Resnick.