@event-driven-io/emmett-mongodb 0.38.7 → 0.39.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 +28 -247
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -10
- package/dist/index.d.ts +2 -10
- package/dist/index.js +10 -229
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -21,11 +21,7 @@ type InlineProjectionHandlerOptions<EventType extends Event = Event, EventMetaDa
|
|
|
21
21
|
updates: UpdateFilter<EventStream<Event>>;
|
|
22
22
|
client: {};
|
|
23
23
|
};
|
|
24
|
-
declare const handleInlineProjections: <EventType extends Event = Event, EventMetaDataType extends MongoDBReadEventMetadata =
|
|
25
|
-
messageId: string;
|
|
26
|
-
streamPosition: bigint;
|
|
27
|
-
streamName: string;
|
|
28
|
-
}>>(options: InlineProjectionHandlerOptions<EventType, EventMetaDataType>) => Promise<void>;
|
|
24
|
+
declare const handleInlineProjections: <EventType extends Event = Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata>(options: InlineProjectionHandlerOptions<EventType, EventMetaDataType>) => Promise<void>;
|
|
29
25
|
type MongoDBWithNotNullDocumentEvolve<Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata> = ((document: Doc, event: ReadEvent<EventType, EventMetaDataType>) => Doc | null) | ((document: Doc, event: ReadEvent<EventType>) => Promise<Doc | null>);
|
|
30
26
|
type MongoDBWithNullableDocumentEvolve<Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata> = ((document: Doc | null, event: ReadEvent<EventType, EventMetaDataType>) => Doc | null) | ((document: Doc | null, event: ReadEvent<EventType>) => Promise<Doc | null>);
|
|
31
27
|
type MongoDBInlineProjectionOptions<Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata> = {
|
|
@@ -38,11 +34,7 @@ type MongoDBInlineProjectionOptions<Doc extends Document, EventType extends Even
|
|
|
38
34
|
evolve: MongoDBWithNotNullDocumentEvolve<Doc, EventType, EventMetaDataType>;
|
|
39
35
|
initialState: () => Doc;
|
|
40
36
|
});
|
|
41
|
-
declare const mongoDBInlineProjection: <Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata =
|
|
42
|
-
messageId: string;
|
|
43
|
-
streamPosition: bigint;
|
|
44
|
-
streamName: string;
|
|
45
|
-
}>>(options: MongoDBInlineProjectionOptions<Doc, EventType, EventMetaDataType>) => MongoDBInlineProjectionDefinition;
|
|
37
|
+
declare const mongoDBInlineProjection: <Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata>(options: MongoDBInlineProjectionOptions<Doc, EventType, EventMetaDataType>) => MongoDBInlineProjectionDefinition;
|
|
46
38
|
|
|
47
39
|
type MongoDBInlineProjectionSpecGivenEvents<StreamNameType extends StreamName, EventType extends Event> = {
|
|
48
40
|
streamName: StreamNameType;
|
package/dist/index.d.ts
CHANGED
|
@@ -21,11 +21,7 @@ type InlineProjectionHandlerOptions<EventType extends Event = Event, EventMetaDa
|
|
|
21
21
|
updates: UpdateFilter<EventStream<Event>>;
|
|
22
22
|
client: {};
|
|
23
23
|
};
|
|
24
|
-
declare const handleInlineProjections: <EventType extends Event = Event, EventMetaDataType extends MongoDBReadEventMetadata =
|
|
25
|
-
messageId: string;
|
|
26
|
-
streamPosition: bigint;
|
|
27
|
-
streamName: string;
|
|
28
|
-
}>>(options: InlineProjectionHandlerOptions<EventType, EventMetaDataType>) => Promise<void>;
|
|
24
|
+
declare const handleInlineProjections: <EventType extends Event = Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata>(options: InlineProjectionHandlerOptions<EventType, EventMetaDataType>) => Promise<void>;
|
|
29
25
|
type MongoDBWithNotNullDocumentEvolve<Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata> = ((document: Doc, event: ReadEvent<EventType, EventMetaDataType>) => Doc | null) | ((document: Doc, event: ReadEvent<EventType>) => Promise<Doc | null>);
|
|
30
26
|
type MongoDBWithNullableDocumentEvolve<Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata> = ((document: Doc | null, event: ReadEvent<EventType, EventMetaDataType>) => Doc | null) | ((document: Doc | null, event: ReadEvent<EventType>) => Promise<Doc | null>);
|
|
31
27
|
type MongoDBInlineProjectionOptions<Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata> = {
|
|
@@ -38,11 +34,7 @@ type MongoDBInlineProjectionOptions<Doc extends Document, EventType extends Even
|
|
|
38
34
|
evolve: MongoDBWithNotNullDocumentEvolve<Doc, EventType, EventMetaDataType>;
|
|
39
35
|
initialState: () => Doc;
|
|
40
36
|
});
|
|
41
|
-
declare const mongoDBInlineProjection: <Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata =
|
|
42
|
-
messageId: string;
|
|
43
|
-
streamPosition: bigint;
|
|
44
|
-
streamName: string;
|
|
45
|
-
}>>(options: MongoDBInlineProjectionOptions<Doc, EventType, EventMetaDataType>) => MongoDBInlineProjectionDefinition;
|
|
37
|
+
declare const mongoDBInlineProjection: <Doc extends Document, EventType extends Event, EventMetaDataType extends MongoDBReadEventMetadata = MongoDBReadEventMetadata>(options: MongoDBInlineProjectionOptions<Doc, EventType, EventMetaDataType>) => MongoDBInlineProjectionDefinition;
|
|
46
38
|
|
|
47
39
|
type MongoDBInlineProjectionSpecGivenEvents<StreamNameType extends StreamName, EventType extends Event> = {
|
|
48
40
|
streamName: StreamNameType;
|
package/dist/index.js
CHANGED
|
@@ -37,27 +37,10 @@ var ConcurrencyError = class _ConcurrencyError extends EmmettError {
|
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
// ../emmett/dist/index.js
|
|
40
|
-
import { v4 as
|
|
40
|
+
import { v4 as uuid3 } from "uuid";
|
|
41
41
|
import { v7 as uuid } from "uuid";
|
|
42
42
|
import retry from "async-retry";
|
|
43
43
|
import { v4 as uuid2 } from "uuid";
|
|
44
|
-
import { TransformStream } from "web-streams-polyfill";
|
|
45
|
-
import { v4 as uuid4 } from "uuid";
|
|
46
|
-
import { v4 as uuid3 } from "uuid";
|
|
47
|
-
import { TransformStream as TransformStream2 } from "web-streams-polyfill";
|
|
48
|
-
import { ReadableStream } from "web-streams-polyfill";
|
|
49
|
-
import "web-streams-polyfill";
|
|
50
|
-
import { TransformStream as TransformStream3 } from "web-streams-polyfill";
|
|
51
|
-
import { TransformStream as TransformStream4 } from "web-streams-polyfill";
|
|
52
|
-
import { TransformStream as TransformStream5 } from "web-streams-polyfill";
|
|
53
|
-
import {
|
|
54
|
-
TransformStream as TransformStream6
|
|
55
|
-
} from "web-streams-polyfill";
|
|
56
|
-
import { TransformStream as TransformStream7 } from "web-streams-polyfill";
|
|
57
|
-
import { TransformStream as TransformStream8 } from "web-streams-polyfill";
|
|
58
|
-
import { TransformStream as TransformStream9 } from "web-streams-polyfill";
|
|
59
|
-
import { TransformStream as TransformStream10 } from "web-streams-polyfill";
|
|
60
|
-
import { TransformStream as TransformStream11 } from "web-streams-polyfill";
|
|
61
44
|
async function tryPublishMessagesAfterCommit(messages, options, context) {
|
|
62
45
|
if (options?.onAfterCommit === void 0) return false;
|
|
63
46
|
try {
|
|
@@ -94,16 +77,16 @@ var hasDuplicates = (array, predicate) => {
|
|
|
94
77
|
return uniqueValues.size < mapped.length;
|
|
95
78
|
};
|
|
96
79
|
var getDuplicates = (array, predicate) => {
|
|
97
|
-
const
|
|
80
|
+
const map = /* @__PURE__ */ new Map();
|
|
98
81
|
for (let i = 0; i < array.length; i++) {
|
|
99
82
|
const item = array[i];
|
|
100
83
|
const key = predicate(item, i, array);
|
|
101
|
-
if (!
|
|
102
|
-
|
|
84
|
+
if (!map.has(key)) {
|
|
85
|
+
map.set(key, []);
|
|
103
86
|
}
|
|
104
|
-
|
|
87
|
+
map.get(key).push(item);
|
|
105
88
|
}
|
|
106
|
-
return Array.from(
|
|
89
|
+
return Array.from(map.values()).filter((group) => group.length > 1).flat();
|
|
107
90
|
};
|
|
108
91
|
var merge = (array, item, where, onExisting, onNotFound = () => void 0) => {
|
|
109
92
|
let wasFound = false;
|
|
@@ -348,29 +331,6 @@ var JSONParser = {
|
|
|
348
331
|
return options?.map ? options.map(parsed) : parsed;
|
|
349
332
|
}
|
|
350
333
|
};
|
|
351
|
-
var asyncRetry = async (fn, opts) => {
|
|
352
|
-
if (opts === void 0 || opts.retries === 0) return fn();
|
|
353
|
-
return retry(
|
|
354
|
-
async (bail) => {
|
|
355
|
-
try {
|
|
356
|
-
const result = await fn();
|
|
357
|
-
if (opts?.shouldRetryResult && opts.shouldRetryResult(result)) {
|
|
358
|
-
throw new EmmettError(
|
|
359
|
-
`Retrying because of result: ${JSONParser.stringify(result)}`
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
return result;
|
|
363
|
-
} catch (error2) {
|
|
364
|
-
if (opts?.shouldRetryError && !opts.shouldRetryError(error2)) {
|
|
365
|
-
bail(error2);
|
|
366
|
-
return void 0;
|
|
367
|
-
}
|
|
368
|
-
throw error2;
|
|
369
|
-
}
|
|
370
|
-
},
|
|
371
|
-
opts ?? { retries: 0 }
|
|
372
|
-
);
|
|
373
|
-
};
|
|
374
334
|
var AssertionError = class extends Error {
|
|
375
335
|
constructor(message2) {
|
|
376
336
|
super(message2);
|
|
@@ -398,44 +358,6 @@ function assertTrue(condition, message2) {
|
|
|
398
358
|
function assertOk(obj, message2) {
|
|
399
359
|
if (!obj) throw new AssertionError(message2 ?? `Condition is not truthy`);
|
|
400
360
|
}
|
|
401
|
-
var notifyAboutNoActiveReadersStream = (onNoActiveReaderCallback, options = {}) => new NotifyAboutNoActiveReadersStream(onNoActiveReaderCallback, options);
|
|
402
|
-
var NotifyAboutNoActiveReadersStream = class extends TransformStream2 {
|
|
403
|
-
constructor(onNoActiveReaderCallback, options = {}) {
|
|
404
|
-
super({
|
|
405
|
-
cancel: (reason) => {
|
|
406
|
-
console.log("Stream was canceled. Reason:", reason);
|
|
407
|
-
this.stopChecking();
|
|
408
|
-
}
|
|
409
|
-
});
|
|
410
|
-
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
411
|
-
this.streamId = options?.streamId ?? uuid3();
|
|
412
|
-
this.onNoActiveReaderCallback = onNoActiveReaderCallback;
|
|
413
|
-
this.startChecking(options?.intervalCheckInMs ?? 20);
|
|
414
|
-
}
|
|
415
|
-
checkInterval = null;
|
|
416
|
-
streamId;
|
|
417
|
-
_isStopped = false;
|
|
418
|
-
get hasActiveSubscribers() {
|
|
419
|
-
return !this._isStopped;
|
|
420
|
-
}
|
|
421
|
-
startChecking(interval) {
|
|
422
|
-
this.checkInterval = setInterval(() => {
|
|
423
|
-
this.checkNoActiveReader();
|
|
424
|
-
}, interval);
|
|
425
|
-
}
|
|
426
|
-
stopChecking() {
|
|
427
|
-
if (!this.checkInterval) return;
|
|
428
|
-
clearInterval(this.checkInterval);
|
|
429
|
-
this.checkInterval = null;
|
|
430
|
-
this._isStopped = true;
|
|
431
|
-
this.onNoActiveReaderCallback(this);
|
|
432
|
-
}
|
|
433
|
-
checkNoActiveReader() {
|
|
434
|
-
if (!this.readable.locked && !this._isStopped) {
|
|
435
|
-
this.stopChecking();
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
361
|
var filterProjections = (type, projections2) => {
|
|
440
362
|
const inlineProjections2 = projections2.filter((projection2) => projection2.type === type).map(({ projection: projection2 }) => projection2);
|
|
441
363
|
const duplicateRegistrations = arrayUtils.getDuplicates(
|
|
@@ -462,153 +384,12 @@ var projections = {
|
|
|
462
384
|
inline: inlineProjections,
|
|
463
385
|
async: asyncProjections
|
|
464
386
|
};
|
|
465
|
-
var filter = (filter2) => new TransformStream3({
|
|
466
|
-
transform(chunk, controller) {
|
|
467
|
-
if (filter2(chunk)) {
|
|
468
|
-
controller.enqueue(chunk);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
var map = (map2) => new TransformStream4({
|
|
473
|
-
transform(chunk, controller) {
|
|
474
|
-
controller.enqueue(map2(chunk));
|
|
475
|
-
}
|
|
476
|
-
});
|
|
477
|
-
var reduce = (reducer, initialValue) => new ReduceTransformStream(reducer, initialValue);
|
|
478
|
-
var ReduceTransformStream = class extends TransformStream5 {
|
|
479
|
-
accumulator;
|
|
480
|
-
reducer;
|
|
481
|
-
constructor(reducer, initialValue) {
|
|
482
|
-
super({
|
|
483
|
-
transform: (chunk) => {
|
|
484
|
-
this.accumulator = this.reducer(this.accumulator, chunk);
|
|
485
|
-
},
|
|
486
|
-
flush: (controller) => {
|
|
487
|
-
controller.enqueue(this.accumulator);
|
|
488
|
-
controller.terminate();
|
|
489
|
-
}
|
|
490
|
-
});
|
|
491
|
-
this.accumulator = initialValue;
|
|
492
|
-
this.reducer = reducer;
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
var retryStream = (createSourceStream, handleChunk2, retryOptions = { forever: true, minTimeout: 25 }) => new TransformStream6({
|
|
496
|
-
start(controller) {
|
|
497
|
-
asyncRetry(
|
|
498
|
-
() => onRestream(createSourceStream, handleChunk2, controller),
|
|
499
|
-
retryOptions
|
|
500
|
-
).catch((error2) => {
|
|
501
|
-
controller.error(error2);
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
});
|
|
505
|
-
var onRestream = async (createSourceStream, handleChunk2, controller) => {
|
|
506
|
-
const sourceStream = createSourceStream();
|
|
507
|
-
const reader = sourceStream.getReader();
|
|
508
|
-
try {
|
|
509
|
-
let done;
|
|
510
|
-
do {
|
|
511
|
-
const result = await reader.read();
|
|
512
|
-
done = result.done;
|
|
513
|
-
await handleChunk2(result, controller);
|
|
514
|
-
if (done) {
|
|
515
|
-
controller.terminate();
|
|
516
|
-
}
|
|
517
|
-
} while (!done);
|
|
518
|
-
} finally {
|
|
519
|
-
reader.releaseLock();
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
|
-
var skip = (limit) => new SkipTransformStream(limit);
|
|
523
|
-
var SkipTransformStream = class extends TransformStream7 {
|
|
524
|
-
count = 0;
|
|
525
|
-
skip;
|
|
526
|
-
constructor(skip2) {
|
|
527
|
-
super({
|
|
528
|
-
transform: (chunk, controller) => {
|
|
529
|
-
this.count++;
|
|
530
|
-
if (this.count > this.skip) {
|
|
531
|
-
controller.enqueue(chunk);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
});
|
|
535
|
-
this.skip = skip2;
|
|
536
|
-
}
|
|
537
|
-
};
|
|
538
|
-
var stopAfter = (stopCondition) => new TransformStream8({
|
|
539
|
-
transform(chunk, controller) {
|
|
540
|
-
controller.enqueue(chunk);
|
|
541
|
-
if (stopCondition(chunk)) {
|
|
542
|
-
controller.terminate();
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
});
|
|
546
|
-
var stopOn = (stopCondition) => new TransformStream9({
|
|
547
|
-
async transform(chunk, controller) {
|
|
548
|
-
if (!stopCondition(chunk)) {
|
|
549
|
-
controller.enqueue(chunk);
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
await Promise.resolve();
|
|
553
|
-
controller.terminate();
|
|
554
|
-
}
|
|
555
|
-
});
|
|
556
|
-
var take = (limit) => new TakeTransformStream(limit);
|
|
557
|
-
var TakeTransformStream = class extends TransformStream10 {
|
|
558
|
-
count = 0;
|
|
559
|
-
limit;
|
|
560
|
-
constructor(limit) {
|
|
561
|
-
super({
|
|
562
|
-
transform: (chunk, controller) => {
|
|
563
|
-
if (this.count < this.limit) {
|
|
564
|
-
this.count++;
|
|
565
|
-
controller.enqueue(chunk);
|
|
566
|
-
} else {
|
|
567
|
-
controller.terminate();
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
});
|
|
571
|
-
this.limit = limit;
|
|
572
|
-
}
|
|
573
|
-
};
|
|
574
|
-
var waitAtMost = (waitTimeInMs) => new TransformStream11({
|
|
575
|
-
start(controller) {
|
|
576
|
-
const timeoutId = setTimeout(() => {
|
|
577
|
-
controller.terminate();
|
|
578
|
-
}, waitTimeInMs);
|
|
579
|
-
const originalTerminate = controller.terminate.bind(controller);
|
|
580
|
-
controller.terminate = () => {
|
|
581
|
-
clearTimeout(timeoutId);
|
|
582
|
-
originalTerminate();
|
|
583
|
-
};
|
|
584
|
-
},
|
|
585
|
-
transform(chunk, controller) {
|
|
586
|
-
controller.enqueue(chunk);
|
|
587
|
-
}
|
|
588
|
-
});
|
|
589
|
-
var streamTransformations = {
|
|
590
|
-
filter,
|
|
591
|
-
take,
|
|
592
|
-
TakeTransformStream,
|
|
593
|
-
skip,
|
|
594
|
-
SkipTransformStream,
|
|
595
|
-
map,
|
|
596
|
-
notifyAboutNoActiveReadersStream,
|
|
597
|
-
NotifyAboutNoActiveReadersStream,
|
|
598
|
-
reduce,
|
|
599
|
-
ReduceTransformStream,
|
|
600
|
-
retry: retryStream,
|
|
601
|
-
stopAfter,
|
|
602
|
-
stopOn,
|
|
603
|
-
waitAtMost
|
|
604
|
-
};
|
|
605
|
-
var { retry: retry2 } = streamTransformations;
|
|
606
387
|
|
|
607
388
|
// src/eventStore/mongoDBEventStore.ts
|
|
608
389
|
import {
|
|
609
390
|
MongoClient as MongoClient2
|
|
610
391
|
} from "mongodb";
|
|
611
|
-
import { v4 as
|
|
392
|
+
import { v4 as uuid4 } from "uuid";
|
|
612
393
|
|
|
613
394
|
// src/eventStore/projections/mongoDBInlineProjection.ts
|
|
614
395
|
var MongoDBDefaultInlineProjectionName = "_default";
|
|
@@ -897,7 +678,7 @@ var MongoDBEventStoreImplementation = class {
|
|
|
897
678
|
const { streamType } = fromStreamName(streamName);
|
|
898
679
|
const expectedStreamVersion = options?.expectedStreamVersion;
|
|
899
680
|
const collection = await this.storage.collectionFor(streamType);
|
|
900
|
-
const
|
|
681
|
+
const filter = {
|
|
901
682
|
streamName: { $eq: streamName }
|
|
902
683
|
};
|
|
903
684
|
const eventsSliceArr = [];
|
|
@@ -910,7 +691,7 @@ var MongoDBEventStoreImplementation = class {
|
|
|
910
691
|
eventsSliceArr.push(Number(options.to));
|
|
911
692
|
}
|
|
912
693
|
const eventsSlice = eventsSliceArr.length > 1 ? { $slice: eventsSliceArr } : 1;
|
|
913
|
-
const stream = await collection.findOne(
|
|
694
|
+
const stream = await collection.findOne(filter, {
|
|
914
695
|
useBigInt64: true,
|
|
915
696
|
projection: {
|
|
916
697
|
metadata: 1,
|
|
@@ -967,7 +748,7 @@ var MongoDBEventStoreImplementation = class {
|
|
|
967
748
|
let streamOffset = currentStreamVersion;
|
|
968
749
|
const eventsToAppend = events.map((event) => {
|
|
969
750
|
const metadata = {
|
|
970
|
-
messageId:
|
|
751
|
+
messageId: uuid4(),
|
|
971
752
|
streamName,
|
|
972
753
|
streamPosition: ++streamOffset
|
|
973
754
|
};
|