16.1.6 • Published 2 days ago

node-pgparser v16.1.6

Weekly downloads
5
License
MIT
Repository
github
Last release
2 days ago

node-pgparser

node-pgparser - Node.js wrapper for libpg_query C library.

It parses the query and returns the parse tree or error object.

Installation

If you use a corporate certificate to internet access please add it to node first:

# linux and macos
export NODE_EXTRA_CA_CERTS=/path/to/cert

# windows
set NODE_EXTRA_CA_CERTS=/path/to/cert

then run the command

npm install node-pgparser

Uses node-pre-gyp to download prebuild binaries from S3 Amazon.

Currently supports Nodejs v16,v18,v20 for Linux, Macos (x64 and arm64) and Windows.

Example

Parse SQL query

const pgparser = require('node-pgparser');

let tree = pgparser('select 1');
if (tree instanceof Error) {
  console.log('Error parsing: ', tree);
} else {
  console.dir(tree, {depth: null});
}

Result

{
  version: 160001,
    stmts: [
    {
      stmt: {
        SelectStmt: {
          targetList: [
            {
              ResTarget: {
                val: { A_Const: { ival: { ival: 1 }, location: 7 } },
                location: 7
              }
            }
          ],
          limitOption: 'LIMIT_OPTION_DEFAULT',
          op: 'SETOP_NONE'
        }
      }
    }
  ]
}

Parse PL/pgSQL

const pgparser = require('node-pgparser');

let tree = pgparser('DO $$ BEGIN SELECT 1; END$$;', true);
if (tree instanceof Error) {
  console.log('Error parsing PL/pgSQL: ', tree);
} else {
  console.dir(tree, {depth: null});
}

Result

[
  {
    PLpgSQL_function: {
      datums: [
        {
          PLpgSQL_var: {
            refname: 'found',
            datatype: { PLpgSQL_type: { typname: 'UNKNOWN' } }
          }
        }
      ],
      action: {
        PLpgSQL_stmt_block: {
          lineno: 1,
          body: [
            {
              PLpgSQL_stmt_execsql: {
                lineno: 1,
                sqlstmt: { PLpgSQL_expr: { query: 'SELECT 1', parseMode: 0 } }
              }
            },
            { PLpgSQL_stmt_return: {} }
          ]
        }
      }
    }
  }
]

Versions

PG Major Versionlibpg_queryBranchnpm
1616-latestpg16node-pgparser@16
1515-latestpg15node-pgparser@15
1414-latestpg14node-pgparser@14
1313-latestpg13node-pgparser@13
16.1.6

2 days ago

16.1.5

2 days ago

16.1.4

3 days ago

16.1.1

24 days ago

16.1.3

24 days ago

16.0.3

1 month ago

16.0.2

1 month ago

14.0.1

1 month ago

16.0.1

1 month ago

13.0.1

1 month ago

15.0.1

1 month ago

2.1.3

1 month ago

2.0.0

3 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago