Permalink
Browse files

Merge remote-tracking branch 'm/master'

  • Loading branch information...
2 parents 48ac8ed + 189be6a commit 04c73c7d96b9bb9311c8e7066551d01ab85112a9 @jamuhl jamuhl committed Jan 26, 2012
View
2 README.markdown
@@ -1,5 +1,7 @@
# Introduction
+[![Build Status](https://secure.travis-ci.org/jamuhl/nodeEventStore.png)](http://travis-ci.org/jamuhl/nodeEventStore)
+
The project goal is to provide an eventstore implementation for node.js:
- load and store events via EventStream object
View
19 lib/eventStore.js
@@ -359,6 +359,25 @@ Store.prototype = {
},
+ // __getLastEventOfStream:__ loads the last event from the given stream in storage.
+ //
+ // `eventStore.getLastEventOfStream(streamId, callback)`
+ //
+ // - __streamId:__ the stream id
+ // - __callback:__ `function(err, event){}`
+ getLastEventOfStream: function(streamId, callback) {
+
+ if (this.hasConfigurationErrors(callback)) return;
+
+ this.storage.getLastEventOfStream(streamId, function(err, event) {
+
+ if (typeof callback === 'function') {
+ callback(err, event);
+ }
+ });
+
+ },
+
// __getEvents:__ loads the events from given storage.
//
// `eventStore.getEvents(index, amount, callback)`
View
19 lib/storage/inMemory/storage.js
@@ -119,6 +119,25 @@ Storage.prototype = {
callback(null, events);
},
+ // __getLastEventOfStream:__ loads the last event from the given stream in storage.
+ //
+ // `storage.getLastEventOfStream(streamId, callback)`
+ //
+ // - __streamId:__ the stream id
+ // - __callback:__ `function(err, event){}`
+ getLastEventOfStream: function(streamId, callback) {
+
+ if (!this.store[streamId]) {
+ callback(null, null);
+ }
+ else if (this.store[streamId].length) {
+ callback(null, this.store[streamId][this.store[streamId].length - 1]);
+ } else {
+ callback(null, null);
+ }
+
+ },
+
// __getEventRange:__ loads the range of events from given storage.
//
// `storage.getEventRange(index, amount, callback)`
View
28 storage/couchDb/storage.js
@@ -138,7 +138,7 @@ Storage.prototype = {
maxRev = -1;
}
- this.client.view(this.options.dbName+'/eventsByStreamId', { 'key': streamId },
+ this.client.view(this.options.dbName+'/eventsByStreamId', { key: streamId },
function(err, res) {
if(!err) {
var result = [];
@@ -159,7 +159,8 @@ Storage.prototype = {
else {
callback(err);
}
- });
+ }
+ );
},
// __getAllEvents:__ loads the events.
@@ -188,6 +189,29 @@ Storage.prototype = {
},
+ // __getLastEventOfStream:__ loads the last event from the given stream in storage.
+ //
+ // `storage.getLastEventOfStream(streamId, callback)`
+ //
+ // - __streamId:__ the stream id
+ // - __callback:__ `function(err, event){}`
+ getLastEventOfStream: function(streamId, callback) {
+ this.client.view(this.options.dbName+'/eventsByStreamId', { key: streamId, descending: true },
+ function(err, res) {
+ if(!err) {
+ if (res.length) {
+ callback(null, res[0].value);
+ } else {
+ callback(null, null);
+ }
+ }
+ else {
+ callback(err);
+ }
+ }
+ );
+ },
+
// __getEventRange:__ loads the range of events from given storage.
//
// `storage.getEventRange(index, amount, callback)`
View
10 storage/couchDb/test/couchDbStorageSpec.js
@@ -132,6 +132,16 @@ vows.describe('The ' + storageName + ' Storage')
assert.equal(events[1].commitId, '2');
}
},
+
+ 'after a successful `fill` we get the latest event': {
+ topic: function (storage) {
+ storage.getLastEventOfStream('2', this.callback);
+ },
+
+ 'we can assert if it is really the last event': function (event) {
+ assert.equal(event.streamRevision, 3);
+ }
+ },
'after a successful `fill with a snapshot` we get the snapshot': {
topic: function (storage) {
View
16 storage/mongoDb/storage.js
@@ -124,6 +124,22 @@ Storage.prototype = {
this.events.find({}, {sort:[['commitStamp','asc']]}).toArray(callback);
},
+ // __getLastEventOfStream:__ loads the last event from the given stream in storage.
+ //
+ // `storage.getLastEventOfStream(streamId, callback)`
+ //
+ // - __streamId:__ the stream id
+ // - __callback:__ `function(err, event){}`
+ getLastEventOfStream: function(streamId, callback) {
+
+ var findStatement = {
+ 'streamId' : streamId
+ };
+
+ this.events.find(findStatement, {sort:[['streamRevision','desc']]}).nextObject(callback);
+
+ },
+
// __getEventRange:__ loads the range of events from given storage.
//
// `storage.getEventRange(index, amount, callback)`
View
10 storage/mongoDb/test/mongoDbStorageSpec.js
@@ -132,6 +132,16 @@ vows.describe('The ' + storageName + ' Storage')
assert.equal(events[1].commitId, '2');
}
},
+
+ 'after a successful `fill` we get the latest event': {
+ topic: function (storage) {
+ storage.getLastEventOfStream('2', this.callback);
+ },
+
+ 'we can assert if it is really the last event': function (event) {
+ assert.equal(event.streamRevision, 3);
+ }
+ },
'after a successful `fill with a snapshot` we get the snapshot': {
topic: function (storage) {
View
25 storage/redis/storage.js
@@ -164,6 +164,31 @@ Storage.prototype = {
});
},
+ // __getLastEventOfStream:__ loads the last event from the given stream in storage.
+ //
+ // `storage.getLastEventOfStream(streamId, callback)`
+ //
+ // - __streamId:__ the stream id
+ // - __callback:__ `function(err, event){}`
+ getLastEventOfStream: function(streamId, callback) {
+ var self =this;
+ this.client.llen(this.options.eventsCollectionName + ':' + streamId, function (err, length) {
+ if (err) {
+ callback(err);
+ } else {
+ self.client.lrange(self.options.eventsCollectionName + ':' + streamId, length - 2, length, function (err, res) {
+ handleResultSet(err, res, function (err, events) {
+ var event = null;
+ if (events.length) {
+ event = events[events.length - 1];
+ }
+ callback(err, event);
+ });
+ });
+ }
+ });
+ },
+
// __getEventRange:__ loads the range of events from given storage.
//
// `storage.getEventRange(index, amount, callback)`
View
10 storage/redis/test/redisStorageSpec.js
@@ -137,6 +137,16 @@ vows.describe('The ' + storageName + ' Storage')
assert.equal(events[1].commitId, '2');
}
},
+
+ 'after a successful `fill` we get the latest event': {
+ topic: function (storage) {
+ storage.getLastEventOfStream('2', this.callback);
+ },
+
+ 'we can assert if it is really the last event': function (event) {
+ assert.equal(event.streamRevision, 3);
+ }
+ },
'after a successful `fill with a snapshot` we get the snapshot': {
topic: function (storage) {
View
10 test/inMemoryStorageSpec.js
@@ -133,6 +133,16 @@ vows.describe('The ' + storageName + ' Storage')
assert.equal(events[1].commitId, '2');
}
},
+
+ 'after a successful `fill` we get the latest event': {
+ topic: function (storage) {
+ storage.getLastEventOfStream('2', this.callback);
+ },
+
+ 'we can assert if it is really the last event': function (event) {
+ assert.equal(event.streamRevision, 3);
+ }
+ },
'after a successful `fill with a snapshot` we get the snapshot': {
topic: function (storage) {

0 comments on commit 04c73c7

Please sign in to comment.