Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update redis.js so that revisions don't have to begin at 0 #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/databases/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ _.extend(Redis.prototype, {
}

var savedKeysAndEvents = events.map(function(event, index) {
var key = prefix + ':' + eventKey(event);
event.streamRevision = parseInt(revisions[index], 10) - 1;
var streamRevision = parseInt(revisions[index], 10) - 1;
var key = prefix + ':' + eventKey(event) + ':' + streamRevision;
event.streamRevision = streamRevision;
event.applyMappings();
return [key, JSON.stringify(event)];
});
Expand Down Expand Up @@ -427,13 +428,24 @@ _.extend(Redis.prototype, {
return s;
});

if (revMax === -1) {
allKeys = allKeys.slice(revMin);
var getRevision = function(key) {
var parts = key.split(':')
return parts[parts.length - 1]
}
else {
allKeys = allKeys.slice(revMin, revMax);

var filterMin = function(key) {
return getRevision(key) >= revMin
}

var filterMinAndMax = function(key) {
var revision = getRevision(key)
return revision >= revMin && revision < revMax
}

var filterFunc = (revMax && revMax !== -1) ? filterMinAndMax : filterMin

allKeys = allKeys.filter(filterFunc)

if (allKeys.length === 0) {
return callback(null, []);
}
Expand Down
32 changes: 31 additions & 1 deletion test/storeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,36 @@ types.forEach(function (type) {

});

if (type === 'redis') {
describe('when events do not start at revision 0', function () {

it('should return the events specified by the revMin and revMax', function (done) {

var firstEventMatcher = options.prefix + ':' + options.eventsCollectionName + ':*:*:*:*:' + 'idWithAgg' + ':*:0'
var revMin = 1
var revMax = 2

store.client.keys(firstEventMatcher, (err, keys) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to supported legacy node versions, and avoid the use of transpilers, arrow functions should be avoided.

if (err) { console.log(err) }

store.client.del(keys[0], (err, keysDeleted) => {
expect(keysDeleted).to.equal(1)

store.getEventsByRevision({ aggregateId: 'idWithAgg' }, revMin, revMax, function (err, evts) {
expect(err).not.to.be.ok();
expect(evts.length).to.eql(1);
expect(evts[0].aggregateId).to.eql(stream2[1].aggregateId);
expect(evts[0].commitStamp.getTime()).to.eql(stream2[1].commitStamp.getTime());
expect(evts[0].streamRevision).to.eql(stream2[1].streamRevision);

done();
});
})
})
})
})
}

});

});
Expand Down Expand Up @@ -2283,7 +2313,7 @@ types.forEach(function (type) {

});

describe('and limit it with skip and limit', function () {
describe('and limit it with revMin and revMax', function () {

it('it should return the correct events', function (done) {

Expand Down