What is pg-minify?
pg-minify is an npm package designed to minify SQL queries. It removes unnecessary whitespace, comments, and other non-essential elements from SQL code, making it more compact and potentially improving performance when sending queries to a PostgreSQL database.
What are pg-minify's main functionalities?
Minify SQL Queries
This feature allows you to minify a given SQL query by removing unnecessary whitespace and comments. The example demonstrates how to use pg-minify to minify a simple SQL query.
const pgMinify = require('pg-minify');
const sql = 'SELECT * FROM users WHERE id = $1; -- Get user by ID';
const minifiedSQL = pgMinify(sql);
console.log(minifiedSQL); // Outputs: 'SELECT * FROM users WHERE id=$1;'
Error Handling
pg-minify provides error handling capabilities to catch and handle any issues that arise during the minification process. The example shows how to wrap the minification process in a try-catch block to handle potential errors.
const pgMinify = require('pg-minify');
try {
const sql = 'SELECT * FROM users WHERE id = $1; -- Get user by ID';
const minifiedSQL = pgMinify(sql);
console.log(minifiedSQL);
} catch (error) {
console.error('Error minifying SQL:', error);
}
Other packages similar to pg-minify
sql-minify
sql-minify is another npm package that offers similar functionality to pg-minify. It focuses on reducing the size of SQL queries by removing unnecessary whitespace and comments. However, it may not be as optimized for PostgreSQL-specific syntax as pg-minify.
sql-formatter
sql-formatter is a package that primarily focuses on formatting SQL queries to improve readability. While it can also minify SQL by removing unnecessary elements, its main purpose is to beautify SQL code, making it somewhat different from pg-minify, which focuses solely on minification.
pg-minify
Minifies PostgreSQL scripts.
Features:
- Removes
/*multi-line*/
(including nested) and --single-line
comments - Preserves special/copyright 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, but works also for MS-SQL and MySQL.
Installing
$ npm install pg-minify
Usage
const minify = require('pg-minify');
const sql = 'SELECT 1; -- comments';
minify(sql);
with compression (removes all unnecessary spaces):
const sql = 'SELECT * FROM "table" WHERE col = 123; -- comments';
minify(sql, {compress: true});
The library's distribution includes TypeScript declarations.
Error Handling
SQLParsingError is thrown on failed SQL parsing:
try {
minify('SELECT \'1');
} catch (error) {
}
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 readtrue
- remove all unnecessary spaces
See also: SQL Compression.
Testing
First, clone the repository and install DEV dependencies.
$ npm test
Testing with coverage:
$ npm run coverage
License
Copyright © 2019 Vitaly Tomilov;
Released under the MIT license.