Skip to content

Commit

Permalink
(daleharvey/pouchdb#1387) - pass object instead of 2 arguments for pl…
Browse files Browse the repository at this point in the history
…ugins
  • Loading branch information
calvinmetcalf committed Feb 27, 2014
1 parent 2c9c3bc commit b59c969
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 51 deletions.
76 changes: 26 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ function httpQuery(db, fun, opts) {
}, callback);
}

module.exports = function (fun, opts, callback) {
exports.query = function (fun, opts, callback) {
var db = this;
if (typeof opts === 'function') {
callback = opts;
Expand All @@ -374,60 +374,36 @@ module.exports = function (fun, opts, callback) {
if (callback) {
opts.complete = callback;
}
var tempCB = opts.complete;
var realCB;
if (opts.complete) {
realCB = function (err, resp) {
process.nextTick(function () {
tempCB(err, resp);
});
};
}
var promise = new Promise(function (resolve, reject) {
opts.complete = function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
};

if (db.type() === 'http') {
if (typeof fun === 'function') {
return httpQuery(db, {map: fun}, opts);
}
return httpQuery(db, fun, opts);
if (db.type() === 'http') {
if (typeof fun === 'function') {
return httpQuery(db, {map: fun}, opts);
}
return httpQuery(db, fun, opts);
}

if (typeof fun === 'object') {
return viewQuery(db, fun, opts);
}
if (typeof fun === 'object') {
return viewQuery(db, fun, opts);
}

if (typeof fun === 'function') {
return viewQuery(db, {map: fun}, opts);
}
if (typeof fun === 'function') {
return viewQuery(db, {map: fun}, opts);
}

var parts = fun.split('/');
db.get('_design/' + parts[0], function (err, doc) {
if (err) {
opts.complete(err);
return;
}
var parts = fun.split('/');
db.get('_design/' + parts[0], function (err, doc) {
if (err) {
opts.complete(err);
return;
}

if (!doc.views[parts[1]]) {
opts.complete({ name: 'not_found', message: 'missing_named_view' });
return;
}
viewQuery(db, {
map: doc.views[parts[1]].map,
reduce: doc.views[parts[1]].reduce
}, opts);
});
if (!doc.views[parts[1]]) {
opts.complete({ name: 'not_found', message: 'missing_named_view' });
return;
}
viewQuery(db, {
map: doc.views[parts[1]].map,
reduce: doc.views[parts[1]].reduce
}, opts);
});
if (realCB) {
promise.then(function (resp) {
realCB(null, resp);
}, realCB);
}
return promise;
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"lie": "~2.5.1",
"pouchdb-collate": "~1.0.0"
},
"peerDependencies": {
"pouchdb": "~2.0.0"
},
"devDependencies": {
"chai": "~1.8.1",
"mocha": "~1.14.0",
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var Pouch = require('Pouchdb');
var Mapreduce = require('../');
Pouch.plugin('query', Mapreduce);
Pouch.plugin(Mapreduce);
var chai = require('chai');
var should = chai.should();
require("mocha-as-promised")();
Expand Down

0 comments on commit b59c969

Please sign in to comment.