1 | pg-minify
|
2 | =========
|
3 |
|
4 | Minifies 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 |
|
22 | This 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
|
38 | const minify = require('pg-minify');
|
39 |
|
40 | const sql = 'SELECT 1; -- comments';
|
41 |
|
42 | minify(sql); //=> SELECT 1;
|
43 | ```
|
44 |
|
45 | with compression (removes all unnecessary spaces):
|
46 |
|
47 | ```js
|
48 | const sql = 'SELECT * FROM "table" WHERE col = 123; -- comments';
|
49 |
|
50 | minify(sql, {compress: true});
|
51 | //=> SELECT*FROM"table"WHERE col=123;
|
52 | ```
|
53 |
|
54 | The library's distribution includes [TypeScript] declarations.
|
55 |
|
56 | ## Error Handling
|
57 |
|
58 | [SQLParsingError] is thrown on failed SQL parsing:
|
59 |
|
60 | ```js
|
61 | try {
|
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 |
|
74 | Minifies SQL into a single line, according to the `options`.
|
75 |
|
76 | ##### options.compress ⇒ Boolean
|
77 |
|
78 | Compresses / 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 |
|
85 | First, clone the repository and install DEV dependencies.
|
86 |
|
87 | ```
|
88 | $ npm test
|
89 | ```
|
90 |
|
91 | Testing with coverage:
|
92 | ```
|
93 | $ npm run coverage
|
94 | ```
|
95 |
|
96 | ## License
|
97 |
|
98 | Copyright © 2019 [Vitaly Tomilov](https://github.com/vitaly-t);
|
99 | Released 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
|