npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

libpg-query-wasm

v0.2.1

Published

Webassembly bindings for libpg_query

Downloads

24

Readme

libpg-query-wasm

npm version

Webassembly bindings and Typescript types for libpg_query.

npm install libpg-query-wasm

API

parse(query: string): ParseResult

Parses a query and returns the parse tree as a Protocol Buffer message.

Usage in libpg_query documentation.

import { parse, ParseResult } from "libpg-query-wasm";
import { strict as assert } from "node:assert";

const message = parse("SELECT 1");
assert.deepEqual(ParseResult.toObject(message, { enums: String }), {
  stmts: [
    {
      stmt: {
        selectStmt: {
          limitOption: "LIMIT_OPTION_DEFAULT",
          op: "SETOP_NONE",
          targetList: [
            {
              resTarget: {
                location: 7,
                val: { aConst: { ival: { ival: 1 }, location: 7 } },
              },
            },
          ],
        },
      },
    },
  ],
  version: 150001,
});

fingerprint(query: string): string

Parses a query and returns the fingerprint of the parse tree as a hex string. Fingerprinting allows you to identify similar queries.

Usage in libpg_query documentation.

import { fingerprint } from "libpg-query-wasm";
import { strict as assert } from "node:assert";

const hex = fingerprint("select 1");
assert.equal(hex, "50fde20626009aba");

scan(query: string): ScanResult

Scans a query and returns the tokens as a Protocol Buffer message.

Usage in libpg_query documentation.

import { scan, ScanResult } from "libpg-query-wasm";
import { strict as assert } from "node:assert";

const message = scan("select * from table -- comment");
assert.deepEqual(ScanResult.toObject(message, { enums: String }), {
  version: 150001,
  tokens: [
    { end: 6, token: "SELECT", keywordKind: "RESERVED_KEYWORD" },
    { start: 7, end: 8, token: "ASCII_42" },
    { start: 9, end: 13, token: "FROM", keywordKind: "RESERVED_KEYWORD" },
    { start: 14, end: 19, token: "TABLE", keywordKind: "RESERVED_KEYWORD" },
    { start: 20, end: 30, token: "SQL_COMMENT" },
  ],
});

parsePLpgSQL(functionSource: string): Array<Record<string, any>>

Parse a PL/pgSQL functions from given source and return the parse trees.

Usage in libpg_query documentation.

import { parsePLpgSQL } from "libpg-query-wasm";
import { strict as assert } from "node:assert";

const functions = parsePLpgSQL(`
CREATE FUNCTION f() RETURNS integer AS $$
BEGIN
    RETURN 1;
END;
$$ LANGUAGE plpgsql;`);
assert.deepEqual(functions, [
  {
    PLpgSQL_function: {
      action: {
        PLpgSQL_stmt_block: {
          body: [
            {
              PLpgSQL_stmt_return: {
                expr: { PLpgSQL_expr: { query: "1" } },
                lineno: 3,
              },
            },
          ],
          lineno: 2,
        },
      },
      datums: [
        {
          PLpgSQL_var: {
            datatype: { PLpgSQL_type: { typname: "UNKNOWN" } },
            refname: "found",
          },
        },
      ],
    },
  },
]);

Using protocol buffers

Some functions return a Protocol Buffer message instance. Using these messages gives you access to the original fields with the correct types. If you'd rather use plain objects you can use the [toObject][to-object] static method to convert the message.

We use the [protobuf.js][protobufjs] library to decode the messages returned by libpg_query. It's also used to generate the Typescript types. You can find more information about the usage of the messages in the [protobufjs documentation][protobufjs-usage].

The full generated protobufjs module and Typescript types are available as the pg_query export from this package.

import { scan, pg_query } from "libpg-query-wasm";
import { strict as assert } from "node:assert";

const message = scan("select 1");
assert.equal(message.tokens[0].token, pg_query.Token.SELECT);

[to-object]: https://github.com/protobufjs/protobuf.js#toolset:~:text=to%20a%20string-,Message.toObject,-(message%3A%20Message [protobufjs]: https://github.com/protobufjs/protobuf.js#readme [protobufjs-usage]: https://github.com/protobufjs/protobuf.js#usage