Skip to content

Commit f5ce3f0

Browse files
authored
Merge pull request #149 from DaNautilus/feature/emit-eventstore-events
feat: optionally emit eventstore events
2 parents 6125519 + c36e30c commit f5ce3f0

File tree

4 files changed

+777
-22
lines changed

4 files changed

+777
-22
lines changed

README.md

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,20 @@ example with mongodb:
4444

4545
var es = require('eventstore')({
4646
type: 'mongodb',
47-
host: 'localhost', // optional
48-
port: 27017, // optional
49-
dbName: 'eventstore', // optional
50-
eventsCollectionName: 'events', // optional
51-
snapshotsCollectionName: 'snapshots', // optional
52-
transactionsCollectionName: 'transactions', // optional
53-
timeout: 10000 // optional
47+
host: 'localhost', // optional
48+
port: 27017, // optional
49+
dbName: 'eventstore', // optional
50+
eventsCollectionName: 'events', // optional
51+
snapshotsCollectionName: 'snapshots', // optional
52+
transactionsCollectionName: 'transactions', // optional
53+
timeout: 10000, // optional
54+
emitStoreEvents: true // optional, by default no store events are emitted
5455
// maxSnapshotsCount: 3 // optional, defaultly will keep all snapshots
55-
// authSource: 'authedicationDatabase', // optional
56-
// username: 'technicalDbUser', // optional
56+
// authSource: 'authedicationDatabase' // optional
57+
// username: 'technicalDbUser' // optional
5758
// password: 'secret' // optional
5859
// url: 'mongodb://user:pass@host:port/db?opts // optional
59-
// positionsCollectionName: 'positions' // optioanl, defaultly wont keep position
60+
// positionsCollectionName: 'positions' // optional, defaultly wont keep position
6061
});
6162

6263
example with redis:
@@ -69,9 +70,10 @@ example with redis:
6970
prefix: 'eventstore', // optional
7071
eventsCollectionName: 'events', // optional
7172
snapshotsCollectionName: 'snapshots', // optional
73+
emitStoreEvents: true, // optional, by default no store events are emitted
7274
timeout: 10000 // optional
73-
// maxSnapshotsCount: 3 // optional, defaultly will keep all snapshots
74-
// password: 'secret' // optional
75+
// maxSnapshotsCount: 3 // optional, defaultly will keep all snapshots
76+
// password: 'secret' // optional
7577
});
7678

7779
example with tingodb:
@@ -82,8 +84,9 @@ example with tingodb:
8284
eventsCollectionName: 'events', // optional
8385
snapshotsCollectionName: 'snapshots', // optional
8486
transactionsCollectionName: 'transactions', // optional
85-
timeout: 10000 // optional
86-
// maxSnapshotsCount: 3 // optional, defaultly will keep all snapshots
87+
timeout: 10000, // optional
88+
emitStoreEvents: true // optional, by default no store events are emitted
89+
// maxSnapshotsCount: 3 // optional, defaultly will keep all snapshots
8790
});
8891

8992
example with elasticsearch:
@@ -95,8 +98,9 @@ example with elasticsearch:
9598
eventsTypeName: 'events', // optional
9699
snapshotsTypeName: 'snapshots', // optional
97100
log: 'warning', // optional
98-
maxSearchResults: 10000 // optional
99-
// maxSnapshotsCount: 3 // optional, defaultly will keep all snapshots
101+
maxSearchResults: 10000, // optional
102+
emitStoreEvents: true // optional, by default no store events are emitted
103+
// maxSnapshotsCount: 3 // optional, defaultly will keep all snapshots
100104
});
101105

102106
example with custom elasticsearch client (e.g. with AWS ElasticSearch client. Note ``` http-aws-es ``` package usage in this example):
@@ -130,9 +134,10 @@ example with azuretable:
130134
storageAccount: 'nodeeventstore',
131135
storageAccessKey: 'aXJaod96t980AbNwG9Vh6T3ewPQnvMWAn289Wft9RTv+heXQBxLsY3Z4w66CI7NN12+1HUnHM8S3sUbcI5zctg==',
132136
storageTableHost: 'https://nodeeventstore.table.core.windows.net/',
133-
eventsTableName: 'events', // optional
134-
snapshotsTableName: 'snapshots', // optional
135-
timeout: 10000 // optional
137+
eventsTableName: 'events', // optional
138+
snapshotsTableName: 'snapshots', // optional
139+
timeout: 10000, // optional
140+
emitStoreEvents: true // optional, by default no store events are emitted
136141
});
137142

138143
example with dynamodb:
@@ -150,7 +155,8 @@ example with dynamodb:
150155
UndispatchedEventsReadCapacityUnits: 1, // optional
151156
useUndispatchedEventsTable: true // optional
152157
eventsTableStreamEnabled: false // optional
153-
eventsTableStreamViewType: 'NEW_IMAGE' // optional
158+
eventsTableStreamViewType: 'NEW_IMAGE', // optional
159+
emitStoreEvents: true // optional, by default no store events are emitted
154160
});
155161

156162
DynamoDB credentials are obtained by eventstore either from environment vars or credentials file. For setup see [AWS Javascript SDK](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html).
@@ -587,6 +593,78 @@ But if you want you can trigger this from outside:
587593
});
588594
});
589595

596+
## Catch before and after eventstore events
597+
var eventstore = require('eventstore');
598+
var es = eventstore();
599+
600+
es.on('before-clear', function({milliseconds}) {});
601+
es.on('after-clear', function({milliseconds}) {});
602+
603+
es.on('before-get-next-positions', function({milliseconds, arguments: [positions]}) {});
604+
es.on('after-get-next-positions', function({milliseconds, arguments: [positions]}) {});
605+
606+
es.on('before-add-events', function({milliseconds, arguments: [events]}) {});
607+
es.on('after-add-events', function(milliseconds, arguments: [events]) {});
608+
609+
es.on('before-get-events', function({milliseconds, arguments: [query, skip, limit]}) {});
610+
es.on('after-get-events', function({milliseconds, arguments: [query, skip, limit]}) {});
611+
612+
es.on('before-get-events-since', function({milliseconds, arguments: [milliseconds, date, skip, limit]}) {});
613+
es.on('after-get-events-since', function({milliseconds, arguments: [date, skip, limit]}) {});
614+
615+
es.on('before-get-events-by-revision', function({milliseconds, arguments: [query, revMin, revMax]}) {});
616+
es.on('after-get-events-by-revision', function({milliseconds, arguments, [query, revMin, revMax]}) {});
617+
618+
es.on('before-get-last-event', function({milliseconds, arguments: [query]}) {});
619+
es.on('after-get-last-event', function({milliseconds, arguments: [query]}) {});
620+
621+
es.on('before-get-undispatched-events', function({milliseconds, arguments: [query]}) {});
622+
es.on('after-get-undispatched-events', function({milliseconds, arguments: [query]}) {});
623+
624+
es.on('before-set-event-to-dispatched', function({milliseconds, arguments: [id]}) {});
625+
es.on('after-set-event-to-dispatched', function({milliseconds, arguments: [id]}) {});
626+
627+
es.on('before-add-snapshot', function({milliseconds, arguments: [snap]}) {});
628+
es.on('after-add-snapshot', function({milliseconds, arguments: [snap]}) {});
629+
630+
es.on('before-clean-snapshots', function({milliseconds, arguments: [query]}) {});
631+
es.on('after-clean-snapshots', function({milliseconds, arguments: [query]}) {});
632+
633+
es.on('before-get-snapshot', function({milliseconds, arguments: [query, revMax]}) {});
634+
es.on('after-get-snapshot', function({milliseconds, arguments: [query, revMax]}) {});
635+
636+
es.on('before-remove-transactions', function({milliseconds}) {});
637+
es.on('after-remove-transactions', function({milliseconds}) {});
638+
639+
es.on('before-get-pending-transactions', function({milliseconds}) {});
640+
es.on('after-get-pending-transactions', function({milliseconds}) {});
641+
642+
es.on('before-repair-failed-transactions', function({milliseconds, arguments: [lastEvt]}) {});
643+
es.on('after-repair-failed-transactions', function({milliseconds, arguments: [lastEvt]}) {});
644+
645+
es.on('before-remove-tables', function({milliseconds}) {});
646+
es.on('after-remove-tables', function({milliseconds}) {});
647+
648+
es.on('before-stream-events', function({milliseconds, arguments: [query, skip, limit]}) {});
649+
es.on('after-stream-events', function({milliseconds, arguments: [query, skip, limit]}) {});
650+
651+
es.on('before-stream-events-since', function({milliseconds}) {});
652+
es.on('after-stream-events-since', function({milliseconds}) {});
653+
654+
es.on('before-get-event-stream', function({milliseconds, parentEventId, arguments: [query, revMin, revMax]}) {});
655+
es.on('after-get-event-stream', function({milliseconds, parentEventId, arguments: [query, revMin, revMax]}) {});
656+
657+
es.on('before-get-from-snapshot', function({milliseconds, parentEventId, arguments: [query, revMax]}) {});
658+
es.on('after-get-from-snapshot', function({milliseconds, parentEventId, arguments: [query, revMax]}) {});
659+
660+
es.on('before-create-snapshot', function({milliseconds, parentEventId, arguments: [obj]}) {});
661+
es.on('after-create-snapshot', function({milliseconds, parentEventId, arguments: [obj]}) {});
662+
663+
es.on('before-commit', function({milliseconds, parentEventId, arguments: [eventstream]}) {});
664+
es.on('after-commit', function({milliseconds, parentEventId, arguments: [eventstream]}) {});
665+
666+
es.on('before-get-last-event-as-stream', function({milliseconds, arguments: [query]}) {});
667+
es.on('after-get-last-event-as-stream', function({milliseconds, arguments: [query]}) {});
590668

591669
# Sample Integration
592670

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var Eventstore = require('./lib/eventstore'),
22
Base = require('./lib/base'),
33
_ = require('lodash'),
4-
debug = require('debug')('eventstore');
4+
debug = require('debug')('eventstore'),
5+
StoreEventEmitter = require('./lib/storeEventEmitter');
56

67
function exists(toCheck) {
78
var _exists = require('fs').existsSync || require('path').existsSync;
@@ -69,7 +70,14 @@ module.exports = function(options) {
6970
throw err;
7071
}
7172

72-
return new Eventstore(options, new Store(options));
73+
var eventstore = new Eventstore(options, new Store(options));
74+
75+
if (options.emitStoreEvents) {
76+
var storeEventEmitter = new StoreEventEmitter(eventstore);
77+
storeEventEmitter.addEventEmitter();
78+
}
79+
80+
return eventstore;
7381
};
7482

7583
module.exports.Store = Base;

0 commit comments

Comments
 (0)