Skip to content

Instantly share code, notes, and snippets.

@JensRantil
Last active February 8, 2021 21:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JensRantil/6417018 to your computer and use it in GitHub Desktop.
Save JensRantil/6417018 to your computer and use it in GitHub Desktop.
Example event view projection for CouchDB event store.
{
"_id": "_design/eventProjections",
"views": {
"map": "function(doc) {
if (doc.aggregateType=='contact' &&
(doc.event.type=='ContactCreated' ||
doc.event.type=='DescriptionChanged') &&
doc.event.description) {
var colon = doc._id.indexOf(':');
var version = parseInt(doc._id.substr(colon + 1));
var id = doc._id.substr(0, colon);
emit(id, {
version: version,
newDescription: doc.event.description
});
}
}",
"reduce": "function(key, values, rereduce) {
var latest = null;
values.forEach(function(v) {
if (latest == null || v.version > latest.version) {
latest = {
version: values[0].version,
description: values[0].newDescription
};
}
});
return latest;
}"
}
}
@RoyalIcing
Copy link

Why is it values[0] and not v?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment