@event-driven-io/emmett-esdb 0.1.7 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/eventStore/eventstoreDBEventStore.d.mts +6 -0
- package/dist/eventStore/eventstoreDBEventStore.d.ts +6 -0
- package/dist/eventStore/eventstoreDBEventStore.e2e.spec.d.mts +2 -0
- package/dist/eventStore/eventstoreDBEventStore.e2e.spec.d.ts +2 -0
- package/dist/eventStore/eventstoreDBEventStore.e2e.spec.js +95 -0
- package/dist/eventStore/eventstoreDBEventStore.e2e.spec.js.map +1 -0
- package/dist/eventStore/eventstoreDBEventStore.e2e.spec.mjs +93 -0
- package/dist/eventStore/eventstoreDBEventStore.e2e.spec.mjs.map +1 -0
- package/dist/eventStore/eventstoreDBEventStore.js +124 -0
- package/dist/eventStore/eventstoreDBEventStore.js.map +1 -0
- package/dist/eventStore/eventstoreDBEventStore.mjs +149 -0
- package/dist/eventStore/eventstoreDBEventStore.mjs.map +1 -0
- package/dist/eventStore/index.d.mts +3 -0
- package/dist/eventStore/index.d.ts +3 -0
- package/dist/eventStore/index.js +2 -0
- package/dist/eventStore/index.js.map +1 -0
- package/dist/eventStore/index.mjs +2 -0
- package/dist/eventStore/index.mjs.map +1 -0
- package/dist/index.d.mts +3 -5
- package/dist/index.d.ts +3 -5
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -10
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
2
|
+
import {} from '@event-driven-io/emmett';
|
|
3
|
+
import { EventStoreDBContainer, StartedEventStoreDBContainer, } from '@event-driven-io/emmett-testcontainers';
|
|
4
|
+
import assert from 'node:assert';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
import { after, before, describe, it } from 'node:test';
|
|
7
|
+
import { getEventStoreDBEventStore } from '.';
|
|
8
|
+
const evolve = (state, { type, data }) => {
|
|
9
|
+
switch (type) {
|
|
10
|
+
case 'ProductItemAdded': {
|
|
11
|
+
const productItem = data.productItem;
|
|
12
|
+
return {
|
|
13
|
+
productItems: [...state.productItems, productItem],
|
|
14
|
+
totalAmount: state.totalAmount + productItem.price * productItem.quantity,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
case 'DiscountApplied':
|
|
18
|
+
return {
|
|
19
|
+
...state,
|
|
20
|
+
totalAmount: state.totalAmount * (1 - data.percent),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const getInitialState = () => {
|
|
25
|
+
return { productItems: [], totalAmount: 0 };
|
|
26
|
+
};
|
|
27
|
+
describe('EventStoreDBEventStore', () => {
|
|
28
|
+
let esdbContainer;
|
|
29
|
+
let eventStore;
|
|
30
|
+
before(async () => {
|
|
31
|
+
esdbContainer = await new EventStoreDBContainer().start();
|
|
32
|
+
eventStore = getEventStoreDBEventStore(esdbContainer.getClient());
|
|
33
|
+
});
|
|
34
|
+
after(() => {
|
|
35
|
+
return esdbContainer.stop();
|
|
36
|
+
});
|
|
37
|
+
describe('aggregateStream', () => {
|
|
38
|
+
it('When called with `to` allows time travelling', async () => {
|
|
39
|
+
// Given
|
|
40
|
+
const productItem = {
|
|
41
|
+
productId: '123',
|
|
42
|
+
quantity: 10,
|
|
43
|
+
price: 3,
|
|
44
|
+
};
|
|
45
|
+
const discount = 10;
|
|
46
|
+
const shoppingCartId = randomUUID();
|
|
47
|
+
await eventStore.appendToStream(shoppingCartId, [
|
|
48
|
+
{ type: 'ProductItemAdded', data: { productItem } },
|
|
49
|
+
]);
|
|
50
|
+
await eventStore.appendToStream(shoppingCartId, [
|
|
51
|
+
{ type: 'ProductItemAdded', data: { productItem } },
|
|
52
|
+
]);
|
|
53
|
+
await eventStore.appendToStream(shoppingCartId, [
|
|
54
|
+
{ type: 'DiscountApplied', data: { percent: discount } },
|
|
55
|
+
]);
|
|
56
|
+
// when
|
|
57
|
+
const resultAt1 = await eventStore.aggregateStream(shoppingCartId, {
|
|
58
|
+
evolve,
|
|
59
|
+
getInitialState,
|
|
60
|
+
read: { to: 1n },
|
|
61
|
+
});
|
|
62
|
+
const resultAt2 = await eventStore.aggregateStream(shoppingCartId, {
|
|
63
|
+
evolve,
|
|
64
|
+
getInitialState,
|
|
65
|
+
read: { to: 2n },
|
|
66
|
+
});
|
|
67
|
+
const resultAt3 = await eventStore.aggregateStream(shoppingCartId, {
|
|
68
|
+
evolve,
|
|
69
|
+
getInitialState,
|
|
70
|
+
read: { to: 3n },
|
|
71
|
+
});
|
|
72
|
+
// then
|
|
73
|
+
assert.ok(resultAt1);
|
|
74
|
+
assert.ok(resultAt2);
|
|
75
|
+
assert.ok(resultAt3);
|
|
76
|
+
// Note that ESDB counts from 0
|
|
77
|
+
assert.equal(resultAt1.currentStreamVersion, 0);
|
|
78
|
+
assert.deepEqual(resultAt1.state, {
|
|
79
|
+
productItems: [productItem],
|
|
80
|
+
totalAmount: productItem.price * productItem.quantity,
|
|
81
|
+
});
|
|
82
|
+
assert.equal(resultAt2.currentStreamVersion, 1);
|
|
83
|
+
assert.deepEqual(resultAt2.state, {
|
|
84
|
+
productItems: [productItem, productItem],
|
|
85
|
+
totalAmount: productItem.price * productItem.quantity * 2,
|
|
86
|
+
});
|
|
87
|
+
assert.equal(resultAt3.currentStreamVersion, 2);
|
|
88
|
+
assert.deepEqual(resultAt3.state, {
|
|
89
|
+
productItems: [productItem, productItem],
|
|
90
|
+
totalAmount: productItem.price * productItem.quantity * 2 * (1 - discount),
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
//# sourceMappingURL=eventstoreDBEventStore.e2e.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eventstoreDBEventStore.e2e.spec.js","sourceRoot":"","sources":["../../src/eventStore/eventstoreDBEventStore.e2e.spec.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,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {} from "@event-driven-io/emmett";
|
|
2
|
+
import {
|
|
3
|
+
EventStoreDBContainer,
|
|
4
|
+
StartedEventStoreDBContainer
|
|
5
|
+
} from "@event-driven-io/emmett-testcontainers";
|
|
6
|
+
import assert from "node:assert";
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { after, before, describe, it } from "node:test";
|
|
9
|
+
import { getEventStoreDBEventStore } from ".";
|
|
10
|
+
const evolve = (state, { type, data }) => {
|
|
11
|
+
switch (type) {
|
|
12
|
+
case "ProductItemAdded": {
|
|
13
|
+
const productItem = data.productItem;
|
|
14
|
+
return {
|
|
15
|
+
productItems: [...state.productItems, productItem],
|
|
16
|
+
totalAmount: state.totalAmount + productItem.price * productItem.quantity
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
case "DiscountApplied":
|
|
20
|
+
return {
|
|
21
|
+
...state,
|
|
22
|
+
totalAmount: state.totalAmount * (1 - data.percent)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const getInitialState = () => {
|
|
27
|
+
return { productItems: [], totalAmount: 0 };
|
|
28
|
+
};
|
|
29
|
+
describe("EventStoreDBEventStore", () => {
|
|
30
|
+
let esdbContainer;
|
|
31
|
+
let eventStore;
|
|
32
|
+
before(async () => {
|
|
33
|
+
esdbContainer = await new EventStoreDBContainer().start();
|
|
34
|
+
eventStore = getEventStoreDBEventStore(esdbContainer.getClient());
|
|
35
|
+
});
|
|
36
|
+
after(() => {
|
|
37
|
+
return esdbContainer.stop();
|
|
38
|
+
});
|
|
39
|
+
describe("aggregateStream", () => {
|
|
40
|
+
it("When called with `to` allows time travelling", async () => {
|
|
41
|
+
const productItem = {
|
|
42
|
+
productId: "123",
|
|
43
|
+
quantity: 10,
|
|
44
|
+
price: 3
|
|
45
|
+
};
|
|
46
|
+
const discount = 10;
|
|
47
|
+
const shoppingCartId = randomUUID();
|
|
48
|
+
await eventStore.appendToStream(shoppingCartId, [
|
|
49
|
+
{ type: "ProductItemAdded", data: { productItem } }
|
|
50
|
+
]);
|
|
51
|
+
await eventStore.appendToStream(shoppingCartId, [
|
|
52
|
+
{ type: "ProductItemAdded", data: { productItem } }
|
|
53
|
+
]);
|
|
54
|
+
await eventStore.appendToStream(shoppingCartId, [
|
|
55
|
+
{ type: "DiscountApplied", data: { percent: discount } }
|
|
56
|
+
]);
|
|
57
|
+
const resultAt1 = await eventStore.aggregateStream(shoppingCartId, {
|
|
58
|
+
evolve,
|
|
59
|
+
getInitialState,
|
|
60
|
+
read: { to: 1n }
|
|
61
|
+
});
|
|
62
|
+
const resultAt2 = await eventStore.aggregateStream(shoppingCartId, {
|
|
63
|
+
evolve,
|
|
64
|
+
getInitialState,
|
|
65
|
+
read: { to: 2n }
|
|
66
|
+
});
|
|
67
|
+
const resultAt3 = await eventStore.aggregateStream(shoppingCartId, {
|
|
68
|
+
evolve,
|
|
69
|
+
getInitialState,
|
|
70
|
+
read: { to: 3n }
|
|
71
|
+
});
|
|
72
|
+
assert.ok(resultAt1);
|
|
73
|
+
assert.ok(resultAt2);
|
|
74
|
+
assert.ok(resultAt3);
|
|
75
|
+
assert.equal(resultAt1.currentStreamVersion, 0);
|
|
76
|
+
assert.deepEqual(resultAt1.state, {
|
|
77
|
+
productItems: [productItem],
|
|
78
|
+
totalAmount: productItem.price * productItem.quantity
|
|
79
|
+
});
|
|
80
|
+
assert.equal(resultAt2.currentStreamVersion, 1);
|
|
81
|
+
assert.deepEqual(resultAt2.state, {
|
|
82
|
+
productItems: [productItem, productItem],
|
|
83
|
+
totalAmount: productItem.price * productItem.quantity * 2
|
|
84
|
+
});
|
|
85
|
+
assert.equal(resultAt3.currentStreamVersion, 2);
|
|
86
|
+
assert.deepEqual(resultAt3.state, {
|
|
87
|
+
productItems: [productItem, productItem],
|
|
88
|
+
totalAmount: productItem.price * productItem.quantity * 2 * (1 - discount)
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
//# sourceMappingURL=eventstoreDBEventStore.e2e.spec.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/eventstoreDBEventStore.e2e.spec.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-floating-promises */\nimport { type Event, type EventStore } from '@event-driven-io/emmett';\nimport {\n EventStoreDBContainer,\n StartedEventStoreDBContainer,\n} from '@event-driven-io/emmett-testcontainers';\nimport assert from 'node:assert';\nimport { randomUUID } from 'node:crypto';\nimport { after, before, describe, it } from 'node:test';\nimport { getEventStoreDBEventStore } from '.';\n\n// Events & Entity\n\ntype PricedProductItem = { productId: string; quantity: number; price: number };\n\ntype ShoppingCart = {\n productItems: PricedProductItem[];\n totalAmount: number;\n};\n\ntype ProductItemAdded = Event<\n 'ProductItemAdded',\n { productItem: PricedProductItem }\n>;\ntype DiscountApplied = Event<'DiscountApplied', { percent: number }>;\n\ntype ShoppingCartEvent = ProductItemAdded | DiscountApplied;\n\nconst evolve = (\n state: ShoppingCart,\n { type, data }: ShoppingCartEvent,\n): ShoppingCart => {\n switch (type) {\n case 'ProductItemAdded': {\n const productItem = data.productItem;\n return {\n productItems: [...state.productItems, productItem],\n totalAmount:\n state.totalAmount + productItem.price * productItem.quantity,\n };\n }\n case 'DiscountApplied':\n return {\n ...state,\n totalAmount: state.totalAmount * (1 - data.percent),\n };\n }\n};\n\nconst getInitialState = (): ShoppingCart => {\n return { productItems: [], totalAmount: 0 };\n};\n\ndescribe('EventStoreDBEventStore', () => {\n let esdbContainer: StartedEventStoreDBContainer;\n let eventStore: EventStore;\n\n before(async () => {\n esdbContainer = await new EventStoreDBContainer().start();\n eventStore = getEventStoreDBEventStore(esdbContainer.getClient());\n });\n\n after(() => {\n return esdbContainer.stop();\n });\n\n describe('aggregateStream', () => {\n it('When called with `to` allows time travelling', async () => {\n // Given\n const productItem: PricedProductItem = {\n productId: '123',\n quantity: 10,\n price: 3,\n };\n const discount = 10;\n const shoppingCartId = randomUUID();\n\n await eventStore.appendToStream<ShoppingCartEvent>(shoppingCartId, [\n { type: 'ProductItemAdded', data: { productItem } },\n ]);\n await eventStore.appendToStream<ShoppingCartEvent>(shoppingCartId, [\n { type: 'ProductItemAdded', data: { productItem } },\n ]);\n await eventStore.appendToStream<ShoppingCartEvent>(shoppingCartId, [\n { type: 'DiscountApplied', data: { percent: discount } },\n ]);\n\n // when\n const resultAt1 = await eventStore.aggregateStream(shoppingCartId, {\n evolve,\n getInitialState,\n read: { to: 1n },\n });\n const resultAt2 = await eventStore.aggregateStream(shoppingCartId, {\n evolve,\n getInitialState,\n read: { to: 2n },\n });\n const resultAt3 = await eventStore.aggregateStream(shoppingCartId, {\n evolve,\n getInitialState,\n read: { to: 3n },\n });\n\n // then\n assert.ok(resultAt1);\n assert.ok(resultAt2);\n assert.ok(resultAt3);\n\n // Note that ESDB counts from 0\n assert.equal(resultAt1.currentStreamVersion, 0);\n assert.deepEqual(resultAt1.state, {\n productItems: [productItem],\n totalAmount: productItem.price * productItem.quantity,\n });\n\n assert.equal(resultAt2.currentStreamVersion, 1);\n assert.deepEqual(resultAt2.state, {\n productItems: [productItem, productItem],\n totalAmount: productItem.price * productItem.quantity * 2,\n });\n\n assert.equal(resultAt3.currentStreamVersion, 2);\n assert.deepEqual(resultAt3.state, {\n productItems: [productItem, productItem],\n totalAmount:\n productItem.price * productItem.quantity * 2 * (1 - discount),\n });\n });\n });\n});\n"],"mappings":"AACA,eAA4C;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,OAAO,YAAY;AACnB,SAAS,kBAAkB;AAC3B,SAAS,OAAO,QAAQ,UAAU,UAAU;AAC5C,SAAS,iCAAiC;AAmB1C,MAAM,SAAS,CACb,OACA,EAAE,MAAM,KAAK,MACI;AACjB,UAAQ,MAAM;AAAA,IACZ,KAAK,oBAAoB;AACvB,YAAM,c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gBAAgB,gBAAgB;AAAA,QACjE;AAAA,QACA;AAAA,QACA,MAAM,EAAE,IAAI,GAAG;AAAA,MACjB,CAAC;AACD,YAAM,YAAY,MAAM,WAAW,gBAAgB,gBAAgB;AAAA,QACjE;AAAA,QACA;AAAA,QACA,MAAM,EAAE,IAAI,GAAG;AAAA,MACjB,CAAC;AAGD,aAAO,GAAG,SAAS;AACnB,aAAO,GAAG,SAAS;AACnB,aAAO,GAAG,SAAS;AAGnB,aAAO,MAAM,UAAU,sBAAsB,CAAC;AAC9C,aAAO,UAAU,UAAU,OAAO;AAAA,QAChC,cAAc,CAAC,WAAW;AAAA,QAC1B,aAAa,YAAY,QAAQ,YAAY;AAAA,MAC/C,CAAC;AAED,aAAO,MAAM,UAAU,sBAAsB,CAAC;AAC9C,aAAO,UAAU,UAAU,OAAO;AAAA,QAChC,cAAc,CAAC,aAAa,WAAW;AAAA,QACvC,aAAa,YAAY,QAAQ,YAAY,WAAW;AAAA,MAC1D,CAAC;AAED,aAAO,MAAM,UAAU,sBAAsB,CAAC;AAC9C,aAAO,UAAU,UAAU,OAAO;AAAA,QAChC,cAAc,CAAC,aAAa,WAAW;AAAA,QACvC,aACE,YAAY,QAAQ,YAAY,WAAW,KAAK,IAAI;AAAA,MACxD,CAAC;AAAA,IACH,CAAC;AAAA,EACH,CAAC;AACH,CAAC;","names":[]}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ExpectedVersionConflictError, NO_CONCURRENCY_CHECK, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, } from '@event-driven-io/emmett';
|
|
2
|
+
import { ANY, STREAM_EXISTS as ESDB_STREAM_EXISTS, EventStoreDBClient, NO_STREAM, StreamNotFoundError, WrongExpectedVersionError, jsonEvent, } from '@eventstore/db-client';
|
|
3
|
+
export const getEventStoreDBEventStore = (eventStore) => {
|
|
4
|
+
return {
|
|
5
|
+
async aggregateStream(streamName, options) {
|
|
6
|
+
try {
|
|
7
|
+
const { evolve, getInitialState, read } = options;
|
|
8
|
+
const expectedStreamVersion = read?.expectedStreamVersion;
|
|
9
|
+
let state = getInitialState();
|
|
10
|
+
let currentStreamVersion = undefined;
|
|
11
|
+
for await (const { event } of eventStore.readStream(streamName)) {
|
|
12
|
+
if (!event)
|
|
13
|
+
continue;
|
|
14
|
+
state = evolve(state, {
|
|
15
|
+
type: event.type,
|
|
16
|
+
data: event.data,
|
|
17
|
+
});
|
|
18
|
+
currentStreamVersion = event.revision;
|
|
19
|
+
}
|
|
20
|
+
assertExpectedVersionMatchesCurrent(currentStreamVersion, expectedStreamVersion);
|
|
21
|
+
return {
|
|
22
|
+
currentStreamVersion: currentStreamVersion ?? 0n,
|
|
23
|
+
state,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (error instanceof StreamNotFoundError) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
readStream: async (streamName, options) => {
|
|
34
|
+
const events = [];
|
|
35
|
+
const readOptions = options
|
|
36
|
+
? {
|
|
37
|
+
fromRevision: 'from' in options ? options.from : undefined,
|
|
38
|
+
maxCount: 'maxCount' in options
|
|
39
|
+
? options.maxCount
|
|
40
|
+
: 'to' in options
|
|
41
|
+
? options.to
|
|
42
|
+
: undefined,
|
|
43
|
+
}
|
|
44
|
+
: undefined;
|
|
45
|
+
let currentStreamVersion = undefined;
|
|
46
|
+
try {
|
|
47
|
+
for await (const { event } of eventStore.readStream(streamName, readOptions)) {
|
|
48
|
+
if (!event)
|
|
49
|
+
continue;
|
|
50
|
+
events.push({
|
|
51
|
+
type: event.type,
|
|
52
|
+
data: event.data,
|
|
53
|
+
});
|
|
54
|
+
currentStreamVersion = event.revision;
|
|
55
|
+
}
|
|
56
|
+
return currentStreamVersion
|
|
57
|
+
? {
|
|
58
|
+
currentStreamVersion,
|
|
59
|
+
events,
|
|
60
|
+
}
|
|
61
|
+
: null;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (error instanceof StreamNotFoundError) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
appendToStream: async (streamName, events, options) => {
|
|
71
|
+
try {
|
|
72
|
+
const serializedEvents = events.map(jsonEvent);
|
|
73
|
+
const expectedRevision = toExpectedRevision(options?.expectedStreamVersion);
|
|
74
|
+
const appendResult = await eventStore.appendToStream(streamName, serializedEvents, {
|
|
75
|
+
expectedRevision,
|
|
76
|
+
});
|
|
77
|
+
return { nextExpectedStreamVersion: appendResult.nextExpectedRevision };
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (error instanceof WrongExpectedVersionError) {
|
|
81
|
+
throw new ExpectedVersionConflictError(error.actualVersion, toExpectedVersion(error.expectedVersion));
|
|
82
|
+
}
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
const toExpectedRevision = (expected) => {
|
|
89
|
+
if (expected === undefined)
|
|
90
|
+
return ANY;
|
|
91
|
+
if (expected === NO_CONCURRENCY_CHECK)
|
|
92
|
+
return ANY;
|
|
93
|
+
if (expected == STREAM_DOES_NOT_EXIST)
|
|
94
|
+
return NO_STREAM;
|
|
95
|
+
if (expected == STREAM_EXISTS)
|
|
96
|
+
return ESDB_STREAM_EXISTS;
|
|
97
|
+
return expected;
|
|
98
|
+
};
|
|
99
|
+
const toExpectedVersion = (expected) => {
|
|
100
|
+
if (expected === undefined)
|
|
101
|
+
return NO_CONCURRENCY_CHECK;
|
|
102
|
+
if (expected === ANY)
|
|
103
|
+
return NO_CONCURRENCY_CHECK;
|
|
104
|
+
if (expected == NO_STREAM)
|
|
105
|
+
return STREAM_DOES_NOT_EXIST;
|
|
106
|
+
if (expected == ESDB_STREAM_EXISTS)
|
|
107
|
+
return STREAM_EXISTS;
|
|
108
|
+
return expected;
|
|
109
|
+
};
|
|
110
|
+
const matchesExpectedVersion = (current, expected) => {
|
|
111
|
+
if (expected === NO_CONCURRENCY_CHECK)
|
|
112
|
+
return true;
|
|
113
|
+
if (expected == STREAM_DOES_NOT_EXIST)
|
|
114
|
+
return current === undefined;
|
|
115
|
+
if (expected == STREAM_EXISTS)
|
|
116
|
+
return current !== undefined;
|
|
117
|
+
return current === expected;
|
|
118
|
+
};
|
|
119
|
+
const assertExpectedVersionMatchesCurrent = (current, expected) => {
|
|
120
|
+
expected ??= NO_CONCURRENCY_CHECK;
|
|
121
|
+
if (!matchesExpectedVersion(current, expected))
|
|
122
|
+
throw new ExpectedVersionConflictError(current, expected);
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=eventstoreDBEventStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eventstoreDBEventStore.js","sourceRoot":"","sources":["../../src/eventStore/eventstoreDBEventStore.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,4BAA4B,EAC5B,oBAAoB,EACpB,qBAAqB,EACrB,aAAa,GAUd,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,GAAG,EACH,aAAa,IAAI,kBAAkB,EACnC,kBAAkB,EAClB,SAAS,EACT,mBAAmB,EACnB,yBAAyB,EACzB,SAAS,GAGV,MAAM,uBAAuB,CAAC;AAE/B,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,UAA8B,EAClB,EAAE;IACd,OAAO;QACL,KAAK,CAAC,eAAe,CACnB,UAAkB,EAClB,OAAiD;YAEjD,IAAI,CAAC;gBACH,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;gBAElD,MAAM,qBAAqB,GAAG,IAAI,EAAE,qBAAqB,CAAC;gBAE1D,IAAI,KAAK,GAAG,eAAe,EAAE,CAAC;gBAC9B,IAAI,oBAAoB,GAAuB,SAAS,CAAC;gBAEzD,IAAI,KAAK,EAAE,MAAM,EA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
|
|
2
|
+
ExpectedVersionConflictError,
|
|
3
|
+
NO_CONCURRENCY_CHECK,
|
|
4
|
+
STREAM_DOES_NOT_EXIST,
|
|
5
|
+
STREAM_EXISTS
|
|
6
|
+
} from "@event-driven-io/emmett";
|
|
7
|
+
import {
|
|
8
|
+
ANY,
|
|
9
|
+
STREAM_EXISTS as ESDB_STREAM_EXISTS,
|
|
10
|
+
EventStoreDBClient,
|
|
11
|
+
NO_STREAM,
|
|
12
|
+
StreamNotFoundError,
|
|
13
|
+
WrongExpectedVersionError,
|
|
14
|
+
jsonEvent
|
|
15
|
+
} from "@eventstore/db-client";
|
|
16
|
+
const toEventStoreDBReadOptions = (options) => {
|
|
17
|
+
return options ? {
|
|
18
|
+
fromRevision: "from" in options ? options.from : void 0,
|
|
19
|
+
maxCount: "maxCount" in options ? options.maxCount : "to" in options ? options.to : void 0
|
|
20
|
+
} : void 0;
|
|
21
|
+
};
|
|
22
|
+
const getEventStoreDBEventStore = (eventStore) => {
|
|
23
|
+
return {
|
|
24
|
+
async aggregateStream(streamName, options) {
|
|
25
|
+
try {
|
|
26
|
+
const { evolve, getInitialState, read } = options;
|
|
27
|
+
const expectedStreamVersion = read?.expectedStreamVersion;
|
|
28
|
+
let state = getInitialState();
|
|
29
|
+
let currentStreamVersion = void 0;
|
|
30
|
+
for await (const { event } of eventStore.readStream(
|
|
31
|
+
streamName,
|
|
32
|
+
toEventStoreDBReadOptions(options.read)
|
|
33
|
+
)) {
|
|
34
|
+
if (!event)
|
|
35
|
+
continue;
|
|
36
|
+
state = evolve(state, {
|
|
37
|
+
type: event.type,
|
|
38
|
+
data: event.data
|
|
39
|
+
});
|
|
40
|
+
currentStreamVersion = event.revision;
|
|
41
|
+
}
|
|
42
|
+
assertExpectedVersionMatchesCurrent(
|
|
43
|
+
currentStreamVersion,
|
|
44
|
+
expectedStreamVersion
|
|
45
|
+
);
|
|
46
|
+
return {
|
|
47
|
+
currentStreamVersion: currentStreamVersion ?? 0n,
|
|
48
|
+
state
|
|
49
|
+
};
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof StreamNotFoundError) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
readStream: async (streamName, options) => {
|
|
58
|
+
const events = [];
|
|
59
|
+
let currentStreamVersion = void 0;
|
|
60
|
+
try {
|
|
61
|
+
for await (const { event } of eventStore.readStream(
|
|
62
|
+
streamName,
|
|
63
|
+
toEventStoreDBReadOptions(options)
|
|
64
|
+
)) {
|
|
65
|
+
if (!event)
|
|
66
|
+
continue;
|
|
67
|
+
events.push({
|
|
68
|
+
type: event.type,
|
|
69
|
+
data: event.data
|
|
70
|
+
});
|
|
71
|
+
currentStreamVersion = event.revision;
|
|
72
|
+
}
|
|
73
|
+
return currentStreamVersion ? {
|
|
74
|
+
currentStreamVersion,
|
|
75
|
+
events
|
|
76
|
+
} : null;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error instanceof StreamNotFoundError) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
appendToStream: async (streamName, events, options) => {
|
|
85
|
+
try {
|
|
86
|
+
const serializedEvents = events.map(jsonEvent);
|
|
87
|
+
const expectedRevision = toExpectedRevision(
|
|
88
|
+
options?.expectedStreamVersion
|
|
89
|
+
);
|
|
90
|
+
const appendResult = await eventStore.appendToStream(
|
|
91
|
+
streamName,
|
|
92
|
+
serializedEvents,
|
|
93
|
+
{
|
|
94
|
+
expectedRevision
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
return { nextExpectedStreamVersion: appendResult.nextExpectedRevision };
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (error instanceof WrongExpectedVersionError) {
|
|
100
|
+
throw new ExpectedVersionConflictError(
|
|
101
|
+
error.actualVersion,
|
|
102
|
+
toExpectedVersion(error.expectedVersion)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
const toExpectedRevision = (expected) => {
|
|
111
|
+
if (expected === void 0)
|
|
112
|
+
return ANY;
|
|
113
|
+
if (expected === NO_CONCURRENCY_CHECK)
|
|
114
|
+
return ANY;
|
|
115
|
+
if (expected == STREAM_DOES_NOT_EXIST)
|
|
116
|
+
return NO_STREAM;
|
|
117
|
+
if (expected == STREAM_EXISTS)
|
|
118
|
+
return ESDB_STREAM_EXISTS;
|
|
119
|
+
return expected;
|
|
120
|
+
};
|
|
121
|
+
const toExpectedVersion = (expected) => {
|
|
122
|
+
if (expected === void 0)
|
|
123
|
+
return NO_CONCURRENCY_CHECK;
|
|
124
|
+
if (expected === ANY)
|
|
125
|
+
return NO_CONCURRENCY_CHECK;
|
|
126
|
+
if (expected == NO_STREAM)
|
|
127
|
+
return STREAM_DOES_NOT_EXIST;
|
|
128
|
+
if (expected == ESDB_STREAM_EXISTS)
|
|
129
|
+
return STREAM_EXISTS;
|
|
130
|
+
return expected;
|
|
131
|
+
};
|
|
132
|
+
const matchesExpectedVersion = (current, expected) => {
|
|
133
|
+
if (expected === NO_CONCURRENCY_CHECK)
|
|
134
|
+
return true;
|
|
135
|
+
if (expected == STREAM_DOES_NOT_EXIST)
|
|
136
|
+
return current === void 0;
|
|
137
|
+
if (expected == STREAM_EXISTS)
|
|
138
|
+
return current !== void 0;
|
|
139
|
+
return current === expected;
|
|
140
|
+
};
|
|
141
|
+
const assertExpectedVersionMatchesCurrent = (current, expected) => {
|
|
142
|
+
expected ??= NO_CONCURRENCY_CHECK;
|
|
143
|
+
if (!matchesExpectedVersion(current, expected))
|
|
144
|
+
throw new ExpectedVersionConflictError(current, expected);
|
|
145
|
+
};
|
|
146
|
+
export {
|
|
147
|
+
getEventStoreDBEventStore
|
|
148
|
+
};
|
|
149
|
+
//# sourceMappingURL=eventstoreDBEventStore.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/eventstoreDBEventStore.ts"],"sourcesContent":["import {\n ExpectedVersionConflictError,\n NO_CONCURRENCY_CHECK,\n STREAM_DOES_NOT_EXIST,\n STREAM_EXISTS,\n type AggregateStreamOptions,\n type AggregateStreamResult,\n type AppendToStreamOptions,\n type AppendToStreamResult,\n type Event,\n type EventStore,\n type ExpectedStreamVersion,\n type ReadStreamOptions,\n type ReadStreamResult,\n} from '@event-driven-io/emmett';\nimport {\n ANY,\n STREAM_EXISTS as ESDB_STREAM_EXISTS,\n EventStoreDBClient,\n NO_STREAM,\n StreamNotFoundError,\n WrongExpectedVersionError,\n jsonEvent,\n type AppendExpectedRevision,\n type ReadStreamOptions as ESDBReadStreamOptions,\n} from '@eventstore/db-client';\n\nconst toEventStoreDBReadOptions = (\n options: ReadStreamOptions | undefined,\n): ESDBReadStreamOptions | undefined => {\n return options\n ? {\n fromRevision: 'from' in options ? options.from : undefined,\n maxCount:\n 'maxCount' in options\n ? options.maxCount\n : 'to' in options\n ? options.to\n : undefined,\n }\n : undefined;\n};\n\nexport const getEventStoreDBEventStore = (\n eventStore: EventStoreDBClient,\n): EventStore => {\n return {\n async aggregateStream<State, EventType extends Event>(\n streamName: string,\n options: AggregateStreamOptions<State, EventType>,\n ): Promise<AggregateStreamResult<State> | null> {\n try {\n const { evolve, getInitialState, read } = options;\n\n const expectedStreamVersion = read?.expectedStreamVersion;\n\n let state = getInitialState();\n let currentStreamVersion: bigint | undefined = undefined;\n\n for await (const { event } of eventStore.readStream(\n streamName,\n toEventStoreDBReadOptions(options.read),\n )) {\n if (!event) continue;\n\n state = evolve(state, <EventType>{\n type: event.type,\n data: event.data,\n });\n currentStreamVersion = event.revision;\n }\n\n assertExpectedVersionMatchesCurrent(\n currentStreamVersion,\n expectedStreamVersion,\n );\n\n return {\n currentStreamVersion: currentStreamVersion ?? 0n,\n state,\n };\n } catch (error) {\n if (error instanceof StreamNotFoundError) {\n return null;\n }\n\n throw error;\n }\n },\n\n readStream: async <EventType extends Event>(\n streamName: string,\n options?: ReadStreamOptions,\n ): Promise<ReadStreamResult<EventType>> => {\n const events: EventType[] = [];\n\n let currentStreamVersion: bigint | undefined = undefined;\n\n try {\n for await (const { event } of eventStore.readStream(\n streamName,\n toEventStoreDBReadOptions(options),\n )) {\n if (!event) continue;\n events.push(<EventType>{\n type: event.type,\n data: event.data,\n });\n currentStreamVersion = event.revision;\n }\n return currentStreamVersion\n ? {\n currentStreamVersion,\n events,\n }\n : null;\n } catch (error) {\n if (error instanceof StreamNotFoundError) {\n return null;\n }\n\n throw error;\n }\n },\n\n appendToStream: async <EventType extends Event>(\n streamName: string,\n events: EventType[],\n options?: AppendToStreamOptions,\n ): Promise<AppendToStreamResult> => {\n try {\n const serializedEvents = events.map(jsonEvent);\n\n const expectedRevision = toExpectedRevision(\n options?.expectedStreamVersion,\n );\n\n const appendResult = await eventStore.appendToStream(\n streamName,\n serializedEvents,\n {\n expectedRevision,\n },\n );\n\n return { nextExpectedStreamVersion: appendResult.nextExpectedRevision };\n } catch (error) {\n if (error instanceof WrongExpectedVersionError) {\n throw new ExpectedVersionConflictError(\n error.actualVersion,\n toExpectedVersion(error.expectedVersion),\n );\n }\n\n throw error;\n }\n },\n };\n};\n\nconst toExpectedRevision = (\n expected: ExpectedStreamVersion | undefined,\n): AppendExpectedRevision => {\n if (expected === undefined) return ANY;\n\n if (expected === NO_CONCURRENCY_CHECK) return ANY;\n\n if (expected == STREAM_DOES_NOT_EXIST) return NO_STREAM;\n\n if (expected == STREAM_EXISTS) return ESDB_STREAM_EXISTS;\n\n return expected as bigint;\n};\n\nconst toExpectedVersion = (\n expected: AppendExpectedRevision | undefined,\n): ExpectedStreamVersion => {\n if (expected === undefined) return NO_CONCURRENCY_CHECK;\n\n if (expected === ANY) return NO_CONCURRENCY_CHECK;\n\n if (expected == NO_STREAM) return STREAM_DOES_NOT_EXIST;\n\n if (expected == ESDB_STREAM_EXISTS) return STREAM_EXISTS;\n\n return expected;\n};\n\nconst matchesExpectedVersion = (\n current: bigint | undefined,\n expected: ExpectedStreamVersion,\n): boolean => {\n if (expected === NO_CONCURRENCY_CHECK) return true;\n\n if (expected == STREAM_DOES_NOT_EXIST) return current === undefined;\n\n if (expected == STREAM_EXISTS) return current !== undefined;\n\n return current === expected;\n};\n\nconst assertExpectedVersionMatchesCurrent = (\n current: bigint | undefined,\n expected: ExpectedStreamVersion | undefined,\n): void => {\n expected ??= NO_CONCURRENCY_CHECK;\n\n if (!matchesExpectedVersion(current, expected))\n throw new ExpectedVersionConflictError(current, expected);\n};\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAUK;AACP;AAAA,EACE;AAAA,EACA,iBAAiB;AAAA,EACjB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AAEP,MAAM,4BAA4B,CAChC,YACsC;AACtC,SAAO,UACH;AAAA,IACE,cAAc,UAAU,UAAU,QAAQ,OAAO;AAAA,IACjD,UACE,cAAc,UACV,QAAQ,WACR,QAAQ,UACN,QAAQ,KACR;AAAA,EACV,IACA;AACN;AAEO,MAAM,4BAA4B,CACvC,eACe;AACf,SAAO;AAAA,IACL,MAAM,gBACJ,YACA,SAC8C;AAC9C,UAAI;AACF,cAAM,EAAE,QAAQ,iBAAiB,KAAK,IAAI;AAE1C,cAAM,wBAAwB,MAAM;AAEpC,YAAI,QAAQ,gBAAgB;AAC5B,YAAI,uBAA2C;AAE/C,yBAAiB,EAAE,MAAM,KAAK,WAAW;AAAA,UACvC;AAAA,UACA,0BAA0B,QAAQ,IAAI;AAAA,QACxC,GAAG;AACD,cAAI,CAAC;AAAO;AAEZ,kBAAQ,OAAO,OAAkB;AAAA,YAC/B,MAAM,MAAM;AAAA,YACZ,MAAM,MAAM;AAAA,UACd,CAAC;AACD,iCAAuB,MAAM;AAAA,QAC/B;AAEA;AAAA,UACE;AAAA,UACA;AAAA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AO;AAE9C,MAAI,YAAY;AAAuB,WAAO;AAE9C,MAAI,YAAY;AAAe,WAAO;AAEtC,SAAO;AACT;AAEA,MAAM,oBAAoB,CACxB,aAC0B;AAC1B,MAAI,aAAa;AAAW,WAAO;AAEnC,MAAI,aAAa;AAAK,WAAO;AAE7B,MAAI,YAAY;AAAW,WAAO;AAElC,MAAI,YAAY;AAAoB,WAAO;AAE3C,SAAO;AACT;AAEA,MAAM,yBAAyB,CAC7B,SACA,aACY;AACZ,MAAI,aAAa;AAAsB,WAAO;AAE9C,MAAI,YAAY;AAAuB,WAAO,YAAY;AAE1D,MAAI,YAAY;AAAe,WAAO,YAAY;AAElD,SAAO,YAAY;AACrB;AAEA,MAAM,sCAAsC,CAC1C,SACA,aACS;AACT,eAAa;AAEb,MAAI,CAAC,uBAAuB,SAAS,QAAQ;AAC3C,UAAM,IAAI,6BAA6B,SAAS,QAAQ;AAC5D;","names":[]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, configurable: true, get: () => obj[key]}); }); }var _eventstoreDBEventStore = require('./eventstoreDBEventStore'); _createStarExport(_eventstoreDBEventStore);
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/index.ts"],"names":[],"mappings":"AAAA,cAAc","sourcesContent":["export * from './eventstoreDBEventStore';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/index.ts"],"sourcesContent":["export * from './eventstoreDBEventStore';\n"],"mappings":"AAAA,cAAc;","names":[]}
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
"use strict"
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, configurable: true, get: () => obj[key]}); }); }var _eventStore = require('./eventStore'); _createStarExport(_eventStore);
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc","sourcesContent":["export * from './eventStore';\n"]}
|
package/dist/index.mjs
CHANGED
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './eventStore';\n"],"mappings":"AAAA,cAAc;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@event-driven-io/emmett-esdb",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Emmett - Event Sourcing development made simple",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Emmett - EventStoreDB - Event Sourcing development made simple",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsup",
|
|
7
7
|
"build:ts": "tsc",
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
"fix:prettier": "prettier --write \"**/**/!(*.d).{ts,json,md}\"",
|
|
14
14
|
"fix:eslint": "eslint **/*.ts --fix",
|
|
15
15
|
"test": "run-s test:unit test:int test:e2e",
|
|
16
|
-
"test:unit": "
|
|
17
|
-
"test:int": "
|
|
18
|
-
"test:e2e": "
|
|
19
|
-
"test:watch": "
|
|
20
|
-
"test:unit:watch": "
|
|
21
|
-
"test:int:watch": "
|
|
22
|
-
"test:e2e:watch": "
|
|
16
|
+
"test:unit": "glob -c \"node --import tsx --test\" **/*.unit.spec.ts",
|
|
17
|
+
"test:int": "glob -c \"node --import tsx --test\" **/*.int.spec.ts",
|
|
18
|
+
"test:e2e": "glob -c \"node --import tsx --test\" **/*.e2e.spec.ts",
|
|
19
|
+
"test:watch": "node --import tsx --test --watch",
|
|
20
|
+
"test:unit:watch": "glob -c \"node --import tsx --test --watch\" **/*.unit.spec.ts",
|
|
21
|
+
"test:int:watch": "glob -c \"node --import tsx --test --watch\" **/*.int.spec.ts",
|
|
22
|
+
"test:e2e:watch": "glob -c \"node --import tsx --test --watch\" **/*.e2e.spec.ts"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
@@ -38,5 +38,12 @@
|
|
|
38
38
|
"types": "./dist/index.d.ts",
|
|
39
39
|
"files": [
|
|
40
40
|
"dist"
|
|
41
|
-
]
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@event-driven-io/emmett": "0.5.0",
|
|
44
|
+
"@eventstore/db-client": "^6.1.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@event-driven-io/emmett-testcontainers": "^0.5.0"
|
|
48
|
+
}
|
|
42
49
|
}
|