Skip to content

Commit 8de301f

Browse files
committed
mkae position option more generic, and mappabale like other options
1 parent 2e7c5c6 commit 8de301f

7 files changed

Lines changed: 119 additions & 129 deletions

File tree

lib/base.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ _.extend(Store.prototype, {
5353
if (callback) callback(null, id);
5454
},
5555

56+
/**
57+
* Use this function to an array containing the next position numbers
58+
* @param {number} positins Number of positions to provide.
59+
* @param {Function} callback The function, that will be called when the this action is completed.
60+
* `function(err, positions){}` positions is either undefined if option is not enabled/supported or array with positions
61+
*/
62+
getNextPositions: function(positions, callback) {
63+
callback(null);
64+
},
65+
5666
/**
5767
* loads the events
5868
* @param {Object} query the query object

lib/databases/inmemory.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ _.extend(InMemory.prototype, {
6363
if (callback) callback(null);
6464
},
6565

66+
getNextPositions: function(positions, callback) {
67+
if (!this.options.trackPosition) {
68+
return callback(null);
69+
}
70+
71+
var range = [];
72+
for(var i=0; i<positions; i++) {
73+
range.push(++this.position);
74+
}
75+
callback(null, range);
76+
},
77+
6678
addEvents: function (events, callback) {
6779
if (!events || events.length === 0) {
6880
callback(null);
@@ -96,9 +108,6 @@ _.extend(InMemory.prototype, {
96108

97109
var self = this;
98110
_.forEach(events, function(evt) {
99-
if (self.options.trackPosition) {
100-
evt.position = ++self.position;
101-
}
102111
self.undispatchedEvents._direct[evt.id] = evt;
103112
});
104113

lib/databases/mongodb.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,15 @@ _.extend(Mongo.prototype, {
314314
invalidCommitId = false;
315315

316316
var self = this;
317-
this.getNextPositions(events.length, function(err, positions) {
317+
// this.getNextPositions(events.length, function(err, positions) {
318+
/*
318319
if (err) {
319320
debug(err);
320321
if (callback) callback(err);
321322
return;
322323
}
324+
*/
325+
323326
_.forEach(events, function (evt, index) {
324327
if (!evt.aggregateId) {
325328
noAggregateId = true;
@@ -331,8 +334,6 @@ _.extend(Mongo.prototype, {
331334

332335
evt._id = evt.id;
333336
evt.dispatched = false;
334-
if (positions)
335-
evt.position = positions[index];
336337
});
337338

338339
if (noAggregateId) {
@@ -378,7 +379,7 @@ _.extend(Mongo.prototype, {
378379
self.removeTransactions(events[events.length - 1], callback);
379380
});
380381
});
381-
});
382+
// });
382383
},
383384

384385
// streaming API

lib/event.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function Event (eventstream, event, eventMappings) {
4444
this.commitSequence = null;
4545
this.commitStamp = null;
4646
this.payload = event || null;
47+
this.position = null;
4748

4849
this.applyMappings = function applyMappings() {
4950
_.keys(eventMappings).forEach(function (key) {

lib/eventstore.js

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -487,38 +487,46 @@ _.extend(Eventstore.prototype, {
487487
currentRevision = eventstream.currentRevision(),
488488
uncommittedEvents = [].concat(eventstream.uncommittedEvents);
489489
eventstream.uncommittedEvents = [];
490-
for (var i = 0, len = uncommittedEvents.length; i < len; i++) {
491-
event = uncommittedEvents[i];
492-
event.id = id + i.toString();
493-
event.commitId = id;
494-
event.commitSequence = i;
495-
event.restInCommitStream = len - 1 - i;
496-
event.commitStamp = new Date();
497-
currentRevision++;
498-
event.streamRevision = currentRevision;
499-
500-
event.applyMappings();
501-
}
502490

503-
self.store.addEvents(uncommittedEvents, function(err) {
504-
if (err) {
505-
// add uncommitted events back to eventstream
506-
eventstream.uncommittedEvents = uncommittedEvents.concat(eventstream.uncommittedEvents);
507-
return callback(err);
491+
self.store.getNextPositions(uncommittedEvents.length, function(err, positions) {
492+
if (err)
493+
return callback(err)
494+
495+
for (var i = 0, len = uncommittedEvents.length; i < len; i++) {
496+
event = uncommittedEvents[i];
497+
event.id = id + i.toString();
498+
event.commitId = id;
499+
event.commitSequence = i;
500+
event.restInCommitStream = len - 1 - i;
501+
event.commitStamp = new Date();
502+
currentRevision++;
503+
event.streamRevision = currentRevision;
504+
if (positions)
505+
event.position = positions[i];
506+
507+
event.applyMappings();
508508
}
509509

510-
if (self.publisher && self.dispatcher) {
511-
// push to undispatchedQueue
512-
self.dispatcher.addUndispatchedEvents(uncommittedEvents);
513-
} else {
514-
eventstream.eventsToDispatch = [].concat(uncommittedEvents);
515-
}
510+
self.store.addEvents(uncommittedEvents, function(err) {
511+
if (err) {
512+
// add uncommitted events back to eventstream
513+
eventstream.uncommittedEvents = uncommittedEvents.concat(eventstream.uncommittedEvents);
514+
return callback(err);
515+
}
516516

517-
// move uncommitted events to events
518-
eventstream.events = eventstream.events.concat(uncommittedEvents);
519-
eventstream.currentRevision();
517+
if (self.publisher && self.dispatcher) {
518+
// push to undispatchedQueue
519+
self.dispatcher.addUndispatchedEvents(uncommittedEvents);
520+
} else {
521+
eventstream.eventsToDispatch = [].concat(uncommittedEvents);
522+
}
520523

521-
callback(null, eventstream);
524+
// move uncommitted events to events
525+
eventstream.events = eventstream.events.concat(uncommittedEvents);
526+
eventstream.currentRevision();
527+
528+
callback(null, eventstream);
529+
});
522530
});
523531
}],
524532

test/eventstoreTest.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ describe('eventstore', function () {
876876

877877
var types = ['inmemory', 'tingodb', 'mongodb', 'redis'/*, 'elasticsearch', 'azuretable', 'dynamodb'*/];
878878
var streamingApiTypes = ['mongodb'];
879+
var positionTypes = ['mongodb', 'inmemory'];
879880

880881
var token = crypto.randomBytes(16).toString('hex');
881882

@@ -1582,7 +1583,62 @@ describe('eventstore', function () {
15821583
});
15831584
}
15841585

1586+
if (positionTypes.indexOf(type) !== -1) {
1587+
describe('setting event position option', function() {
1588+
beforeEach(function (done) {
1589+
es = eventstore({
1590+
type: type,
1591+
positionsCollectionName: 'positions',
1592+
trackPosition: true,
1593+
});
1594+
es.defineEventMappings({ commitStamp: 'head.position' });
1595+
es.init(function(err) {
1596+
es.store.clear(done);
1597+
});
1598+
});
1599+
1600+
afterEach(function (done) {
1601+
es.store.clear(done);
1602+
});
1603+
1604+
it('it should save the event with position', function(done) {
1605+
es.getEventStream('streamIdWithPosition', function (err, stream) {
1606+
expect(err).not.to.be.ok();
1607+
stream.addEvent({ one: 'event' });
1608+
stream.addEvent({ one: 'event-other' });
1609+
1610+
stream.commit(function (err, st) {
1611+
expect(err).not.to.be.ok();
1612+
1613+
expect(st.events.length).to.eql(2);
1614+
expect(st.events[0].position).to.eql(1);
1615+
expect(st.events[1].position).to.eql(2);
1616+
1617+
done();
1618+
});
1619+
});
1620+
});
1621+
1622+
it('it should map position to payload', function(done) {
1623+
es.getEventStream('streamIdWithPosition', function (err, stream) {
1624+
expect(err).not.to.be.ok();
1625+
stream.addEvent({ one: 'event' });
1626+
stream.addEvent({ one: 'event-other' });
1627+
1628+
stream.commit(function (err, st) {
1629+
expect(err).not.to.be.ok();
1630+
1631+
expect(st.events.length).to.eql(2);
1632+
expect(st.events[0].payload.head.position).to.eql(1);
1633+
expect(st.events[1].payload.head.position).to.eql(2);
1634+
1635+
done();
1636+
});
1637+
});
1638+
});
15851639

1640+
});
1641+
}
15861642
});
15871643

15881644
});

test/storeTest.js

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,101 +3126,6 @@ types.forEach(function (type) {
31263126
})
31273127

31283128
});
3129-
3130-
if (type === "mongodb" || type === "inmemory") {
3131-
var positionstore = new Store({
3132-
positionsCollectionName: 'positions',
3133-
trackPosition: true,
3134-
});
3135-
3136-
describe('adding position number', function(done) {
3137-
beforeEach(function (done) {
3138-
positionstore.connect(done);
3139-
});
3140-
3141-
afterEach(function (done) {
3142-
positionstore.clear(done);
3143-
});
3144-
describe('with one event in the array', function () {
3145-
3146-
it('it should save the event with position', function(done) {
3147-
3148-
var event = {
3149-
aggregateId: 'id1',
3150-
id: '111',
3151-
streamRevision: 0,
3152-
commitId: '111',
3153-
commitStamp: new Date(),
3154-
commitSequence: 0,
3155-
payload: {
3156-
event:'bla',
3157-
array: []
3158-
},
3159-
applyMappings: function () {}
3160-
};
3161-
3162-
positionstore.addEvents([event], function(err) {
3163-
expect(err).not.to.be.ok();
3164-
3165-
positionstore.getEvents({}, 0, -1, function(err, evts) {
3166-
expect(err).not.to.be.ok();
3167-
expect(evts[0].position).to.eql(1);
3168-
done();
3169-
});
3170-
});
3171-
3172-
});
3173-
3174-
});
3175-
3176-
describe('with multiple events in the array', function () {
3177-
3178-
it('it should save the events with positions', function(done) {
3179-
3180-
var event1 = {
3181-
aggregateId: 'id2',
3182-
streamRevision: 0,
3183-
id: '112',
3184-
commitId: '987',
3185-
commitStamp: new Date(Date.now() + 1),
3186-
commitSequence: 0,
3187-
restInCommitStream: 1,
3188-
payload: {
3189-
event:'bla'
3190-
},
3191-
applyMappings: function () {}
3192-
};
3193-
3194-
var event2 = {
3195-
aggregateId: 'id2',
3196-
streamRevision: 1,
3197-
id:'113',
3198-
commitId: '987',
3199-
commitStamp: new Date(Date.now() + 1),
3200-
commitSequence: 1,
3201-
restInCommitStream: 0,
3202-
payload: {
3203-
event:'bla2'
3204-
},
3205-
applyMappings: function () {}
3206-
};
3207-
3208-
positionstore.addEvents([event1, event2], function(err) {
3209-
expect(err).not.to.be.ok();
3210-
3211-
positionstore.getEvents({}, 0, -1, function(err, evts) {
3212-
expect(err).not.to.be.ok();
3213-
expect(evts).to.be.an('array');
3214-
expect(evts).to.have.length(2);
3215-
expect(evts[0].position).to.eql(1);
3216-
expect(evts[1].position).to.eql(2);
3217-
done();
3218-
});
3219-
});
3220-
});
3221-
});
3222-
});
3223-
}
32243129
});
32253130
});
32263131
});

0 commit comments

Comments
 (0)