Gathering detailed insights and metrics for @pgsql/types
Gathering detailed insights and metrics for @pgsql/types
Gathering detailed insights and metrics for @pgsql/types
Gathering detailed insights and metrics for @pgsql/types
npm install @pgsql/types
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
161 Stars
510 Commits
24 Forks
4 Watching
4 Branches
9 Contributors
Updated on 17 Nov 2024
PLpgSQL (51.59%)
TypeScript (25.47%)
JavaScript (22.95%)
Cumulative downloads
Total Downloads
Last day
-14.6%
3,787
Compared to previous day
Last week
5.2%
22,895
Compared to previous week
Last month
48.8%
91,744
Compared to previous month
Last year
0%
417,822
Compared to previous year
The real PostgreSQL parser for Node.js, pgsql-parser
provides symmetric parsing and deparsing of SQL statements using the actual PostgreSQL parser. It allows you to parse SQL queries into AST and modify or reconstruct SQL queries from the AST.
1npm install pgsql-parser
Rewrite part of a SQL query:
1import { parse, deparse } from 'pgsql-parser'; 2 3const stmts = parse('SELECT * FROM test_table'); 4 5// Assuming the structure of stmts is known and matches the expected type 6stmts[0].RawStmt.stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table'; 7 8console.log(deparse(stmts)); 9 10// SELECT * FROM "another_table"
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
:
1import ast, { SelectStmt } from '@pgsql/utils'; 2import { deparse } from 'pgsql-deparser'; 3 4// This could have been obtained from any JSON or AST, not necessarily @pgsql/utils 5const stmt: SelectStmt = ast.selectStmt({ 6 targetList: [ 7 ast.resTarget({ 8 val: ast.columnRef({ 9 fields: [ast.aStar()] 10 }) 11 }) 12 ], 13 fromClause: [ 14 ast.rangeVar({ 15 relname: 'some_table', 16 inh: true, 17 relpersistence: 'p' 18 }) 19 ], 20 limitOption: 'LIMIT_OPTION_DEFAULT', 21 op: 'SETOP_NONE' 22}); 23 24// Modify the AST if needed 25stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table'; 26 27// Deparse the modified AST back to a SQL string 28console.log(deparse(stmts)); 29 30// Output: SELECT * FROM another_table
npm install -g pgsql-parser
1pgsql-parser <sqlfile>
parser.parse(sql)
Parses the query and returns a parse object.
parameter | type | description |
---|---|---|
query | String | SQL query |
Returns an object in the format:
{ query: <query|Object>,
error: { message: <message|String>,
fileName: <fileName|String>,
lineNumber: <line|Number>,
cursorPosition: <cursor|Number> }
parser.deparse(query)
Deparses the query tree and returns a formatted SQL statement. This function takes as input a query AST
in the same format as the query
property of on the result of the parse
method. This is the primary
functionality of this module.
parameter | type | description |
---|---|---|
query | Object | Query tree obtained from parse |
Returns a normalized formatted SQL string.
As of PG 13, PG majors versions maintained will have a matching dedicated major npm version. Only the latest Postgres stable release receives active updates.
Our latest is built with 13-latest
branch from libpg_query
PostgreSQL Major Version | libpg_query | Status | npm |
---|---|---|---|
13 | 13-latest | Active development | latest |
12 | (n/a) | Not supported | |
11 | (n/a) | Not supported | |
10 | 10-latest | Not supported | @1.3.1 (tree) |
pgsql-parser
.pgsql-parser
pgsql-parser
for parsing and deparsing SQL queries.Thanks @lfittl for building the core libpg_query
suite:
Thanks to @zhm for the OG parser that started it all:
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.
No vulnerabilities found.
No security vulnerabilities found.