Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pgparser

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pgparser

Parse PostgreSQL statements in plain Javascript

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

node-pgparser

Parse PostgreSQL statements in Javascript!

pgparser is basically just lib_pgquery ported to Javascript using Emscripten, with a tiny bit of glue! It is a direct port of PostgreSQL's query parser. The output format is a JSON representation of the parse tree.

Note that this is very similar to pg-query-parser. It has the advantage of being plain JS, so it should run fine in browsers (although it's quite large). The drawback of this package is that I haven't implemented a way to transform the parse tree back into SQL (yet!).

Usage

Currently, pgparser simply parses queries into a parse tree. It returns a Promise when invoked:

const pgparser = require('pgparser');

let sql = `
    SELECT
        id,
        first_name
    FROM
        users
`;

pgparser.parse(sql).then((parse_tree) => {
    console.log(parse_tree);
}, (error) => {
    console.warn(`${error.message} near character ${error.cursorpos}`);
});

The parse tree for the sample query is below:

[
  {
    "SelectStmt": {
      "targetList": [
        {
          "ResTarget": {
            "val": {
              "ColumnRef": {
                "fields": [
                  {
                    "String": {
                      "str": "id"
                    }
                  }
                ],
                "location": 19
              }
            },
            "location": 19
          }
        },
        {
          "ResTarget": {
            "val": {
              "ColumnRef": {
                "fields": [
                  {
                    "String": {
                      "str": "first_name"
                    }
                  }
                ],
                "location": 31
              }
            },
            "location": 31
          }
        }
      ],
      "fromClause": [
        {
          "RangeVar": {
            "relname": "users",
            "inhOpt": 2,
            "relpersistence": "p",
            "location": 59
          }
        }
      ],
      "op": 0
    }
  }
]

Keywords

FAQs

Package last updated on 24 Jul 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc