UNPKG

2.96 kBMarkdownView Raw
1pg-minify
2=========
3
4Minifies PostgreSQL scripts.
5
6[![Build Status](https://travis-ci.org/vitaly-t/pg-minify.svg?branch=master)](https://travis-ci.org/vitaly-t/pg-minify)
7[![Coverage Status](https://coveralls.io/repos/vitaly-t/pg-minify/badge.svg?branch=master)](https://coveralls.io/r/vitaly-t/pg-minify?branch=master)
8[![Downloads Count](http://img.shields.io/npm/dm/pg-minify.svg)](https://www.npmjs.com/package/pg-minify)
9[![Join the chat at https://gitter.im/vitaly-t/pg-minify](https://badges.gitter.im/vitaly-t/pg-minify.svg)](https://gitter.im/vitaly-t/pg-minify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
10
11**Features:**
12
13* Removes both `/*multi-line*/` and `--single-line` comments
14* Preserves special multi-line comments that start with `/*!`
15* Concatenates multi-line strings into a single line with `\n`
16* Fixes multi-line text, prefixing it with `E` where needed
17* Removes redundant line gaps: line breaks, tabs and spaces
18* Provides basic parsing and error reporting for invalid SQL
19* Flattens the resulting script into a single line
20* Optionally, compresses SQL for minimum space
21
22This library is originally for PostgreSQL, though it works for most of MS-SQL and MySQL.
23
24**Limitations:**
25
26* Multi-line quoted identifiers are not supported, throwing an error when encountered.
27* Nested multi-line comments are not supported, throwing an error when encountered.
28
29## Installing
30
31```
32$ npm install pg-minify
33```
34
35## Usage
36
37```js
38const minify = require('pg-minify');
39
40const sql = 'SELECT 1; -- comments';
41
42minify(sql); //=> SELECT 1;
43```
44
45with compression (removes all unnecessary spaces):
46
47```js
48const sql = 'SELECT * FROM "table" WHERE col = 123; -- comments';
49
50minify(sql, {compress: true});
51//=> SELECT*FROM"table"WHERE col=123;
52```
53
54The library's distribution includes [TypeScript] declarations.
55
56## Error Handling
57
58[SQLParsingError] is thrown on failed SQL parsing:
59
60```js
61try {
62 minify('SELECT \'1');
63} catch (error) {
64 // error is minify.SQLParsingError instance
65 // error.message:
66 // Error parsing SQL at {line:1,col:8}: Unclosed text block.
67}
68```
69
70## API
71
72### minify(sql, [options]) ⇒ String
73
74Minifies SQL into a single line, according to the `options`.
75
76##### options.compress ⇒ Boolean
77
78Compresses / uglifies the SQL to its bare minimum, by removing all unnecessary spaces.
79
80* `false (default)` - keep minimum spaces, for easier read
81* `true` - remove all unnecessary spaces
82
83## Testing
84
85First, clone the repository and install DEV dependencies.
86
87```
88$ npm test
89```
90
91Testing with coverage:
92```
93$ npm run coverage
94```
95
96## License
97
98Copyright © 2019 [Vitaly Tomilov](https://github.com/vitaly-t);
99Released under the MIT license.
100
101[SQLParsingError]:https://github.com/vitaly-t/pg-minify/blob/master/lib/error.js#L24
102[TypeScript]:https://github.com/vitaly-t/pg-minify/tree/master/typescript