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.
npm install pgsql-deparser
- ⚡ 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
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
npm install pgsql-deparser
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)
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.
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.
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
- pgsql-parser: The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
-
pgsql-deparser: A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement
pgsql-parser
. - @pgsql/parser: Multi-version PostgreSQL parser with dynamic version selection at runtime, supporting PostgreSQL 15, 16, and 17 in a single package.
- @pgsql/types: Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
- @pgsql/enums: Provides TypeScript enum definitions for PostgreSQL constants, enabling type-safe usage of PostgreSQL enums and constants in your applications.
- @pgsql/utils: A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.
- @pgsql/traverse: PostgreSQL AST traversal utilities for pgsql-parser, providing a visitor pattern for traversing PostgreSQL Abstract Syntax Tree nodes, similar to Babel's traverse functionality but specifically designed for PostgreSQL AST structures.
- pg-proto-parser: A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
-
libpg-query: The real PostgreSQL parser exposed for Node.js, used primarily in
pgsql-parser
for parsing and deparsing SQL queries.
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.