Skip to content

Instantly share code, notes, and snippets.

@BigBlueHat
Forked from RichardLitt/gist:48a6068c4edbba4f58ac
Last active August 29, 2015 14:25
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 BigBlueHat/9aebc2a84ee72092b06d to your computer and use it in GitHub Desktop.
Save BigBlueHat/9aebc2a84ee72092b06d to your computer and use it in GitHub Desktop.

Map/Reduce in PouchDB/CouchDB does have a "closed scope"--it's not the lamda it looks like.

Essentially, you don't query with the map/reduce...you build an index.

In CouchDB parlance, the map/reduce code is called a view. The thing it builds is an index. You then query the index using a...query. 😉

In CouchDB it's clearer than in PouchDB as you actually use query params to query an index (which lives at a URL, so it's more obviously a stored something or other that you're querying via HTTP).

In PouchDB, the first part of .query() works like a "save this view + build an index for it" action, the second part are the "query params" from CouchDB land.

Check the code again to see if it makes more sense.

module.exports.getConversationsForUser = function (user, cb) {
db.query(
function (doc) {
if (doc.author) {
emit(doc.author)
}
},
{include_docs: true, key: user.userId}
).then(function (response) {
// response.doc should have the whole doc
// response.key should === user.userId (because of the above query param)
// response.value should === null
// response.id should === doc._id
console.log('response', response)
cb(null, response)
}).catch(function (err) {
cb(err)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment