Skip to content

Instantly share code, notes, and snippets.

@marten-de-vries
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save marten-de-vries/f938337b94773112d7b5 to your computer and use it in GitHub Desktop.

Select an option

Save marten-de-vries/f938337b94773112d7b5 to your computer and use it in GitHub Desktop.

Revisions

  1. marten-de-vries revised this gist Jan 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    How to run
    ----------

    1. get index.js and test.js in the same (otherwise empty) directory.
    1. Get index.js and test.js in the same (otherwise empty) directory.
    2. Execute the following commands:

    ```
  2. marten-de-vries revised this gist Jan 21, 2015. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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
    ```
  3. marten-de-vries created this gist Jan 21, 2015.
    19 changes: 19 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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;
    36 changes: 36 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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' ]
    }));
    });