Skip to content

Commit

Permalink
code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Dec 13, 2015
1 parent 7fc1080 commit bd99713
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
35 changes: 25 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -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);
};

/**
Expand All @@ -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);
};

/**
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit bd99713

Please sign in to comment.