tapeworm 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE +22 -22
- package/README.md +332 -332
- package/biome.json +58 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/src/dispatch/dispatch_service.d.ts +8 -0
- package/dist/src/dispatch/dispatch_service.d.ts.map +1 -0
- package/dist/src/dispatch/dispatch_service.js +9 -0
- package/dist/src/dispatch/dispatch_service.js.map +1 -0
- package/dist/src/event.d.ts +10 -0
- package/dist/src/event.d.ts.map +1 -0
- package/dist/src/event.js +12 -0
- package/dist/src/event.js.map +1 -0
- package/dist/src/event_store.d.ts +29 -0
- package/dist/src/event_store.d.ts.map +1 -0
- package/dist/src/event_store.js +71 -0
- package/dist/src/event_store.js.map +1 -0
- package/dist/src/event_store_partition.d.ts +28 -0
- package/dist/src/event_store_partition.d.ts.map +1 -0
- package/dist/src/event_store_partition.js +130 -0
- package/dist/src/event_store_partition.js.map +1 -0
- package/dist/src/event_stream.d.ts +32 -0
- package/dist/src/event_stream.d.ts.map +1 -0
- package/dist/src/event_stream.js +142 -0
- package/dist/src/event_stream.js.map +1 -0
- package/dist/src/persistence/commit.d.ts +4 -0
- package/dist/src/persistence/commit.d.ts.map +1 -0
- package/dist/src/persistence/commit.js +11 -0
- package/dist/src/persistence/commit.js.map +1 -0
- package/dist/src/persistence/concurrency_error.d.ts +6 -0
- package/dist/src/persistence/concurrency_error.d.ts.map +1 -0
- package/dist/src/persistence/concurrency_error.js +13 -0
- package/dist/src/persistence/concurrency_error.js.map +1 -0
- package/dist/src/persistence/duplicate_commit_error.d.ts +6 -0
- package/dist/src/persistence/duplicate_commit_error.d.ts.map +1 -0
- package/dist/src/persistence/duplicate_commit_error.js +13 -0
- package/dist/src/persistence/duplicate_commit_error.js.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_partition.d.ts +23 -0
- package/dist/src/persistence/inmemory/inmemory_partition.d.ts.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_partition.js +138 -0
- package/dist/src/persistence/inmemory/inmemory_partition.js.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.d.ts +13 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.d.ts.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.js +26 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.js.map +1 -0
- package/dist/src/persistence/persistence_partition.d.ts +12 -0
- package/dist/src/persistence/persistence_partition.d.ts.map +1 -0
- package/dist/src/persistence/persistence_partition.js +21 -0
- package/dist/src/persistence/persistence_partition.js.map +1 -0
- package/dist/src/persistence/persistence_provider_template.d.ts +10 -0
- package/dist/src/persistence/persistence_provider_template.d.ts.map +1 -0
- package/dist/src/persistence/persistence_provider_template.js +11 -0
- package/dist/src/persistence/persistence_provider_template.js.map +1 -0
- package/dist/src/replication/master_replication_service.d.ts +7 -0
- package/dist/src/replication/master_replication_service.d.ts.map +1 -0
- package/dist/src/replication/master_replication_service.js +9 -0
- package/dist/src/replication/master_replication_service.js.map +1 -0
- package/dist/src/types.d.ts +68 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/index.ts +21 -0
- package/package.json +44 -46
- package/scripts/postbuild.js +27 -0
- package/src/dispatch/dispatch_service.ts +14 -0
- package/src/event.ts +24 -0
- package/src/event_store.ts +82 -0
- package/src/event_store_partition.ts +270 -0
- package/src/event_stream.ts +184 -0
- package/src/persistence/commit.ts +24 -0
- package/src/persistence/concurrency_error.ts +16 -0
- package/src/persistence/duplicate_commit_error.ts +16 -0
- package/src/persistence/inmemory/inmemory_partition.ts +194 -0
- package/src/persistence/inmemory/inmemory_persistence.ts +35 -0
- package/src/persistence/persistence_partition.ts +34 -0
- package/src/persistence/persistence_provider_template.ts +20 -0
- package/src/replication/master_replication_service.ts +12 -0
- package/src/types.ts +128 -0
- package/test/event_partition.ts +151 -0
- package/test/event_store.ts +25 -0
- package/test/event_stream.ts +160 -0
- package/test/persistence/inmemory.ts +139 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +13 -0
- package/.eslintrc +0 -4
- package/.travis.yml +0 -4
- package/checkstyle-result.xml +0 -1
- package/index.js +0 -1
- package/lib/dispatch/dispatch_service.js +0 -7
- package/lib/event.js +0 -10
- package/lib/event_store.js +0 -53
- package/lib/event_store_partition.js +0 -148
- package/lib/event_stream.js +0 -150
- package/lib/persistence/commit.js +0 -12
- package/lib/persistence/concurrency_error.js +0 -10
- package/lib/persistence/duplicate_commit_error.js +0 -10
- package/lib/persistence/inmemory/inmemory_partition.js +0 -146
- package/lib/persistence/inmemory/inmemory_persistence.js +0 -25
- package/lib/persistence/persistence_partition.js +0 -23
- package/lib/persistence/persistence_provider_template.js +0 -17
- package/lib/replication/master_replication_service.js +0 -7
- package/test/event_partition.js +0 -232
- package/test/event_store.js +0 -36
- package/test/event_stream.js +0 -214
- package/test/persistence/inmemory.js +0 -162
package/lib/event_stream.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
var Promise = require('bluebird');
|
|
2
|
-
var Commit = require('./persistence/commit');
|
|
3
|
-
var _ = require('lodash');
|
|
4
|
-
|
|
5
|
-
// writeOnly - will never read entire stream from
|
|
6
|
-
var EventStream = function (eventPartition, streamId, writeOnly) {
|
|
7
|
-
if (streamId === undefined) {
|
|
8
|
-
throw new Error('StreamId must be defined!');
|
|
9
|
-
}
|
|
10
|
-
this._partition = eventPartition;
|
|
11
|
-
this._streamId = streamId;
|
|
12
|
-
this._writeOnly = writeOnly;
|
|
13
|
-
this._uncommittedEvents = [];
|
|
14
|
-
this._committedEvents = [];
|
|
15
|
-
this._version = 0;
|
|
16
|
-
this._isDeleted = false;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
EventStream.prototype._prepareStream = function (callback) {
|
|
20
|
-
this._committedEvents = [];
|
|
21
|
-
this._commitSequence = -1;
|
|
22
|
-
var self = this;
|
|
23
|
-
if (self._writeOnly === true && self._partition.getLatestCommit) {
|
|
24
|
-
// if stream should only be opened for appending commits, only really care about getting the correct commitSequence (from last commit)
|
|
25
|
-
return self._partition
|
|
26
|
-
.getLatestCommit(self._streamId)
|
|
27
|
-
.then(function (commit) {
|
|
28
|
-
if (commit) {
|
|
29
|
-
self._commitSequence = commit.commitSequence;
|
|
30
|
-
var lastEvent = commit.events[commit.events.length - 1];
|
|
31
|
-
if (lastEvent && lastEvent.type === '$stream.deleted.event') {
|
|
32
|
-
self._isDeleted = true;
|
|
33
|
-
}
|
|
34
|
-
self._version = lastEvent.version + 1;
|
|
35
|
-
}
|
|
36
|
-
// if no commit is found, assume its a new stream, and keep default versions
|
|
37
|
-
})
|
|
38
|
-
.then(function () {
|
|
39
|
-
return self;
|
|
40
|
-
});
|
|
41
|
-
} else {
|
|
42
|
-
return self._partition
|
|
43
|
-
._queryStream(self._streamId, callback)
|
|
44
|
-
.then(function (commits) {
|
|
45
|
-
self._version = 0;
|
|
46
|
-
|
|
47
|
-
if (!commits || commits.length === 0) {
|
|
48
|
-
commits = [];
|
|
49
|
-
self._version = -1;
|
|
50
|
-
} else {
|
|
51
|
-
if (commits[0] && commits[0].events && commits[0].events[0] && commits[0].events[0].type === '$stream.deleted.event') {
|
|
52
|
-
throw new Error('Stream is deleted');
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
var version = 0;
|
|
56
|
-
for (var i = 0; i < commits.length; i++) {
|
|
57
|
-
self._commitSequence++;
|
|
58
|
-
for (var j = 0; j < commits[i].events.length; j++) {
|
|
59
|
-
self._version++;
|
|
60
|
-
commits[i].events[j].version = version++;
|
|
61
|
-
self._committedEvents.push(commits[i].events[j]);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
.then(function () {
|
|
66
|
-
return self;
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
EventStream.prototype.getVersion = function () {
|
|
72
|
-
return this._version;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
EventStream.prototype.append = function (event) {
|
|
76
|
-
this._uncommittedEvents.push(event);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
EventStream.prototype.hasChanges = function () {
|
|
80
|
-
return this._uncommittedEvents.length > 0;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
EventStream.prototype.commit = function (commitId, callback) {
|
|
84
|
-
var self = this;
|
|
85
|
-
|
|
86
|
-
if (this._isDeleted) {
|
|
87
|
-
throw new Error(
|
|
88
|
-
'Stream is deleted, unable to commit: ' +
|
|
89
|
-
this._uncommittedEvents.map(function (event) {
|
|
90
|
-
return event.type;
|
|
91
|
-
})
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (!this.hasChanges()) {
|
|
96
|
-
//nothing to commit
|
|
97
|
-
return Promise.resolve().nodeify(callback);
|
|
98
|
-
} else {
|
|
99
|
-
var commit = this._buildCommit(commitId, this._uncommittedEvents);
|
|
100
|
-
return this._partition.append(commit, callback).then(function (commit) {
|
|
101
|
-
//rebuild local state
|
|
102
|
-
var events = self._uncommittedEvents;
|
|
103
|
-
self._version = events[events.length - 1].version + 1;
|
|
104
|
-
if (self._writeOnly === true) {
|
|
105
|
-
self._commitSequence++;
|
|
106
|
-
self._clearChanges();
|
|
107
|
-
} else {
|
|
108
|
-
for (var i = 0; i < events.length; i++) {
|
|
109
|
-
self._committedEvents.push(events[i]);
|
|
110
|
-
}
|
|
111
|
-
self._clearChanges();
|
|
112
|
-
self._commitSequence++;
|
|
113
|
-
return self;
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
EventStream.prototype._clearChanges = function () {
|
|
120
|
-
this._uncommittedEvents = [];
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
EventStream.prototype.revertChanges = function () {
|
|
124
|
-
//trunc the uncomitted events log
|
|
125
|
-
this._uncommittedEvents = [];
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
EventStream.prototype._buildCommit = function (commitId, events) {
|
|
129
|
-
var commitSequence = this._commitSequence;
|
|
130
|
-
var commit = new Commit(commitId, this._partition._partitionId, this._streamId, ++commitSequence, events);
|
|
131
|
-
var version = this._version == -1 ? 0 : this._version;
|
|
132
|
-
|
|
133
|
-
_.forEach(events, function (evt) {
|
|
134
|
-
evt.version = version++;
|
|
135
|
-
});
|
|
136
|
-
return commit;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
EventStream.prototype.getCommittedEvents = function () {
|
|
140
|
-
if (this._writeOnly) {
|
|
141
|
-
throw new Error('Cannot access committed events when using writeOnly mode...');
|
|
142
|
-
}
|
|
143
|
-
return this._committedEvents.slice();
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
EventStream.prototype.getUncommittedEvents = function () {
|
|
147
|
-
return this._uncommittedEvents.slice();
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
module.exports = EventStream;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
var Commit = function(id, partitionId, streamId, commitSequence, events)
|
|
2
|
-
{
|
|
3
|
-
this.id = id;
|
|
4
|
-
this.partitionId = partitionId;
|
|
5
|
-
this.streamId = streamId;
|
|
6
|
-
this.commitSequence = commitSequence;
|
|
7
|
-
this.events = events;
|
|
8
|
-
//this.eventTypes = [];
|
|
9
|
-
//this.metadata = {};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
module.exports = Commit;
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
var _ = require('lodash');
|
|
2
|
-
var Promise = require('bluebird');
|
|
3
|
-
var ConcurrencyError = require('../concurrency_error');
|
|
4
|
-
var DuplicateCommitError = require('../duplicate_commit_error');
|
|
5
|
-
|
|
6
|
-
var InMemoryPartition = function () {
|
|
7
|
-
this._commits = [];
|
|
8
|
-
this._streamIndex = {};
|
|
9
|
-
this._commitIds = [];
|
|
10
|
-
this._commitConcurrencyCheck = [];
|
|
11
|
-
this._snapshots = {};
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
function getConcurrencyKey(commit) {
|
|
15
|
-
return commit.streamId + '-' + commit.commitSequence;
|
|
16
|
-
}
|
|
17
|
-
InMemoryPartition.prototype._promisify = function (value, callback) {
|
|
18
|
-
return Promise.resolve(value).nodeify(callback);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
InMemoryPartition.prototype.truncateStreamFrom = function (streamId, commitSequence, callback) {
|
|
22
|
-
var self = this;
|
|
23
|
-
var commits = Array.from(this._streamIndex[streamId]);
|
|
24
|
-
for (var i = 0; i < commits.length; i++) {
|
|
25
|
-
var commit = commits[i];
|
|
26
|
-
|
|
27
|
-
if (commit.commitSequence >= commitSequence) {
|
|
28
|
-
//remove from commitId
|
|
29
|
-
self._commitIds = _.without(self._commitIds, commit.id);
|
|
30
|
-
self._commitConcurrencyCheck = _.without(self._commitConcurrencyCheck, getConcurrencyKey(commit));
|
|
31
|
-
self._commits = _.without(self._commits, commit);
|
|
32
|
-
// i--;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
commits = commits.slice(0, Math.max(commitSequence, 0));
|
|
36
|
-
this._streamIndex[streamId] = commits;
|
|
37
|
-
return this._promisify(this);
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// InMemoryPartition.prototype.delete = function (streamId, headers, callback) {
|
|
41
|
-
|
|
42
|
-
// }
|
|
43
|
-
|
|
44
|
-
InMemoryPartition.prototype.applyCommitHeader = function (commitId, header, callback) {
|
|
45
|
-
var self = this;
|
|
46
|
-
var commit = _.find(this._commits, { id: commitId });
|
|
47
|
-
if (commit) {
|
|
48
|
-
_.assign(commit, header);
|
|
49
|
-
} else {
|
|
50
|
-
throw new Error('Trying to apply header to missing commit: ' + commitId);
|
|
51
|
-
}
|
|
52
|
-
return this._promisify(commit, callback);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
InMemoryPartition.prototype.append = function (commit, callback) {
|
|
56
|
-
commit.isDispatched = false;
|
|
57
|
-
//check for duplicates
|
|
58
|
-
if (_.includes(this._commitIds, commit.id)) {
|
|
59
|
-
throw new DuplicateCommitError('Duplicate commit of ' + commit.id);
|
|
60
|
-
}
|
|
61
|
-
var concurrencyKey = getConcurrencyKey(commit);
|
|
62
|
-
if (_.includes(this._commitConcurrencyCheck, concurrencyKey)) {
|
|
63
|
-
throw new ConcurrencyError('Concurrency error on stream ' + commit.streamId);
|
|
64
|
-
}
|
|
65
|
-
//check concurrency
|
|
66
|
-
this._commits.push(commit);
|
|
67
|
-
this._commitIds.push(commit.id);
|
|
68
|
-
this._commitConcurrencyCheck.push(concurrencyKey);
|
|
69
|
-
var index = this._streamIndex[commit.streamId];
|
|
70
|
-
if (!index) {
|
|
71
|
-
index = this._streamIndex[commit.streamId] = [];
|
|
72
|
-
}
|
|
73
|
-
index.push(commit);
|
|
74
|
-
return this._promisify(commit, callback);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
InMemoryPartition.prototype.storeSnapshot = function (streamId, snapshot, version, callback) {
|
|
78
|
-
return this._promisify((this._snapshots[streamId] = { id: streamId, version: version, snapshot: snapshot }), callback);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// Loads the latest snapshot
|
|
82
|
-
InMemoryPartition.prototype.loadSnapshot = function (streamId, callback) {
|
|
83
|
-
return this._promisify(this._snapshots[streamId], callback);
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
InMemoryPartition.prototype.markAsDispatched = function (commit, callback) {
|
|
87
|
-
commit.isDispatched = true;
|
|
88
|
-
return this._promisify(commit, callback);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
InMemoryPartition.prototype.getUndispatched = function (callback) {
|
|
92
|
-
var commits = this.queryAll;
|
|
93
|
-
var undispatched = [];
|
|
94
|
-
for (var i = 0; i < commits.length; i++) {
|
|
95
|
-
if (!commits[i].isDispatched) {
|
|
96
|
-
undispatched.push(commit);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return this._promisify(undispatched, callback);
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
InMemoryPartition.prototype.queryAll = function (callback) {
|
|
103
|
-
return this._promisify(this._commits.slice(), callback);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
InMemoryPartition.prototype.getLatestCommit = function (streamId, callback) {
|
|
107
|
-
var result = this._streamIndex[streamId];
|
|
108
|
-
if (result) {
|
|
109
|
-
result = result.slice().pop();
|
|
110
|
-
}
|
|
111
|
-
return this._promisify(result, callback);
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
InMemoryPartition.prototype.queryStream = function (streamId, fromEventSequence, callback) {
|
|
115
|
-
if (_.isFunction(fromEventSequence)) {
|
|
116
|
-
callback = fromEventSequence;
|
|
117
|
-
fromEventSequence = 0;
|
|
118
|
-
}
|
|
119
|
-
var result = this._streamIndex[streamId];
|
|
120
|
-
|
|
121
|
-
if (result) {
|
|
122
|
-
result = result.slice();
|
|
123
|
-
if (fromEventSequence > 0) {
|
|
124
|
-
var startCommitId = 0;
|
|
125
|
-
var foundEvents = 0;
|
|
126
|
-
for (var i = 0; i < result.length; i++) {
|
|
127
|
-
foundEvents += result[0].events.length;
|
|
128
|
-
startCommitId++;
|
|
129
|
-
if (foundEvents >= fromEventSequence) {
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
var tooMany = foundEvents - fromEventSequence;
|
|
134
|
-
|
|
135
|
-
result = result.slice(startCommitId - (tooMany > 0 ? 1 : 0));
|
|
136
|
-
if (tooMany > 0) {
|
|
137
|
-
result[0] = _.clone(result[0]); // avoid modifying reference
|
|
138
|
-
result[0].events = result[0].events.slice(result[0].events.length - tooMany);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return this._promisify(result, callback);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
module.exports = InMemoryPartition;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
var Promise = require('bluebird');
|
|
2
|
-
var Partition = require('./inmemory_partition');
|
|
3
|
-
|
|
4
|
-
var InMemoryPersistence = function InMemoryPersistance() {
|
|
5
|
-
this._partitions = [];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
InMemoryPersistence.prototype._promisify = function(value, callback) {
|
|
9
|
-
return Promise.resolve(value).nodeify(callback);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
InMemoryPersistence.prototype.openPartition = function(partitionId, callback) {
|
|
13
|
-
return this._promisify(this._getPartition(partitionId), callback);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
InMemoryPersistence.prototype._getPartition = function(partitionId) {
|
|
17
|
-
var currentPartitionId = partitionId || 'master';
|
|
18
|
-
var partition = this._partitions[currentPartitionId];
|
|
19
|
-
if (partition == null) {
|
|
20
|
-
partition = this._partitions[currentPartitionId] = new Partition();
|
|
21
|
-
}
|
|
22
|
-
return partition;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
module.exports = InMemoryPersistence;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
var PersistencePartition = function() {
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
PersistencePartition.prototype.append = function(commit, callback) {
|
|
6
|
-
//add commit
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
PersistencePartition.prototype.getUndispatched = function(callback) {
|
|
10
|
-
//return events which are not dispatched
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
PersistencePartition.prototype.markAsDispatched = function(commit, callback) {
|
|
14
|
-
//return events which are not dispatched
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
PersistencePartition.prototype.queryAll = function() {
|
|
18
|
-
//return Query
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
PersistencePartition.prototype.queryStream = function(streamId) {
|
|
22
|
-
//return Query;
|
|
23
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
var PersistenceProvider = function() {
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
PersistenceProvider.prototype.openPartition = function(partitionId, callback) {
|
|
7
|
-
// body...
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
PersistenceProvider.prototype.deletePartition = function(partitionId, callback) {
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
PersistenceProvider.prototype.close = function(callback) {
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
|
package/test/event_partition.js
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
var should = require('should');
|
|
2
|
-
var Promise = require('bluebird');
|
|
3
|
-
var uuid = require('uuid').v4;
|
|
4
|
-
|
|
5
|
-
var EventStore = require('..');
|
|
6
|
-
var Commit = EventStore.Commit;
|
|
7
|
-
|
|
8
|
-
describe('Partition', function () {
|
|
9
|
-
describe('#append', function (done) {
|
|
10
|
-
it('should return commit if added', function (done) {
|
|
11
|
-
var es = new EventStore();
|
|
12
|
-
es.openPartition('location')
|
|
13
|
-
.then(function (partition) {
|
|
14
|
-
partition.append(new Commit('1', 'location', '1', 0, [])).then(function (c) {
|
|
15
|
-
c[0].events.length.should.equal(0);
|
|
16
|
-
done();
|
|
17
|
-
});
|
|
18
|
-
})
|
|
19
|
-
.catch(function (err) {
|
|
20
|
-
done(err);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
it('should return after all commits are persisted', function (done) {
|
|
24
|
-
var es = new EventStore();
|
|
25
|
-
es.openPartition('location')
|
|
26
|
-
.then(function (partition) {
|
|
27
|
-
return partition.append([new Commit('1', 'location', '1', 0, []), new Commit('2', 'location', '1', 1, [])]).then(function (c) {
|
|
28
|
-
c.length.should.equal(2);
|
|
29
|
-
done();
|
|
30
|
-
});
|
|
31
|
-
})
|
|
32
|
-
.catch(function (err) {
|
|
33
|
-
done(err);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
describe('#_truncateStreamFrom', function () {
|
|
38
|
-
it('should remove all commits ', function (done) {
|
|
39
|
-
var es = new EventStore();
|
|
40
|
-
es.openPartition('location')
|
|
41
|
-
.then(function (partition) {
|
|
42
|
-
return partition.append([new Commit('1', 'location', '1', 0, []), new Commit('2', 'location', '1', 1, [])]).then(function (c) {
|
|
43
|
-
return partition._truncateStreamFrom('1', 0).then(function () {
|
|
44
|
-
return partition.openStream('1').then(function (stream) {
|
|
45
|
-
stream.getVersion().should.equal(-1);
|
|
46
|
-
done();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
})
|
|
51
|
-
.catch(function (err) {
|
|
52
|
-
done(err);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
it('should remove all commits after commitSequence', function (done) {
|
|
56
|
-
var es = new EventStore();
|
|
57
|
-
es.openPartition('location')
|
|
58
|
-
.then(function (partition) {
|
|
59
|
-
return partition.append([new Commit('1', 'location', '1', 0, [{}]), new Commit('2', 'location', '1', 1, [{}])]).then(function (c) {
|
|
60
|
-
return partition._truncateStreamFrom('1', 1).then(function () {
|
|
61
|
-
return partition.openStream('1').then(function (stream) {
|
|
62
|
-
stream.getVersion().should.equal(1);
|
|
63
|
-
done();
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
})
|
|
68
|
-
.catch(function (err) {
|
|
69
|
-
done(err);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
describe('#_applyCommitHeader', function () {
|
|
74
|
-
it('should add to commit ', function (done) {
|
|
75
|
-
var es = new EventStore();
|
|
76
|
-
es.openPartition('location')
|
|
77
|
-
.then(function (partition) {
|
|
78
|
-
var commit = new Commit('1', 'location', '1', 0, []);
|
|
79
|
-
return partition.append([commit, new Commit('2', 'location', '1', 1, [])]).then(function (c) {
|
|
80
|
-
return partition._applyCommitHeader(commit.id, { authorative: true }).then(function (commit) {
|
|
81
|
-
commit.authorative.should.be.ok;
|
|
82
|
-
done();
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
})
|
|
86
|
-
.catch(function (err) {
|
|
87
|
-
done(err);
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
it('should throw if commit id is unknown', function (done) {
|
|
91
|
-
var es = new EventStore();
|
|
92
|
-
es.openPartition('location')
|
|
93
|
-
.then(function (partition) {
|
|
94
|
-
var commit = new Commit('1', 'location', '1', 0, []);
|
|
95
|
-
return partition.append([commit, new Commit('2', 'location', '1', 1, [])]).then(function (c) {
|
|
96
|
-
return partition._applyCommitHeader('ID MISSING', { authorative: true }).then(function (commit) {
|
|
97
|
-
done(new Error('Unreachable code'));
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
})
|
|
101
|
-
.catch(function (err) {
|
|
102
|
-
done();
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
describe('#queryStreamWithSnapshot', function () {
|
|
107
|
-
it('queryStreamWithSnapshot should return snapshot and missing commits', function (done) {
|
|
108
|
-
var es = new EventStore();
|
|
109
|
-
var streamId = '1';
|
|
110
|
-
var stream;
|
|
111
|
-
es.openPartition('location')
|
|
112
|
-
.call('openStream', streamId)
|
|
113
|
-
.then(function (stream1) {
|
|
114
|
-
stream = stream1;
|
|
115
|
-
stream.append({ event: '123' });
|
|
116
|
-
stream.append({ event: '999' });
|
|
117
|
-
return stream.commit(uuid());
|
|
118
|
-
})
|
|
119
|
-
.then(function () {
|
|
120
|
-
stream.append({ event: '666' });
|
|
121
|
-
stream.append({ event: '777' });
|
|
122
|
-
return stream.commit(uuid());
|
|
123
|
-
})
|
|
124
|
-
.then(function () {
|
|
125
|
-
es.openPartition('location').then(function (part) {
|
|
126
|
-
part.storeSnapshot(streamId, { test: 'snapshot' }, 2);
|
|
127
|
-
part.queryStreamWithSnapshot(streamId).then(function (res) {
|
|
128
|
-
res.snapshot.version.should.eql(2);
|
|
129
|
-
res.commits.length.should.eql(1);
|
|
130
|
-
res.commits[0].events.length.should.eql(2);
|
|
131
|
-
res.commits[0].events[0].version.should.eql(2);
|
|
132
|
-
done();
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
})
|
|
136
|
-
.catch(function (err) {
|
|
137
|
-
done(err);
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
it('queryStreamWithSnapshot should return snapshot and no commit if up to date', function (done) {
|
|
141
|
-
var es = new EventStore();
|
|
142
|
-
var streamId = '1';
|
|
143
|
-
es.openPartition('location')
|
|
144
|
-
.call('openStream', streamId)
|
|
145
|
-
.then(function (stream) {
|
|
146
|
-
stream.append({ event: '123' });
|
|
147
|
-
stream.append({ event: '999' });
|
|
148
|
-
return stream.commit(uuid());
|
|
149
|
-
})
|
|
150
|
-
.then(function () {
|
|
151
|
-
es.openPartition('location').then(function (part) {
|
|
152
|
-
part.storeSnapshot(streamId, { test: 'snapshot' }, 2);
|
|
153
|
-
part.queryStreamWithSnapshot(streamId, function (err, res) {
|
|
154
|
-
res.snapshot.version.should.eql(2);
|
|
155
|
-
res.commits.length.should.eql(0);
|
|
156
|
-
done();
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
})
|
|
160
|
-
.catch(function (err) {
|
|
161
|
-
done(err);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
describe('#delete', function () {
|
|
166
|
-
it('should delete stream and all commits', function (done) {
|
|
167
|
-
var didIGetaDeleteEvent = false;
|
|
168
|
-
var es = new EventStore(null, (commit) => {
|
|
169
|
-
if (commit.events[0].type === '$stream.deleted.event') {
|
|
170
|
-
didIGetaDeleteEvent = true;
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
es.openPartition('location')
|
|
174
|
-
.then(function (partition) {
|
|
175
|
-
return partition
|
|
176
|
-
.append([new Commit('1', 'location', '1', 0, [{ type: 'dummy.event' }]), new Commit('2', 'location', '1', 1, [{ type: 'dummy2.event' }])])
|
|
177
|
-
.then(function (c) {
|
|
178
|
-
return partition.delete('1', { some: 'header-value', payload: { test: true }, type: 'fail' }).then(function () {
|
|
179
|
-
return partition
|
|
180
|
-
.openStream('1')
|
|
181
|
-
.then(function (stream) {
|
|
182
|
-
console.log(JSON.stringify(stream, null, 2));
|
|
183
|
-
done(new Error('able to open deleted stream'));
|
|
184
|
-
})
|
|
185
|
-
.catch(function (error) {
|
|
186
|
-
error.message.should.equal('Stream is deleted');
|
|
187
|
-
didIGetaDeleteEvent.should.be.true;
|
|
188
|
-
done();
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
})
|
|
193
|
-
.catch(function (err) {
|
|
194
|
-
done(err);
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('should delete and placeholder commit should have an id', function (done) {
|
|
199
|
-
var didIGetaDeleteEvent = false;
|
|
200
|
-
var es = new EventStore(null, (commit) => {
|
|
201
|
-
console.log(commit.id);
|
|
202
|
-
if (commit.events[0].type === '$stream.deleted.event' && commit.id) {
|
|
203
|
-
didIGetaDeleteEvent = true;
|
|
204
|
-
}
|
|
205
|
-
should(commit.id).not.be.null;
|
|
206
|
-
});
|
|
207
|
-
es.openPartition('location')
|
|
208
|
-
.then(function (partition) {
|
|
209
|
-
return partition
|
|
210
|
-
.append([new Commit('1', 'location', '1', 0, [{ type: 'dummy.event' }]), new Commit('2', 'location', '1', 1, [{ type: 'dummy2.event' }])])
|
|
211
|
-
.then(function (c) {
|
|
212
|
-
return partition.delete('1', { some: 'header-value', payload: { test: true }, type: 'fail' }).then(function () {
|
|
213
|
-
return partition
|
|
214
|
-
.openStream('1')
|
|
215
|
-
.then(function (stream) {
|
|
216
|
-
console.log(JSON.stringify(stream, null, 2));
|
|
217
|
-
done(new Error('able to open deleted stream'));
|
|
218
|
-
})
|
|
219
|
-
.catch(function (error) {
|
|
220
|
-
error.message.should.equal('Stream is deleted');
|
|
221
|
-
didIGetaDeleteEvent.should.be.true;
|
|
222
|
-
done();
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
})
|
|
227
|
-
.catch(function (err) {
|
|
228
|
-
done(err);
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
});
|
package/test/event_store.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
var should = require('should');
|
|
2
|
-
var Promise = require('bluebird');
|
|
3
|
-
|
|
4
|
-
var EventStore = require('..');
|
|
5
|
-
|
|
6
|
-
describe('event_store', function() {
|
|
7
|
-
describe('#openPartition', function(done) {
|
|
8
|
-
it('should return non null partition when using new id', function(done) {
|
|
9
|
-
var es = new EventStore();
|
|
10
|
-
es.openPartition('location').then(function(partition) {
|
|
11
|
-
partition.should.not.be.null;
|
|
12
|
-
done();
|
|
13
|
-
}).catch(function(err) {
|
|
14
|
-
done(err);
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
it('should return same instance when called multiple times', function(done) {
|
|
18
|
-
var es = new EventStore();
|
|
19
|
-
Promise.join(es.openPartition('location'), es.openPartition('location'), function(p1, p2) {
|
|
20
|
-
p1.should.equal(p2);
|
|
21
|
-
done();
|
|
22
|
-
}).catch(function(err) {
|
|
23
|
-
done(err);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
it('should return different instances for different partitionIds', function(done) {
|
|
27
|
-
var es = new EventStore();
|
|
28
|
-
Promise.join(es.openPartition('location'), es.openPartition('location2'), function(p1, p2) {
|
|
29
|
-
p1.should.not.equal(p2);
|
|
30
|
-
done();
|
|
31
|
-
}).catch(function(err) {
|
|
32
|
-
done(err);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
});
|