libpg-query provides Node.js and browser bindings for `libpg_query`, the real PostgreSQL C parser, compiled to WebAssembly (WASM). This library offers 100% spec-accurate parsing of SQL queries into PostgreSQL's internal Abstract Syntax Tree (AST) format, supporting PostgreSQL versions 13 through 17. Its key differentiators include being entirely cross-platform with zero native dependencies, eliminating common `node-gyp` compilation headaches. The package currently ships as version 17.7.3 and generally releases updates in alignment with new PostgreSQL major versions. It is distinct from other parsers by using the actual PostgreSQL source code, ensuring fidelity, and provides full TypeScript support for robust development.
npm install libpg-queryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates both asynchronous and synchronous SQL parsing using `libpg-query`, including a PL/pgSQL function. It highlights the requirement to load the WASM module explicitly for synchronous operations and logs a partial view of the generated Abstract Syntax Tree (AST).
Ensure `await loadModule();` is called before any synchronous parsing function in your application's lifecycle, typically at startup or during module initialization.
When expecting a single query result, access the first element of the returned array, e.g., `const ast = await parse(sql); const firstStatement = ast[0];`.
Evaluate your specific needs. If deparsing or multi-version runtime selection is required, refer to `pgsql-parser` or `@pgsql/parser` which build upon `libpg-query`.
Ensure your build environment (Node.js version, `pnpm` or `npm` version) is up-to-date. If encountering `node-gyp` errors, try rebuilding WASM artifacts with `pnpm run clean && pnpm run build`. For persistent issues, isolate `libpg-query` from other native dependency-heavy packages if possible.
Add `await loadModule();` to your application's startup code or before the first synchronous call. This only needs to be done once per application instance.
Rebuild the WASM artifacts by running `pnpm run clean && pnpm run build` (or `npm run clean && npm run build` if using npm), then retry the tests.
Ensure all necessary build tools for `node-gyp` are installed on your system. For `libpg-query` specifically, ensure you are using the WebAssembly distribution correctly, as it is designed to avoid native compilation. If the error persists, check related issues on the project's GitHub for specific OS/Node.js version solutions.
No dependency data recorded yet.