@event-driven-io/emmett-esdb 0.1.7 → 0.4.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.js +144 -0
- package/dist/eventStore/eventstoreDBEventStore.js.map +1 -0
- package/dist/eventStore/eventstoreDBEventStore.mjs +144 -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 +16 -10
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _emmett = require('@event-driven-io/emmett');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
var _dbclient = require('@eventstore/db-client');
|
|
16
|
+
const getEventStoreDBEventStore = (eventStore) => {
|
|
17
|
+
return {
|
|
18
|
+
async aggregateStream(streamName, options) {
|
|
19
|
+
try {
|
|
20
|
+
const { evolve, getInitialState, read } = options;
|
|
21
|
+
const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _ => _.expectedStreamVersion]);
|
|
22
|
+
let state = getInitialState();
|
|
23
|
+
let currentStreamVersion = void 0;
|
|
24
|
+
for await (const { event } of eventStore.readStream(streamName)) {
|
|
25
|
+
if (!event)
|
|
26
|
+
continue;
|
|
27
|
+
state = evolve(state, {
|
|
28
|
+
type: event.type,
|
|
29
|
+
data: event.data
|
|
30
|
+
});
|
|
31
|
+
currentStreamVersion = event.revision;
|
|
32
|
+
}
|
|
33
|
+
assertExpectedVersionMatchesCurrent(
|
|
34
|
+
currentStreamVersion,
|
|
35
|
+
expectedStreamVersion
|
|
36
|
+
);
|
|
37
|
+
return {
|
|
38
|
+
currentStreamVersion: _nullishCoalesce(currentStreamVersion, () => ( 0n)),
|
|
39
|
+
state
|
|
40
|
+
};
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error instanceof _dbclient.StreamNotFoundError) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
readStream: async (streamName, options) => {
|
|
49
|
+
const events = [];
|
|
50
|
+
const readOptions = options ? {
|
|
51
|
+
fromRevision: "from" in options ? options.from : void 0,
|
|
52
|
+
maxCount: "maxCount" in options ? options.maxCount : "to" in options ? options.to : void 0
|
|
53
|
+
} : void 0;
|
|
54
|
+
let currentStreamVersion = void 0;
|
|
55
|
+
try {
|
|
56
|
+
for await (const { event } of eventStore.readStream(
|
|
57
|
+
streamName,
|
|
58
|
+
readOptions
|
|
59
|
+
)) {
|
|
60
|
+
if (!event)
|
|
61
|
+
continue;
|
|
62
|
+
events.push({
|
|
63
|
+
type: event.type,
|
|
64
|
+
data: event.data
|
|
65
|
+
});
|
|
66
|
+
currentStreamVersion = event.revision;
|
|
67
|
+
}
|
|
68
|
+
return currentStreamVersion ? {
|
|
69
|
+
currentStreamVersion,
|
|
70
|
+
events
|
|
71
|
+
} : null;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (error instanceof _dbclient.StreamNotFoundError) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
appendToStream: async (streamName, events, options) => {
|
|
80
|
+
try {
|
|
81
|
+
const serializedEvents = events.map(_dbclient.jsonEvent);
|
|
82
|
+
const expectedRevision = toExpectedRevision(
|
|
83
|
+
_optionalChain([options, 'optionalAccess', _2 => _2.expectedStreamVersion])
|
|
84
|
+
);
|
|
85
|
+
const appendResult = await eventStore.appendToStream(
|
|
86
|
+
streamName,
|
|
87
|
+
serializedEvents,
|
|
88
|
+
{
|
|
89
|
+
expectedRevision
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
return { nextExpectedStreamVersion: appendResult.nextExpectedRevision };
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (error instanceof _dbclient.WrongExpectedVersionError) {
|
|
95
|
+
throw new (0, _emmett.ExpectedVersionConflictError)(
|
|
96
|
+
error.actualVersion,
|
|
97
|
+
toExpectedVersion(error.expectedVersion)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const toExpectedRevision = (expected) => {
|
|
106
|
+
if (expected === void 0)
|
|
107
|
+
return _dbclient.ANY;
|
|
108
|
+
if (expected === _emmett.NO_CONCURRENCY_CHECK)
|
|
109
|
+
return _dbclient.ANY;
|
|
110
|
+
if (expected == _emmett.STREAM_DOES_NOT_EXIST)
|
|
111
|
+
return _dbclient.NO_STREAM;
|
|
112
|
+
if (expected == _emmett.STREAM_EXISTS)
|
|
113
|
+
return _dbclient.STREAM_EXISTS;
|
|
114
|
+
return expected;
|
|
115
|
+
};
|
|
116
|
+
const toExpectedVersion = (expected) => {
|
|
117
|
+
if (expected === void 0)
|
|
118
|
+
return _emmett.NO_CONCURRENCY_CHECK;
|
|
119
|
+
if (expected === _dbclient.ANY)
|
|
120
|
+
return _emmett.NO_CONCURRENCY_CHECK;
|
|
121
|
+
if (expected == _dbclient.NO_STREAM)
|
|
122
|
+
return _emmett.STREAM_DOES_NOT_EXIST;
|
|
123
|
+
if (expected == _dbclient.STREAM_EXISTS)
|
|
124
|
+
return _emmett.STREAM_EXISTS;
|
|
125
|
+
return expected;
|
|
126
|
+
};
|
|
127
|
+
const matchesExpectedVersion = (current, expected) => {
|
|
128
|
+
if (expected === _emmett.NO_CONCURRENCY_CHECK)
|
|
129
|
+
return true;
|
|
130
|
+
if (expected == _emmett.STREAM_DOES_NOT_EXIST)
|
|
131
|
+
return current === void 0;
|
|
132
|
+
if (expected == _emmett.STREAM_EXISTS)
|
|
133
|
+
return current !== void 0;
|
|
134
|
+
return current === expected;
|
|
135
|
+
};
|
|
136
|
+
const assertExpectedVersionMatchesCurrent = (current, expected) => {
|
|
137
|
+
expected ??= _emmett.NO_CONCURRENCY_CHECK;
|
|
138
|
+
if (!matchesExpectedVersion(current, expected))
|
|
139
|
+
throw new (0, _emmett.ExpectedVersionConflictError)(current, expected);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
exports.getEventStoreDBEventStore = getEventStoreDBEventStore;
|
|
144
|
+
//# sourceMappingURL=eventstoreDBEventStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/eventstoreDBEventStore.ts"],"names":[],"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;AA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REAM_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\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(streamName)) {\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 const readOptions: ESDBReadStreamOptions | undefined = 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 let currentStreamVersion: bigint | undefined = undefined;\n\n try {\n for await (const { event } of eventStore.readStream(\n streamName,\n readOptions,\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"]}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
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 getEventStoreDBEventStore = (eventStore) => {
|
|
17
|
+
return {
|
|
18
|
+
async aggregateStream(streamName, options) {
|
|
19
|
+
try {
|
|
20
|
+
const { evolve, getInitialState, read } = options;
|
|
21
|
+
const expectedStreamVersion = read?.expectedStreamVersion;
|
|
22
|
+
let state = getInitialState();
|
|
23
|
+
let currentStreamVersion = void 0;
|
|
24
|
+
for await (const { event } of eventStore.readStream(streamName)) {
|
|
25
|
+
if (!event)
|
|
26
|
+
continue;
|
|
27
|
+
state = evolve(state, {
|
|
28
|
+
type: event.type,
|
|
29
|
+
data: event.data
|
|
30
|
+
});
|
|
31
|
+
currentStreamVersion = event.revision;
|
|
32
|
+
}
|
|
33
|
+
assertExpectedVersionMatchesCurrent(
|
|
34
|
+
currentStreamVersion,
|
|
35
|
+
expectedStreamVersion
|
|
36
|
+
);
|
|
37
|
+
return {
|
|
38
|
+
currentStreamVersion: currentStreamVersion ?? 0n,
|
|
39
|
+
state
|
|
40
|
+
};
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error instanceof StreamNotFoundError) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
readStream: async (streamName, options) => {
|
|
49
|
+
const events = [];
|
|
50
|
+
const readOptions = options ? {
|
|
51
|
+
fromRevision: "from" in options ? options.from : void 0,
|
|
52
|
+
maxCount: "maxCount" in options ? options.maxCount : "to" in options ? options.to : void 0
|
|
53
|
+
} : void 0;
|
|
54
|
+
let currentStreamVersion = void 0;
|
|
55
|
+
try {
|
|
56
|
+
for await (const { event } of eventStore.readStream(
|
|
57
|
+
streamName,
|
|
58
|
+
readOptions
|
|
59
|
+
)) {
|
|
60
|
+
if (!event)
|
|
61
|
+
continue;
|
|
62
|
+
events.push({
|
|
63
|
+
type: event.type,
|
|
64
|
+
data: event.data
|
|
65
|
+
});
|
|
66
|
+
currentStreamVersion = event.revision;
|
|
67
|
+
}
|
|
68
|
+
return currentStreamVersion ? {
|
|
69
|
+
currentStreamVersion,
|
|
70
|
+
events
|
|
71
|
+
} : null;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (error instanceof StreamNotFoundError) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
appendToStream: async (streamName, events, options) => {
|
|
80
|
+
try {
|
|
81
|
+
const serializedEvents = events.map(jsonEvent);
|
|
82
|
+
const expectedRevision = toExpectedRevision(
|
|
83
|
+
options?.expectedStreamVersion
|
|
84
|
+
);
|
|
85
|
+
const appendResult = await eventStore.appendToStream(
|
|
86
|
+
streamName,
|
|
87
|
+
serializedEvents,
|
|
88
|
+
{
|
|
89
|
+
expectedRevision
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
return { nextExpectedStreamVersion: appendResult.nextExpectedRevision };
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (error instanceof WrongExpectedVersionError) {
|
|
95
|
+
throw new ExpectedVersionConflictError(
|
|
96
|
+
error.actualVersion,
|
|
97
|
+
toExpectedVersion(error.expectedVersion)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const toExpectedRevision = (expected) => {
|
|
106
|
+
if (expected === void 0)
|
|
107
|
+
return ANY;
|
|
108
|
+
if (expected === NO_CONCURRENCY_CHECK)
|
|
109
|
+
return ANY;
|
|
110
|
+
if (expected == STREAM_DOES_NOT_EXIST)
|
|
111
|
+
return NO_STREAM;
|
|
112
|
+
if (expected == STREAM_EXISTS)
|
|
113
|
+
return ESDB_STREAM_EXISTS;
|
|
114
|
+
return expected;
|
|
115
|
+
};
|
|
116
|
+
const toExpectedVersion = (expected) => {
|
|
117
|
+
if (expected === void 0)
|
|
118
|
+
return NO_CONCURRENCY_CHECK;
|
|
119
|
+
if (expected === ANY)
|
|
120
|
+
return NO_CONCURRENCY_CHECK;
|
|
121
|
+
if (expected == NO_STREAM)
|
|
122
|
+
return STREAM_DOES_NOT_EXIST;
|
|
123
|
+
if (expected == ESDB_STREAM_EXISTS)
|
|
124
|
+
return STREAM_EXISTS;
|
|
125
|
+
return expected;
|
|
126
|
+
};
|
|
127
|
+
const matchesExpectedVersion = (current, expected) => {
|
|
128
|
+
if (expected === NO_CONCURRENCY_CHECK)
|
|
129
|
+
return true;
|
|
130
|
+
if (expected == STREAM_DOES_NOT_EXIST)
|
|
131
|
+
return current === void 0;
|
|
132
|
+
if (expected == STREAM_EXISTS)
|
|
133
|
+
return current !== void 0;
|
|
134
|
+
return current === expected;
|
|
135
|
+
};
|
|
136
|
+
const assertExpectedVersionMatchesCurrent = (current, expected) => {
|
|
137
|
+
expected ??= NO_CONCURRENCY_CHECK;
|
|
138
|
+
if (!matchesExpectedVersion(current, expected))
|
|
139
|
+
throw new ExpectedVersionConflictError(current, expected);
|
|
140
|
+
};
|
|
141
|
+
export {
|
|
142
|
+
getEventStoreDBEventStore
|
|
143
|
+
};
|
|
144
|
+
//# 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\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(streamName)) {\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 const readOptions: ESDBReadStreamOptions | undefined = 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 let currentStreamVersion: bigint | undefined = undefined;\n\n try {\n for await (const { event } of eventStore.readStream(\n streamName,\n readOptions,\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;AAEA,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,WAAW,UAAU,GAAG;AAC/D,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,QACF;AAEA,eAAO;AAAA,UACL,sBAAsB,wBAAwB;AAAA,UAC9C;AAAA,QACF;AAAA,MACF,SAAS,OAAO;AACd,YAAI,iBAAiB,qBAAqB;AACxC,iBAAO;AAAA,QACT;AAEA,cAAM;AAAA,MACR;AAAA,IACF;AAAA,IAEA,YAAY,OACV,YACA,YACyC;AACzC,YAAM,SAAsB,C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AoB,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.4.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,11 @@
|
|
|
38
38
|
"types": "./dist/index.d.ts",
|
|
39
39
|
"files": [
|
|
40
40
|
"dist"
|
|
41
|
-
]
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@event-driven-io/emmett": "0.3.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@eventstore/db-client": "^6.1.0"
|
|
47
|
+
}
|
|
42
48
|
}
|