JSPM

  • Created
  • Published
  • Downloads 779088
  • Score
    100M100P100Q191019F
  • License MIT

Minifies PostgreSQL scripts.

Package Exports

  • pg-minify

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pg-minify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

pg-minify

Minifies PostgreSQL scripts.

Build Status Coverage Status Downloads Count Join the chat at https://gitter.im/vitaly-t/pg-minify

Features:

  • Removes both /*multi-line*/ and --single-line comments
  • Preserves special multi-line comments that start with /*!
  • Concatenates multi-line strings into a single line with \n
  • Fixes multi-line text, prefixing it with E where needed
  • Removes redundant line gaps: line breaks, tabs and spaces
  • Provides basic parsing and error reporting for invalid SQL
  • Flattens the resulting script into a single line
  • Optionally, compresses SQL for minimum space

This library is originally for PostgreSQL, though it works for MS-SQL and MySQL also.

Limitations:

  • Multi-line quoted identifiers are not supported, throwing an error when encountered.
  • Nested multi-line comments are not supported, throwing an error when encountered.

Installing

$ npm install pg-minify

Usage

var minify = require('pg-minify');

var sql = 'SELECT 1; -- comments';

minify(sql); //=> SELECT 1;

with compression (removes all unnecessary spaces):

var sql = 'SELECT * FROM "table" WHERE col = 123; -- comments';

minify(sql, {compress: true});
//=> SELECT*FROM"table"WHERE col=123;

The library's distribution includes TypeScript declarations.

Error Handling

SQLParsingError is thrown on failed SQL parsing:

try {
    minify('SELECT \'1');
} catch (error) {
    // error is minify.SQLParsingError instance
    // error.message:
    // Error parsing SQL at {line:1,col:8}: Unclosed text block.
}

API

minify(sql, [options]) ⇒ String

Minifies SQL into a single line, according to the options.

options.compress ⇒ Boolean

Compresses / uglifies the SQL to its bare minimum, by removing all unnecessary spaces.

  • false (default) - keep minimum spaces, for easier read
  • true - remove all unnecessary spaces

Testing

First, clone the repository and install DEV dependencies.

$ npm test

Testing with coverage:

$ npm run coverage

License

Copyright © 2018 Vitaly Tomilov; Released under the MIT license.