@event-driven-io/emmett-mongodb 0.23.0-alpha.9 → 0.24.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/dist/index.cjs +315 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +52 -21
- package/dist/index.d.ts +52 -21
- package/dist/index.js +303 -11
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4;// ../emmett/dist/chunk-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4;// ../emmett/dist/chunk-4E7QLAH5.js
|
|
2
2
|
var isNumber = (val) => typeof val === "number" && val === val;
|
|
3
3
|
var isString = (val) => typeof val === "string";
|
|
4
|
+
var isErrorConstructor = (expect) => {
|
|
5
|
+
return typeof expect === "function" && expect.prototype && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
6
|
+
expect.prototype.constructor === expect;
|
|
7
|
+
};
|
|
4
8
|
var EmmettError = class _EmmettError extends Error {
|
|
5
9
|
|
|
6
10
|
constructor(options) {
|
|
@@ -15,7 +19,7 @@ var ConcurrencyError = class _ConcurrencyError extends EmmettError {
|
|
|
15
19
|
constructor(current, expected, message) {
|
|
16
20
|
super({
|
|
17
21
|
errorCode: 412,
|
|
18
|
-
message: _nullishCoalesce(message, () => ( `Expected version ${expected.toString()} does not match current ${_optionalChain([current, 'optionalAccess',
|
|
22
|
+
message: _nullishCoalesce(message, () => ( `Expected version ${expected.toString()} does not match current ${_optionalChain([current, 'optionalAccess', _2 => _2.toString, 'call', _3 => _3()])}`))
|
|
19
23
|
});
|
|
20
24
|
this.current = current;
|
|
21
25
|
this.expected = expected;
|
|
@@ -43,6 +47,16 @@ var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefa
|
|
|
43
47
|
|
|
44
48
|
|
|
45
49
|
|
|
50
|
+
async function tryPublishMessagesAfterCommit(messages, options, context) {
|
|
51
|
+
if (_optionalChain([options, 'optionalAccess', _4 => _4.onAfterCommit]) === void 0) return false;
|
|
52
|
+
try {
|
|
53
|
+
await _optionalChain([options, 'optionalAccess', _5 => _5.onAfterCommit, 'call', _6 => _6(messages, context)]);
|
|
54
|
+
return true;
|
|
55
|
+
} catch (error2) {
|
|
56
|
+
console.error(`Error in on after commit hook`, error2);
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
46
60
|
var STREAM_EXISTS = "STREAM_EXISTS";
|
|
47
61
|
var STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
|
|
48
62
|
var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
|
|
@@ -59,7 +73,7 @@ var assertExpectedVersionMatchesCurrent = (current, expected, defaultVersion) =>
|
|
|
59
73
|
};
|
|
60
74
|
var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends ConcurrencyError {
|
|
61
75
|
constructor(current, expected) {
|
|
62
|
-
super(_optionalChain([current, 'optionalAccess',
|
|
76
|
+
super(_optionalChain([current, 'optionalAccess', _7 => _7.toString, 'call', _8 => _8()]), _optionalChain([expected, 'optionalAccess', _9 => _9.toString, 'call', _10 => _10()]));
|
|
63
77
|
Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
|
|
64
78
|
}
|
|
65
79
|
};
|
|
@@ -73,9 +87,9 @@ var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfi
|
|
|
73
87
|
}
|
|
74
88
|
});_class.prototype.__init.call(this);_class.prototype.__init2.call(this);;
|
|
75
89
|
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
76
|
-
this.streamId = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
90
|
+
this.streamId = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _11 => _11.streamId]), () => ( _uuid.v4.call(void 0, )));
|
|
77
91
|
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
78
|
-
this.startChecking(_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
92
|
+
this.startChecking(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _12 => _12.intervalCheckInMs]), () => ( 20)));
|
|
79
93
|
}
|
|
80
94
|
__init() {this.checkInterval = null}
|
|
81
95
|
|
|
@@ -101,6 +115,72 @@ var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfi
|
|
|
101
115
|
}
|
|
102
116
|
}
|
|
103
117
|
}, _class);
|
|
118
|
+
var hasDuplicates = (array, predicate) => {
|
|
119
|
+
const mapped = array.map(predicate);
|
|
120
|
+
const uniqueValues = new Set(mapped);
|
|
121
|
+
return uniqueValues.size < mapped.length;
|
|
122
|
+
};
|
|
123
|
+
var getDuplicates = (array, predicate) => {
|
|
124
|
+
const map2 = /* @__PURE__ */ new Map();
|
|
125
|
+
for (let i = 0; i < array.length; i++) {
|
|
126
|
+
const item = array[i];
|
|
127
|
+
const key = predicate(item, i, array);
|
|
128
|
+
if (!map2.has(key)) {
|
|
129
|
+
map2.set(key, []);
|
|
130
|
+
}
|
|
131
|
+
map2.get(key).push(item);
|
|
132
|
+
}
|
|
133
|
+
return Array.from(map2.values()).filter((group) => group.length > 1).flat();
|
|
134
|
+
};
|
|
135
|
+
var merge = (array, item, where, onExisting, onNotFound = () => void 0) => {
|
|
136
|
+
let wasFound = false;
|
|
137
|
+
const result = array.map((p) => {
|
|
138
|
+
if (!where(p)) return p;
|
|
139
|
+
wasFound = true;
|
|
140
|
+
return onExisting(p);
|
|
141
|
+
}).filter((p) => p !== void 0).map((p) => {
|
|
142
|
+
if (!p) throw Error("That should not happen");
|
|
143
|
+
return p;
|
|
144
|
+
});
|
|
145
|
+
if (!wasFound) {
|
|
146
|
+
const result2 = onNotFound();
|
|
147
|
+
if (result2 !== void 0) return [...array, item];
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
};
|
|
151
|
+
var arrayUtils = {
|
|
152
|
+
merge,
|
|
153
|
+
hasDuplicates,
|
|
154
|
+
getDuplicates
|
|
155
|
+
};
|
|
156
|
+
var deepEquals = (left, right) => {
|
|
157
|
+
if (isEquatable(left)) {
|
|
158
|
+
return left.equals(right);
|
|
159
|
+
}
|
|
160
|
+
if (Array.isArray(left)) {
|
|
161
|
+
return Array.isArray(right) && left.length === right.length && left.every((val, index) => deepEquals(val, right[index]));
|
|
162
|
+
}
|
|
163
|
+
if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) {
|
|
164
|
+
return left === right;
|
|
165
|
+
}
|
|
166
|
+
if (Array.isArray(right)) return false;
|
|
167
|
+
const keys1 = Object.keys(left);
|
|
168
|
+
const keys2 = Object.keys(right);
|
|
169
|
+
if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key)))
|
|
170
|
+
return false;
|
|
171
|
+
for (const key in left) {
|
|
172
|
+
if (left[key] instanceof Function && right[key] instanceof Function)
|
|
173
|
+
continue;
|
|
174
|
+
const isEqual = deepEquals(left[key], right[key]);
|
|
175
|
+
if (!isEqual) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return true;
|
|
180
|
+
};
|
|
181
|
+
var isEquatable = (left) => {
|
|
182
|
+
return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
|
|
183
|
+
};
|
|
104
184
|
var asyncRetry = async (fn, opts) => {
|
|
105
185
|
if (opts === void 0 || opts.retries === 0) return fn();
|
|
106
186
|
return _asyncretry2.default.call(void 0,
|
|
@@ -108,7 +188,7 @@ var asyncRetry = async (fn, opts) => {
|
|
|
108
188
|
try {
|
|
109
189
|
return await fn();
|
|
110
190
|
} catch (error2) {
|
|
111
|
-
if (_optionalChain([opts, 'optionalAccess',
|
|
191
|
+
if (_optionalChain([opts, 'optionalAccess', _13 => _13.shouldRetryError]) && !opts.shouldRetryError(error2)) {
|
|
112
192
|
bail(error2);
|
|
113
193
|
}
|
|
114
194
|
throw error2;
|
|
@@ -117,6 +197,47 @@ var asyncRetry = async (fn, opts) => {
|
|
|
117
197
|
_nullishCoalesce(opts, () => ( { retries: 0 }))
|
|
118
198
|
);
|
|
119
199
|
};
|
|
200
|
+
var ParseError = class extends Error {
|
|
201
|
+
constructor(text) {
|
|
202
|
+
super(`Cannot parse! ${text}`);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
var JSONParser = {
|
|
206
|
+
stringify: (value, options) => {
|
|
207
|
+
return JSON.stringify(
|
|
208
|
+
_optionalChain([options, 'optionalAccess', _14 => _14.map]) ? options.map(value) : value,
|
|
209
|
+
//TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
|
|
210
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
211
|
+
(_, v) => typeof v === "bigint" ? v.toString() : v
|
|
212
|
+
);
|
|
213
|
+
},
|
|
214
|
+
parse: (text, options) => {
|
|
215
|
+
const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess', _15 => _15.reviver]));
|
|
216
|
+
if (_optionalChain([options, 'optionalAccess', _16 => _16.typeCheck]) && !_optionalChain([options, 'optionalAccess', _17 => _17.typeCheck, 'call', _18 => _18(parsed)]))
|
|
217
|
+
throw new ParseError(text);
|
|
218
|
+
return _optionalChain([options, 'optionalAccess', _19 => _19.map]) ? options.map(parsed) : parsed;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
var filterProjections = (type, projections2) => {
|
|
222
|
+
const inlineProjections2 = projections2.filter((projection2) => projection2.type === type).map(({ projection: projection2 }) => projection2);
|
|
223
|
+
const duplicateRegistrations = arrayUtils.getDuplicates(
|
|
224
|
+
inlineProjections2,
|
|
225
|
+
(proj) => proj.name
|
|
226
|
+
);
|
|
227
|
+
if (duplicateRegistrations.length > 0) {
|
|
228
|
+
throw new EmmettError(`You cannot register multiple projections with the same name (or without the name).
|
|
229
|
+
Ensure that:
|
|
230
|
+
${JSONParser.stringify(duplicateRegistrations)}
|
|
231
|
+
have different names`);
|
|
232
|
+
}
|
|
233
|
+
return inlineProjections2;
|
|
234
|
+
};
|
|
235
|
+
var inlineProjections = (definitions) => definitions.map((projection2) => ({ type: "inline", projection: projection2 }));
|
|
236
|
+
var asyncProjections = (definitions) => definitions.map((projection2) => ({ type: "async", projection: projection2 }));
|
|
237
|
+
var projections = {
|
|
238
|
+
inline: inlineProjections,
|
|
239
|
+
async: asyncProjections
|
|
240
|
+
};
|
|
120
241
|
var filter = (filter2) => new (0, _webstreamspolyfill.TransformStream)({
|
|
121
242
|
transform(chunk, controller) {
|
|
122
243
|
if (filter2(chunk)) {
|
|
@@ -258,6 +379,33 @@ var streamTransformations = {
|
|
|
258
379
|
waitAtMost
|
|
259
380
|
};
|
|
260
381
|
var { retry: retry2 } = streamTransformations;
|
|
382
|
+
var AssertionError = class extends Error {
|
|
383
|
+
constructor(message) {
|
|
384
|
+
super(message);
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
var isSubset = (superObj, subObj) => {
|
|
388
|
+
const sup = superObj;
|
|
389
|
+
const sub = subObj;
|
|
390
|
+
assertOk(sup);
|
|
391
|
+
assertOk(sub);
|
|
392
|
+
return Object.keys(sub).every((ele) => {
|
|
393
|
+
if (typeof sub[ele] == "object") {
|
|
394
|
+
return isSubset(sup[ele], sub[ele]);
|
|
395
|
+
}
|
|
396
|
+
return sub[ele] === sup[ele];
|
|
397
|
+
});
|
|
398
|
+
};
|
|
399
|
+
var assertFails = (message) => {
|
|
400
|
+
throw new AssertionError(_nullishCoalesce(message, () => ( "That should not ever happened, right?")));
|
|
401
|
+
};
|
|
402
|
+
function assertTrue(condition, message) {
|
|
403
|
+
if (condition !== true)
|
|
404
|
+
throw new AssertionError(_nullishCoalesce(message, () => ( `Condition is false`)));
|
|
405
|
+
}
|
|
406
|
+
function assertOk(obj, message) {
|
|
407
|
+
if (!obj) throw new AssertionError(_nullishCoalesce(message, () => ( `Condition is not truthy`)));
|
|
408
|
+
}
|
|
261
409
|
|
|
262
410
|
// src/eventStore/mongoDBEventStore.ts
|
|
263
411
|
|
|
@@ -265,7 +413,7 @@ var { retry: retry2 } = streamTransformations;
|
|
|
265
413
|
var _mongodb = require('mongodb');
|
|
266
414
|
|
|
267
415
|
|
|
268
|
-
// src/eventStore/projections/
|
|
416
|
+
// src/eventStore/projections/mongoDBInlineProjection.ts
|
|
269
417
|
var MongoDBDefaultInlineProjectionName = "_default";
|
|
270
418
|
var handleInlineProjections = async (options) => {
|
|
271
419
|
const {
|
|
@@ -277,10 +425,10 @@ var handleInlineProjections = async (options) => {
|
|
|
277
425
|
readModels
|
|
278
426
|
} = options;
|
|
279
427
|
const eventTypes = events.map((e) => e.type);
|
|
280
|
-
const
|
|
428
|
+
const projections2 = allProjections.filter(
|
|
281
429
|
(p) => p.canHandle.some((type) => eventTypes.includes(type))
|
|
282
430
|
);
|
|
283
|
-
for (const projection of
|
|
431
|
+
for (const projection of projections2) {
|
|
284
432
|
await projection.handle(events, {
|
|
285
433
|
document: _nullishCoalesce(readModels[projection.name], () => ( null)),
|
|
286
434
|
streamId,
|
|
@@ -318,6 +466,135 @@ var mongoDBInlineProjection = (options) => {
|
|
|
318
466
|
};
|
|
319
467
|
};
|
|
320
468
|
|
|
469
|
+
// src/eventStore/projections/mongoDBInlineProjectionSpec.ts
|
|
470
|
+
|
|
471
|
+
var MongoDBInlineProjectionSpec = {
|
|
472
|
+
for: (options) => {
|
|
473
|
+
{
|
|
474
|
+
const { projection, ...connectionOptions } = options;
|
|
475
|
+
return (givenStream) => {
|
|
476
|
+
const { streamName, events: givenEvents } = givenStream;
|
|
477
|
+
return {
|
|
478
|
+
when: (events) => {
|
|
479
|
+
const allEvents = [...givenEvents, ...events];
|
|
480
|
+
const run = (eventStore) => eventStore.appendToStream(streamName, allEvents);
|
|
481
|
+
return {
|
|
482
|
+
then: async (assert, message) => {
|
|
483
|
+
const client = "client" in connectionOptions && connectionOptions.client ? connectionOptions.client : new (0, _mongodb.MongoClient)(
|
|
484
|
+
connectionOptions.connectionString,
|
|
485
|
+
connectionOptions.clientOptions
|
|
486
|
+
);
|
|
487
|
+
const eventStore = getMongoDBEventStore({
|
|
488
|
+
projections: projections.inline([projection]),
|
|
489
|
+
client
|
|
490
|
+
});
|
|
491
|
+
try {
|
|
492
|
+
await run(eventStore);
|
|
493
|
+
const succeeded = await assert({ eventStore, streamName });
|
|
494
|
+
if (succeeded !== void 0 && succeeded === false)
|
|
495
|
+
assertFails(
|
|
496
|
+
_nullishCoalesce(message, () => ( "Projection specification didn't match the criteria"))
|
|
497
|
+
);
|
|
498
|
+
} finally {
|
|
499
|
+
await client.close();
|
|
500
|
+
}
|
|
501
|
+
},
|
|
502
|
+
thenThrows: async (...args) => {
|
|
503
|
+
const client = "client" in connectionOptions && connectionOptions.client ? connectionOptions.client : new (0, _mongodb.MongoClient)(
|
|
504
|
+
connectionOptions.connectionString,
|
|
505
|
+
connectionOptions.clientOptions
|
|
506
|
+
);
|
|
507
|
+
const eventStore = getMongoDBEventStore({
|
|
508
|
+
projections: projections.inline([projection]),
|
|
509
|
+
client
|
|
510
|
+
});
|
|
511
|
+
try {
|
|
512
|
+
await run(eventStore);
|
|
513
|
+
throw new AssertionError("Handler did not fail as expected");
|
|
514
|
+
} catch (error) {
|
|
515
|
+
if (error instanceof AssertionError) throw error;
|
|
516
|
+
if (args.length === 0) return;
|
|
517
|
+
if (!isErrorConstructor(args[0])) {
|
|
518
|
+
assertTrue(
|
|
519
|
+
args[0](error),
|
|
520
|
+
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _20 => _20.toString, 'call', _21 => _21()])}`
|
|
521
|
+
);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
assertTrue(
|
|
525
|
+
error instanceof args[0],
|
|
526
|
+
`Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _22 => _22.toString, 'call', _23 => _23()])}`
|
|
527
|
+
);
|
|
528
|
+
if (args[1]) {
|
|
529
|
+
assertTrue(
|
|
530
|
+
args[1](error),
|
|
531
|
+
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _24 => _24.toString, 'call', _25 => _25()])}`
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
} finally {
|
|
535
|
+
await client.close();
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
var eventInStream = (streamName, event) => ({
|
|
546
|
+
streamName,
|
|
547
|
+
events: [event]
|
|
548
|
+
});
|
|
549
|
+
var eventsInStream = (streamName, events) => ({
|
|
550
|
+
streamName,
|
|
551
|
+
events
|
|
552
|
+
});
|
|
553
|
+
var expectReadModelToMatch = async (options) => {
|
|
554
|
+
const { streamName, projectionName, eventStore, match } = options;
|
|
555
|
+
const readModel = await eventStore.projections.inline.findOne({
|
|
556
|
+
streamName,
|
|
557
|
+
projectionName
|
|
558
|
+
});
|
|
559
|
+
return match(readModel);
|
|
560
|
+
};
|
|
561
|
+
var expectInlineReadModelWithName = (projectionName) => ({
|
|
562
|
+
toHave: (expected) => ({ eventStore, streamName }) => expectReadModelToMatch({
|
|
563
|
+
eventStore,
|
|
564
|
+
streamName,
|
|
565
|
+
projectionName,
|
|
566
|
+
match: (readModel) => isSubset(readModel, expected)
|
|
567
|
+
}),
|
|
568
|
+
toDeepEquals: (expected) => ({ eventStore, streamName }) => expectReadModelToMatch({
|
|
569
|
+
eventStore,
|
|
570
|
+
streamName,
|
|
571
|
+
projectionName,
|
|
572
|
+
match: (readModel) => deepEquals(readModel, expected)
|
|
573
|
+
}),
|
|
574
|
+
toMatch: (match) => ({ eventStore, streamName }) => expectReadModelToMatch({
|
|
575
|
+
eventStore,
|
|
576
|
+
streamName,
|
|
577
|
+
projectionName,
|
|
578
|
+
match
|
|
579
|
+
}),
|
|
580
|
+
notToExist: () => ({ eventStore, streamName }) => expectReadModelToMatch({
|
|
581
|
+
eventStore,
|
|
582
|
+
streamName,
|
|
583
|
+
projectionName,
|
|
584
|
+
match: (readModel) => readModel === null
|
|
585
|
+
}),
|
|
586
|
+
toExist: () => ({ eventStore, streamName }) => expectReadModelToMatch({
|
|
587
|
+
eventStore,
|
|
588
|
+
streamName,
|
|
589
|
+
projectionName,
|
|
590
|
+
match: (readModel) => readModel !== null
|
|
591
|
+
})
|
|
592
|
+
});
|
|
593
|
+
var expectInlineReadModel = {
|
|
594
|
+
withName: (name) => expectInlineReadModelWithName(name),
|
|
595
|
+
...expectInlineReadModelWithName(MongoDBDefaultInlineProjectionName)
|
|
596
|
+
};
|
|
597
|
+
|
|
321
598
|
// src/eventStore/storage/mongoDBEventStoreStorage.ts
|
|
322
599
|
var DefaultMongoDBEventStoreStorageOptions = "COLLECTION_PER_STREAM_TYPE";
|
|
323
600
|
var DefaultMongoDBEventStoreCollectionName = "emt:streams";
|
|
@@ -398,15 +675,18 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
398
675
|
__init5() {this.isClosed = false}
|
|
399
676
|
|
|
400
677
|
|
|
678
|
+
|
|
401
679
|
constructor(options) {;_class4.prototype.__init5.call(this);_class4.prototype.__init6.call(this);_class4.prototype.__init7.call(this);_class4.prototype.__init8.call(this);
|
|
680
|
+
this.options = options;
|
|
402
681
|
this.client = "client" in options && options.client ? options.client : new (0, _mongodb.MongoClient)(options.connectionString, options.clientOptions);
|
|
403
682
|
this.shouldManageClientLifetime = !("client" in options);
|
|
404
683
|
this.storage = mongoDBEventStoreStorage({
|
|
405
684
|
storage: options.storage,
|
|
406
685
|
getConnectedClient: () => this.getConnectedClient()
|
|
407
686
|
});
|
|
408
|
-
this.inlineProjections = (
|
|
409
|
-
|
|
687
|
+
this.inlineProjections = filterProjections(
|
|
688
|
+
"inline",
|
|
689
|
+
_nullishCoalesce(options.projections, () => ( []))
|
|
410
690
|
);
|
|
411
691
|
this.projections = {
|
|
412
692
|
inline: {
|
|
@@ -418,7 +698,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
418
698
|
}
|
|
419
699
|
async readStream(streamName, options) {
|
|
420
700
|
const { streamType } = fromStreamName(streamName);
|
|
421
|
-
const expectedStreamVersion = _optionalChain([options, 'optionalAccess',
|
|
701
|
+
const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _26 => _26.expectedStreamVersion]);
|
|
422
702
|
const collection = await this.storage.collectionFor(streamType);
|
|
423
703
|
const filter2 = {
|
|
424
704
|
streamName: { $eq: streamName }
|
|
@@ -459,7 +739,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
459
739
|
};
|
|
460
740
|
}
|
|
461
741
|
async aggregateStream(streamName, options) {
|
|
462
|
-
const stream = await this.readStream(streamName, _optionalChain([options, 'optionalAccess',
|
|
742
|
+
const stream = await this.readStream(streamName, _optionalChain([options, 'optionalAccess', _27 => _27.read]));
|
|
463
743
|
const state = stream.events.reduce(options.evolve, options.initialState());
|
|
464
744
|
return {
|
|
465
745
|
state,
|
|
@@ -469,7 +749,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
469
749
|
}
|
|
470
750
|
async appendToStream(streamName, events, options) {
|
|
471
751
|
const { streamId, streamType } = fromStreamName(streamName);
|
|
472
|
-
const expectedStreamVersion = _optionalChain([options, 'optionalAccess',
|
|
752
|
+
const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _28 => _28.expectedStreamVersion]);
|
|
473
753
|
const collection = await this.storage.collectionFor(streamType);
|
|
474
754
|
const stream = await collection.findOne(
|
|
475
755
|
{ streamName: { $eq: streamName } },
|
|
@@ -481,7 +761,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
481
761
|
}
|
|
482
762
|
}
|
|
483
763
|
);
|
|
484
|
-
const currentStreamVersion = _nullishCoalesce(_optionalChain([stream, 'optionalAccess',
|
|
764
|
+
const currentStreamVersion = _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _29 => _29.metadata, 'access', _30 => _30.streamPosition]), () => ( MongoDBEventStoreDefaultStreamVersion));
|
|
485
765
|
assertExpectedVersionMatchesCurrent(
|
|
486
766
|
currentStreamVersion,
|
|
487
767
|
expectedStreamVersion,
|
|
@@ -499,7 +779,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
499
779
|
data: event.data,
|
|
500
780
|
metadata: {
|
|
501
781
|
...metadata,
|
|
502
|
-
..._nullishCoalesce(event.metadata, () => ( {}))
|
|
782
|
+
..."metadata" in event ? _nullishCoalesce(event.metadata, () => ( {})) : {}
|
|
503
783
|
}
|
|
504
784
|
};
|
|
505
785
|
});
|
|
@@ -519,7 +799,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
519
799
|
};
|
|
520
800
|
if (this.inlineProjections) {
|
|
521
801
|
await handleInlineProjections({
|
|
522
|
-
readModels: _nullishCoalesce(_optionalChain([stream, 'optionalAccess',
|
|
802
|
+
readModels: _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _31 => _31.projections]), () => ( {})),
|
|
523
803
|
streamId,
|
|
524
804
|
events: eventsToAppend,
|
|
525
805
|
projections: this.inlineProjections,
|
|
@@ -539,9 +819,17 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
539
819
|
if (!updatedStream) {
|
|
540
820
|
throw new ExpectedVersionConflictError(
|
|
541
821
|
currentStreamVersion,
|
|
542
|
-
_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
822
|
+
_nullishCoalesce(_optionalChain([options, 'optionalAccess', _32 => _32.expectedStreamVersion]), () => ( 0n))
|
|
543
823
|
);
|
|
544
824
|
}
|
|
825
|
+
await tryPublishMessagesAfterCommit(
|
|
826
|
+
// @ts-expect-error Issues with `globalPosition` not being present causing the type for metadata to expect `never`
|
|
827
|
+
eventsToAppend,
|
|
828
|
+
this.options.hooks
|
|
829
|
+
// {
|
|
830
|
+
// TODO: same context as InlineProjectionHandlerContext for mongodb?
|
|
831
|
+
// },
|
|
832
|
+
);
|
|
545
833
|
return {
|
|
546
834
|
nextExpectedStreamVersion: currentStreamVersion + BigInt(eventsToAppend.length),
|
|
547
835
|
createdNewStream: currentStreamVersion === MongoDBEventStoreDefaultStreamVersion
|
|
@@ -583,7 +871,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
583
871
|
projection: { [`projections.${projectionName}`]: 1 }
|
|
584
872
|
}
|
|
585
873
|
);
|
|
586
|
-
return _nullishCoalesce(_optionalChain([result, 'optionalAccess',
|
|
874
|
+
return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _33 => _33.projections, 'optionalAccess', _34 => _34[projectionName]]), () => ( null));
|
|
587
875
|
}
|
|
588
876
|
async findInlineProjection(streamFilter, projectionQuery, queryOptions) {
|
|
589
877
|
const parsedStreamFilter = parseMultiProjectionQueryStreamFilter(streamFilter);
|
|
@@ -608,13 +896,13 @@ var MongoDBEventStoreImplementation = (_class4 = class {
|
|
|
608
896
|
projection: { [`projections.${projectionName}`]: 1 }
|
|
609
897
|
}
|
|
610
898
|
);
|
|
611
|
-
if (_optionalChain([queryOptions, 'optionalAccess',
|
|
899
|
+
if (_optionalChain([queryOptions, 'optionalAccess', _35 => _35.skip])) {
|
|
612
900
|
query = query.skip(queryOptions.skip);
|
|
613
901
|
}
|
|
614
|
-
if (_optionalChain([queryOptions, 'optionalAccess',
|
|
902
|
+
if (_optionalChain([queryOptions, 'optionalAccess', _36 => _36.limit])) {
|
|
615
903
|
query = query.limit(queryOptions.limit);
|
|
616
904
|
}
|
|
617
|
-
if (_optionalChain([queryOptions, 'optionalAccess',
|
|
905
|
+
if (_optionalChain([queryOptions, 'optionalAccess', _37 => _37.sort])) {
|
|
618
906
|
const sort = prependMongoFilterWithProjectionPrefix(
|
|
619
907
|
queryOptions.sort,
|
|
620
908
|
prefix
|
|
@@ -766,5 +1054,9 @@ function fromStreamCollectionName(streamCollectionName) {
|
|
|
766
1054
|
|
|
767
1055
|
|
|
768
1056
|
|
|
769
|
-
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
exports.DefaultMongoDBEventStoreCollectionName = DefaultMongoDBEventStoreCollectionName; exports.DefaultMongoDBEventStoreStorageOptions = DefaultMongoDBEventStoreStorageOptions; exports.MongoDBDefaultInlineProjectionName = MongoDBDefaultInlineProjectionName; exports.MongoDBEventStoreDefaultStreamVersion = MongoDBEventStoreDefaultStreamVersion; exports.MongoDBInlineProjectionSpec = MongoDBInlineProjectionSpec; exports.eventInStream = eventInStream; exports.eventsInStream = eventsInStream; exports.expectInlineReadModel = expectInlineReadModel; exports.fromStreamCollectionName = fromStreamCollectionName; exports.fromStreamName = fromStreamName; exports.getMongoDBEventStore = getMongoDBEventStore; exports.handleInlineProjections = handleInlineProjections; exports.mongoDBEventStoreStorage = mongoDBEventStoreStorage; exports.mongoDBInlineProjection = mongoDBInlineProjection; exports.prependMongoFilterWithProjectionPrefix = prependMongoFilterWithProjectionPrefix; exports.toStreamCollectionName = toStreamCollectionName; exports.toStreamName = toStreamName;
|
|
770
1062
|
//# sourceMappingURL=index.cjs.map
|