Node-m-r – A Simple CQRS Example using Node.js

December 12th, 2013

Anyone learning about DDD, CQRS and/or event sourcing has probably read the source code of Greg Young’s simple CQRS example at some point or another. This is one of the simplest examples possible demonstrating the practical side of CQRS and event sourcing, originally developed using .NET.

I made an attempt to rewrite the source code of this example using Node.js, trying to apply as many features of the core library as possible, particularly streams and event emitters. For example, the implementation of the base aggregate root object uses an event emitter internally for applying events in derived objects. An aggregate root is also a writable stream, which is used by the repository for replaying events. The report aggregators are also writable streams, which receive events from the message bus.

I’ve put the resulting source code up on GitHub. This is still very much a work in progress though. Some improvements that I’m considering is spawning a separate child process for both the command handlers as well as the report aggregators. No data is being persisted at the moment, so everything is stored in memory. Adding physical data stores is something that I’m also looking into. There’s no UI available either. You can run the example by executing the following command:

$ node src/application.js

Have a look at the source code and let me know what you think.

Until next time.