Skip to content

Commit

Permalink
adding after option for minification.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Feb 28, 2016
1 parent 2f41ab4 commit 560c129
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
29 changes: 19 additions & 10 deletions lib/queryFile.js
Expand Up @@ -35,19 +35,24 @@ var format = require('./formatting').as.format;
* The default for this property is `true` when `NODE_ENV` = `development`,
* or `false` otherwise.
*
* @param {Boolean} [options.minify=false]
* Parses and minifies the SQL using $[pg-minify].
* @param {Boolean|String} [options.minify=false]
* Parses and minifies the SQL using $[pg-minify]:
* - `false` - do not use $[pg-minify]
* - `true` - use $[pg-minify] to parse and minify SQL
* - `'after'` - use $[pg-minify] after applying static formatting parameters
* (option `params`), as opposed to before it (default)
*
* If option `compress` is set, then the default for `minify` is also `true`.
* If option `compress` is set, then the default for `minify` is `true`.
*
* Failure to parse SQL will result in $[SQLParsingError].
*
* @param {Boolean} [options.compress=false]
* Sets option `compress` as supported by $[pg-minify], to uglify the SQL.
* Sets option `compress` as supported by $[pg-minify], to uglify the SQL:
* - `false` - no compression to be applied, keep minimum spaces for easier read
* - `true` - remove all unnecessary spaces from SQL
*
* This option has no meaning, if `minify` is explicitly set
* to `false`. However, if `minify` is not specified and `compress`
* is specified as `true`, then `minify` defaults to `true`.
* This option has no meaning, if `minify` is explicitly set to `false`. However, if `minify` is not
* specified and `compress` is specified as `true`, then `minify` defaults to `true`.
*
* @param {Array|Object|value} [options.params]
* Static formatting parameters to be applied to the SQL, using the same method {@link formatting.format as.format},
Expand Down Expand Up @@ -116,7 +121,7 @@ function QueryFile(file, options) {
return new QueryFile(file, options);
}

var sql, error, ready, modTime, opt = {
var sql, error, ready, modTime, after, opt = {
debug: process.env.NODE_ENV === 'development',
minify: false,
compress: false
Expand All @@ -127,7 +132,8 @@ function QueryFile(file, options) {
opt.debug = !!options.debug;
}
if (options.minify !== undefined) {
opt.minify = !!options.minify;
after = options.minify === 'after';
opt.minify = after ? 'after' : !!options.minify;
}
if (options.compress !== undefined) {
opt.compress = !!options.compress;
Expand Down Expand Up @@ -171,12 +177,15 @@ function QueryFile(file, options) {
try {
sql = fs.readFileSync(file, 'utf8');
modTime = lastMod || fs.statSync(file).mtime.getTime();
if (opt.minify) {
if (opt.minify && !after) {
sql = minify(sql, {compress: opt.compress});
}
if (opt.params !== undefined) {
sql = format(sql, opt.params, {partial: true});
}
if (opt.minify && after) {
sql = minify(sql, {compress: opt.compress});
}
ready = true;
error = undefined;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "3.2.5",
"version": "3.2.6",
"description": "PostgreSQL via promises",
"main": "lib/index.js",
"scripts": {
Expand Down
18 changes: 17 additions & 1 deletion test/fileSpec.js
Expand Up @@ -37,7 +37,7 @@ describe("QueryFile / Positive:", function () {
});
});

describe("with params set", function () {
describe("default with params", function () {
var params = {
schema: "public",
table: "users"
Expand All @@ -48,6 +48,22 @@ describe("QueryFile / Positive:", function () {
});
});

describe("compression with params", function () {
var params = {
schema: "public",
table: "users",
column: "col"
};
var qf1 = new QueryFile(sqlParams, {minify: true, compress: true, params: params});
it("must return uncompressed replacements by default", function () {
expect(qf1.query).toBe('SELECT "col" FROM "public"."users"');
});
var qf2 = new QueryFile(sqlParams, {minify: "after", compress: true, params: params});
it("must return compressed replacements for 'after'", function () {
expect(qf2.query).toBe('SELECT"col"FROM"public"."users"');
});
});

describe("non-minified query", function () {
var result;
beforeEach(function (done) {
Expand Down

0 comments on commit 560c129

Please sign in to comment.