@ddd-ts/event-sourcing-firestore 0.0.13 → 0.0.14
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/flat/firestore.es-aggregate-store.d.ts +12 -7
- package/dist/flat/firestore.es-aggregate-store.d.ts.map +1 -1
- package/dist/flat/firestore.es-aggregate-store.js +15 -3
- package/dist/flat/firestore.es-aggregate-store.js.map +1 -1
- package/dist/flat/firestore.event-store.d.ts +3 -2
- package/dist/flat/firestore.event-store.d.ts.map +1 -1
- package/dist/flat/firestore.event-store.js +8 -5
- package/dist/flat/firestore.event-store.js.map +1 -1
- package/dist/flat/firestore.snapshotter.d.ts +5 -4
- package/dist/flat/firestore.snapshotter.d.ts.map +1 -1
- package/dist/flat/firestore.snapshotter.js +9 -6
- package/dist/flat/firestore.snapshotter.js.map +1 -1
- package/dist/nested/bank.spec.js +1 -1
- package/dist/nested/bank.spec.js.map +1 -1
- package/dist/nested/firestore.es-aggregate-store.d.ts +10 -7
- package/dist/nested/firestore.es-aggregate-store.d.ts.map +1 -1
- package/dist/nested/firestore.es-aggregate-store.js +71 -31
- package/dist/nested/firestore.es-aggregate-store.js.map +1 -1
- package/dist/nested/firestore.event-store.d.ts +11 -2
- package/dist/nested/firestore.event-store.d.ts.map +1 -1
- package/dist/nested/firestore.event-store.js +34 -17
- package/dist/nested/firestore.event-store.js.map +1 -1
- package/dist/nested/firestore.event-store.spec.js +206 -1
- package/dist/nested/firestore.event-store.spec.js.map +1 -1
- package/dist/nested/firestore.snapshotter.d.ts +5 -8
- package/dist/nested/firestore.snapshotter.d.ts.map +1 -1
- package/dist/nested/firestore.snapshotter.js +22 -33
- package/dist/nested/firestore.snapshotter.js.map +1 -1
- package/package.json +8 -8
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { HasTrait } from "@ddd-ts/traits";
|
|
2
|
-
import {
|
|
3
|
-
import { type FirestoreTransactionPerformer } from "@ddd-ts/store-firestore";
|
|
2
|
+
import { AggregateStreamId, EventSourced, type Identifiable, type IEsAggregateStore, type IEventBus, type ISerializer, type Transaction } from "@ddd-ts/core";
|
|
3
|
+
import { type FirestoreTransaction, type FirestoreTransactionPerformer } from "@ddd-ts/store-firestore";
|
|
4
4
|
import type { FlatFirestoreSnapshotter } from "./firestore.snapshotter";
|
|
5
5
|
import type { FlatFirestoreEventStore } from "./firestore.event-store";
|
|
6
6
|
export declare const MakeFlatFirestoreEsAggregateStore: <A extends Omit<(abstract new (props: {}) => {
|
|
7
7
|
acknowledgedRevision: number;
|
|
8
8
|
changes: import("@ddd-ts/core").IEsEvent<string, any>[];
|
|
9
|
-
getAggregateStreamId(this: any): any;
|
|
10
9
|
load(fact: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
11
10
|
apply(change: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
12
11
|
play(event: import("@ddd-ts/core").IEsEvent<string, any>): any;
|
|
@@ -14,14 +13,12 @@ export declare const MakeFlatFirestoreEsAggregateStore: <A extends Omit<(abstrac
|
|
|
14
13
|
acknowledgeChanges(): void;
|
|
15
14
|
}) & {
|
|
16
15
|
events: import("@ddd-ts/types").Constructor<import("@ddd-ts/core").IEsEvent<string, any>>[];
|
|
17
|
-
getAggregateStreamId(id: import("@ddd-ts/core").Identifier): AggregateStreamId;
|
|
18
16
|
instanciate<TH extends import("@ddd-ts/types").Constructor>(this: TH, event: import("@ddd-ts/core").IEsEvent<string, any>): InstanceType<TH>;
|
|
19
17
|
loadFirst<TH_1 extends import("@ddd-ts/types").Constructor>(this: TH_1, event: import("@ddd-ts/core").IEsEvent<string, any>): InstanceType<TH_1>;
|
|
20
18
|
"new"<TH_2 extends import("@ddd-ts/types").Constructor>(this: TH_2, event: import("@ddd-ts/core").IEsEvent<string, any>): InstanceType<TH_2>;
|
|
21
19
|
}, ""> & (new (...args: any[]) => {
|
|
22
20
|
acknowledgedRevision: number;
|
|
23
21
|
changes: import("@ddd-ts/core").IEsEvent<string, any>[];
|
|
24
|
-
getAggregateStreamId(this: any): any;
|
|
25
22
|
load(fact: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
26
23
|
apply(change: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
27
24
|
play(event: import("@ddd-ts/core").IEsEvent<string, any>): any;
|
|
@@ -42,7 +39,11 @@ export declare const MakeFlatFirestoreEsAggregateStore: <A extends Omit<(abstrac
|
|
|
42
39
|
_publishEventsTo?: IEventBus;
|
|
43
40
|
publishEventsTo(eventBus: IEventBus): void;
|
|
44
41
|
load(id: InstanceType<A>["id"]): Promise<InstanceType<A> | undefined>;
|
|
45
|
-
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated
|
|
44
|
+
*/
|
|
45
|
+
saveAll(aggregates: InstanceType<A>[], trx?: Transaction | undefined): Promise<void>;
|
|
46
|
+
save(aggregate: InstanceType<A>, trx?: FirestoreTransaction, attempts?: number): Promise<void>;
|
|
46
47
|
};
|
|
47
48
|
};
|
|
48
49
|
export declare abstract class FlatFirestoreEsAggregateStore<A extends HasTrait<typeof EventSourced> & HasTrait<typeof Identifiable>> implements IEsAggregateStore<InstanceType<A>> {
|
|
@@ -56,6 +57,10 @@ export declare abstract class FlatFirestoreEsAggregateStore<A extends HasTrait<t
|
|
|
56
57
|
_publishEventsTo?: IEventBus;
|
|
57
58
|
publishEventsTo(eventBus: IEventBus): void;
|
|
58
59
|
load(id: InstanceType<A>["id"]): Promise<InstanceType<A> | undefined>;
|
|
59
|
-
|
|
60
|
+
/**
|
|
61
|
+
* @deprecated
|
|
62
|
+
*/
|
|
63
|
+
saveAll(aggregates: InstanceType<A>[], trx?: Transaction | undefined): Promise<void>;
|
|
64
|
+
save(aggregate: InstanceType<A>, trx?: FirestoreTransaction, attempts?: number): Promise<void>;
|
|
60
65
|
}
|
|
61
66
|
//# sourceMappingURL=firestore.es-aggregate-store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.es-aggregate-store.d.ts","sourceRoot":"","sources":["../../src/flat/firestore.es-aggregate-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,
|
|
1
|
+
{"version":3,"file":"firestore.es-aggregate-store.d.ts","sourceRoot":"","sources":["../../src/flat/firestore.es-aggregate-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EACL,iBAAiB,EAEjB,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,WAAW,EACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,KAAK,oBAAoB,EACzB,KAAK,6BAA6B,EACnC,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACxE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAEvE,eAAO,MAAM,iCAAiC,GAUJ,CAAC;;;;;;;;;;;;;wBA0GjC,KAAM;;;;;;;;;;wBAAN,KAAM;;eA1G0B,CAAC,AAP7B;qBAqBkB,uBAAuB,eACtB,6BAA6B;yBAnBzC,YAAY,CAIS,CAAC,AAJR,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG,YAAY,CAI5B,CAAC,AAJ6B,CAAC;iCAI5C,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,iBAAiB;6BActC,uBAAuB;8BACtB,6BAA6B;;;2BAUzC,SAAS;kCACF,SAAS;;QAkCnC;;WAEG;qDAGK,WAAW,GAAG,SAAS,GAC5B,OAAO,CAAC,IAAI,CAAC;+CAMR,oBAAoB,sBAEzB,OAAO,CAAC,IAAI,CAAC;;CAnEjB,CAAC;AAEF,8BAAsB,6BAA6B,CACjD,CAAC,SAAS,QAAQ,CAAC,OAAO,YAAY,CAAC,GAAG,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,7 +8,10 @@ const MakeFlatFirestoreEsAggregateStore = (AGGREGATE) => {
|
|
|
8
8
|
return AGGREGATE.loadFirst(event);
|
|
9
9
|
}
|
|
10
10
|
getAggregateStreamId(id) {
|
|
11
|
-
return
|
|
11
|
+
return new core_1.AggregateStreamId({
|
|
12
|
+
aggregate: AGGREGATE.name,
|
|
13
|
+
id: id.toString(),
|
|
14
|
+
});
|
|
12
15
|
}
|
|
13
16
|
};
|
|
14
17
|
};
|
|
@@ -52,7 +55,16 @@ class FlatFirestoreEsAggregateStore {
|
|
|
52
55
|
}
|
|
53
56
|
return instance;
|
|
54
57
|
}
|
|
55
|
-
|
|
58
|
+
/**
|
|
59
|
+
* @deprecated
|
|
60
|
+
*/
|
|
61
|
+
async saveAll(aggregates, trx) {
|
|
62
|
+
throw new Error("SaveAll not supported");
|
|
63
|
+
}
|
|
64
|
+
async save(aggregate, trx, attempts = 5) {
|
|
65
|
+
if (trx) {
|
|
66
|
+
throw new Error("Transactions are not supported in FlatFirestore");
|
|
67
|
+
}
|
|
56
68
|
const streamId = this.getAggregateStreamId(aggregate.id);
|
|
57
69
|
const changes = [...aggregate.changes];
|
|
58
70
|
const serialized = await Promise.all(changes.map((event) => this.serializer.serialize(event)));
|
|
@@ -78,7 +90,7 @@ class FlatFirestoreEsAggregateStore {
|
|
|
78
90
|
for (const change of changes) {
|
|
79
91
|
pristine.apply(change);
|
|
80
92
|
}
|
|
81
|
-
return await this.save(pristine, attempts - 1);
|
|
93
|
+
return await this.save(pristine, trx, attempts - 1);
|
|
82
94
|
}
|
|
83
95
|
throw error;
|
|
84
96
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.es-aggregate-store.js","sourceRoot":"","sources":["../../src/flat/firestore.es-aggregate-store.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"firestore.es-aggregate-store.js","sourceRoot":"","sources":["../../src/flat/firestore.es-aggregate-store.ts"],"names":[],"mappings":";;;AACA,u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,9 +1,10 @@
|
|
|
1
1
|
import { type AggregateStreamId, type IChange, type IFact } from "@ddd-ts/core";
|
|
2
|
-
import type
|
|
2
|
+
import { type FirestoreTransaction } from "@ddd-ts/store-firestore";
|
|
3
3
|
import * as fb from "firebase-admin";
|
|
4
4
|
export declare class FlatFirestoreEventStore {
|
|
5
5
|
readonly firestore: fb.firestore.Firestore;
|
|
6
|
-
|
|
6
|
+
readonly converter: fb.firestore.FirestoreDataConverter<fb.firestore.DocumentData>;
|
|
7
|
+
constructor(firestore: fb.firestore.Firestore, converter?: fb.firestore.FirestoreDataConverter<fb.firestore.DocumentData>);
|
|
7
8
|
get aggregateCollection(): fb.firestore.CollectionReference<fb.firestore.DocumentData>;
|
|
8
9
|
close(): Promise<void>;
|
|
9
10
|
append(streamId: AggregateStreamId, changes: IChange[], expectedRevision: number, trx: FirestoreTransaction): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.event-store.d.ts","sourceRoot":"","sources":["../../src/flat/firestore.event-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,iBAAiB,EAEtB,KAAK,OAAO,EACZ,KAAK,KAAK,EACX,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"firestore.event-store.d.ts","sourceRoot":"","sources":["../../src/flat/firestore.event-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,iBAAiB,EAEtB,KAAK,OAAO,EACZ,KAAK,KAAK,EACX,MAAM,cAAc,CAAC;AACtB,OAAO,EAEL,KAAK,oBAAoB,EAC1B,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAErC,qBAAa,uBAAuB;aAEhB,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,SAAS;aACjC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,sBAAsB,CAAC,EAAE,CAAC,SAAS,CAAC,YAAY,CAAC;gBADzE,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,SAAS,EACjC,SAAS,GAAE,EAAE,CAAC,SAAS,CAAC,sBAAsB,CAAC,EAAE,CAAC,SAAS,CAAC,YAAY,CAA0B;IAGpH,IAAI,mBAAmB,gEAEtB;IAEK,KAAK;IAEL,MAAM,CACV,QAAQ,EAAE,iBAAiB,EAC3B,OAAO,EAAE,OAAO,EAAE,EAClB,gBAAgB,EAAE,MAAM,EACxB,GAAG,EAAE,oBAAoB,GACxB,OAAO,CAAC,IAAI,CAAC;IAmCT,IAAI,CACT,QAAQ,EAAE,iBAAiB,EAC3B,IAAI,CAAC,EAAE,MAAM,GACZ,aAAa,CAAC,KAAK,CAAC;CAsBxB"}
|
|
@@ -25,11 +25,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.FlatFirestoreEventStore = void 0;
|
|
27
27
|
const core_1 = require("@ddd-ts/core");
|
|
28
|
+
const store_firestore_1 = require("@ddd-ts/store-firestore");
|
|
28
29
|
const fb = __importStar(require("firebase-admin"));
|
|
29
30
|
class FlatFirestoreEventStore {
|
|
30
31
|
firestore;
|
|
31
|
-
|
|
32
|
+
converter;
|
|
33
|
+
constructor(firestore, converter = new store_firestore_1.DefaultConverter()) {
|
|
32
34
|
this.firestore = firestore;
|
|
35
|
+
this.converter = converter;
|
|
33
36
|
}
|
|
34
37
|
get aggregateCollection() {
|
|
35
38
|
return this.firestore.collection("events");
|
|
@@ -46,7 +49,7 @@ class FlatFirestoreEventStore {
|
|
|
46
49
|
}
|
|
47
50
|
let revision = expectedRevision + 1;
|
|
48
51
|
for (const change of changes) {
|
|
49
|
-
trx.transaction.create(this.aggregateCollection.doc(change.id.toString()), {
|
|
52
|
+
trx.transaction.create(this.aggregateCollection.doc(change.id.toString()), this.converter.toFirestore({
|
|
50
53
|
aggregateType: streamId.aggregate,
|
|
51
54
|
id: change.id.toString(),
|
|
52
55
|
aggregateId: streamId.id.toString(),
|
|
@@ -54,7 +57,7 @@ class FlatFirestoreEventStore {
|
|
|
54
57
|
type: change.name,
|
|
55
58
|
payload: change.payload,
|
|
56
59
|
occurredAt: fb.firestore.FieldValue.serverTimestamp(),
|
|
57
|
-
});
|
|
60
|
+
}));
|
|
58
61
|
await new Promise((r) => setTimeout(r, 1)); // ensure occurredAt is unique
|
|
59
62
|
revision++;
|
|
60
63
|
}
|
|
@@ -69,13 +72,13 @@ class FlatFirestoreEventStore {
|
|
|
69
72
|
}
|
|
70
73
|
for await (const event of query.stream()) {
|
|
71
74
|
const e = event;
|
|
72
|
-
const data =
|
|
75
|
+
const data = this.converter.fromFirestore(e);
|
|
73
76
|
yield {
|
|
74
77
|
id: e.id,
|
|
75
78
|
revision: data.revision,
|
|
76
79
|
name: data.type,
|
|
77
80
|
payload: data.payload,
|
|
78
|
-
occurredAt: data.occurredAt
|
|
81
|
+
occurredAt: data.occurredAt,
|
|
79
82
|
};
|
|
80
83
|
}
|
|
81
84
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.event-store.js","sourceRoot":"","sources":["../../src/flat/firestore.event-store.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAKsB;
|
|
1
|
+
{"version":3,"file":"firestore.event-store.js","sourceRoot":"","sources":["../../src/flat/firestore.event-store.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAKsB;AACtB,6DAGiC;AACjC,mDAAqC;AAErC,MAAa,uBAAuB;IAEhB;IACA;IAFlB,YACkB,SAAiC,EACjC,YAA4E,IAAI,kCAAgB,EAAE;QADlG,cAAS,GAAT,SAAS,CAAwB;QACjC,cAAS,GAAT,SAAS,CAAyF;IACjH,CAAC;IAEJ,IAAI,mBAAmB;QACrB,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,KAAK,KAA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,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,8BAA8B;YAC1E,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAED,KAAK,CAAC,CAAC,IAAI,CACT,QAA2B,EAC3B,IAAa;QAEb,IAAI,KAAK,GAAG,IAAI,CAAC,mBAAmB;aACjC,KAAK,CAAC,eAAe,EAAE,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC;aAChD,KAAK,CAAC,aAAa,EAAE,IAAI,EAAE,QAAQ,CAAC,EAAE,CAAC;aACvC,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAE9B,IAAI,IAAI,EAAE,CAAC;YACT,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QAC7C,CAAC;QAED,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;YACzC,MAAM,CAAC,GAAG,KAAuD,CAAC;YAClE,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;YAC7C,MAAM;gBACJ,EAAE,EAAE,CAAC,CAAC,EAAE;gBACR,QAAQ,EAAE,IAAI,CAAC,QAAQ;gBACvB,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,UAAU,EAAE,IAAI,CAAC,UAAU;aAC5B,CAAC;QACJ,CAAC;IACH,CAAC;CACF;AA7ED,0DA6EC"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type
|
|
1
|
+
import { type IEventSourced, type ISerializer, type IIdentifiable } from "@ddd-ts/core";
|
|
2
|
+
import { type FirestoreTransaction } from "@ddd-ts/store-firestore";
|
|
3
3
|
export declare class FlatFirestoreSnapshotter<A extends IEventSourced & IIdentifiable> {
|
|
4
4
|
private readonly db;
|
|
5
5
|
readonly serializer: ISerializer<A>;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
readonly converter: FirebaseFirestore.FirestoreDataConverter<FirebaseFirestore.DocumentData>;
|
|
7
|
+
constructor(db: FirebaseFirestore.Firestore, serializer: ISerializer<A>, converter?: FirebaseFirestore.FirestoreDataConverter<FirebaseFirestore.DocumentData>);
|
|
8
|
+
load(id: A["id"]): Promise<A | undefined>;
|
|
8
9
|
save(aggregate: A, trx: FirestoreTransaction): Promise<void>;
|
|
9
10
|
}
|
|
10
11
|
//# sourceMappingURL=firestore.snapshotter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.snapshotter.d.ts","sourceRoot":"","sources":["../../src/flat/firestore.snapshotter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,
|
|
1
|
+
{"version":3,"file":"firestore.snapshotter.d.ts","sourceRoot":"","sources":["../../src/flat/firestore.snapshotter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,aAAa,EACnB,MAAM,cAAc,CAAC;AACtB,OAAO,EAEL,KAAK,oBAAoB,EAC1B,MAAM,yBAAyB,CAAC;AAEjC,qBAAa,wBAAwB,CAAC,CAAC,SAAS,aAAa,GAAG,aAAa;IAEzE,OAAO,CAAC,QAAQ,CAAC,EAAE;aACH,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC;aAC1B,SAAS,EAAE,iBAAiB,CAAC,sBAAsB,CAAC,iBAAiB,CAAC,YAAY,CAAC;gBAFlF,EAAE,EAAE,iBAAiB,CAAC,SAAS,EAChC,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC,EAC1B,SAAS,GAAE,iBAAiB,CAAC,sBAAsB,CAAC,iBAAiB,CAAC,YAAY,CAA0B;IAGxH,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAyBzC,IAAI,CAAC,SAAS,EAAE,CAAC,EAAE,GAAG,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;CAanE"}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FlatFirestoreSnapshotter = void 0;
|
|
4
|
+
const store_firestore_1 = require("@ddd-ts/store-firestore");
|
|
4
5
|
class FlatFirestoreSnapshotter {
|
|
5
6
|
db;
|
|
6
7
|
serializer;
|
|
7
|
-
|
|
8
|
+
converter;
|
|
9
|
+
constructor(db, serializer, converter = new store_firestore_1.DefaultConverter()) {
|
|
8
10
|
this.db = db;
|
|
9
11
|
this.serializer = serializer;
|
|
12
|
+
this.converter = converter;
|
|
10
13
|
}
|
|
11
|
-
async load(
|
|
14
|
+
async load(id) {
|
|
12
15
|
const query = await this.db
|
|
13
16
|
.collection("snapshots")
|
|
14
|
-
.where("id", "==",
|
|
17
|
+
.where("id", "==", id.toString())
|
|
15
18
|
.orderBy("revision", "desc")
|
|
16
19
|
.limit(1)
|
|
17
20
|
.get();
|
|
@@ -19,7 +22,7 @@ class FlatFirestoreSnapshotter {
|
|
|
19
22
|
if (!document || !document.exists) {
|
|
20
23
|
return undefined;
|
|
21
24
|
}
|
|
22
|
-
const snapshot =
|
|
25
|
+
const snapshot = this.converter.fromFirestore(document);
|
|
23
26
|
if (!snapshot) {
|
|
24
27
|
return undefined;
|
|
25
28
|
}
|
|
@@ -31,11 +34,11 @@ class FlatFirestoreSnapshotter {
|
|
|
31
34
|
const id = aggregate.id.toString();
|
|
32
35
|
await trx.transaction.set(this.db
|
|
33
36
|
.collection("snapshots")
|
|
34
|
-
.doc(`${id.toString()}.${aggregate.acknowledgedRevision.toString()}`), {
|
|
37
|
+
.doc(`${id.toString()}.${aggregate.acknowledgedRevision.toString()}`), this.converter.toFirestore({
|
|
35
38
|
id: id.toString(),
|
|
36
39
|
revision: Number(aggregate.acknowledgedRevision),
|
|
37
40
|
serialized: await this.serializer.serialize(aggregate),
|
|
38
|
-
});
|
|
41
|
+
}));
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
exports.FlatFirestoreSnapshotter = FlatFirestoreSnapshotter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.snapshotter.js","sourceRoot":"","sources":["../../src/flat/firestore.snapshotter.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"firestore.snapshotter.js","sourceRoot":"","sources":["../../src/flat/firestore.snapshotter.ts"],"names":[],"mappings":";;;AAKA,6DAGiC;AAEjC,MAAa,wBAAwB;IAEhB;IACD;IACA;IAHlB,YACmB,EAA+B,EAChC,UAA0B,EAC1B,YAAsF,IAAI,kCAAgB,EAAE;QAF3G,OAAE,GAAF,EAAE,CAA6B;QAChC,eAAU,GAAV,UAAU,CAAgB;QAC1B,cAAS,GAAT,SAAS,CAAmG;IAC3H,CAAC;IAEJ,KAAK,CAAC,IAAI,CAAC,EAAW;QACpB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,EAAE;aACxB,UAAU,CAAC,WAAW,CAAC;aACvB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC;aAChC,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC;aAC3B,KAAK,CAAC,CAAC,CAAC;aACR,GAAG,EAAE,CAAC;QAET,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAE/B,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;YAClC,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAExD,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QACxE,QAAQ,CAAC,oBAAoB,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC1D,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,SAAY,EAAE,GAAyB;QAChD,MAAM,EAAE,GAAG,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;QACnC,MAAM,GAAG,CAAC,WAAW,CAAC,GAAG,CACvB,IAAI,CAAC,EAAE;aACJ,UAAU,CAAC,WAAW,CAAC;aACvB,GAAG,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,IAAI,SAAS,CAAC,oBAAoB,CAAC,QAAQ,EAAE,EAAE,CAAC,EACvE,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC;YACzB,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE;YACjB,QAAQ,EAAE,MAAM,CAAC,SAAS,CAAC,oBAAoB,CAAC;YAChD,UAAU,EAAE,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,SAAS,CAAC;SACvD,CAAC,CACH,CAAC;IACJ,CAAC;CACF;AA7CD,4DA6CC"}
|
package/dist/nested/bank.spec.js
CHANGED
|
@@ -42,7 +42,7 @@ describe("NestedFirestore Bank Test", () => {
|
|
|
42
42
|
const store = new store_firestore_1.FirestoreStore(name, firestore, serializer);
|
|
43
43
|
return store;
|
|
44
44
|
}, (AGGREGATE, serializer, eventsSerializers) => {
|
|
45
|
-
const snapshotter = new firestore_snapshotter_1.NestedFirestoreSnapshotter(firestore, serializer);
|
|
45
|
+
const snapshotter = new firestore_snapshotter_1.NestedFirestoreSnapshotter(AGGREGATE.name, firestore, serializer);
|
|
46
46
|
const Store = (0, firestore_es_aggregate_store_1.MakeNestedFirestoreEsAggregateStore)(AGGREGATE);
|
|
47
47
|
const store = new Store(es, transaction, eventsSerializers, snapshotter);
|
|
48
48
|
store.publishEventsTo(eventBus);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bank.spec.js","sourceRoot":"","sources":["../../src/nested/bank.spec.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,CAAC,GAAG,CAAC,uBAAuB,GAAG,gBAAgB,CAAC;AAEvD,mDAAqC;AAErC,yCAA0C;AAC1C,uCAAgD;AAChD,6DAGiC;AAEjC,mEAAoE;AACpE,mEAAqE;AACrE,iFAAqF;AAErF,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;AAExB,QAAQ,CAAC,2BAA2B,EAAE,GAAG,EAAE;IACzC,MAAM,GAAG,GAAG,EAAE,CAAC,aAAa,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,GAAG,CAAC,SAAS,EAAE,CAAC;IAElC,MAAM,WAAW,GAAG,IAAI,+CAA6B,CAAC,SAAS,CAAC,CAAC;IACjE,MAAM,EAAE,GAAG,IAAI,iDAAyB,CAAC,SAAS,CAAC,CAAC;IACpD,MAAM,QAAQ,GAAG,IAAI,uBAAgB,EAAE,CAAC;IAExC,IAAA,iBAAS,EACP,QAAQ,EACR,CAAC,UAAU,EAAE,IAAI,EAAE,EAAE;QACnB,MAAM,KAAK,GAAG,IAAI,gCAAc,CAAC,IAAI,EAAE,SAAS,EAAE,UAAU,CAAQ,CAAC;QACrE,OAAO,KAAK,CAAC;IACf,CAAC,EACD,CAAC,SAAS,EAAE,UAAU,EAAE,iBAAiB,EAAE,EAAE;QAC3C,MAAM,WAAW,GAAG,IAAI,kDAA0B,CAAC,SAAS,
|
|
1
|
+
{"version":3,"file":"bank.spec.js","sourceRoot":"","sources":["../../src/nested/bank.spec.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,CAAC,GAAG,CAAC,uBAAuB,GAAG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|
2
|
-
import {
|
|
3
|
-
import type { FirestoreTransactionPerformer } from "@ddd-ts/store-firestore";
|
|
2
|
+
import { AggregateStreamId, EventSourced, type Identifiable, type IEsAggregateStore, type IEventBus, type ISerializer } from "@ddd-ts/core";
|
|
3
|
+
import type { FirestoreTransaction, FirestoreTransactionPerformer } from "@ddd-ts/store-firestore";
|
|
4
4
|
import type { NestedFirestoreSnapshotter } from "./firestore.snapshotter";
|
|
5
5
|
import type { NestedFirestoreEventStore } from "./firestore.event-store";
|
|
6
6
|
export declare const MakeNestedFirestoreEsAggregateStore: <A extends Omit<(abstract new (props: {}) => {
|
|
7
7
|
acknowledgedRevision: number;
|
|
8
8
|
changes: import("@ddd-ts/core").IEsEvent<string, any>[];
|
|
9
|
-
getAggregateStreamId(this: any): any;
|
|
10
9
|
load(fact: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
11
10
|
apply(change: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
12
11
|
play(event: import("@ddd-ts/core").IEsEvent<string, any>): any;
|
|
@@ -14,14 +13,12 @@ export declare const MakeNestedFirestoreEsAggregateStore: <A extends Omit<(abstr
|
|
|
14
13
|
acknowledgeChanges(): void;
|
|
15
14
|
}) & {
|
|
16
15
|
events: import("@ddd-ts/types").Constructor<import("@ddd-ts/core").IEsEvent<string, any>>[];
|
|
17
|
-
getAggregateStreamId(id: import("@ddd-ts/core").Identifier): AggregateStreamId;
|
|
18
16
|
instanciate<TH extends import("@ddd-ts/types").Constructor>(this: TH, event: import("@ddd-ts/core").IEsEvent<string, any>): InstanceType<TH>;
|
|
19
17
|
loadFirst<TH_1 extends import("@ddd-ts/types").Constructor>(this: TH_1, event: import("@ddd-ts/core").IEsEvent<string, any>): InstanceType<TH_1>;
|
|
20
18
|
"new"<TH_2 extends import("@ddd-ts/types").Constructor>(this: TH_2, event: import("@ddd-ts/core").IEsEvent<string, any>): InstanceType<TH_2>;
|
|
21
19
|
}, ""> & (new (...args: any[]) => {
|
|
22
20
|
acknowledgedRevision: number;
|
|
23
21
|
changes: import("@ddd-ts/core").IEsEvent<string, any>[];
|
|
24
|
-
getAggregateStreamId(this: any): any;
|
|
25
22
|
load(fact: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
26
23
|
apply(change: import("@ddd-ts/core").IEsEvent<string, any>): void;
|
|
27
24
|
play(event: import("@ddd-ts/core").IEsEvent<string, any>): any;
|
|
@@ -41,8 +38,11 @@ export declare const MakeNestedFirestoreEsAggregateStore: <A extends Omit<(abstr
|
|
|
41
38
|
readonly snapshotter?: NestedFirestoreSnapshotter<InstanceType<A>> | undefined;
|
|
42
39
|
_publishEventsTo?: IEventBus;
|
|
43
40
|
publishEventsTo(eventBus: IEventBus): void;
|
|
41
|
+
loadFromSnapshot(snapshot: InstanceType<A>): Promise<InstanceType<A>>;
|
|
44
42
|
load(id: InstanceType<A>["id"]): Promise<InstanceType<A> | undefined>;
|
|
45
|
-
|
|
43
|
+
performTransaction(trx: FirestoreTransaction | undefined, callback: (trx: FirestoreTransaction) => Promise<void>): Promise<void>;
|
|
44
|
+
saveAll(aggregates: InstanceType<A>[], parentTrx?: FirestoreTransaction, attempts?: number): Promise<void>;
|
|
45
|
+
save(aggregate: InstanceType<A>, trx?: FirestoreTransaction, attempts?: number): Promise<void>;
|
|
46
46
|
};
|
|
47
47
|
};
|
|
48
48
|
export declare abstract class NestedFirestoreEsAggregateStore<A extends HasTrait<typeof EventSourced> & HasTrait<typeof Identifiable>> implements IEsAggregateStore<InstanceType<A>> {
|
|
@@ -55,7 +55,10 @@ export declare abstract class NestedFirestoreEsAggregateStore<A extends HasTrait
|
|
|
55
55
|
abstract loadFirst(event: InstanceType<A>["changes"][number]): InstanceType<A>;
|
|
56
56
|
_publishEventsTo?: IEventBus;
|
|
57
57
|
publishEventsTo(eventBus: IEventBus): void;
|
|
58
|
+
loadFromSnapshot(snapshot: InstanceType<A>): Promise<InstanceType<A>>;
|
|
58
59
|
load(id: InstanceType<A>["id"]): Promise<InstanceType<A> | undefined>;
|
|
59
|
-
|
|
60
|
+
performTransaction(trx: FirestoreTransaction | undefined, callback: (trx: FirestoreTransaction) => Promise<void>): Promise<void>;
|
|
61
|
+
saveAll(aggregates: InstanceType<A>[], parentTrx?: FirestoreTransaction, attempts?: number): Promise<void>;
|
|
62
|
+
save(aggregate: InstanceType<A>, trx?: FirestoreTransaction, attempts?: number): Promise<void>;
|
|
60
63
|
}
|
|
61
64
|
//# sourceMappingURL=firestore.es-aggregate-store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.es-aggregate-store.d.ts","sourceRoot":"","sources":["../../src/nested/firestore.es-aggregate-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,
|
|
1
|
+
{"version":3,"file":"firestore.es-aggregate-store.d.ts","sourceRoot":"","sources":["../../src/nested/firestore.es-aggregate-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EACL,iBAAiB,EAEjB,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACd,KAAK,WAAW,EACjB,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EACV,oBAAoB,EACpB,6BAA6B,EAC9B,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AAC1E,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AAEzE,eAAO,MAAM,mCAAmC,GAUN,CAAC;;;;;;;;;;;;;wBAkGZ,KAAM;;;;;;;;;;wBAAN,KAAM;;eAlGK,CAAC,AAP7B;qBAqBkB,yBAAyB,eACxB,6BAA6B;yBAnBzC,YAAY,CAIS,CAAC,AAJR,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG,YAAY,CAI5B,CAAC,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AS,CAAC;IAC7B,eAAe,CAAC,QAAQ,EAAE,SAAS;IAI7B,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC;IAgB1C,IAAI,CAAC,EAAE,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAsBpC,kBAAkB,CAChB,GAAG,EAAE,oBAAoB,GAAG,SAAS,EACrC,QAAQ,EAAE,CAAC,GAAG,EAAE,oBAAoB,KAAK,OAAO,CAAC,IAAI,CAAC;IAKlD,OAAO,CACX,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,EAC7B,SAAS,CAAC,EAAE,oBAAoB,EAChC,QAAQ,SAAK,GACZ,OAAO,CAAC,IAAI,CAAC;IAgFV,IAAI,CACR,SAAS,EAAE,YAAY,CAAC,CAAC,CAAC,EAC1B,GAAG,CAAC,EAAE,oBAAoB,EAC1B,QAAQ,SAAK,GACZ,OAAO,CAAC,IAAI,CAAC;CAGjB"}
|
|
@@ -8,7 +8,10 @@ const MakeNestedFirestoreEsAggregateStore = (AGGREGATE) => {
|
|
|
8
8
|
return AGGREGATE.loadFirst(event);
|
|
9
9
|
}
|
|
10
10
|
getAggregateStreamId(id) {
|
|
11
|
-
return
|
|
11
|
+
return new core_1.AggregateStreamId({
|
|
12
|
+
aggregate: AGGREGATE.name,
|
|
13
|
+
id: id.toString(),
|
|
14
|
+
});
|
|
12
15
|
}
|
|
13
16
|
};
|
|
14
17
|
};
|
|
@@ -28,16 +31,20 @@ class NestedFirestoreEsAggregateStore {
|
|
|
28
31
|
publishEventsTo(eventBus) {
|
|
29
32
|
this._publishEventsTo = eventBus;
|
|
30
33
|
}
|
|
34
|
+
async loadFromSnapshot(snapshot) {
|
|
35
|
+
const streamId = this.getAggregateStreamId(snapshot.id);
|
|
36
|
+
const stream = this.eventStore.read(streamId, snapshot.acknowledgedRevision + 1);
|
|
37
|
+
for await (const serialized of stream) {
|
|
38
|
+
const event = await this.serializer.deserialize(serialized);
|
|
39
|
+
snapshot.load(event);
|
|
40
|
+
}
|
|
41
|
+
return snapshot;
|
|
42
|
+
}
|
|
31
43
|
async load(id) {
|
|
32
44
|
const streamId = this.getAggregateStreamId(id);
|
|
33
|
-
const snapshot = await this.snapshotter?.load(
|
|
45
|
+
const snapshot = await this.snapshotter?.load(id);
|
|
34
46
|
if (snapshot) {
|
|
35
|
-
|
|
36
|
-
for await (const serialized of stream) {
|
|
37
|
-
const event = await this.serializer.deserialize(serialized);
|
|
38
|
-
snapshot.load(event);
|
|
39
|
-
}
|
|
40
|
-
return snapshot;
|
|
47
|
+
return this.loadFromSnapshot(snapshot);
|
|
41
48
|
}
|
|
42
49
|
let instance = undefined;
|
|
43
50
|
for await (const serialized of this.eventStore.read(streamId)) {
|
|
@@ -51,38 +58,71 @@ class NestedFirestoreEsAggregateStore {
|
|
|
51
58
|
}
|
|
52
59
|
return instance;
|
|
53
60
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
61
|
+
performTransaction(trx, callback) {
|
|
62
|
+
return trx ? callback(trx) : this.transaction.perform(callback);
|
|
63
|
+
}
|
|
64
|
+
async saveAll(aggregates, parentTrx, attempts = 10) {
|
|
65
|
+
const toSave = await Promise.all(aggregates.map(async (aggregate) => ({
|
|
66
|
+
aggregate: aggregate,
|
|
67
|
+
streamId: this.getAggregateStreamId(aggregate.id),
|
|
68
|
+
changes: [...aggregate.changes],
|
|
69
|
+
serialized: await Promise.all(aggregate.changes.map((event) => this.serializer.serialize(event))),
|
|
70
|
+
acknowledgedRevision: aggregate.acknowledgedRevision,
|
|
71
|
+
})));
|
|
59
72
|
try {
|
|
60
|
-
await this.
|
|
61
|
-
await this.eventStore.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
await this.performTransaction(parentTrx, async (trx) => {
|
|
74
|
+
await this.eventStore.bulkAppend(toSave.map((save) => ({
|
|
75
|
+
streamId: save.streamId,
|
|
76
|
+
changes: save.serialized,
|
|
77
|
+
expectedRevision: save.acknowledgedRevision,
|
|
78
|
+
})), trx);
|
|
79
|
+
for (const save of toSave) {
|
|
80
|
+
save.aggregate.acknowledgeChanges();
|
|
81
|
+
}
|
|
82
|
+
if (!parentTrx) {
|
|
83
|
+
trx.onCommit(async () => {
|
|
84
|
+
if (this._publishEventsTo) {
|
|
85
|
+
for (const { changes } of toSave) {
|
|
86
|
+
for (const event of changes) {
|
|
87
|
+
await this._publishEventsTo.publish(event);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
67
90
|
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
await this.snapshotter?.save(aggregate, trx);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
await Promise.all(toSave.map((save) => this.snapshotter?.save(save.aggregate, trx)));
|
|
71
94
|
});
|
|
72
95
|
}
|
|
73
96
|
catch (error) {
|
|
74
97
|
if (error instanceof core_1.ConcurrencyError && attempts > 0) {
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
98
|
+
const pristines = await Promise.all(toSave.map(async ({ aggregate, changes }) => {
|
|
99
|
+
const pristine = await this.load(aggregate.id);
|
|
100
|
+
if (!pristine) {
|
|
101
|
+
throw new Error("Invalid concurrency error, aggregate not found");
|
|
102
|
+
}
|
|
103
|
+
for (const change of changes) {
|
|
104
|
+
pristine.apply(change);
|
|
105
|
+
}
|
|
106
|
+
return pristine;
|
|
107
|
+
}));
|
|
108
|
+
return await this.saveAll(pristines, parentTrx, attempts - 1);
|
|
83
109
|
}
|
|
84
110
|
throw error;
|
|
85
111
|
}
|
|
112
|
+
if (parentTrx) {
|
|
113
|
+
parentTrx.onCommit(async () => {
|
|
114
|
+
if (this._publishEventsTo) {
|
|
115
|
+
for (const { changes } of toSave) {
|
|
116
|
+
for (const event of changes) {
|
|
117
|
+
await this._publishEventsTo.publish(event);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async save(aggregate, trx, attempts = 10) {
|
|
125
|
+
return this.saveAll([aggregate], trx, attempts);
|
|
86
126
|
}
|
|
87
127
|
}
|
|
88
128
|
exports.NestedFirestoreEsAggregateStore = NestedFirestoreEsAggregateStore;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.es-aggregate-store.js","sourceRoot":"","sources":["../../src/nested/firestore.es-aggregate-store.ts"],"names":[],"mappings":";;;AACA,uCAQsB;
|
|
1
|
+
{"version":3,"file":"firestore.es-aggregate-store.js","sourceRoot":"","sources":["../../src/nested/firestore.es-aggregate-store.ts"],"names":[],"mappings":";;;AACA,uCAQsB;AASf,MAAM,mCAAmC,GAAG,CAGjD,SAAY,EACZ,EAAE;IACF,OAAO,MAAM,gCAAiC,SAAQ,+BAAkC;QACtF,SAAS,CAAC,KAAyC;YACjD,OAAO,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACpC,CAAC;QAED,oBAAoB,CAAC,EAAyB;YAC5C,OAAO,IAAI,wBAAiB,CAAC;gBAC3B,SAAS,EAAE,SAAS,CAAC,IAAI;gBACzB,EAAE,EAAE,EAAE,CAAC,QAAQ,EAAE;aAClB,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAjBW,QAAA,mCAAmC,uCAiB9C;AAEF,MAAsB,+BAA+B;IAKjC;IACA;IACA;IACA;IAJlB,YACkB,UAAqC,EACrC,WAA0C,EAC1C,UAA2D,EAC3D,WAAyD;QAHzD,eAAU,GAAV,UAAU,CAA2B;QACrC,gBAAW,GAAX,WAAW,CAA+B;QAC1C,eAAU,GAAV,UAAU,CAAiD;QAC3D,gBAAW,GAAX,WAAW,CAA8C;IACxE,CAAC;IAOJ,gBAAgB,CAAa;IAC7B,eAAe,CAAC,QAAmB;QACjC,IAA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AED,OAAO,QAAQ,CAAC;gBAClB,CAAC,CAAC,CACH,CAAC;gBAEF,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,SAAS,EAAE,QAAQ,GAAG,CAAC,CAAC,CAAC;YAChE,CAAC;YAED,MAAM,KAAK,CAAC;QACd,CAAC;QAED,IAAI,SAAS,EAAE,CAAC;YACd,SAAS,CAAC,QAAQ,CAAC,KAAK,IAAI,EAAE;gBAC5B,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;oBAC1B,KAAK,MAAM,EAAE,OAAO,EAAE,IAAI,MAAM,EAAE,CAAC;wBACjC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;4BAC5B,MAAM,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;wBAC7C,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI,CACR,SAA0B,EAC1B,GAA0B,EAC1B,QAAQ,GAAG,EAAE;QAEb,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC;CACF;AA7JD,0EA6JC"}
|
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import { AggregateStreamId, IChange, IFact } from "@ddd-ts/core";
|
|
2
|
-
import
|
|
2
|
+
import { FirestoreTransaction } from "@ddd-ts/store-firestore";
|
|
3
3
|
import * as fb from "firebase-admin";
|
|
4
4
|
export declare class NestedFirestoreEventStore {
|
|
5
5
|
readonly firestore: fb.firestore.Firestore;
|
|
6
|
-
|
|
6
|
+
readonly converter: fb.firestore.FirestoreDataConverter<fb.firestore.DocumentData>;
|
|
7
|
+
constructor(firestore: fb.firestore.Firestore, converter?: fb.firestore.FirestoreDataConverter<fb.firestore.DocumentData>);
|
|
7
8
|
get collection(): fb.firestore.CollectionReference<fb.firestore.DocumentData>;
|
|
9
|
+
getStream(streamId: AggregateStreamId): fb.firestore.CollectionReference<fb.firestore.DocumentData>;
|
|
8
10
|
close(): Promise<void>;
|
|
11
|
+
bulkAppend(toAppend: {
|
|
12
|
+
streamId: AggregateStreamId;
|
|
13
|
+
changes: IChange[];
|
|
14
|
+
expectedRevision: number;
|
|
15
|
+
}[], trx: FirestoreTransaction): Promise<void>;
|
|
16
|
+
private lockDocument;
|
|
17
|
+
private commitChanges;
|
|
9
18
|
append(streamId: AggregateStreamId, changes: IChange[], expectedRevision: number, trx: FirestoreTransaction): Promise<void>;
|
|
10
19
|
read(streamId: AggregateStreamId, from?: number): AsyncIterable<IFact>;
|
|
11
20
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.event-store.d.ts","sourceRoot":"","sources":["../../src/nested/firestore.event-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAEjB,OAAO,EACP,KAAK,EACN,MAAM,cAAc,CAAC;AACtB,OAAO,
|
|
1
|
+
{"version":3,"file":"firestore.event-store.d.ts","sourceRoot":"","sources":["../../src/nested/firestore.event-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAEjB,OAAO,EACP,KAAK,EACN,MAAM,cAAc,CAAC;AACtB,OAAO,EAEL,oBAAoB,EACrB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAErC,qBAAa,yBAAyB;aAElB,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,SAAS;aACjC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,sBAAsB,CAAC,EAAE,CAAC,SAAS,CAAC,YAAY,CAAC;gBADzE,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,SAAS,EACjC,SAAS,GAAE,EAAE,CAAC,SAAS,CAAC,sBAAsB,CAAC,EAAE,CAAC,SAAS,CAAC,YAAY,CAA0B;IAGpH,IAAI,UAAU,gEAEb;IAED,SAAS,CAAC,QAAQ,EAAE,iBAAiB;IAQ/B,KAAK;IAEL,UAAU,CACd,QAAQ,EAAE;QACR,QAAQ,EAAE,iBAAiB,CAAC;QAC5B,OAAO,EAAE,OAAO,EAAE,CAAC;QACnB,gBAAgB,EAAE,MAAM,CAAC;KAC1B,EAAE,EACH,GAAG,EAAE,oBAAoB;YAgBb,YAAY;YA2BZ,aAAa;IA0BrB,MAAM,CACV,QAAQ,EAAE,iBAAiB,EAC3B,OAAO,EAAE,OAAO,EAAE,EAClB,gBAAgB,EAAE,MAAM,EACxB,GAAG,EAAE,oBAAoB,GACxB,OAAO,CAAC,IAAI,CAAC;IAKT,IAAI,CACT,QAAQ,EAAE,iBAAiB,EAC3B,IAAI,CAAC,EAAE,MAAM,GACZ,aAAa,CAAC,KAAK,CAAC;CAmBxB"}
|
|
@@ -25,35 +25,52 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.NestedFirestoreEventStore = void 0;
|
|
27
27
|
const core_1 = require("@ddd-ts/core");
|
|
28
|
+
const store_firestore_1 = require("@ddd-ts/store-firestore");
|
|
28
29
|
const fb = __importStar(require("firebase-admin"));
|
|
29
30
|
class NestedFirestoreEventStore {
|
|
30
31
|
firestore;
|
|
31
|
-
|
|
32
|
+
converter;
|
|
33
|
+
constructor(firestore, converter = new store_firestore_1.DefaultConverter()) {
|
|
32
34
|
this.firestore = firestore;
|
|
35
|
+
this.converter = converter;
|
|
33
36
|
}
|
|
34
37
|
get collection() {
|
|
35
38
|
return this.firestore.collection("event-store");
|
|
36
39
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const collection = this.collection
|
|
40
|
+
getStream(streamId) {
|
|
41
|
+
return this.collection
|
|
40
42
|
.doc(streamId.aggregate)
|
|
41
43
|
.collection("streams")
|
|
42
44
|
.doc(streamId.id)
|
|
43
45
|
.collection("events");
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
}
|
|
47
|
+
async close() { }
|
|
48
|
+
async bulkAppend(toAppend, trx) {
|
|
49
|
+
await Promise.all(toAppend.map(async ({ streamId, expectedRevision }) => await this.lockDocument(streamId, expectedRevision, trx)));
|
|
50
|
+
await Promise.all(toAppend.map(async ({ streamId, changes, expectedRevision }) => {
|
|
51
|
+
await this.commitChanges(streamId, changes, expectedRevision, trx);
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
async lockDocument(streamId, expectedRevision, trx) {
|
|
55
|
+
const collection = this.getStream(streamId);
|
|
56
|
+
const prevDocument = collection.doc(`${expectedRevision}`);
|
|
57
|
+
const targetDocument = collection.doc(`${expectedRevision + 1}`);
|
|
58
|
+
const target = await trx.transaction.get(targetDocument);
|
|
59
|
+
if (target.exists) {
|
|
46
60
|
throw new core_1.ConcurrencyError(`Concurrency error on ${streamId.aggregate} ${streamId.id}`);
|
|
47
61
|
}
|
|
48
62
|
if (expectedRevision > -1) {
|
|
49
|
-
const
|
|
50
|
-
if (!
|
|
63
|
+
const previous = await trx.transaction.get(prevDocument);
|
|
64
|
+
if (!previous.exists) {
|
|
51
65
|
throw new Error(`Expected revision ${expectedRevision} not found for ${streamId.aggregate} ${streamId.id}`);
|
|
52
66
|
}
|
|
53
67
|
}
|
|
68
|
+
}
|
|
69
|
+
async commitChanges(streamId, changes, expectedRevision, trx) {
|
|
70
|
+
const collection = this.getStream(streamId);
|
|
54
71
|
let revision = expectedRevision + 1;
|
|
55
72
|
for (const change of changes) {
|
|
56
|
-
trx.transaction.create(collection.doc(`${revision}`), {
|
|
73
|
+
trx.transaction.create(collection.doc(`${revision}`), this.converter.toFirestore({
|
|
57
74
|
aggregateType: streamId.aggregate,
|
|
58
75
|
eventId: change.id.toString(),
|
|
59
76
|
aggregateId: streamId.id.toString(),
|
|
@@ -61,28 +78,28 @@ class NestedFirestoreEventStore {
|
|
|
61
78
|
name: change.name,
|
|
62
79
|
payload: change.payload,
|
|
63
80
|
occurredAt: fb.firestore.FieldValue.serverTimestamp(),
|
|
64
|
-
});
|
|
81
|
+
}));
|
|
65
82
|
revision++;
|
|
66
83
|
}
|
|
67
84
|
}
|
|
85
|
+
async append(streamId, changes, expectedRevision, trx) {
|
|
86
|
+
await this.lockDocument(streamId, expectedRevision, trx);
|
|
87
|
+
await this.commitChanges(streamId, changes, expectedRevision, trx);
|
|
88
|
+
}
|
|
68
89
|
async *read(streamId, from) {
|
|
69
|
-
const collection = this.
|
|
70
|
-
.doc(streamId.aggregate)
|
|
71
|
-
.collection("streams")
|
|
72
|
-
.doc(streamId.id)
|
|
73
|
-
.collection("events");
|
|
90
|
+
const collection = this.getStream(streamId);
|
|
74
91
|
const query = collection
|
|
75
92
|
.where("revision", ">=", from || 0)
|
|
76
93
|
.orderBy("revision", "asc");
|
|
77
94
|
for await (const event of query.stream()) {
|
|
78
95
|
const e = event;
|
|
79
|
-
const data =
|
|
96
|
+
const data = this.converter.fromFirestore(e);
|
|
80
97
|
yield {
|
|
81
98
|
id: data.eventId,
|
|
82
99
|
revision: data.revision,
|
|
83
100
|
name: data.name,
|
|
84
101
|
payload: data.payload,
|
|
85
|
-
occurredAt: data.occurredAt
|
|
102
|
+
occurredAt: data.occurredAt,
|
|
86
103
|
};
|
|
87
104
|
}
|
|
88
105
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.event-store.js","sourceRoot":"","sources":["../../src/nested/firestore.event-store.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAKsB;
|
|
1
|
+
{"version":3,"file":"firestore.event-store.js","sourceRoot":"","sources":["../../src/nested/firestore.event-store.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAKsB;AACtB,6DAGiC;AACjC,mDAAqC;AAErC,MAAa,yBAAyB;IAElB;IACA;IAFlB,YACkB,SAAiC,EACjC,YAA4E,IAAI,kCAAgB,EAAE;QADlG,cAAS,GAAT,SAAS,CAAwB;QACjC,cAAS,GAAT,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
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
18
24
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
25
|
if (mod && mod.__esModule) return mod;
|
|
20
26
|
var result = {};
|
|
@@ -25,11 +31,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
32
|
process.env.FIRESTORE_EMULATOR_HOST = "localhost:8080";
|
|
27
33
|
const fb = __importStar(require("firebase-admin"));
|
|
34
|
+
const core_1 = require("@ddd-ts/core");
|
|
28
35
|
const store_firestore_1 = require("@ddd-ts/store-firestore");
|
|
29
36
|
const tests_1 = require("@ddd-ts/tests");
|
|
30
37
|
const firestore_event_store_1 = require("./firestore.event-store");
|
|
31
38
|
const firestore_es_aggregate_store_1 = require("./firestore.es-aggregate-store");
|
|
32
39
|
const firestore_snapshotter_1 = require("./firestore.snapshotter");
|
|
40
|
+
const dist_1 = require("../../../shape/dist");
|
|
33
41
|
jest.setTimeout(10000);
|
|
34
42
|
describe("NestedFirestoreEventStore", () => {
|
|
35
43
|
const app = fb.initializeApp({
|
|
@@ -39,10 +47,207 @@ describe("NestedFirestoreEventStore", () => {
|
|
|
39
47
|
const transaction = new store_firestore_1.FirestoreTransactionPerformer(firestore);
|
|
40
48
|
const eventStore = new firestore_event_store_1.NestedFirestoreEventStore(firestore);
|
|
41
49
|
function makeAggregateStore(AGGREGATE, eventSerializer, serializer) {
|
|
42
|
-
const snapshotter = new firestore_snapshotter_1.NestedFirestoreSnapshotter(firestore, serializer);
|
|
50
|
+
const snapshotter = new firestore_snapshotter_1.NestedFirestoreSnapshotter(AGGREGATE.name, firestore, serializer);
|
|
43
51
|
const Store = (0, firestore_es_aggregate_store_1.MakeNestedFirestoreEsAggregateStore)(AGGREGATE);
|
|
44
52
|
return new Store(eventStore, transaction, eventSerializer, snapshotter);
|
|
45
53
|
}
|
|
46
54
|
(0, tests_1.EsAggregateStoreSuite)(makeAggregateStore);
|
|
55
|
+
it("should support saveAll with transactions", async () => {
|
|
56
|
+
class MockEventBus {
|
|
57
|
+
off() {
|
|
58
|
+
throw new Error("Method not implemented.");
|
|
59
|
+
}
|
|
60
|
+
on() {
|
|
61
|
+
throw new Error("Method not implemented.");
|
|
62
|
+
}
|
|
63
|
+
events = [];
|
|
64
|
+
publish(event) {
|
|
65
|
+
this.events.push(event);
|
|
66
|
+
return Promise.resolve();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
class AccountOpened extends (0, core_1.EsEvent)("AccountOpened", {
|
|
70
|
+
id: String,
|
|
71
|
+
index: Number,
|
|
72
|
+
registryId: String,
|
|
73
|
+
}) {
|
|
74
|
+
}
|
|
75
|
+
class Account extends (0, core_1.EsAggregate)("Account", {
|
|
76
|
+
events: [AccountOpened],
|
|
77
|
+
state: {
|
|
78
|
+
id: String,
|
|
79
|
+
index: Number,
|
|
80
|
+
balance: Number,
|
|
81
|
+
registryId: String,
|
|
82
|
+
},
|
|
83
|
+
}) {
|
|
84
|
+
static onAccountOpened(event) {
|
|
85
|
+
return new Account({
|
|
86
|
+
id: event.payload.id,
|
|
87
|
+
index: event.payload.index,
|
|
88
|
+
balance: 0,
|
|
89
|
+
registryId: event.payload.registryId,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
static open(registryId, index) {
|
|
93
|
+
return this.new(AccountOpened.new({
|
|
94
|
+
id: Math.random().toString(36).slice(2),
|
|
95
|
+
index,
|
|
96
|
+
registryId,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
__decorate([
|
|
101
|
+
(0, core_1.On)(AccountOpened)
|
|
102
|
+
], Account, "onAccountOpened", null);
|
|
103
|
+
class AccountRegistry extends (0, dist_1.Shape)({
|
|
104
|
+
id: String,
|
|
105
|
+
index: Number,
|
|
106
|
+
}) {
|
|
107
|
+
increment(shouldFailInMiddle = false) {
|
|
108
|
+
this.index++;
|
|
109
|
+
if (this.index > 10 && shouldFailInMiddle) {
|
|
110
|
+
throw new Error("Too many accounts");
|
|
111
|
+
}
|
|
112
|
+
return this.index;
|
|
113
|
+
}
|
|
114
|
+
static new() {
|
|
115
|
+
return new AccountRegistry({
|
|
116
|
+
id: Math.random().toString().slice(2),
|
|
117
|
+
index: 0,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const registryStore = new store_firestore_1.FirestoreStore("account-registry", firestore, new ((0, core_1.AutoSerializer)(AccountRegistry, 1))());
|
|
122
|
+
const accountStore = new ((0, firestore_es_aggregate_store_1.MakeNestedFirestoreEsAggregateStore)(Account))(eventStore, transaction, new core_1.SerializerRegistry().add(AccountOpened, new ((0, core_1.AutoSerializer)(AccountOpened, 1))()), new firestore_snapshotter_1.NestedFirestoreSnapshotter(Account.name, firestore, new ((0, core_1.AutoSerializer)(Account, 1))()));
|
|
123
|
+
const eventBus = new MockEventBus();
|
|
124
|
+
accountStore.publishEventsTo(eventBus);
|
|
125
|
+
const registry = AccountRegistry.new();
|
|
126
|
+
await registryStore.save(registry);
|
|
127
|
+
let willFailForAttempt = 3;
|
|
128
|
+
await transaction.perform(async (trx) => {
|
|
129
|
+
willFailForAttempt--;
|
|
130
|
+
const reg = await registryStore.load(registry.id, trx);
|
|
131
|
+
const accounts = [...Array(30).keys()].map((i) => Account.open(registry.id, reg.increment(willFailForAttempt === 0)));
|
|
132
|
+
await accountStore.saveAll(accounts, trx);
|
|
133
|
+
await registryStore.save(reg, trx);
|
|
134
|
+
});
|
|
135
|
+
const freshRegistry = await registryStore.load(registry.id);
|
|
136
|
+
expect(freshRegistry.index).toBe(30);
|
|
137
|
+
const result = await accountStore.snapshotter?.collection
|
|
138
|
+
.where("registryId", "==", registry.id)
|
|
139
|
+
.get();
|
|
140
|
+
const documents = result?.docs.map((doc) => doc.data());
|
|
141
|
+
expect(documents?.length).toBe(30);
|
|
142
|
+
const indices = documents
|
|
143
|
+
?.map((doc) => Number(doc.index))
|
|
144
|
+
.sort((a, b) => a - b);
|
|
145
|
+
expect(indices).toEqual([
|
|
146
|
+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
|
|
147
|
+
22, 23, 24, 25, 26, 27, 28, 29, 30,
|
|
148
|
+
]);
|
|
149
|
+
expect(eventBus.events.length).toBe(30);
|
|
150
|
+
});
|
|
151
|
+
it("should support saveAll with transactions", async () => {
|
|
152
|
+
class MockEventBus {
|
|
153
|
+
off() {
|
|
154
|
+
throw new Error("Method not implemented.");
|
|
155
|
+
}
|
|
156
|
+
on() {
|
|
157
|
+
throw new Error("Method not implemented.");
|
|
158
|
+
}
|
|
159
|
+
events = [];
|
|
160
|
+
publish(event) {
|
|
161
|
+
this.events.push(event);
|
|
162
|
+
return Promise.resolve();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
class AccountOpened extends (0, core_1.EsEvent)("AccountOpened", {
|
|
166
|
+
id: String,
|
|
167
|
+
index: Number,
|
|
168
|
+
registryId: String,
|
|
169
|
+
}) {
|
|
170
|
+
}
|
|
171
|
+
class Account extends (0, core_1.EsAggregate)("Account", {
|
|
172
|
+
events: [AccountOpened],
|
|
173
|
+
state: {
|
|
174
|
+
id: String,
|
|
175
|
+
index: Number,
|
|
176
|
+
balance: Number,
|
|
177
|
+
registryId: String,
|
|
178
|
+
},
|
|
179
|
+
}) {
|
|
180
|
+
static onAccountOpened(event) {
|
|
181
|
+
return new Account({
|
|
182
|
+
id: event.payload.id,
|
|
183
|
+
index: event.payload.index,
|
|
184
|
+
balance: 0,
|
|
185
|
+
registryId: event.payload.registryId,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
static open(registryId, index) {
|
|
189
|
+
return this.new(AccountOpened.new({
|
|
190
|
+
id: Math.random().toString(36).slice(2),
|
|
191
|
+
index,
|
|
192
|
+
registryId,
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
__decorate([
|
|
197
|
+
(0, core_1.On)(AccountOpened)
|
|
198
|
+
], Account, "onAccountOpened", null);
|
|
199
|
+
class AccountRegistry extends (0, dist_1.Shape)({
|
|
200
|
+
id: String,
|
|
201
|
+
index: Number,
|
|
202
|
+
}) {
|
|
203
|
+
increment(shouldFailInMiddle = false) {
|
|
204
|
+
this.index++;
|
|
205
|
+
if (this.index > 10 && shouldFailInMiddle) {
|
|
206
|
+
throw new Error("Too many accounts");
|
|
207
|
+
}
|
|
208
|
+
return this.index;
|
|
209
|
+
}
|
|
210
|
+
static new() {
|
|
211
|
+
return new AccountRegistry({
|
|
212
|
+
id: Math.random().toString().slice(2),
|
|
213
|
+
index: 0,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const registryStore = new store_firestore_1.FirestoreStore("account-registry", firestore, new ((0, core_1.AutoSerializer)(AccountRegistry, 1))());
|
|
218
|
+
const accountStore = new ((0, firestore_es_aggregate_store_1.MakeNestedFirestoreEsAggregateStore)(Account))(eventStore, transaction, new core_1.SerializerRegistry().add(AccountOpened, new ((0, core_1.AutoSerializer)(AccountOpened, 1))()), new firestore_snapshotter_1.NestedFirestoreSnapshotter(Account.name, firestore, new ((0, core_1.AutoSerializer)(Account, 1))()));
|
|
219
|
+
const eventBus = new MockEventBus();
|
|
220
|
+
accountStore.publishEventsTo(eventBus);
|
|
221
|
+
const registry = AccountRegistry.new();
|
|
222
|
+
await registryStore.save(registry);
|
|
223
|
+
await Promise.all([
|
|
224
|
+
transaction.perform(async (trx) => {
|
|
225
|
+
const reg = await registryStore.load(registry.id, trx);
|
|
226
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
227
|
+
const account = Account.open(registry.id, reg.increment());
|
|
228
|
+
await accountStore.save(account, trx);
|
|
229
|
+
await registryStore.save(reg, trx);
|
|
230
|
+
}),
|
|
231
|
+
transaction.perform(async (trx) => {
|
|
232
|
+
await new Promise((resolve) => setTimeout(resolve, 40));
|
|
233
|
+
const reg = await registryStore.load(registry.id, trx);
|
|
234
|
+
const account = Account.open(registry.id, reg.increment());
|
|
235
|
+
await accountStore.save(account, trx);
|
|
236
|
+
await registryStore.save(reg, trx);
|
|
237
|
+
}),
|
|
238
|
+
]);
|
|
239
|
+
const freshRegistry = await registryStore.load(registry.id);
|
|
240
|
+
expect(freshRegistry.index).toBe(2);
|
|
241
|
+
const result = await accountStore.snapshotter?.collection
|
|
242
|
+
.where("registryId", "==", registry.id)
|
|
243
|
+
.get();
|
|
244
|
+
const documents = result?.docs.map((doc) => doc.data());
|
|
245
|
+
expect(documents?.length).toBe(2);
|
|
246
|
+
const indices = documents
|
|
247
|
+
?.map((doc) => Number(doc.index))
|
|
248
|
+
.sort((a, b) => a - b);
|
|
249
|
+
expect(indices).toEqual([1, 2]);
|
|
250
|
+
expect(eventBus.events.length).toBe(2);
|
|
251
|
+
});
|
|
47
252
|
});
|
|
48
253
|
//# sourceMappingURL=firestore.event-store.spec.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.event-store.spec.js","sourceRoot":"","sources":["../../src/nested/firestore.event-store.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"firestore.event-store.spec.js","sourceRoot":"","sources":["../../src/nested/firestore.event-store.spec.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,CAAC,GAAG,CAAC,uBAAuB,GAAG,gBAAgB,CAAC;AAEvD,mDAAqC;A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C,CAAC;aACrC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAEzB,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QAEhC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import
|
|
3
|
-
export declare class NestedFirestoreSnapshotter<A extends IEventSourced & IIdentifiable> {
|
|
4
|
-
private readonly
|
|
5
|
-
|
|
6
|
-
constructor(db: FirebaseFirestore.Firestore, serializer: ISerializer<A>);
|
|
7
|
-
load(streamId: AggregateStreamId): Promise<A | undefined>;
|
|
8
|
-
save(aggregate: A, trx: FirestoreTransaction): Promise<void>;
|
|
1
|
+
import { type IEventSourced, type IIdentifiable, type ISerializer } from "@ddd-ts/core";
|
|
2
|
+
import { FirestoreStore } from "@ddd-ts/store-firestore";
|
|
3
|
+
export declare class NestedFirestoreSnapshotter<A extends IEventSourced & IIdentifiable> extends FirestoreStore<A> {
|
|
4
|
+
private readonly aggregate;
|
|
5
|
+
constructor(aggregate: string, db: FirebaseFirestore.Firestore, serializer: ISerializer<A>, converter?: FirebaseFirestore.FirestoreDataConverter<FirebaseFirestore.DocumentData>);
|
|
9
6
|
}
|
|
10
7
|
//# sourceMappingURL=firestore.snapshotter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.snapshotter.d.ts","sourceRoot":"","sources":["../../src/nested/firestore.snapshotter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,
|
|
1
|
+
{"version":3,"file":"firestore.snapshotter.d.ts","sourceRoot":"","sources":["../../src/nested/firestore.snapshotter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,WAAW,EACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD,qBAAa,0BAA0B,CACrC,CAAC,SAAS,aAAa,GAAG,aAAa,CACvC,SAAQ,cAAc,CAAC,CAAC,CAAC;IAEvB,OAAO,CAAC,QAAQ,CAAC,SAAS;gBAAT,SAAS,EAAE,MAAM,EAClC,EAAE,EAAE,iBAAiB,CAAC,SAAS,EAC/B,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC,EAC1B,SAAS,CAAC,EAAE,iBAAiB,CAAC,sBAAsB,CAAC,iBAAiB,CAAC,YAAY,CAAC;CA2BvF"}
|
|
@@ -1,41 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NestedFirestoreSnapshotter = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
constructor(db, serializer) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
const instance = await this.serializer.deserialize(snapshot.content);
|
|
26
|
-
instance.acknowledgedRevision = Number(snapshot.revision);
|
|
27
|
-
return instance;
|
|
28
|
-
}
|
|
29
|
-
async save(aggregate, trx) {
|
|
30
|
-
const streamId = aggregate.getAggregateStreamId();
|
|
31
|
-
trx.transaction.set(this.db
|
|
4
|
+
const store_firestore_1 = require("@ddd-ts/store-firestore");
|
|
5
|
+
class NestedFirestoreSnapshotter extends store_firestore_1.FirestoreStore {
|
|
6
|
+
aggregate;
|
|
7
|
+
constructor(aggregate, db, serializer, converter) {
|
|
8
|
+
super("irrelevant", db, {
|
|
9
|
+
deserialize: async (serialized) => {
|
|
10
|
+
const { revision, ...content } = serialized;
|
|
11
|
+
const instance = await serializer.deserialize(content);
|
|
12
|
+
instance.acknowledgedRevision = Number(revision);
|
|
13
|
+
return instance;
|
|
14
|
+
},
|
|
15
|
+
serialize: async (instance) => {
|
|
16
|
+
return {
|
|
17
|
+
revision: instance.acknowledgedRevision,
|
|
18
|
+
...(await serializer.serialize(instance)),
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
}, converter);
|
|
22
|
+
this.aggregate = aggregate;
|
|
23
|
+
this.collection = db
|
|
32
24
|
.collection("event-store")
|
|
33
|
-
.doc(
|
|
25
|
+
.doc(this.aggregate)
|
|
34
26
|
.collection("streams")
|
|
35
|
-
.
|
|
36
|
-
revision: aggregate.acknowledgedRevision,
|
|
37
|
-
content: await this.serializer.serialize(aggregate),
|
|
38
|
-
});
|
|
27
|
+
.withConverter(this.converter);
|
|
39
28
|
}
|
|
40
29
|
}
|
|
41
30
|
exports.NestedFirestoreSnapshotter = NestedFirestoreSnapshotter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.snapshotter.js","sourceRoot":"","sources":["../../src/nested/firestore.snapshotter.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"firestore.snapshotter.js","sourceRoot":"","sources":["../../src/nested/firestore.snapshotter.ts"],"names":[],"mappings":";;;AAKA,6DAAyD;AAEzD,MAAa,0BAEX,SAAQ,gCAAiB;IAEN;IADnB,YACmB,SAAiB,EAClC,EAA+B,EAC/B,UAA0B,EAC1B,SAAoF;QAEpF,KAAK,CACH,YAAY,EACZ,EAAE,EACF;YACE,WAAW,EAAE,KAAK,EAAE,UAAe,EAAE,EAAE;gBACrC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE,GAAG,UAAU,CAAC;gBAC5C,MAAM,QAAQ,GAAG,MAAM,UAAU,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;gBACvD,QAAQ,CAAC,oBAAoB,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;gBACjD,OAAO,QAAQ,CAAC;YAClB,CAAC;YACD,SAAS,EAAE,KAAK,EAAE,QAAW,EAAE,EAAE;gBAC/B,OAAO;oBACL,QAAQ,EAAE,QAAQ,CAAC,oBAAoB;oBACvC,GAAG,CAAC,MAAM,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;iBAC1C,CAAC;YACJ,CAAC;SACF,EACD,SAAS,CACV,CAAC;QAvBe,cAAS,GAAT,SAAS,CAAQ;QAwBlC,IAAI,CAAC,UAAU,GAAG,EAAE;aACjB,UAAU,CAAC,aAAa,CAAC;aACzB,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC;aACnB,UAAU,CAAC,SAAS,CAAC;aACrB,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;CACF;AAlCD,gEAkCC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ddd-ts/event-sourcing-firestore",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,16 +12,16 @@
|
|
|
12
12
|
"build": "tsc"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@ddd-ts/core": "0.0.
|
|
16
|
-
"@ddd-ts/traits": "0.0.
|
|
17
|
-
"@ddd-ts/types": "0.0.
|
|
15
|
+
"@ddd-ts/core": "0.0.14",
|
|
16
|
+
"@ddd-ts/traits": "0.0.14",
|
|
17
|
+
"@ddd-ts/types": "0.0.14",
|
|
18
18
|
"firebase-admin": "^11.5.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@ddd-ts/store-firestore": "0.0.
|
|
22
|
-
"@ddd-ts/tests": "0.0.
|
|
23
|
-
"@ddd-ts/tools": "0.0.
|
|
24
|
-
"@ddd-ts/types": "0.0.
|
|
21
|
+
"@ddd-ts/store-firestore": "0.0.14",
|
|
22
|
+
"@ddd-ts/tests": "0.0.14",
|
|
23
|
+
"@ddd-ts/tools": "0.0.14",
|
|
24
|
+
"@ddd-ts/types": "0.0.14",
|
|
25
25
|
"@types/jest": "^29.5.1"
|
|
26
26
|
}
|
|
27
27
|
}
|