Skip to content

Instantly share code, notes, and snippets.

@janderit
Created July 16, 2012 12:04
Show Gist options
  • Save janderit/3122328 to your computer and use it in GitHub Desktop.
Save janderit/3122328 to your computer and use it in GitHub Desktop.
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