@event-driven-io/emmett-mongodb 0.23.0-alpha.8 → 0.23.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 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-AEEEXE2R.js
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', _ => _.toString, 'call', _2 => _2()])}`))
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;
@@ -59,7 +63,7 @@ var assertExpectedVersionMatchesCurrent = (current, expected, defaultVersion) =>
59
63
  };
60
64
  var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends ConcurrencyError {
61
65
  constructor(current, expected) {
62
- super(_optionalChain([current, 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]), _optionalChain([expected, 'optionalAccess', _5 => _5.toString, 'call', _6 => _6()]));
66
+ super(_optionalChain([current, 'optionalAccess', _4 => _4.toString, 'call', _5 => _5()]), _optionalChain([expected, 'optionalAccess', _6 => _6.toString, 'call', _7 => _7()]));
63
67
  Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
64
68
  }
65
69
  };
@@ -73,9 +77,9 @@ var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfi
73
77
  }
74
78
  });_class.prototype.__init.call(this);_class.prototype.__init2.call(this);;
75
79
  this.onNoActiveReaderCallback = onNoActiveReaderCallback;
76
- this.streamId = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _7 => _7.streamId]), () => ( _uuid.v4.call(void 0, )));
80
+ this.streamId = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _8 => _8.streamId]), () => ( _uuid.v4.call(void 0, )));
77
81
  this.onNoActiveReaderCallback = onNoActiveReaderCallback;
78
- this.startChecking(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _8 => _8.intervalCheckInMs]), () => ( 20)));
82
+ this.startChecking(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _9 => _9.intervalCheckInMs]), () => ( 20)));
79
83
  }
80
84
  __init() {this.checkInterval = null}
81
85
 
@@ -101,6 +105,72 @@ var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfi
101
105
  }
102
106
  }
103
107
  }, _class);
108
+ var hasDuplicates = (array, predicate) => {
109
+ const mapped = array.map(predicate);
110
+ const uniqueValues = new Set(mapped);
111
+ return uniqueValues.size < mapped.length;
112
+ };
113
+ var getDuplicates = (array, predicate) => {
114
+ const map2 = /* @__PURE__ */ new Map();
115
+ for (let i = 0; i < array.length; i++) {
116
+ const item = array[i];
117
+ const key = predicate(item, i, array);
118
+ if (!map2.has(key)) {
119
+ map2.set(key, []);
120
+ }
121
+ map2.get(key).push(item);
122
+ }
123
+ return Array.from(map2.values()).filter((group) => group.length > 1).flat();
124
+ };
125
+ var merge = (array, item, where, onExisting, onNotFound = () => void 0) => {
126
+ let wasFound = false;
127
+ const result = array.map((p) => {
128
+ if (!where(p)) return p;
129
+ wasFound = true;
130
+ return onExisting(p);
131
+ }).filter((p) => p !== void 0).map((p) => {
132
+ if (!p) throw Error("That should not happen");
133
+ return p;
134
+ });
135
+ if (!wasFound) {
136
+ const result2 = onNotFound();
137
+ if (result2 !== void 0) return [...array, item];
138
+ }
139
+ return result;
140
+ };
141
+ var arrayUtils = {
142
+ merge,
143
+ hasDuplicates,
144
+ getDuplicates
145
+ };
146
+ var deepEquals = (left, right) => {
147
+ if (isEquatable(left)) {
148
+ return left.equals(right);
149
+ }
150
+ if (Array.isArray(left)) {
151
+ return Array.isArray(right) && left.length === right.length && left.every((val, index) => deepEquals(val, right[index]));
152
+ }
153
+ if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) {
154
+ return left === right;
155
+ }
156
+ if (Array.isArray(right)) return false;
157
+ const keys1 = Object.keys(left);
158
+ const keys2 = Object.keys(right);
159
+ if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key)))
160
+ return false;
161
+ for (const key in left) {
162
+ if (left[key] instanceof Function && right[key] instanceof Function)
163
+ continue;
164
+ const isEqual = deepEquals(left[key], right[key]);
165
+ if (!isEqual) {
166
+ return false;
167
+ }
168
+ }
169
+ return true;
170
+ };
171
+ var isEquatable = (left) => {
172
+ return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
173
+ };
104
174
  var asyncRetry = async (fn, opts) => {
105
175
  if (opts === void 0 || opts.retries === 0) return fn();
106
176
  return _asyncretry2.default.call(void 0,
@@ -108,7 +178,7 @@ var asyncRetry = async (fn, opts) => {
108
178
  try {
109
179
  return await fn();
110
180
  } catch (error2) {
111
- if (_optionalChain([opts, 'optionalAccess', _9 => _9.shouldRetryError]) && !opts.shouldRetryError(error2)) {
181
+ if (_optionalChain([opts, 'optionalAccess', _10 => _10.shouldRetryError]) && !opts.shouldRetryError(error2)) {
112
182
  bail(error2);
113
183
  }
114
184
  throw error2;
@@ -117,6 +187,47 @@ var asyncRetry = async (fn, opts) => {
117
187
  _nullishCoalesce(opts, () => ( { retries: 0 }))
118
188
  );
119
189
  };
190
+ var ParseError = class extends Error {
191
+ constructor(text) {
192
+ super(`Cannot parse! ${text}`);
193
+ }
194
+ };
195
+ var JSONParser = {
196
+ stringify: (value, options) => {
197
+ return JSON.stringify(
198
+ _optionalChain([options, 'optionalAccess', _11 => _11.map]) ? options.map(value) : value,
199
+ //TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
200
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
201
+ (_, v) => typeof v === "bigint" ? v.toString() : v
202
+ );
203
+ },
204
+ parse: (text, options) => {
205
+ const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess', _12 => _12.reviver]));
206
+ if (_optionalChain([options, 'optionalAccess', _13 => _13.typeCheck]) && !_optionalChain([options, 'optionalAccess', _14 => _14.typeCheck, 'call', _15 => _15(parsed)]))
207
+ throw new ParseError(text);
208
+ return _optionalChain([options, 'optionalAccess', _16 => _16.map]) ? options.map(parsed) : parsed;
209
+ }
210
+ };
211
+ var filterProjections = (type, projections2) => {
212
+ const inlineProjections2 = projections2.filter((projection2) => projection2.type === type).map(({ projection: projection2 }) => projection2);
213
+ const duplicateRegistrations = arrayUtils.getDuplicates(
214
+ inlineProjections2,
215
+ (proj) => proj.name
216
+ );
217
+ if (duplicateRegistrations.length > 0) {
218
+ throw new EmmettError(`You cannot register multiple projections with the same name (or without the name).
219
+ Ensure that:
220
+ ${JSONParser.stringify(duplicateRegistrations)}
221
+ have different names`);
222
+ }
223
+ return inlineProjections2;
224
+ };
225
+ var inlineProjections = (definitions) => definitions.map((projection2) => ({ type: "inline", projection: projection2 }));
226
+ var asyncProjections = (definitions) => definitions.map((projection2) => ({ type: "async", projection: projection2 }));
227
+ var projections = {
228
+ inline: inlineProjections,
229
+ async: asyncProjections
230
+ };
120
231
  var filter = (filter2) => new (0, _webstreamspolyfill.TransformStream)({
121
232
  transform(chunk, controller) {
122
233
  if (filter2(chunk)) {
@@ -258,6 +369,33 @@ var streamTransformations = {
258
369
  waitAtMost
259
370
  };
260
371
  var { retry: retry2 } = streamTransformations;
372
+ var AssertionError = class extends Error {
373
+ constructor(message) {
374
+ super(message);
375
+ }
376
+ };
377
+ var isSubset = (superObj, subObj) => {
378
+ const sup = superObj;
379
+ const sub = subObj;
380
+ assertOk(sup);
381
+ assertOk(sub);
382
+ return Object.keys(sub).every((ele) => {
383
+ if (typeof sub[ele] == "object") {
384
+ return isSubset(sup[ele], sub[ele]);
385
+ }
386
+ return sub[ele] === sup[ele];
387
+ });
388
+ };
389
+ var assertFails = (message) => {
390
+ throw new AssertionError(_nullishCoalesce(message, () => ( "That should not ever happened, right?")));
391
+ };
392
+ function assertTrue(condition, message) {
393
+ if (condition !== true)
394
+ throw new AssertionError(_nullishCoalesce(message, () => ( `Condition is false`)));
395
+ }
396
+ function assertOk(obj, message) {
397
+ if (!obj) throw new AssertionError(_nullishCoalesce(message, () => ( `Condition is not truthy`)));
398
+ }
261
399
 
262
400
  // src/eventStore/mongoDBEventStore.ts
263
401
 
@@ -265,7 +403,7 @@ var { retry: retry2 } = streamTransformations;
265
403
  var _mongodb = require('mongodb');
266
404
 
267
405
 
268
- // src/eventStore/projections/index.ts
406
+ // src/eventStore/projections/mongoDBInlineProjection.ts
269
407
  var MongoDBDefaultInlineProjectionName = "_default";
270
408
  var handleInlineProjections = async (options) => {
271
409
  const {
@@ -277,10 +415,10 @@ var handleInlineProjections = async (options) => {
277
415
  readModels
278
416
  } = options;
279
417
  const eventTypes = events.map((e) => e.type);
280
- const projections = allProjections.filter(
418
+ const projections2 = allProjections.filter(
281
419
  (p) => p.canHandle.some((type) => eventTypes.includes(type))
282
420
  );
283
- for (const projection of projections) {
421
+ for (const projection of projections2) {
284
422
  await projection.handle(events, {
285
423
  document: _nullishCoalesce(readModels[projection.name], () => ( null)),
286
424
  streamId,
@@ -318,6 +456,135 @@ var mongoDBInlineProjection = (options) => {
318
456
  };
319
457
  };
320
458
 
459
+ // src/eventStore/projections/mongoDBInlineProjectionSpec.ts
460
+
461
+ var MongoDBInlineProjectionSpec = {
462
+ for: (options) => {
463
+ {
464
+ const { projection, ...connectionOptions } = options;
465
+ return (givenStream) => {
466
+ const { streamName, events: givenEvents } = givenStream;
467
+ return {
468
+ when: (events) => {
469
+ const allEvents = [...givenEvents, ...events];
470
+ const run = (eventStore) => eventStore.appendToStream(streamName, allEvents);
471
+ return {
472
+ then: async (assert, message) => {
473
+ const client = "client" in connectionOptions && connectionOptions.client ? connectionOptions.client : new (0, _mongodb.MongoClient)(
474
+ connectionOptions.connectionString,
475
+ connectionOptions.clientOptions
476
+ );
477
+ const eventStore = getMongoDBEventStore({
478
+ projections: projections.inline([projection]),
479
+ client
480
+ });
481
+ try {
482
+ await run(eventStore);
483
+ const succeeded = await assert({ eventStore, streamName });
484
+ if (succeeded !== void 0 && succeeded === false)
485
+ assertFails(
486
+ _nullishCoalesce(message, () => ( "Projection specification didn't match the criteria"))
487
+ );
488
+ } finally {
489
+ await client.close();
490
+ }
491
+ },
492
+ thenThrows: async (...args) => {
493
+ const client = "client" in connectionOptions && connectionOptions.client ? connectionOptions.client : new (0, _mongodb.MongoClient)(
494
+ connectionOptions.connectionString,
495
+ connectionOptions.clientOptions
496
+ );
497
+ const eventStore = getMongoDBEventStore({
498
+ projections: projections.inline([projection]),
499
+ client
500
+ });
501
+ try {
502
+ await run(eventStore);
503
+ throw new AssertionError("Handler did not fail as expected");
504
+ } catch (error) {
505
+ if (error instanceof AssertionError) throw error;
506
+ if (args.length === 0) return;
507
+ if (!isErrorConstructor(args[0])) {
508
+ assertTrue(
509
+ args[0](error),
510
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _17 => _17.toString, 'call', _18 => _18()])}`
511
+ );
512
+ return;
513
+ }
514
+ assertTrue(
515
+ error instanceof args[0],
516
+ `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _19 => _19.toString, 'call', _20 => _20()])}`
517
+ );
518
+ if (args[1]) {
519
+ assertTrue(
520
+ args[1](error),
521
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _21 => _21.toString, 'call', _22 => _22()])}`
522
+ );
523
+ }
524
+ } finally {
525
+ await client.close();
526
+ }
527
+ }
528
+ };
529
+ }
530
+ };
531
+ };
532
+ }
533
+ }
534
+ };
535
+ var eventInStream = (streamName, event) => ({
536
+ streamName,
537
+ events: [event]
538
+ });
539
+ var eventsInStream = (streamName, events) => ({
540
+ streamName,
541
+ events
542
+ });
543
+ var expectReadModelToMatch = async (options) => {
544
+ const { streamName, projectionName, eventStore, match } = options;
545
+ const readModel = await eventStore.projections.inline.findOne({
546
+ streamName,
547
+ projectionName
548
+ });
549
+ return match(readModel);
550
+ };
551
+ var expectInlineReadModelWithName = (projectionName) => ({
552
+ toHave: (expected) => ({ eventStore, streamName }) => expectReadModelToMatch({
553
+ eventStore,
554
+ streamName,
555
+ projectionName,
556
+ match: (readModel) => isSubset(readModel, expected)
557
+ }),
558
+ toDeepEquals: (expected) => ({ eventStore, streamName }) => expectReadModelToMatch({
559
+ eventStore,
560
+ streamName,
561
+ projectionName,
562
+ match: (readModel) => deepEquals(readModel, expected)
563
+ }),
564
+ toMatch: (match) => ({ eventStore, streamName }) => expectReadModelToMatch({
565
+ eventStore,
566
+ streamName,
567
+ projectionName,
568
+ match
569
+ }),
570
+ notToExist: () => ({ eventStore, streamName }) => expectReadModelToMatch({
571
+ eventStore,
572
+ streamName,
573
+ projectionName,
574
+ match: (readModel) => readModel === null
575
+ }),
576
+ toExist: () => ({ eventStore, streamName }) => expectReadModelToMatch({
577
+ eventStore,
578
+ streamName,
579
+ projectionName,
580
+ match: (readModel) => readModel !== null
581
+ })
582
+ });
583
+ var expectInlineReadModel = {
584
+ withName: (name) => expectInlineReadModelWithName(name),
585
+ ...expectInlineReadModelWithName(MongoDBDefaultInlineProjectionName)
586
+ };
587
+
321
588
  // src/eventStore/storage/mongoDBEventStoreStorage.ts
322
589
  var DefaultMongoDBEventStoreStorageOptions = "COLLECTION_PER_STREAM_TYPE";
323
590
  var DefaultMongoDBEventStoreCollectionName = "emt:streams";
@@ -405,8 +672,9 @@ var MongoDBEventStoreImplementation = (_class4 = class {
405
672
  storage: options.storage,
406
673
  getConnectedClient: () => this.getConnectedClient()
407
674
  });
408
- this.inlineProjections = (_nullishCoalesce(options.projections, () => ( []))).filter(({ type }) => type === "inline").map(
409
- ({ projection }) => projection
675
+ this.inlineProjections = filterProjections(
676
+ "inline",
677
+ _nullishCoalesce(options.projections, () => ( []))
410
678
  );
411
679
  this.projections = {
412
680
  inline: {
@@ -418,7 +686,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
418
686
  }
419
687
  async readStream(streamName, options) {
420
688
  const { streamType } = fromStreamName(streamName);
421
- const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _10 => _10.expectedStreamVersion]);
689
+ const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _23 => _23.expectedStreamVersion]);
422
690
  const collection = await this.storage.collectionFor(streamType);
423
691
  const filter2 = {
424
692
  streamName: { $eq: streamName }
@@ -459,7 +727,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
459
727
  };
460
728
  }
461
729
  async aggregateStream(streamName, options) {
462
- const stream = await this.readStream(streamName, _optionalChain([options, 'optionalAccess', _11 => _11.read]));
730
+ const stream = await this.readStream(streamName, _optionalChain([options, 'optionalAccess', _24 => _24.read]));
463
731
  const state = stream.events.reduce(options.evolve, options.initialState());
464
732
  return {
465
733
  state,
@@ -469,7 +737,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
469
737
  }
470
738
  async appendToStream(streamName, events, options) {
471
739
  const { streamId, streamType } = fromStreamName(streamName);
472
- const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _12 => _12.expectedStreamVersion]);
740
+ const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _25 => _25.expectedStreamVersion]);
473
741
  const collection = await this.storage.collectionFor(streamType);
474
742
  const stream = await collection.findOne(
475
743
  { streamName: { $eq: streamName } },
@@ -481,7 +749,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
481
749
  }
482
750
  }
483
751
  );
484
- const currentStreamVersion = _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _13 => _13.metadata, 'access', _14 => _14.streamPosition]), () => ( MongoDBEventStoreDefaultStreamVersion));
752
+ const currentStreamVersion = _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _26 => _26.metadata, 'access', _27 => _27.streamPosition]), () => ( MongoDBEventStoreDefaultStreamVersion));
485
753
  assertExpectedVersionMatchesCurrent(
486
754
  currentStreamVersion,
487
755
  expectedStreamVersion,
@@ -499,7 +767,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
499
767
  data: event.data,
500
768
  metadata: {
501
769
  ...metadata,
502
- ..._nullishCoalesce(event.metadata, () => ( {}))
770
+ ..."metadata" in event ? _nullishCoalesce(event.metadata, () => ( {})) : {}
503
771
  }
504
772
  };
505
773
  });
@@ -519,7 +787,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
519
787
  };
520
788
  if (this.inlineProjections) {
521
789
  await handleInlineProjections({
522
- readModels: _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _15 => _15.projections]), () => ( {})),
790
+ readModels: _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _28 => _28.projections]), () => ( {})),
523
791
  streamId,
524
792
  events: eventsToAppend,
525
793
  projections: this.inlineProjections,
@@ -539,7 +807,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
539
807
  if (!updatedStream) {
540
808
  throw new ExpectedVersionConflictError(
541
809
  currentStreamVersion,
542
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _16 => _16.expectedStreamVersion]), () => ( 0n))
810
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _29 => _29.expectedStreamVersion]), () => ( 0n))
543
811
  );
544
812
  }
545
813
  return {
@@ -566,15 +834,13 @@ var MongoDBEventStoreImplementation = (_class4 = class {
566
834
  async findOneInlineProjection(streamFilter, projectionQuery) {
567
835
  const { projectionName, streamName, streamType } = parseSingleProjectionQueryStreamFilter(streamFilter);
568
836
  const collection = await this.storage.collectionFor(streamType);
569
- const query = prependMongoFilterWithProjectionPrefix(
570
- // @ts-expect-error we are turning the `Filter<ProjectSchema>` into a `Filter<EventStream>`
571
- projectionQuery,
572
- `projections.${projectionName}`
573
- );
837
+ const query = prependMongoFilterWithProjectionPrefix(projectionQuery, `projections.${projectionName}`);
574
838
  const filters = [
575
- query,
576
839
  { [`projections.${projectionName}`]: { $exists: true } }
577
840
  ];
841
+ if (query) {
842
+ filters.push(query);
843
+ }
578
844
  if (streamName) {
579
845
  filters.push({ streamName: { $eq: streamName } });
580
846
  }
@@ -585,7 +851,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
585
851
  projection: { [`projections.${projectionName}`]: 1 }
586
852
  }
587
853
  );
588
- return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _17 => _17.projections, 'optionalAccess', _18 => _18[projectionName]]), () => ( null));
854
+ return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _30 => _30.projections, 'optionalAccess', _31 => _31[projectionName]]), () => ( null));
589
855
  }
590
856
  async findInlineProjection(streamFilter, projectionQuery, queryOptions) {
591
857
  const parsedStreamFilter = parseMultiProjectionQueryStreamFilter(streamFilter);
@@ -593,15 +859,13 @@ var MongoDBEventStoreImplementation = (_class4 = class {
593
859
  const { projectionName, streamNames, streamType } = parsedStreamFilter;
594
860
  const collection = await this.storage.collectionFor(streamType);
595
861
  const prefix = `projections.${projectionName}`;
596
- const projectionFilter = prependMongoFilterWithProjectionPrefix(
597
- // @ts-expect-error we are turning the `Filter<ProjectSchema>` into a `Filter<EventStream>`
598
- projectionQuery,
599
- prefix
600
- );
862
+ const projectionFilter = prependMongoFilterWithProjectionPrefix(projectionQuery, prefix);
601
863
  const filters = [
602
- projectionFilter,
603
864
  { [`projections.${projectionName}`]: { $exists: true } }
604
865
  ];
866
+ if (projectionFilter) {
867
+ filters.push(projectionFilter);
868
+ }
605
869
  if (streamNames) {
606
870
  filters.push({ streamName: { $in: streamNames } });
607
871
  }
@@ -612,13 +876,13 @@ var MongoDBEventStoreImplementation = (_class4 = class {
612
876
  projection: { [`projections.${projectionName}`]: 1 }
613
877
  }
614
878
  );
615
- if (_optionalChain([queryOptions, 'optionalAccess', _19 => _19.skip])) {
879
+ if (_optionalChain([queryOptions, 'optionalAccess', _32 => _32.skip])) {
616
880
  query = query.skip(queryOptions.skip);
617
881
  }
618
- if (_optionalChain([queryOptions, 'optionalAccess', _20 => _20.limit])) {
882
+ if (_optionalChain([queryOptions, 'optionalAccess', _33 => _33.limit])) {
619
883
  query = query.limit(queryOptions.limit);
620
884
  }
621
- if (_optionalChain([queryOptions, 'optionalAccess', _21 => _21.sort])) {
885
+ if (_optionalChain([queryOptions, 'optionalAccess', _34 => _34.sort])) {
622
886
  const sort = prependMongoFilterWithProjectionPrefix(
623
887
  queryOptions.sort,
624
888
  prefix
@@ -634,15 +898,13 @@ var MongoDBEventStoreImplementation = (_class4 = class {
634
898
  const { projectionName, streamNames, streamType } = parsedStreamFilter;
635
899
  const collection = await this.storage.collectionFor(streamType);
636
900
  const prefix = `projections.${projectionName}`;
637
- const projectionFilter = prependMongoFilterWithProjectionPrefix(
638
- // @ts-expect-error we are turning the `Filter<ProjectSchema>` into a `Filter<EventStream>`
639
- projectionQuery,
640
- prefix
641
- );
901
+ const projectionFilter = prependMongoFilterWithProjectionPrefix(projectionQuery, prefix);
642
902
  const filters = [
643
- projectionFilter,
644
903
  { [`projections.${projectionName}`]: { $exists: true } }
645
904
  ];
905
+ if (projectionFilter) {
906
+ filters.push(projectionFilter);
907
+ }
646
908
  if (streamNames) {
647
909
  filters.push({ streamName: { $in: streamNames } });
648
910
  }
@@ -707,7 +969,7 @@ function parseMultiProjectionQueryStreamFilter(streamFilter) {
707
969
  };
708
970
  }
709
971
  function prependMongoFilterWithProjectionPrefix(obj, prefix) {
710
- if (typeof obj !== "object" || obj === null) {
972
+ if (typeof obj !== "object" || obj === null || obj === void 0) {
711
973
  return obj;
712
974
  }
713
975
  if (Array.isArray(obj)) {
@@ -772,5 +1034,9 @@ function fromStreamCollectionName(streamCollectionName) {
772
1034
 
773
1035
 
774
1036
 
775
- exports.DefaultMongoDBEventStoreCollectionName = DefaultMongoDBEventStoreCollectionName; exports.DefaultMongoDBEventStoreStorageOptions = DefaultMongoDBEventStoreStorageOptions; exports.MongoDBDefaultInlineProjectionName = MongoDBDefaultInlineProjectionName; exports.MongoDBEventStoreDefaultStreamVersion = MongoDBEventStoreDefaultStreamVersion; 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;
1037
+
1038
+
1039
+
1040
+
1041
+ 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;
776
1042
  //# sourceMappingURL=index.cjs.map