Instantly share code, notes, and snippets.

Embed
What would you like to do?
Very brief cqrs projection proof of concept in node.js
function UserWasCreated(id, name){
this.Event = arguments.callee.name;
this.UserId=id;
this.Name=name;
}
function UserWasRenamed(id, name){
this.Event = arguments.callee.name;
this.UserId=id;
this.Name=name;
}
function Retrieve(theclass, history){
var instance = new theclass();
instance.loadHistory(history);
return instance;
}
function Aggregate() {
}
Aggregate.prototype.handle = function(e){this[e.Event](e);};
Aggregate.prototype.loadHistory = function(history){history.forEach(function(e){ this.handle(e);}, this)};
function AddConcept(prototype, concept){
for (var k in concept) {
if (concept.hasOwnProperty(k)) {
prototype[k]=concept[k];
}
}
}
var Username={
UserWasCreated : function(e){this.Id= e.UserId;this.Name=e.Name;},
UserWasRenamed : function(e){this.Name=e.Name;}
}
function User() { }
User.prototype = new Aggregate();
AddConcept(User.prototype, Username)
var hist = [new UserWasCreated(1, "Philip"),new UserWasRenamed(1, "Ph. Jander")];
var user = Retrieve(User, hist);
console.log(user.Name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment