pgsql-deparser is the lightning-fast, pure TypeScript solution for converting PostgreSQL ASTs back into SQL queries. Perfect companion to pgsql-parser, this focused tool delivers SQL generation without any native dependencies or WebAssembly overhead.
Installation
npm install pgsql-deparser
Features
- โก Pure TypeScript Performance โ Zero runtime dependencies, no WASM, no compilation - just blazing fast SQL generation
- ๐ชถ Ultra Lightweight โ Minimal footprint with laser-focused functionality for AST-to-SQL conversion only
- ๐งช Battle-Tested Reliability โ Validated against 23,000+ SQL statements ensuring production-grade stability
- ๐ Universal Compatibility โ Runs anywhere JavaScript does - browsers, Node.js, edge functions, you name it
Deparser Example
The pgsql-deparser module serializes ASTs to SQL in pure TypeScript, avoiding the full parser's native dependencies. It's useful when only SQL string conversion from ASTs is needed, and is written in pure TypeScript for easy cross-environment deployment.
Here's how you can use the deparser in your TypeScript code, using @pgsql/utils to create an AST for deparse:
import * as t from '@pgsql/utils'; import { RangeVar, SelectStmt } from '@pgsql/types'; import { deparseSync as deparse } from 'pgsql-deparser'; // This could have been obtained from any JSON or AST, not necessarily @pgsql/utils const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({ targetList: [ t.nodes.resTarget({ val: t.nodes.columnRef({ fields: [t.nodes.aStar()] }) }) ], fromClause: [ t.nodes.rangeVar({ relname: 'some_table', inh: true, relpersistence: 'p' }) ], limitOption: 'LIMIT_OPTION_DEFAULT', op: 'SETOP_NONE' }); // Modify the AST if needed (stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table'; // Deparse the modified AST back to a SQL string console.log(deparse(stmt)); // Output: SELECT * FROM another_table
Latest Version (PostgreSQL 17)
npm install pgsql-deparser
Version-Specific Packages (PostgreSQL 13-16)
While we highly recommend using PG17, for PostgreSQL versions 13-16, use the version-specific packages:
npm install pgsql-deparser@pg13 # PostgreSQL 13 npm install pgsql-deparser@pg14 # PostgreSQL 14 npm install pgsql-deparser@pg15 # PostgreSQL 15 npm install pgsql-deparser@pg16 # PostgreSQL 16
Version Status:
- PG17: ๐ Recommended (stable + modern AST)
- PG14-16: โ ๏ธ Experimental (modern AST, hardening in progress)
- PG13: Stable (legacy AST format)
Options
The deparser accepts optional configuration for formatting and output control:
import { deparseSync as deparse } from 'pgsql-deparser'; const options = { pretty: true, // Enable pretty formatting (default: true) newline: '\n', // Newline character (default: '\n') tab: ' ', // Tab/indentation character (default: ' ') semicolons: true // Add semicolons to statements (default: true) }; const sql = deparse(ast, options);
| Option | Type | Default | Description |
|---|---|---|---|
pretty |
boolean |
true |
Enable pretty formatting with indentation and line breaks |
newline |
string |
'\n' |
Character(s) used for line breaks |
tab |
string |
' ' |
Character(s) used for indentation |
semicolons |
boolean |
true |
Add semicolons to SQL statements |
Pretty formatting example:
// Without pretty formatting const sql1 = deparse(selectAst, { pretty: false }); // "SELECT id, name FROM users WHERE active = true;" // With pretty formatting const sql2 = deparse(selectAst, { pretty: true }); // SELECT // id, // name // FROM users // WHERE // active = true;
For complete documentation and advanced options, see DEPARSER_USAGE.md.
Why Use pgsql-deparser?
pgsql-deparser is particularly useful in development environments where native dependencies are problematic or in applications where only the deparser functionality is required. Its independence from the full pgsql-parser package allows for more focused and lightweight SQL generation tasks.
Credits
Built on the excellent work of several contributors:
- Dan Lynch โ official maintainer since 2018 and architect of the current implementation
- Lukas Fittl for libpg_query โ the core PostgreSQL parser that powers this project
- Greg Richardson for AST guidance and pushing the transition to WASM and multiple PG runtimes for better interoperability
- Ethan Resnick for the original Node.js N-API bindings
- Zac McCormick for the foundational node-pg-query-native parser