You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

pgsql-deparser

Package Overview
Dependencies
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pgsql-deparser

PostgreSQL AST Deparser

17.10.0
latest
Source
npmnpm
Version published
Weekly downloads
56K
-18.5%
Maintainers
1
Weekly downloads
 
Created
Source

pgsql-deparser

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);
OptionTypeDefaultDescription
prettybooleantrueEnable pretty formatting with indentation and line breaks
newlinestring'\n'Character(s) used for line breaks
tabstring' 'Character(s) used for indentation
semicolonsbooleantrueAdd 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:

  • 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.

Disclaimer

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.

Keywords

sql

FAQs

Package last updated on 23 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts