Last active
August 29, 2015 14:13
-
-
Save marten-de-vries/f938337b94773112d7b5 to your computer and use it in GitHub Desktop.
evaljs mapreduce - testing the new API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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