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

pgsql-minify

v1.0.7

Published

Generate a consistently formatted, minified version of any valid postgres SQL

Downloads

90

Readme

pgsql-minify

A simple typescript library to generate a minified version of a valid Postgres SQL statement.

The minifier works by breaking the raw SQL statement into a collection of 'tokens' using a simple lexical analyzer (lexer), and then reconstructs the statement from the tokens using standardised case and spacing.

Installation

npm install pgsql-minify

Or

yarn add pgsql-minify

Quickstart

Typescript

import { minify } from 'pgsql-minify';
:
const rawSQL = `select col1, col2,\n\tcol3\nFROM\tsome_table;`;
const niceSQL = minify(rawSQL); // select col1, col2, col3 from some_table;

Javascript

const pgmin = require('pgsql-minify')
:
const rawSQL = 'select    abc  from       def   ;   ';
const niceSQL = pgmin.minify(rawSQL); // select abc from def;

API and Components

In addition to the minify function seen above, the package also exposes a number of other components that may be useful if you want to roll your own minify function.

TokenType

A single character string representing the 'type' of a token discovered while parsing a raw SQL string. One of:

  • (
  • )
  • [
  • ]
  • ,
  • ;
  • .
  • ..
  • :
  • ::
  • k (keyword)
  • i (identifier)
  • o (operator)
  • ' (string constant)
  • 0 (numeric constant)
  • " (quoted identifier)
  • $ (positional parameter)
  • b (binary bit-string)
  • x (hex bit-string)
  • c (comment)

Token

A Tuple comprised of a TokenType and an optional string holding the 'value' of the token. From the above list, token types k, i, o, ', 0, ", $, b, x, and c will have a value.

PgSqlMinifyOptions

An interface defining options that may be passed to the minify or lex functions. It contains the following (optional) members:

  • keywords - a Set of string values defining the subset of identifiers that are considered keywords. The main difference between a keyword and an identifier is that the minify function will insert a space between a keyword and a ( token, but will not insert a space between and identifier and a ( token.
  • includeComments - a boolean value that defaults to false that controls whether the minify function includes comment tokens or skips them. Because the minify function attempts to generate SQL without newline characters, any 'to end of line' comments are converted to C-style comments, newlines and tabs are converted to a single space, and consecutive whitespace characters are ignored.
  • includeTrailingSemicolon - a boolean value that defaults to true that controls whether to include any trailing semicolon in the minified string. If true, any statement that (for whatever reason) only includes a semicolon is left alone.

lex

A function that analyzes a raw (valid) SQL string and returns an array of Token tuples. For example:

import { lex } from 'pgsql-minify';
:
const rawSQL = 'select    abc  from       def   ;   ';
const tokens = lex(rawSQL); // [[ 'k', 'select' ], [ 'i', 'abc' ], [ 'k', 'from' ], [ 'i', 'def' ], [ ';' ]]
:

The function takes an instance of PgSqlMinifyOptions as an optional second parameter to allow the default set of keywords to be overridden.

minify

A function that converts a raw (valid) SQL string into a standardised, 'minified' format with minimal whitespace. Here are some examples of actual output from the minify function:

(before)


select  col1,  col2,col3,
        col4, ( col5+(col6*col7)  )
from    sometable as t1
    join  othertable as t2
        on  t2.st_id = t1.id
;

(after)

select col1, col2, col3, col4, (col5 + (col6 * col7)) from sometable as t1 join othertable as t2 on t2.st_id = t1.id;

(before)

select ts -- This is the row creation timestamp
from   blah
Order
BY     1
;

(after - excluding comments)

select ts from blah order by 1;

(after - including comments)

select ts /* This is the row creation timestamp */ from blah order by 1;

Maintainer