diff --git a/README.md b/README.md index 6a5a6a45..c2e6429d 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,8 @@ In addition, the library provides: * its own, more flexible query formatting; * event reporting for connectivity, errors, queries and transactions; -* support for all popular promise libraries + ES6 generators -* declarative approach to controlling query results; +* support for all popular promise libraries + ES6 generators; +* declarative approach to controlling query results. # Installing ``` diff --git a/lib/index.js b/lib/index.js index 00769242..5cb6e36c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -197,6 +197,18 @@ function Task(ctx, tag, isTX) { $extend(ctx, this); // extending task protocol; } +///////////////////////////// +// Special Query type; +function SpecialQuery(type) { + this.isStream = type === 'stream'; + this.isResult = type === 'result'; +} + +var $cache = { + resultQuery: new SpecialQuery('result'), + streamQuery: new SpecialQuery('stream') +}; + //////////////////////////////////////////////////// // Injects additional methods into an access object, // extending the protocol's base method 'query'. @@ -319,7 +331,7 @@ function $extend(ctx, obj) { * - resolves with the original $[Result] object */ obj.result = function (query, values) { - return obj.query.call(this, query, values, 'result'); + return obj.query.call(this, query, values, $cache.resultQuery); }; /** @@ -342,7 +354,7 @@ function $extend(ctx, obj) { * - `Invalid or missing stream initialization callback.` */ obj.stream = function (qs, init) { - return obj.query.call(this, qs, init, 'stream'); + return obj.query.call(this, qs, init, $cache.streamQuery); }; /** @@ -487,16 +499,19 @@ function $extend(ctx, obj) { ////////////////////////////// // Generic query method; function $query(ctx, query, values, qrm) { - if (qrm === 'stream') { - return $stream.call(this, ctx, query, values); + var isResult = false; + if (qrm instanceof SpecialQuery) { + if (qrm.isStream) { + return $stream.call(this, ctx, query, values); + } + isResult = qrm.isResult; } var errMsg, textErr, isFunc = $npm.utils.isObject(query, ['funcName']), // function call; isPS = $npm.utils.isObject(query, ['name', 'text']), // prepared statement; - options = ctx.options, - pgFormatting = (options && options.pgFormatting) || isPS, - params = pgFormatting ? values : undefined, - isResult = qrm === 'result'; + opt = ctx.options, + pgFormatting = (opt && opt.pgFormatting) || isPS, + params = pgFormatting ? values : undefined; return $p(function (resolve, reject) { @@ -548,7 +563,7 @@ function $query(ctx, query, values, qrm) { if (notifyReject()) { return; } - errMsg = $notify.query(options, { + errMsg = $notify.query(opt, { client: ctx.db.client, query: query, params: params, @@ -618,7 +633,7 @@ function $query(ctx, query, values, qrm) { errMsg = "Loose request outside an expired connection."; } if (errMsg !== undefined) { - $notify.error(options, errMsg, { + $notify.error(opt, errMsg, { client: client, query: query, params: params,