@event-driven-io/emmett-mongodb 0.23.0 → 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 CHANGED
@@ -47,6 +47,16 @@ var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefa
47
47
 
48
48
 
49
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
+ }
50
60
  var STREAM_EXISTS = "STREAM_EXISTS";
51
61
  var STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
52
62
  var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
@@ -63,7 +73,7 @@ var assertExpectedVersionMatchesCurrent = (current, expected, defaultVersion) =>
63
73
  };
64
74
  var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends ConcurrencyError {
65
75
  constructor(current, expected) {
66
- super(_optionalChain([current, 'optionalAccess', _4 => _4.toString, 'call', _5 => _5()]), _optionalChain([expected, 'optionalAccess', _6 => _6.toString, 'call', _7 => _7()]));
76
+ super(_optionalChain([current, 'optionalAccess', _7 => _7.toString, 'call', _8 => _8()]), _optionalChain([expected, 'optionalAccess', _9 => _9.toString, 'call', _10 => _10()]));
67
77
  Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
68
78
  }
69
79
  };
@@ -77,9 +87,9 @@ var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfi
77
87
  }
78
88
  });_class.prototype.__init.call(this);_class.prototype.__init2.call(this);;
79
89
  this.onNoActiveReaderCallback = onNoActiveReaderCallback;
80
- this.streamId = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _8 => _8.streamId]), () => ( _uuid.v4.call(void 0, )));
90
+ this.streamId = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _11 => _11.streamId]), () => ( _uuid.v4.call(void 0, )));
81
91
  this.onNoActiveReaderCallback = onNoActiveReaderCallback;
82
- this.startChecking(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _9 => _9.intervalCheckInMs]), () => ( 20)));
92
+ this.startChecking(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _12 => _12.intervalCheckInMs]), () => ( 20)));
83
93
  }
84
94
  __init() {this.checkInterval = null}
85
95
 
@@ -178,7 +188,7 @@ var asyncRetry = async (fn, opts) => {
178
188
  try {
179
189
  return await fn();
180
190
  } catch (error2) {
181
- if (_optionalChain([opts, 'optionalAccess', _10 => _10.shouldRetryError]) && !opts.shouldRetryError(error2)) {
191
+ if (_optionalChain([opts, 'optionalAccess', _13 => _13.shouldRetryError]) && !opts.shouldRetryError(error2)) {
182
192
  bail(error2);
183
193
  }
184
194
  throw error2;
@@ -195,17 +205,17 @@ var ParseError = class extends Error {
195
205
  var JSONParser = {
196
206
  stringify: (value, options) => {
197
207
  return JSON.stringify(
198
- _optionalChain([options, 'optionalAccess', _11 => _11.map]) ? options.map(value) : value,
208
+ _optionalChain([options, 'optionalAccess', _14 => _14.map]) ? options.map(value) : value,
199
209
  //TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
200
210
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
201
211
  (_, v) => typeof v === "bigint" ? v.toString() : v
202
212
  );
203
213
  },
204
214
  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)]))
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)]))
207
217
  throw new ParseError(text);
208
- return _optionalChain([options, 'optionalAccess', _16 => _16.map]) ? options.map(parsed) : parsed;
218
+ return _optionalChain([options, 'optionalAccess', _19 => _19.map]) ? options.map(parsed) : parsed;
209
219
  }
210
220
  };
211
221
  var filterProjections = (type, projections2) => {
@@ -507,18 +517,18 @@ var MongoDBInlineProjectionSpec = {
507
517
  if (!isErrorConstructor(args[0])) {
508
518
  assertTrue(
509
519
  args[0](error),
510
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _17 => _17.toString, 'call', _18 => _18()])}`
520
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _20 => _20.toString, 'call', _21 => _21()])}`
511
521
  );
512
522
  return;
513
523
  }
514
524
  assertTrue(
515
525
  error instanceof args[0],
516
- `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _19 => _19.toString, 'call', _20 => _20()])}`
526
+ `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _22 => _22.toString, 'call', _23 => _23()])}`
517
527
  );
518
528
  if (args[1]) {
519
529
  assertTrue(
520
530
  args[1](error),
521
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _21 => _21.toString, 'call', _22 => _22()])}`
531
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _24 => _24.toString, 'call', _25 => _25()])}`
522
532
  );
523
533
  }
524
534
  } finally {
@@ -665,7 +675,9 @@ var MongoDBEventStoreImplementation = (_class4 = class {
665
675
  __init5() {this.isClosed = false}
666
676
 
667
677
 
678
+
668
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;
669
681
  this.client = "client" in options && options.client ? options.client : new (0, _mongodb.MongoClient)(options.connectionString, options.clientOptions);
670
682
  this.shouldManageClientLifetime = !("client" in options);
671
683
  this.storage = mongoDBEventStoreStorage({
@@ -686,7 +698,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
686
698
  }
687
699
  async readStream(streamName, options) {
688
700
  const { streamType } = fromStreamName(streamName);
689
- const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _23 => _23.expectedStreamVersion]);
701
+ const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _26 => _26.expectedStreamVersion]);
690
702
  const collection = await this.storage.collectionFor(streamType);
691
703
  const filter2 = {
692
704
  streamName: { $eq: streamName }
@@ -727,7 +739,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
727
739
  };
728
740
  }
729
741
  async aggregateStream(streamName, options) {
730
- const stream = await this.readStream(streamName, _optionalChain([options, 'optionalAccess', _24 => _24.read]));
742
+ const stream = await this.readStream(streamName, _optionalChain([options, 'optionalAccess', _27 => _27.read]));
731
743
  const state = stream.events.reduce(options.evolve, options.initialState());
732
744
  return {
733
745
  state,
@@ -737,7 +749,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
737
749
  }
738
750
  async appendToStream(streamName, events, options) {
739
751
  const { streamId, streamType } = fromStreamName(streamName);
740
- const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _25 => _25.expectedStreamVersion]);
752
+ const expectedStreamVersion = _optionalChain([options, 'optionalAccess', _28 => _28.expectedStreamVersion]);
741
753
  const collection = await this.storage.collectionFor(streamType);
742
754
  const stream = await collection.findOne(
743
755
  { streamName: { $eq: streamName } },
@@ -749,7 +761,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
749
761
  }
750
762
  }
751
763
  );
752
- const currentStreamVersion = _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _26 => _26.metadata, 'access', _27 => _27.streamPosition]), () => ( MongoDBEventStoreDefaultStreamVersion));
764
+ const currentStreamVersion = _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _29 => _29.metadata, 'access', _30 => _30.streamPosition]), () => ( MongoDBEventStoreDefaultStreamVersion));
753
765
  assertExpectedVersionMatchesCurrent(
754
766
  currentStreamVersion,
755
767
  expectedStreamVersion,
@@ -787,7 +799,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
787
799
  };
788
800
  if (this.inlineProjections) {
789
801
  await handleInlineProjections({
790
- readModels: _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _28 => _28.projections]), () => ( {})),
802
+ readModels: _nullishCoalesce(_optionalChain([stream, 'optionalAccess', _31 => _31.projections]), () => ( {})),
791
803
  streamId,
792
804
  events: eventsToAppend,
793
805
  projections: this.inlineProjections,
@@ -807,9 +819,17 @@ var MongoDBEventStoreImplementation = (_class4 = class {
807
819
  if (!updatedStream) {
808
820
  throw new ExpectedVersionConflictError(
809
821
  currentStreamVersion,
810
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _29 => _29.expectedStreamVersion]), () => ( 0n))
822
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _32 => _32.expectedStreamVersion]), () => ( 0n))
811
823
  );
812
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
+ );
813
833
  return {
814
834
  nextExpectedStreamVersion: currentStreamVersion + BigInt(eventsToAppend.length),
815
835
  createdNewStream: currentStreamVersion === MongoDBEventStoreDefaultStreamVersion
@@ -851,7 +871,7 @@ var MongoDBEventStoreImplementation = (_class4 = class {
851
871
  projection: { [`projections.${projectionName}`]: 1 }
852
872
  }
853
873
  );
854
- return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _30 => _30.projections, 'optionalAccess', _31 => _31[projectionName]]), () => ( null));
874
+ return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _33 => _33.projections, 'optionalAccess', _34 => _34[projectionName]]), () => ( null));
855
875
  }
856
876
  async findInlineProjection(streamFilter, projectionQuery, queryOptions) {
857
877
  const parsedStreamFilter = parseMultiProjectionQueryStreamFilter(streamFilter);
@@ -876,13 +896,13 @@ var MongoDBEventStoreImplementation = (_class4 = class {
876
896
  projection: { [`projections.${projectionName}`]: 1 }
877
897
  }
878
898
  );
879
- if (_optionalChain([queryOptions, 'optionalAccess', _32 => _32.skip])) {
899
+ if (_optionalChain([queryOptions, 'optionalAccess', _35 => _35.skip])) {
880
900
  query = query.skip(queryOptions.skip);
881
901
  }
882
- if (_optionalChain([queryOptions, 'optionalAccess', _33 => _33.limit])) {
902
+ if (_optionalChain([queryOptions, 'optionalAccess', _36 => _36.limit])) {
883
903
  query = query.limit(queryOptions.limit);
884
904
  }
885
- if (_optionalChain([queryOptions, 'optionalAccess', _34 => _34.sort])) {
905
+ if (_optionalChain([queryOptions, 'optionalAccess', _37 => _37.sort])) {
886
906
  const sort = prependMongoFilterWithProjectionPrefix(
887
907
  queryOptions.sort,
888
908
  prefix