
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
pgsql-deparser
Advanced tools
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
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:
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
?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:
pgsql-parser
.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.
FAQs
PostgreSQL AST Deparser
The npm package pgsql-deparser receives a total of 44,251 weekly downloads. As such, pgsql-deparser popularity was classified as popular.
We found that pgsql-deparser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.