@event-nest/core 4.0.2 → 5.0.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/README.md +249 -4
- package/package.json +2 -1
- package/src/index.d.ts +16 -0
- package/src/index.js +16 -0
- package/src/index.js.map +1 -1
- package/src/lib/aggregate-root/aggregate-root-config.d.ts +37 -0
- package/src/lib/aggregate-root/aggregate-root-config.js +25 -0
- package/src/lib/aggregate-root/aggregate-root-config.js.map +1 -0
- package/src/lib/aggregate-root/aggregate-root-name.d.ts +1 -1
- package/src/lib/aggregate-root/aggregate-root-name.js +1 -5
- package/src/lib/aggregate-root/aggregate-root-name.js.map +1 -1
- package/src/lib/aggregate-root/aggregate-root.d.ts +3 -5
- package/src/lib/aggregate-root/aggregate-root.js +10 -9
- package/src/lib/aggregate-root/aggregate-root.js.map +1 -1
- package/src/lib/aggregate-root/apply-event.decorator.d.ts +1 -1
- package/src/lib/aggregate-root/apply-event.decorator.js.map +1 -1
- package/src/lib/aggregate-root/reflection.d.ts +1 -1
- package/src/lib/aggregate-root/snapshot-aware.d.ts +47 -0
- package/src/lib/aggregate-root/snapshot-aware.js +52 -0
- package/src/lib/aggregate-root/snapshot-aware.js.map +1 -0
- package/src/lib/domain-event-registrations.d.ts +1 -1
- package/src/lib/domain-event-registrations.js.map +1 -1
- package/src/lib/domain-event-subscription.d.ts +1 -1
- package/src/lib/domain-event-subscription.js.map +1 -1
- package/src/lib/exceptions/aggregate-class-not-snapshot-aware-exception.d.ts +6 -0
- package/src/lib/exceptions/aggregate-class-not-snapshot-aware-exception.js +13 -0
- package/src/lib/exceptions/aggregate-class-not-snapshot-aware-exception.js.map +1 -0
- package/src/lib/exceptions/aggregate-instance-not-snapshot-aware-exception.d.ts +6 -0
- package/src/lib/exceptions/aggregate-instance-not-snapshot-aware-exception.js +13 -0
- package/src/lib/exceptions/aggregate-instance-not-snapshot-aware-exception.js.map +1 -0
- package/src/lib/exceptions/snapshot-revision-mismatch-exception.d.ts +6 -0
- package/src/lib/exceptions/snapshot-revision-mismatch-exception.js +14 -0
- package/src/lib/exceptions/snapshot-revision-mismatch-exception.js.map +1 -0
- package/src/lib/metadata-keys.d.ts +1 -0
- package/src/lib/metadata-keys.js +2 -1
- package/src/lib/metadata-keys.js.map +1 -1
- package/src/lib/snapshot-strategy/all-of-snapshot-strategy.d.ts +29 -0
- package/src/lib/snapshot-strategy/all-of-snapshot-strategy.js +40 -0
- package/src/lib/snapshot-strategy/all-of-snapshot-strategy.js.map +1 -0
- package/src/lib/snapshot-strategy/any-of-snapshot-strategy.d.ts +30 -0
- package/src/lib/snapshot-strategy/any-of-snapshot-strategy.js +41 -0
- package/src/lib/snapshot-strategy/any-of-snapshot-strategy.js.map +1 -0
- package/src/lib/snapshot-strategy/for-aggregate-roots-strategy.d.ts +30 -0
- package/src/lib/snapshot-strategy/for-aggregate-roots-strategy.js +51 -0
- package/src/lib/snapshot-strategy/for-aggregate-roots-strategy.js.map +1 -0
- package/src/lib/snapshot-strategy/for-count-snapshot-strategy.d.ts +28 -0
- package/src/lib/snapshot-strategy/for-count-snapshot-strategy.js +41 -0
- package/src/lib/snapshot-strategy/for-count-snapshot-strategy.js.map +1 -0
- package/src/lib/snapshot-strategy/for-events-snapshot-strategy.d.ts +33 -0
- package/src/lib/snapshot-strategy/for-events-snapshot-strategy.js +37 -0
- package/src/lib/snapshot-strategy/for-events-snapshot-strategy.js.map +1 -0
- package/src/lib/snapshot-strategy/no-snapshot-strategy.d.ts +18 -0
- package/src/lib/snapshot-strategy/no-snapshot-strategy.js +25 -0
- package/src/lib/snapshot-strategy/no-snapshot-strategy.js.map +1 -0
- package/src/lib/snapshot-strategy/snapshot-strategy.d.ts +26 -0
- package/src/lib/snapshot-strategy/snapshot-strategy.js +20 -0
- package/src/lib/snapshot-strategy/snapshot-strategy.js.map +1 -0
- package/src/lib/storage/abstract-event-store.d.ts +8 -2
- package/src/lib/storage/abstract-event-store.js +8 -3
- package/src/lib/storage/abstract-event-store.js.map +1 -1
- package/src/lib/storage/event-store.d.ts +29 -0
- package/src/lib/storage/event-store.js.map +1 -1
- package/src/lib/storage/snapshot/abstract-snapshot-store.d.ts +14 -0
- package/src/lib/storage/snapshot/abstract-snapshot-store.js +26 -0
- package/src/lib/storage/snapshot/abstract-snapshot-store.js.map +1 -0
- package/src/lib/storage/snapshot/no-op-snapshot-store.d.ts +11 -0
- package/src/lib/storage/snapshot/no-op-snapshot-store.js +25 -0
- package/src/lib/storage/snapshot/no-op-snapshot-store.js.map +1 -0
- package/src/lib/storage/snapshot/snapshot-store.d.ts +38 -0
- package/src/lib/storage/snapshot/snapshot-store.js +5 -0
- package/src/lib/storage/snapshot/snapshot-store.js.map +1 -0
- package/src/lib/storage/snapshot/stored-snapshot.d.ts +17 -0
- package/src/lib/storage/snapshot/stored-snapshot.js +36 -0
- package/src/lib/storage/snapshot/stored-snapshot.js.map +1 -0
- package/src/lib/storage/stored-event.d.ts +1 -1
- package/src/lib/storage/stored-event.js.map +1 -1
- package/src/lib/utils/type-utils.d.ts +4 -0
- package/src/lib/utils/type-utils.js.map +1 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { SnapshotAwareAggregateRoot } from "../storage/event-store";
|
|
2
|
+
import { AggregateRoot } from "./aggregate-root";
|
|
3
|
+
/**
|
|
4
|
+
* Interface that defines methods that should be implemented by an aggregate root to support snapshots.
|
|
5
|
+
* Snapshots allow for faster reconstitution of aggregate roots by storing their current state
|
|
6
|
+
* at specific points in time, rather than replaying all events from the beginning.
|
|
7
|
+
*
|
|
8
|
+
* @template Snapshot The type of the snapshot data structure
|
|
9
|
+
*/
|
|
10
|
+
export interface SnapshotAware<Snapshot = unknown> {
|
|
11
|
+
/**
|
|
12
|
+
* Applies a snapshot to restore the aggregate root's state from a previously saved snapshot.
|
|
13
|
+
* @param snapshot The snapshot data to apply
|
|
14
|
+
*/
|
|
15
|
+
applySnapshot(snapshot: Snapshot): void;
|
|
16
|
+
/**
|
|
17
|
+
* Should return a snapshot of the current state of the aggregate root.
|
|
18
|
+
* @returns The snapshot data representing the current state of the aggregate root.
|
|
19
|
+
*/
|
|
20
|
+
toSnapshot(): Promise<Snapshot> | Snapshot;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Type guard that checks if an aggregate root instance implements the SnapshotAware interface.
|
|
24
|
+
*
|
|
25
|
+
* @param aggregate The aggregate root instance to check
|
|
26
|
+
* @returns true if the aggregate root instance implements SnapshotAware, false otherwise
|
|
27
|
+
*/
|
|
28
|
+
export declare const isAggregateInstanceSnapshotAware: (aggregate: AggregateRoot) => aggregate is AggregateRoot & SnapshotAware;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if an aggregate root class supports snapshots by validating
|
|
31
|
+
* aggregateRootSnapshotRevision metadata value.
|
|
32
|
+
*
|
|
33
|
+
* @see {@link AggregateRootConfig} for metadata attachment details
|
|
34
|
+
* @param aggregateRoot The aggregate root instance to check
|
|
35
|
+
* @returns true if the aggregate root's class has correct numeric `snapshotRevision` attached, false otherwise
|
|
36
|
+
*/
|
|
37
|
+
export declare const isAggregateClassSnapshotAware: (aggregateRoot: AggregateRoot) => boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Asserts that the given aggregate root instance implements the SnapshotAware interface.
|
|
40
|
+
* Throws an error if the instance does not implement the interface.
|
|
41
|
+
*
|
|
42
|
+
* @param aggregateRoot The aggregate root instance to check
|
|
43
|
+
* @throws {AggregateInstanceNotSnapshotAwareException} if the instance does not implement SnapshotAware
|
|
44
|
+
* @throws {AggregateClassNotSnapshotAwareException} if the class does not have a valid snapshot revision
|
|
45
|
+
* @throws {MissingAggregateRootNameException} if the aggregate root name is missing
|
|
46
|
+
*/
|
|
47
|
+
export declare function assertIsSnapshotAwareAggregateRoot(aggregateRoot: AggregateRoot): asserts aggregateRoot is SnapshotAwareAggregateRoot;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isAggregateClassSnapshotAware = exports.isAggregateInstanceSnapshotAware = void 0;
|
|
4
|
+
exports.assertIsSnapshotAwareAggregateRoot = assertIsSnapshotAwareAggregateRoot;
|
|
5
|
+
const es_toolkit_1 = require("es-toolkit");
|
|
6
|
+
const aggregate_class_not_snapshot_aware_exception_1 = require("../exceptions/aggregate-class-not-snapshot-aware-exception");
|
|
7
|
+
const aggregate_instance_not_snapshot_aware_exception_1 = require("../exceptions/aggregate-instance-not-snapshot-aware-exception");
|
|
8
|
+
const missing_aggregate_root_name_exception_1 = require("../exceptions/missing-aggregate-root-name-exception");
|
|
9
|
+
const aggregate_root_config_1 = require("./aggregate-root-config");
|
|
10
|
+
/**
|
|
11
|
+
* Type guard that checks if an aggregate root instance implements the SnapshotAware interface.
|
|
12
|
+
*
|
|
13
|
+
* @param aggregate The aggregate root instance to check
|
|
14
|
+
* @returns true if the aggregate root instance implements SnapshotAware, false otherwise
|
|
15
|
+
*/
|
|
16
|
+
const isAggregateInstanceSnapshotAware = (aggregate) => "toSnapshot" in aggregate &&
|
|
17
|
+
(0, es_toolkit_1.isFunction)(aggregate.toSnapshot) &&
|
|
18
|
+
"applySnapshot" in aggregate &&
|
|
19
|
+
(0, es_toolkit_1.isFunction)(aggregate.applySnapshot);
|
|
20
|
+
exports.isAggregateInstanceSnapshotAware = isAggregateInstanceSnapshotAware;
|
|
21
|
+
/**
|
|
22
|
+
* Checks if an aggregate root class supports snapshots by validating
|
|
23
|
+
* aggregateRootSnapshotRevision metadata value.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link AggregateRootConfig} for metadata attachment details
|
|
26
|
+
* @param aggregateRoot The aggregate root instance to check
|
|
27
|
+
* @returns true if the aggregate root's class has correct numeric `snapshotRevision` attached, false otherwise
|
|
28
|
+
*/
|
|
29
|
+
const isAggregateClassSnapshotAware = (aggregateRoot) => typeof (0, aggregate_root_config_1.getAggregateRootSnapshotRevision)(aggregateRoot.constructor) === "number";
|
|
30
|
+
exports.isAggregateClassSnapshotAware = isAggregateClassSnapshotAware;
|
|
31
|
+
/**
|
|
32
|
+
* Asserts that the given aggregate root instance implements the SnapshotAware interface.
|
|
33
|
+
* Throws an error if the instance does not implement the interface.
|
|
34
|
+
*
|
|
35
|
+
* @param aggregateRoot The aggregate root instance to check
|
|
36
|
+
* @throws {AggregateInstanceNotSnapshotAwareException} if the instance does not implement SnapshotAware
|
|
37
|
+
* @throws {AggregateClassNotSnapshotAwareException} if the class does not have a valid snapshot revision
|
|
38
|
+
* @throws {MissingAggregateRootNameException} if the aggregate root name is missing
|
|
39
|
+
*/
|
|
40
|
+
function assertIsSnapshotAwareAggregateRoot(aggregateRoot) {
|
|
41
|
+
const aggregateRootName = (0, aggregate_root_config_1.getAggregateRootName)(aggregateRoot.constructor);
|
|
42
|
+
if (!aggregateRootName) {
|
|
43
|
+
throw new missing_aggregate_root_name_exception_1.MissingAggregateRootNameException(aggregateRoot.constructor.name);
|
|
44
|
+
}
|
|
45
|
+
if (!(0, exports.isAggregateInstanceSnapshotAware)(aggregateRoot)) {
|
|
46
|
+
throw new aggregate_instance_not_snapshot_aware_exception_1.AggregateInstanceNotSnapshotAwareException(aggregateRoot.constructor.name);
|
|
47
|
+
}
|
|
48
|
+
if (!(0, exports.isAggregateClassSnapshotAware)(aggregateRoot)) {
|
|
49
|
+
throw new aggregate_class_not_snapshot_aware_exception_1.AggregateClassNotSnapshotAwareException(aggregateRoot.constructor.name);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=snapshot-aware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot-aware.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/aggregate-root/snapshot-aware.ts"],"names":[],"mappings":";;;AAgEA,gFAgBC;AAhFD,2CAAwC;AAIxC,6HAAqH;AACrH,mIAA2H;AAC3H,+GAAwG;AAExG,mEAAiG;AAsBjG;;;;;GAKG;AACI,MAAM,gCAAgC,GAAG,CAC5C,SAAwB,EACkB,EAAE,CAC5C,YAAY,IAAI,SAAS;IACzB,IAAA,uBAAU,EAAC,SAAS,CAAC,UAAU,CAAC;IAChC,eAAe,IAAI,SAAS;IAC5B,IAAA,uBAAU,EAAC,SAAS,CAAC,aAAa,CAAC,CAAC;AAN3B,QAAA,gCAAgC,oCAML;AAExC;;;;;;;GAOG;AACI,MAAM,6BAA6B,GAAG,CAAC,aAA4B,EAAW,EAAE,CACnF,OAAO,IAAA,wDAAgC,EAAC,aAAa,CAAC,WAAW,CAAC,KAAK,QAAQ,CAAC;AADvE,QAAA,6BAA6B,iCAC0C;AAEpF;;;;;;;;GAQG;AACH,SAAgB,kCAAkC,CAC9C,aAA4B;IAE5B,MAAM,iBAAiB,GAAG,IAAA,4CAAoB,EAAC,aAAa,CAAC,WAAW,CAAC,CAAC;IAE1E,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACrB,MAAM,IAAI,yEAAiC,CAAC,aAAa,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAChF,CAAC;IAED,IAAI,CAAC,IAAA,wCAAgC,EAAC,aAAa,CAAC,EAAE,CAAC;QACnD,MAAM,IAAI,4FAA0C,CAAC,aAAa,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IACzF,CAAC;IAED,IAAI,CAAC,IAAA,qCAA6B,EAAC,aAAa,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,sFAAuC,CAAC,aAAa,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IACtF,CAAC;AACL,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domain-event-registrations.js","sourceRoot":"","sources":["../../../../../libs/core/src/lib/domain-event-registrations.ts"],"names":[],"mappings":";;AAgBA,sCAGC;AAMD,oCAGC;AAED,oCAEC;AAED,sCAMC;AAxCD,2CAAmC;
|
|
1
|
+
{"version":3,"file":"domain-event-registrations.js","sourceRoot":"","sources":["../../../../../libs/core/src/lib/domain-event-registrations.ts"],"names":[],"mappings":";;AAgBA,sCAGC;AAMD,oCAGC;AAED,oCAEC;AAED,sCAMC;AAxCD,2CAAmC;AAEnC,8FAAwF;AAQxF,MAAM,aAAa,GAAwB,EAAE,CAAC;AAE9C;;;GAGG;AACH,SAAgB,aAAa,CAAI,IAAY;IACzC,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,SAAS,KAAK,IAAI,CAAC,CAAC;IACpF,OAAO,IAAA,kBAAK,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAE,KAAK,CAAC,UAAuB,CAAC;AACrE,CAAC;AAED;;;GAGG;AACH,SAAgB,YAAY,CAAC,MAAc;IACvC,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,KAAK,MAAM,CAAC,WAAW,CAAC,CAAC;IACnG,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/C,CAAC;AAED,SAAgB,YAAY,CAAC,KAAa;IACtC,OAAO,CAAC,IAAA,kBAAK,EAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;AACvC,CAAC;AAED,SAAgB,aAAa,CAAC,eAA6B;IACvD,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,SAAS,KAAK,eAAe,CAAC,SAAS,CAAC,EAAE,CAAC;QAC7F,MAAM,IAAI,0DAA0B,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;IACpE,CAAC;IAED,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"domain-event-subscription.js","sourceRoot":"","sources":["../../../../../libs/core/src/lib/domain-event-subscription.ts"],"names":[],"mappings":";;AAqBA,0DA2BC;AAGD,gCAEC;AAED,oFAGC;AAED,4DAGC;AAED,8DAGC;AApED,2CAAwD;AACxD,6CAAyC;
|
|
1
|
+
{"version":3,"file":"domain-event-subscription.js","sourceRoot":"","sources":["../../../../../libs/core/src/lib/domain-event-subscription.ts"],"names":[],"mappings":";;AAqBA,0DA2BC;AAGD,gCAEC;AAED,oFAGC;AAED,4DAGC;AAED,8DAGC;AApED,2CAAwD;AACxD,6CAAyC;AAEzC,mDAAkF;AAkBlF,SAAgB,uBAAuB,CACnC,kBAA8D,EAC9D,GAAG,YAA8B;IAEjC,MAAM,kBAAkB,GAAG,IAAA,0BAAa,EAAC,kBAAkB,CAAC;QACxD,CAAC,CAAE,kBAAgD,CAAC,YAAY;QAChE,CAAC,CAAC,CAAC,kBAAkB,EAAE,GAAG,YAAY,CAAC,CAAC;IAE5C,IAAI,OAAO,GAAG,IAAI,CAAC;IACnB,IAAI,IAAA,0BAAa,EAAC,kBAAkB,CAAC,EAAE,CAAC;QACpC,OAAO,GAAI,kBAAgD,CAAC,OAAO,IAAI,IAAI,CAAC;IAChF,CAAC;IAED,MAAM,kBAAkB,GAAG,IAAA,iBAAI,EAAC,kBAAkB,CAAC,CAAC;IACpD,OAAO,CAAC,MAAc,EAAE,EAAE;QACtB,KAAK,MAAM,KAAK,IAAI,kBAAkB,EAAE,CAAC;YACrC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,gCAAgB,EAAE,KAAK,CAAC,EAAE,CAAC;gBACnD,OAAO,CAAC,cAAc,CAClB,gCAAgB,EAChB,EAAE,mBAAmB,EAAE,GAAG,KAAK,CAAC,IAAI,IAAI,IAAA,wBAAU,GAAE,EAAE,EAAE,EACxD,KAAK,CACR,CAAC;YACN,CAAC;QACL,CAAC;QAED,OAAO,CAAC,cAAc,CAAC,6CAA6B,EAAE,EAAE,MAAM,EAAE,kBAAkB,EAAE,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC;IAC3G,CAAC,CAAC;AACN,CAAC;AAED,sEAAsE;AACtE,SAAgB,UAAU,CAAC,gBAA0B;IACjD,OAAO,OAAO,CAAC,WAAW,CAAC,gCAAgB,EAAE,gBAAgB,CAAC,EAAE,mBAAmB,CAAC;AACxF,CAAC;AAED,SAAgB,oCAAoC,CAAC,oBAA4C;IAC7F,MAAM,QAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,6CAA6B,EAAE,oBAAoB,CAAC,WAAW,CAAC,CAAC;IACtG,OAAO,IAAA,kBAAK,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC;AAClD,CAAC;AAED,SAAgB,wBAAwB,CAAC,oBAA4C;IACjF,MAAM,QAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,6CAA6B,EAAE,oBAAoB,CAAC,WAAW,CAAC,CAAC;IACtG,OAAO,IAAA,kBAAK,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC;AACrD,CAAC;AAED,SAAgB,yBAAyB,CAAC,cAAsB;IAC5D,MAAM,WAAW,GAAG,OAAO,CAAC,cAAc,CAAC,6CAA6B,EAAE,cAAc,CAAC,WAAW,CAAC,CAAC;IACtG,OAAO,WAAW,IAAI,OAAQ,cAAyC,CAAC,aAAa,KAAK,UAAU,CAAC;AACzG,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exception thrown whenever AggregateRoot doesn't have snapshotRevision value attached, but it's needed either for snapshot creation or restoration.
|
|
3
|
+
*/
|
|
4
|
+
export declare class AggregateClassNotSnapshotAwareException extends Error {
|
|
5
|
+
constructor(aggregateRootName?: string);
|
|
6
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AggregateClassNotSnapshotAwareException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Exception thrown whenever AggregateRoot doesn't have snapshotRevision value attached, but it's needed either for snapshot creation or restoration.
|
|
6
|
+
*/
|
|
7
|
+
class AggregateClassNotSnapshotAwareException extends Error {
|
|
8
|
+
constructor(aggregateRootName) {
|
|
9
|
+
super(`Missing snapshot revision for aggregate root: ${aggregateRootName}\n. Did you forget to add snapshotRevision in AggregateRootConfig?`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.AggregateClassNotSnapshotAwareException = AggregateClassNotSnapshotAwareException;
|
|
13
|
+
//# sourceMappingURL=aggregate-class-not-snapshot-aware-exception.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate-class-not-snapshot-aware-exception.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/exceptions/aggregate-class-not-snapshot-aware-exception.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,uCAAwC,SAAQ,KAAK;IAC9D,YAAY,iBAA0B;QAClC,KAAK,CACD,iDAAiD,iBAAiB,oEAAoE,CACzI,CAAC;IACN,CAAC;CACJ;AAND,0FAMC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exception thrown whenever Strategy matches to create a snapshot for a specified AggregateRoot, but AggregateRoot doesn't implement the SnapshotAware interface correctly
|
|
3
|
+
*/
|
|
4
|
+
export declare class AggregateInstanceNotSnapshotAwareException extends Error {
|
|
5
|
+
constructor(aggregateRootName?: string);
|
|
6
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AggregateInstanceNotSnapshotAwareException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Exception thrown whenever Strategy matches to create a snapshot for a specified AggregateRoot, but AggregateRoot doesn't implement the SnapshotAware interface correctly
|
|
6
|
+
*/
|
|
7
|
+
class AggregateInstanceNotSnapshotAwareException extends Error {
|
|
8
|
+
constructor(aggregateRootName) {
|
|
9
|
+
super(`The snasphot should be created, but ${aggregateRootName} doesn't implement SnapshotAware interface correctly\n. Did you forget to implement .toSnapshot() or .applySnapshot()?`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.AggregateInstanceNotSnapshotAwareException = AggregateInstanceNotSnapshotAwareException;
|
|
13
|
+
//# sourceMappingURL=aggregate-instance-not-snapshot-aware-exception.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate-instance-not-snapshot-aware-exception.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/exceptions/aggregate-instance-not-snapshot-aware-exception.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,0CAA2C,SAAQ,KAAK;IACjE,YAAY,iBAA0B;QAClC,KAAK,CACD,uCAAuC,iBAAiB,wHAAwH,CACnL,CAAC;IACN,CAAC;CACJ;AAND,gGAMC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SnapshotRevisionMismatchException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Exception thrown whenever there's a mismatch between a snapshot record revision and a revision on an aggregate root class.
|
|
6
|
+
*/
|
|
7
|
+
class SnapshotRevisionMismatchException extends Error {
|
|
8
|
+
constructor(aggregateRootName) {
|
|
9
|
+
super(`The snapshotRevision on ${aggregateRootName} doesn't match the latest stored snapshot.
|
|
10
|
+
Please, check that the snapshotRevision defined in AggregateRootConfig is correclty configured.`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.SnapshotRevisionMismatchException = SnapshotRevisionMismatchException;
|
|
14
|
+
//# sourceMappingURL=snapshot-revision-mismatch-exception.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot-revision-mismatch-exception.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/exceptions/snapshot-revision-mismatch-exception.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,iCAAkC,SAAQ,KAAK;IACxD,YAAY,iBAAyB;QACjC,KAAK,CACD,2BAA2B,iBAAiB;6GACqD,CACpG,CAAC;IACN,CAAC;CACJ;AAPD,8EAOC"}
|
|
@@ -2,3 +2,4 @@ export declare const APPLY_EVENT_DECORATOR_KEY = "event-nest-process-event-meta"
|
|
|
2
2
|
export declare const DOMAIN_EVENT_SUBSCRIPTION_KEY = "event-nest-domain-event-subscription-meta";
|
|
3
3
|
export declare const DOMAIN_EVENT_KEY = "event-nest-domain-event-meta";
|
|
4
4
|
export declare const AGGREGATE_ROOT_NAME_KEY = "event-nest-aggregate-root-name-meta";
|
|
5
|
+
export declare const AGGREGATE_ROOT_SNAPSHOT_REVISION_KEY = "event-nest-aggregate-root-config-key";
|
package/src/lib/metadata-keys.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AGGREGATE_ROOT_NAME_KEY = exports.DOMAIN_EVENT_KEY = exports.DOMAIN_EVENT_SUBSCRIPTION_KEY = exports.APPLY_EVENT_DECORATOR_KEY = void 0;
|
|
3
|
+
exports.AGGREGATE_ROOT_SNAPSHOT_REVISION_KEY = exports.AGGREGATE_ROOT_NAME_KEY = exports.DOMAIN_EVENT_KEY = exports.DOMAIN_EVENT_SUBSCRIPTION_KEY = exports.APPLY_EVENT_DECORATOR_KEY = void 0;
|
|
4
4
|
exports.APPLY_EVENT_DECORATOR_KEY = "event-nest-process-event-meta";
|
|
5
5
|
exports.DOMAIN_EVENT_SUBSCRIPTION_KEY = "event-nest-domain-event-subscription-meta";
|
|
6
6
|
exports.DOMAIN_EVENT_KEY = "event-nest-domain-event-meta";
|
|
7
7
|
exports.AGGREGATE_ROOT_NAME_KEY = "event-nest-aggregate-root-name-meta";
|
|
8
|
+
exports.AGGREGATE_ROOT_SNAPSHOT_REVISION_KEY = "event-nest-aggregate-root-config-key";
|
|
8
9
|
//# sourceMappingURL=metadata-keys.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata-keys.js","sourceRoot":"","sources":["../../../../../libs/core/src/lib/metadata-keys.ts"],"names":[],"mappings":";;;AAAa,QAAA,yBAAyB,GAAG,+BAA+B,CAAC;AAC5D,QAAA,6BAA6B,GAAG,2CAA2C,CAAC;AAC5E,QAAA,gBAAgB,GAAG,8BAA8B,CAAC;AAClD,QAAA,uBAAuB,GAAG,qCAAqC,CAAC"}
|
|
1
|
+
{"version":3,"file":"metadata-keys.js","sourceRoot":"","sources":["../../../../../libs/core/src/lib/metadata-keys.ts"],"names":[],"mappings":";;;AAAa,QAAA,yBAAyB,GAAG,+BAA+B,CAAC;AAC5D,QAAA,6BAA6B,GAAG,2CAA2C,CAAC;AAC5E,QAAA,gBAAgB,GAAG,8BAA8B,CAAC;AAClD,QAAA,uBAAuB,GAAG,qCAAqC,CAAC;AAChE,QAAA,oCAAoC,GAAG,sCAAsC,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { AggregateRoot } from "../aggregate-root/aggregate-root";
|
|
2
|
+
import { SnapshotStrategy } from "./snapshot-strategy";
|
|
3
|
+
/**
|
|
4
|
+
* A composite snapshot strategy that creates a snapshot only when ALL underlying strategies
|
|
5
|
+
* return true. This implements an AND logic - all conditions must be satisfied.
|
|
6
|
+
*
|
|
7
|
+
* This strategy is useful when you need to combine multiple conditions. For example, you might
|
|
8
|
+
* want to create snapshots only for specific aggregate types AND only after a certain number
|
|
9
|
+
* of events have been processed.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* new AllOfSnapshotStrategy([
|
|
14
|
+
* new ForAggregateRootsStrategy({ aggregates: [User] }),
|
|
15
|
+
* new ForCountSnapshotStrategy({ count: 10 })
|
|
16
|
+
* ])
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* In this example, a snapshot will be created only if:
|
|
20
|
+
* - The aggregate root is of type User, AND
|
|
21
|
+
* - At least 10 events have been processed since the last snapshot
|
|
22
|
+
*
|
|
23
|
+
* If any of the underlying strategies returns false, no snapshot will be created.
|
|
24
|
+
*/
|
|
25
|
+
export declare class AllOfSnapshotStrategy extends SnapshotStrategy {
|
|
26
|
+
private readonly strategies;
|
|
27
|
+
constructor(strategies: SnapshotStrategy[]);
|
|
28
|
+
shouldCreateSnapshot(aggregateRoot: AggregateRoot): boolean;
|
|
29
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AllOfSnapshotStrategy = void 0;
|
|
4
|
+
const snapshot_strategy_1 = require("./snapshot-strategy");
|
|
5
|
+
/**
|
|
6
|
+
* A composite snapshot strategy that creates a snapshot only when ALL underlying strategies
|
|
7
|
+
* return true. This implements an AND logic - all conditions must be satisfied.
|
|
8
|
+
*
|
|
9
|
+
* This strategy is useful when you need to combine multiple conditions. For example, you might
|
|
10
|
+
* want to create snapshots only for specific aggregate types AND only after a certain number
|
|
11
|
+
* of events have been processed.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* new AllOfSnapshotStrategy([
|
|
16
|
+
* new ForAggregateRootsStrategy({ aggregates: [User] }),
|
|
17
|
+
* new ForCountSnapshotStrategy({ count: 10 })
|
|
18
|
+
* ])
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* In this example, a snapshot will be created only if:
|
|
22
|
+
* - The aggregate root is of type User, AND
|
|
23
|
+
* - At least 10 events have been processed since the last snapshot
|
|
24
|
+
*
|
|
25
|
+
* If any of the underlying strategies returns false, no snapshot will be created.
|
|
26
|
+
*/
|
|
27
|
+
class AllOfSnapshotStrategy extends snapshot_strategy_1.SnapshotStrategy {
|
|
28
|
+
constructor(strategies) {
|
|
29
|
+
super();
|
|
30
|
+
this.strategies = strategies;
|
|
31
|
+
if (strategies.length === 0) {
|
|
32
|
+
throw new Error("AllOfSnapshotStrategy requires at least one strategy");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
shouldCreateSnapshot(aggregateRoot) {
|
|
36
|
+
return this.strategies.every((strategy) => strategy.shouldCreateSnapshot(aggregateRoot));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.AllOfSnapshotStrategy = AllOfSnapshotStrategy;
|
|
40
|
+
//# sourceMappingURL=all-of-snapshot-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-of-snapshot-strategy.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/snapshot-strategy/all-of-snapshot-strategy.ts"],"names":[],"mappings":";;;AACA,2DAAuD;AAEvD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,qBAAsB,SAAQ,oCAAgB;IACvD,YAA6B,UAA8B;QACvD,KAAK,EAAE,CAAC;QADiB,eAAU,GAAV,UAAU,CAAoB;QAEvD,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC5E,CAAC;IACL,CAAC;IAED,oBAAoB,CAAC,aAA4B;QAC7C,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,oBAAoB,CAAC,aAAa,CAAC,CAAC,CAAC;IAC7F,CAAC;CACJ;AAXD,sDAWC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AggregateRoot } from "../aggregate-root/aggregate-root";
|
|
2
|
+
import { SnapshotStrategy } from "./snapshot-strategy";
|
|
3
|
+
/**
|
|
4
|
+
* A composite snapshot strategy that creates a snapshot when at least ONE underlying strategy
|
|
5
|
+
* returns true. This implements an OR logic - any condition can be satisfied.
|
|
6
|
+
*
|
|
7
|
+
* This strategy is useful when you want to create snapshots under multiple different conditions.
|
|
8
|
+
* For example, you might want to create snapshots either when a specific event occurs OR when
|
|
9
|
+
* certain aggregate types reach a certain event count.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* new AnyOfSnapshotStrategy([
|
|
14
|
+
* new AllOfSnapshotStrategy([
|
|
15
|
+
* new ForAggregateRootsStrategy({ aggregates: [User] }),
|
|
16
|
+
* new ForCountSnapshotStrategy({ count: 10 })
|
|
17
|
+
* ]),
|
|
18
|
+
* new ForEventsSnapshotStrategy({ eventClasses: [UserIdentityChangedEvent] })
|
|
19
|
+
* ])
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* In this example, a snapshot will be created if any of the following conditions are met:
|
|
23
|
+
* - The aggregate root is of type User AND at least 10 events have been processed since the last snapshot, OR
|
|
24
|
+
* - A UserIdentityChangedEvent event is present
|
|
25
|
+
*/
|
|
26
|
+
export declare class AnyOfSnapshotStrategy extends SnapshotStrategy {
|
|
27
|
+
private readonly strategies;
|
|
28
|
+
constructor(strategies: SnapshotStrategy[]);
|
|
29
|
+
shouldCreateSnapshot(aggregateRoot: AggregateRoot): boolean;
|
|
30
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnyOfSnapshotStrategy = void 0;
|
|
4
|
+
const snapshot_strategy_1 = require("./snapshot-strategy");
|
|
5
|
+
/**
|
|
6
|
+
* A composite snapshot strategy that creates a snapshot when at least ONE underlying strategy
|
|
7
|
+
* returns true. This implements an OR logic - any condition can be satisfied.
|
|
8
|
+
*
|
|
9
|
+
* This strategy is useful when you want to create snapshots under multiple different conditions.
|
|
10
|
+
* For example, you might want to create snapshots either when a specific event occurs OR when
|
|
11
|
+
* certain aggregate types reach a certain event count.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* new AnyOfSnapshotStrategy([
|
|
16
|
+
* new AllOfSnapshotStrategy([
|
|
17
|
+
* new ForAggregateRootsStrategy({ aggregates: [User] }),
|
|
18
|
+
* new ForCountSnapshotStrategy({ count: 10 })
|
|
19
|
+
* ]),
|
|
20
|
+
* new ForEventsSnapshotStrategy({ eventClasses: [UserIdentityChangedEvent] })
|
|
21
|
+
* ])
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* In this example, a snapshot will be created if any of the following conditions are met:
|
|
25
|
+
* - The aggregate root is of type User AND at least 10 events have been processed since the last snapshot, OR
|
|
26
|
+
* - A UserIdentityChangedEvent event is present
|
|
27
|
+
*/
|
|
28
|
+
class AnyOfSnapshotStrategy extends snapshot_strategy_1.SnapshotStrategy {
|
|
29
|
+
constructor(strategies) {
|
|
30
|
+
super();
|
|
31
|
+
this.strategies = strategies;
|
|
32
|
+
if (strategies.length === 0) {
|
|
33
|
+
throw new Error("AnyOfSnapshotStrategy requires at least one strategy");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
shouldCreateSnapshot(aggregateRoot) {
|
|
37
|
+
return this.strategies.some((strategy) => strategy.shouldCreateSnapshot(aggregateRoot));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.AnyOfSnapshotStrategy = AnyOfSnapshotStrategy;
|
|
41
|
+
//# sourceMappingURL=any-of-snapshot-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"any-of-snapshot-strategy.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/snapshot-strategy/any-of-snapshot-strategy.ts"],"names":[],"mappings":";;;AACA,2DAAuD;AAEvD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAa,qBAAsB,SAAQ,oCAAgB;IACvD,YAA6B,UAA8B;QACvD,KAAK,EAAE,CAAC;QADiB,eAAU,GAAV,UAAU,CAAoB;QAEvD,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC5E,CAAC;IACL,CAAC;IAED,oBAAoB,CAAC,aAA4B;QAC7C,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,oBAAoB,CAAC,aAAa,CAAC,CAAC,CAAC;IAC5F,CAAC;CACJ;AAXD,sDAWC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AggregateRoot } from "../aggregate-root/aggregate-root";
|
|
2
|
+
import { AggregateRootClass } from "../storage/event-store";
|
|
3
|
+
import { SnapshotStrategy } from "./snapshot-strategy";
|
|
4
|
+
export interface ForAggregateRootsStrategyConfig {
|
|
5
|
+
aggregates: AggregateRootClass<unknown>[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A snapshot strategy that creates snapshots only for a specific list of aggregate root types.
|
|
9
|
+
* This strategy is useful when you want to enable snapshotting for certain aggregates while
|
|
10
|
+
* excluding others.
|
|
11
|
+
* It is meant to be used alongside with the {@link AllOfSnapshotStrategy} to filter out snapshot creation for specific aggregate root types.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Create snapshots only for User aggregates
|
|
16
|
+
* new ForAggregateRootsStrategy({ aggregates: [User] })
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create snapshots for multiple aggregate types
|
|
22
|
+
* new ForAggregateRootsStrategy({ aggregates: [User, Order, Product] })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare class ForAggregateRootsStrategy extends SnapshotStrategy {
|
|
26
|
+
private aggregateRootNames;
|
|
27
|
+
private logger;
|
|
28
|
+
constructor(config: ForAggregateRootsStrategyConfig);
|
|
29
|
+
shouldCreateSnapshot(aggregateRoot: AggregateRoot): boolean;
|
|
30
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ForAggregateRootsStrategy = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const es_toolkit_1 = require("es-toolkit");
|
|
6
|
+
const aggregate_root_config_1 = require("../aggregate-root/aggregate-root-config");
|
|
7
|
+
const snapshot_strategy_1 = require("./snapshot-strategy");
|
|
8
|
+
/**
|
|
9
|
+
* A snapshot strategy that creates snapshots only for a specific list of aggregate root types.
|
|
10
|
+
* This strategy is useful when you want to enable snapshotting for certain aggregates while
|
|
11
|
+
* excluding others.
|
|
12
|
+
* It is meant to be used alongside with the {@link AllOfSnapshotStrategy} to filter out snapshot creation for specific aggregate root types.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Create snapshots only for User aggregates
|
|
17
|
+
* new ForAggregateRootsStrategy({ aggregates: [User] })
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Create snapshots for multiple aggregate types
|
|
23
|
+
* new ForAggregateRootsStrategy({ aggregates: [User, Order, Product] })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class ForAggregateRootsStrategy extends snapshot_strategy_1.SnapshotStrategy {
|
|
27
|
+
constructor(config) {
|
|
28
|
+
super();
|
|
29
|
+
this.logger = new common_1.Logger(ForAggregateRootsStrategy.name);
|
|
30
|
+
this.aggregateRootNames = config.aggregates
|
|
31
|
+
.map((aggregateRootClass) => {
|
|
32
|
+
const aggregateRootName = (0, aggregate_root_config_1.getAggregateRootName)(aggregateRootClass);
|
|
33
|
+
if ((0, es_toolkit_1.isNil)(aggregateRootName)) {
|
|
34
|
+
this.logger.error(`Missing aggregate root name for class: ${aggregateRootClass.name}. Use the @AggregateRootName decorator.`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
return aggregateRootName;
|
|
38
|
+
})
|
|
39
|
+
.filter((name) => typeof name === "string");
|
|
40
|
+
}
|
|
41
|
+
shouldCreateSnapshot(aggregateRoot) {
|
|
42
|
+
const aggregateRootName = (0, aggregate_root_config_1.getAggregateRootName)(aggregateRoot.constructor);
|
|
43
|
+
if (!aggregateRootName) {
|
|
44
|
+
this.logger.error(`Missing aggregate root name for class: ${aggregateRoot.constructor}. Use the @AggregateRootName decorator.`);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return this.aggregateRootNames.includes(aggregateRootName);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.ForAggregateRootsStrategy = ForAggregateRootsStrategy;
|
|
51
|
+
//# sourceMappingURL=for-aggregate-roots-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"for-aggregate-roots-strategy.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/snapshot-strategy/for-aggregate-roots-strategy.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AACxC,2CAAmC;AAGnC,mFAA+E;AAE/E,2DAAuD;AAMvD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,yBAA0B,SAAQ,oCAAgB;IAI3D,YAAY,MAAuC;QAC/C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,IAAI,eAAM,CAAC,yBAAyB,CAAC,IAAI,CAAC,CAAC;QACzD,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC,UAAU;aACtC,GAAG,CAAC,CAAC,kBAAkB,EAAE,EAAE;YACxB,MAAM,iBAAiB,GAAG,IAAA,4CAAoB,EAAC,kBAAkB,CAAC,CAAC;YACnE,IAAI,IAAA,kBAAK,EAAC,iBAAiB,CAAC,EAAE,CAAC;gBAC3B,IAAI,CAAC,MAAM,CAAC,KAAK,CACb,0CAA0C,kBAAkB,CAAC,IAAI,yCAAyC,CAC7G,CAAC;gBAEF,OAAO;YACX,CAAC;YAED,OAAO,iBAAiB,CAAC;QAC7B,CAAC,CAAC;aACD,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC;IACpD,CAAC;IAEQ,oBAAoB,CAAC,aAA4B;QACtD,MAAM,iBAAiB,GAAG,IAAA,4CAAoB,EAAC,aAAa,CAAC,WAAW,CAAC,CAAC;QAC1E,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACrB,IAAI,CAAC,MAAM,CAAC,KAAK,CACb,0CAA0C,aAAa,CAAC,WAAW,yCAAyC,CAC/G,CAAC;YAEF,OAAO,KAAK,CAAC;QACjB,CAAC;QAED,OAAO,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC;IAC/D,CAAC;CACJ;AAnCD,8DAmCC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AggregateRoot } from "../aggregate-root/aggregate-root";
|
|
2
|
+
import { SnapshotStrategy } from "./snapshot-strategy";
|
|
3
|
+
export interface ForCountSnapshotStrategyConfig {
|
|
4
|
+
count: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* A snapshot strategy that creates snapshots based on the number of events processed.
|
|
8
|
+
* A snapshot will be created when the aggregate root has processed at least the specified
|
|
9
|
+
* number of events since the last snapshot.
|
|
10
|
+
*
|
|
11
|
+
* If you append more than `count` events in a single batch (before calling commit), only one
|
|
12
|
+
* snapshot will be created for the entire batch, not multiple snapshots. This ensures that
|
|
13
|
+
* even if you append many events at once, you get a single snapshot representing the state
|
|
14
|
+
* after all those events are applied.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Create a snapshot after at least 10 events
|
|
19
|
+
* new ForCountSnapshotStrategy({ count: 10 })
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @throws {Error} If count is less than 1
|
|
23
|
+
*/
|
|
24
|
+
export declare class ForCountSnapshotStrategy extends SnapshotStrategy {
|
|
25
|
+
private readonly config;
|
|
26
|
+
constructor(config: ForCountSnapshotStrategyConfig);
|
|
27
|
+
shouldCreateSnapshot(aggregateRoot: AggregateRoot): boolean;
|
|
28
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ForCountSnapshotStrategy = void 0;
|
|
4
|
+
const snapshot_strategy_1 = require("./snapshot-strategy");
|
|
5
|
+
/**
|
|
6
|
+
* A snapshot strategy that creates snapshots based on the number of events processed.
|
|
7
|
+
* A snapshot will be created when the aggregate root has processed at least the specified
|
|
8
|
+
* number of events since the last snapshot.
|
|
9
|
+
*
|
|
10
|
+
* If you append more than `count` events in a single batch (before calling commit), only one
|
|
11
|
+
* snapshot will be created for the entire batch, not multiple snapshots. This ensures that
|
|
12
|
+
* even if you append many events at once, you get a single snapshot representing the state
|
|
13
|
+
* after all those events are applied.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Create a snapshot after at least 10 events
|
|
18
|
+
* new ForCountSnapshotStrategy({ count: 10 })
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @throws {Error} If count is less than 1
|
|
22
|
+
*/
|
|
23
|
+
class ForCountSnapshotStrategy extends snapshot_strategy_1.SnapshotStrategy {
|
|
24
|
+
constructor(config) {
|
|
25
|
+
super();
|
|
26
|
+
this.config = config;
|
|
27
|
+
if (config.count < 1) {
|
|
28
|
+
throw new Error("PerCountSnapshotStrategy: config.count may not be less than 1");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
shouldCreateSnapshot(aggregateRoot) {
|
|
32
|
+
const currentVersion = aggregateRoot.version;
|
|
33
|
+
const newEventsCount = aggregateRoot.uncommittedEvents.length;
|
|
34
|
+
const projectedVersion = currentVersion + newEventsCount;
|
|
35
|
+
const currentBlock = Math.floor(currentVersion / this.config.count);
|
|
36
|
+
const projectedBlock = Math.floor(projectedVersion / this.config.count);
|
|
37
|
+
return projectedBlock - currentBlock > 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ForCountSnapshotStrategy = ForCountSnapshotStrategy;
|
|
41
|
+
//# sourceMappingURL=for-count-snapshot-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"for-count-snapshot-strategy.js","sourceRoot":"","sources":["../../../../../../libs/core/src/lib/snapshot-strategy/for-count-snapshot-strategy.ts"],"names":[],"mappings":";;;AACA,2DAAuD;AAMvD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,wBAAyB,SAAQ,oCAAgB;IAC1D,YAA6B,MAAsC;QAC/D,KAAK,EAAE,CAAC;QADiB,WAAM,GAAN,MAAM,CAAgC;QAE/D,IAAI,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;QACrF,CAAC;IACL,CAAC;IAED,oBAAoB,CAAC,aAA4B;QAC7C,MAAM,cAAc,GAAG,aAAa,CAAC,OAAO,CAAC;QAC7C,MAAM,cAAc,GAAG,aAAa,CAAC,iBAAiB,CAAC,MAAM,CAAC;QAC9D,MAAM,gBAAgB,GAAG,cAAc,GAAG,cAAc,CAAC;QAEzD,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACpE,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,gBAAgB,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAExE,OAAO,cAAc,GAAG,YAAY,GAAG,CAAC,CAAC;IAC7C,CAAC;CACJ;AAlBD,4DAkBC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AggregateRoot } from "../aggregate-root/aggregate-root";
|
|
2
|
+
import { Class } from "../utils/type-utils";
|
|
3
|
+
import { SnapshotStrategy } from "./snapshot-strategy";
|
|
4
|
+
export interface ForEventsSnapshotStrategyConfig {
|
|
5
|
+
eventClasses: Class<unknown>[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A snapshot strategy that creates snapshots when specific event types are present in the
|
|
9
|
+
* aggregate root's uncommitted events.
|
|
10
|
+
*
|
|
11
|
+
* This is useful when you want to create snapshots for specific important events, such as
|
|
12
|
+
* events that significantly change the aggregate's state or events that are expensive to
|
|
13
|
+
* replay.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Create a snapshot whenever a UserIdentityChangedEvent is present
|
|
18
|
+
* new ForEventsSnapshotStrategy({ eventClasses: [UserIdentityChangedEvent] })
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Create a snapshot for multiple event types
|
|
24
|
+
* new ForEventsSnapshotStrategy({
|
|
25
|
+
* eventClasses: [UserIdentityChangedEvent, UserOrderUpdatedEvent]
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class ForEventsSnapshotStrategy extends SnapshotStrategy {
|
|
30
|
+
private readonly config;
|
|
31
|
+
constructor(config: ForEventsSnapshotStrategyConfig);
|
|
32
|
+
shouldCreateSnapshot(aggregateRoot: AggregateRoot): boolean;
|
|
33
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ForEventsSnapshotStrategy = void 0;
|
|
4
|
+
const snapshot_strategy_1 = require("./snapshot-strategy");
|
|
5
|
+
/**
|
|
6
|
+
* A snapshot strategy that creates snapshots when specific event types are present in the
|
|
7
|
+
* aggregate root's uncommitted events.
|
|
8
|
+
*
|
|
9
|
+
* This is useful when you want to create snapshots for specific important events, such as
|
|
10
|
+
* events that significantly change the aggregate's state or events that are expensive to
|
|
11
|
+
* replay.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Create a snapshot whenever a UserIdentityChangedEvent is present
|
|
16
|
+
* new ForEventsSnapshotStrategy({ eventClasses: [UserIdentityChangedEvent] })
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create a snapshot for multiple event types
|
|
22
|
+
* new ForEventsSnapshotStrategy({
|
|
23
|
+
* eventClasses: [UserIdentityChangedEvent, UserOrderUpdatedEvent]
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
class ForEventsSnapshotStrategy extends snapshot_strategy_1.SnapshotStrategy {
|
|
28
|
+
constructor(config) {
|
|
29
|
+
super();
|
|
30
|
+
this.config = config;
|
|
31
|
+
}
|
|
32
|
+
shouldCreateSnapshot(aggregateRoot) {
|
|
33
|
+
return aggregateRoot.uncommittedEvents.some((event_) => this.config.eventClasses.some((class_) => event_.payload instanceof class_));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.ForEventsSnapshotStrategy = ForEventsSnapshotStrategy;
|
|
37
|
+
//# sourceMappingURL=for-events-snapshot-strategy.js.map
|