Skip to content

Instantly share code, notes, and snippets.

@marten-de-vries
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marten-de-vries/f938337b94773112d7b5 to your computer and use it in GitHub Desktop.
Save marten-de-vries/f938337b94773112d7b5 to your computer and use it in GitHub Desktop.
evaljs mapreduce - testing the new API

How to run

  1. Get index.js and test.js in the same (otherwise empty) directory.
  2. Execute the following commands:
npm install pouchdb memdown pouchdb-abstract-mapreduce evaljs
node test.js
var abstractMapReduce = require('pouchdb-abstract-mapreduce');
var evaljs = require('evaljs');
var methods = abstractMapReduce({
name: 'evaljsviews',
mapper: function (mapFunDef, emit) {
var env = new evaljs.Environment({emit: emit});
return env.gen('(' + mapFunDef + ')')();
},
reducer: function (reduceFunDef) {
// TODO
},
ddocValidator: function (ddoc, viewName) {
// TODO
}
});
exports.evaljsQuery = methods.query;
exports.evaljsViewCleanup = methods.viewCleanup;
var PouchDB = require('pouchdb');
var plugin = require('./index');
PouchDB.plugin(plugin);
var db = new PouchDB('test', {db: require('memdown')});
db.bulkDocs([
{
_id: '_design/test',
views: {
test: {
map: function (doc) {
// a relatively 'complicated' view
for (var i = 0; i < doc.roles.length; i++) {
var role = doc.roles[i];
if (role.indexOf('a') === -1) {
emit(role);
}
}
}.toString()
}
}
},
{
roles: ['b', 'c', 'a']
},
{
roles: ['e', 'f', 'b']
}
]).then(function () {
return db.evaljsQuery('test/test');
}).then(function (resp) {
console.log(resp.rows.map(function (item) {
return item.key; // [ 'b', 'b', 'c', 'e', 'f' ]
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment