Skip to content

Commit

Permalink
feat(views): allow reduce
Browse files Browse the repository at this point in the history
Allow reduce on views that don't emit owners
  • Loading branch information
stropitek committed Nov 10, 2016
1 parent 6375901 commit 1c203d4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/couch/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const methods = {
const data = new Map();
const userStartKey = options.key ? [options.key] : (options.startkey ? options.startkey : []);
const userEndKey = options.key ? [options.key] : (options.endkey ? options.endkey : []);


for (const group of userGroups) {
const startkey = [group].concat(userStartKey);
const endkey = [group].concat(userEndKey);
Expand Down Expand Up @@ -80,8 +82,19 @@ const methods = {
async queryViewByUser(user, view, options, rights) {
debug(`queryViewByUser (${user}, ${view})`);
options = Object.assign({}, options);
if(options.reduce) {
if (this._viewsWithOwner.has(view)) {
// We don't allow this. Reduce with emit owner make little sense
// since each document can be emited more than once...
throw new CouchError(`${view} is a view with owner`, 'unauthorized');
}
// !! if reduce we bypass security
// Reduce should not contain sensible data
return await nanoPromise.queryView(this._db, view, options);
}


options.include_docs = true;
options.reduce = false;
options.skip = 0;
var limit = options.limit || 1;
var cumRows = [];
Expand Down
10 changes: 10 additions & 0 deletions test/homedir/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ module.exports = {
emitWithOwner(['x', 'y', 'z'], doc.$id);
},
withOwner: true
},
testReduce: {
map: function (doc) {
if(doc.$type === 'entry') {
emit(doc._id, 1); // eslint-disable-line no-undef
}
},
reduce: function(keys, values) {
return sum(values);
}
}
}
},
Expand Down
13 changes: 12 additions & 1 deletion test/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('Query view with owner (group right)', function () {
});
});

describe('Query view filter groups', function () {
describe('Query entries filter groups', function () {
before(noRights);
it('should only return entries owned by the user', function () {
return couch.queryEntriesByRight('a@a.com', 'entryIdByRight', null, {groups: 'a@a.com'})
Expand Down Expand Up @@ -129,6 +129,17 @@ describe('Query view filter groups', function () {
});
});

describe('Query view with reduce', function () {
before(data);
it('Should query by user id', function () {
return couch.queryViewByUser('a@a.com', 'testReduce', {reduce: true})
.then(rows => {
// counts the entries
rows[0].value.should.equal(5);
});
});
});

function sortByValue(a, b) {
if (a.value < b.value) return -1;
else if (a.value > b.value) return 1;
Expand Down
1 change: 0 additions & 1 deletion views/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ <h3>CouchDB login</h3>
}

function doLogin(url) {
debugger;
if(window.parent !== window) {
url += '/popup';
var popup = window.open(url, 'loginPopup');
Expand Down

0 comments on commit 1c203d4

Please sign in to comment.