Last active
February 8, 2021 21:31
-
-
Save JensRantil/6417018 to your computer and use it in GitHub Desktop.
Example event view projection for CouchDB event store.
This file contains 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
{ | |
"_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; | |
}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why is it
values[0]
and notv
?