@event-driven-io/emmett 0.5.0 → 0.5.2
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/commandHandling/handleCommand.js +31 -33
- package/dist/commandHandling/handleCommand.js.map +1 -1
- package/dist/commandHandling/handleCommandWithDecider.js +13 -4
- package/dist/commandHandling/handleCommandWithDecider.js.map +1 -1
- package/dist/commandHandling/index.js +2 -2
- package/dist/commandHandling/index.js.map +1 -1
- package/dist/errors/index.js +51 -67
- package/dist/errors/index.js.map +1 -1
- package/dist/eventStore/eventStore.js +1 -2
- package/dist/eventStore/eventStore.js.map +1 -1
- package/dist/eventStore/expectedVersion.js +29 -22
- package/dist/eventStore/expectedVersion.js.map +1 -1
- package/dist/eventStore/inMemoryEventStore.js +69 -65
- package/dist/eventStore/inMemoryEventStore.js.map +1 -1
- package/dist/eventStore/inMemoryEventStore.mjs +2 -2
- package/dist/eventStore/inMemoryEventStore.mjs.map +1 -1
- package/dist/eventStore/index.js +3 -3
- package/dist/eventStore/index.js.map +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/dist/serialization/index.js +1 -1
- package/dist/serialization/index.js.map +1 -1
- package/dist/serialization/json/JSONParser.js +20 -16
- package/dist/serialization/json/JSONParser.js.map +1 -1
- package/dist/serialization/json/index.js +1 -1
- package/dist/serialization/json/index.js.map +1 -1
- package/dist/testing/assertions.js +24 -12
- package/dist/testing/assertions.js.map +1 -1
- package/dist/testing/deciderSpecification.js +39 -40
- package/dist/testing/deciderSpecification.js.map +1 -1
- package/dist/testing/index.js +2 -2
- package/dist/testing/index.js.map +1 -1
- package/dist/typing/command.js +1 -2
- package/dist/typing/command.js.map +1 -1
- package/dist/typing/decider.js +1 -2
- package/dist/typing/decider.js.map +1 -1
- package/dist/typing/event.js +1 -2
- package/dist/typing/event.js.map +1 -1
- package/dist/typing/index.d.mts +8 -8
- package/dist/typing/index.d.ts +8 -8
- package/dist/typing/index.js +4 -4
- package/dist/typing/index.js.map +1 -1
- package/dist/typing/workflow.d.ts +1 -57
- package/dist/typing/workflow.js +55 -45
- package/dist/typing/workflow.js.map +1 -1
- package/dist/utils/index.js +6 -3
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/iterators.js +10 -8
- package/dist/utils/iterators.js.map +1 -1
- package/dist/utils/merge.js +21 -27
- package/dist/utils/merge.js.map +1 -1
- package/dist/validation/index.js +33 -25
- package/dist/validation/index.js.map +1 -1
- package/package.json +13 -10
- package/dist/commandHandling/handleCommand.unit.spec.d.mts +0 -2
- package/dist/commandHandling/handleCommand.unit.spec.d.ts +0 -2
- package/dist/commandHandling/handleCommand.unit.spec.js +0 -83
- package/dist/commandHandling/handleCommand.unit.spec.js.map +0 -1
- package/dist/commandHandling/handleCommand.unit.spec.mjs +0 -93
- package/dist/commandHandling/handleCommand.unit.spec.mjs.map +0 -1
- package/dist/eventStore/expectedVersion.unit.spec.d.mts +0 -2
- package/dist/eventStore/expectedVersion.unit.spec.d.ts +0 -2
- package/dist/eventStore/expectedVersion.unit.spec.js +0 -50
- package/dist/eventStore/expectedVersion.unit.spec.js.map +0 -1
- package/dist/eventStore/expectedVersion.unit.spec.mjs +0 -62
- package/dist/eventStore/expectedVersion.unit.spec.mjs.map +0 -1
- package/dist/eventStore/inMemoryEventStore.unit.spec.d.mts +0 -2
- package/dist/eventStore/inMemoryEventStore.unit.spec.d.ts +0 -2
- package/dist/eventStore/inMemoryEventStore.unit.spec.js +0 -85
- package/dist/eventStore/inMemoryEventStore.unit.spec.js.map +0 -1
- package/dist/eventStore/inMemoryEventStore.unit.spec.mjs +0 -81
- package/dist/eventStore/inMemoryEventStore.unit.spec.mjs.map +0 -1
- package/dist/validation/validation.spec.d.mts +0 -2
- package/dist/validation/validation.spec.d.ts +0 -2
- package/dist/validation/validation.spec.js +0 -17
- package/dist/validation/validation.spec.js.map +0 -1
- package/dist/validation/validation.spec.mjs +0 -13
- package/dist/validation/validation.spec.mjs.map +0 -1
|
@@ -1,35 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
expectedStreamVersion,
|
|
30
|
-
});
|
|
31
|
-
// 5. Return result with updated state
|
|
32
|
-
return { ...appendResult, newState: newEvents.reduce(evolve, state) };
|
|
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
|
+
var _eventStore = require('../eventStore');
|
|
5
|
+
const CommandHandler = (evolve, getInitialState, mapToStreamId = (id) => id) => async (eventStore, id, handle, options) => {
|
|
6
|
+
const streamName = mapToStreamId(id);
|
|
7
|
+
const aggregationResult = await eventStore.aggregateStream(streamName, {
|
|
8
|
+
evolve,
|
|
9
|
+
getInitialState,
|
|
10
|
+
read: {
|
|
11
|
+
// expected stream version is passed to fail fast
|
|
12
|
+
// if stream is in the wrong state
|
|
13
|
+
expectedStreamVersion: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.expectedStreamVersion]), () => ( _eventStore.NO_CONCURRENCY_CHECK))
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
const state = _nullishCoalesce(_optionalChain([aggregationResult, 'optionalAccess', _2 => _2.state]), () => ( getInitialState()));
|
|
17
|
+
const currentStreamVersion = _optionalChain([aggregationResult, 'optionalAccess', _3 => _3.currentStreamVersion]);
|
|
18
|
+
const result = handle(state);
|
|
19
|
+
const newEvents = Array.isArray(result) ? result : [result];
|
|
20
|
+
const expectedStreamVersion = _nullishCoalesce(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _4 => _4.expectedStreamVersion]), () => ( currentStreamVersion)), () => ( _eventStore.STREAM_DOES_NOT_EXIST));
|
|
21
|
+
const appendResult = await eventStore.appendToStream(
|
|
22
|
+
streamName,
|
|
23
|
+
newEvents,
|
|
24
|
+
{
|
|
25
|
+
expectedStreamVersion
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
return { ...appendResult, newState: newEvents.reduce(evolve, state) };
|
|
33
29
|
};
|
|
34
|
-
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
exports.CommandHandler = CommandHandler;
|
|
35
33
|
//# sourceMappingURL=handleCommand.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/commandHandling/handleCommand.ts"],"names":[],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,OAKK;AASA,MAAM,iBACX,CACE,QACA,iBACA,gBAAwC,CAAC,OAAO,OAElD,OACE,YACA,IACA,QACA,YAGwD;AACxD,QAAM,aAAa,cAAc,EAAE;AAGnC,QAAM,oBAAoB,MAAM,WAAW,gBAGzC,YAAY;AAAA,IACZ;AAAA,IACA;AAAA,IACA,MAAM;AAAA;AAAA;AAAA,MAGJ,uBACE,SAAS,yBAAyB;AAAA,IACtC;AAAA,EACF,CAAC;AAGD,QAAM,QAAQ,mBAAmB,SAAS,gBAAgB;AAC1D,QAAM,uBAAuB,mBAAmB;AAGhD,QAAM,SAAS,OAAO,KAAK;AAE3B,QAAM,YAAY,MAAM,QAAQ,MAAM,IAAI,SAAS,CAAC,MAAM;AAM1D,QAAM,wBACJ,SAAS,yBACT,wBACA;AAGF,QAAM,eAAe,MAAM,WAAW;AAAA,IACpC;AAAA,IACA;AAAA,IACA;AAAA,MACE;AAAA,IACF;AAAA,EACF;AAGA,SAAO,EAAE,GAAG,cAAc,UAAU,UAAU,OAAO,QAAQ,KAAK,EAAE;AACtE","sourcesContent":["import {\n NO_CONCURRENCY_CHECK,\n STREAM_DOES_NOT_EXIST,\n type AppendToStreamResult,\n type DefaultStreamVersionType,\n type EventStore,\n type ExpectedStreamVersion,\n} from '../eventStore';\nimport type { Event } from '../typing';\n\n// #region command-handler\nexport type CommandHandlerResult<\n State,\n StreamVersion = DefaultStreamVersionType,\n> = AppendToStreamResult<StreamVersion> & { newState: State };\n\nexport const CommandHandler =\n <State, StreamEvent extends Event, StreamVersion = DefaultStreamVersionType>(\n evolve: (state: State, event: StreamEvent) => State,\n getInitialState: () => State,\n mapToStreamId: (id: string) => string = (id) => id,\n ) =>\n async (\n eventStore: EventStore<StreamVersion>,\n id: string,\n handle: (state: State) => StreamEvent | StreamEvent[],\n options?: {\n expectedStreamVersion?: ExpectedStreamVersion<StreamVersion>;\n },\n ): Promise<CommandHandlerResult<State, StreamVersion>> => {\n const streamName = mapToStreamId(id);\n\n // 1. Aggregate the stream\n const aggregationResult = await eventStore.aggregateStream<\n State,\n StreamEvent\n >(streamName, {\n evolve,\n getInitialState,\n read: {\n // expected stream version is passed to fail fast\n // if stream is in the wrong state\n expectedStreamVersion:\n options?.expectedStreamVersion ?? NO_CONCURRENCY_CHECK,\n },\n });\n\n // 2. Use the aggregate state or the initial one (when e.g. stream does not exist)\n const state = aggregationResult?.state ?? getInitialState();\n const currentStreamVersion = aggregationResult?.currentStreamVersion;\n\n // 3. Run business logic\n const result = handle(state);\n\n const newEvents = Array.isArray(result) ? result : [result];\n\n // Either use:\n // - provided expected stream version,\n // - current stream version got from stream aggregation,\n // - or expect stream not to exists otherwise.\n const expectedStreamVersion: ExpectedStreamVersion<StreamVersion> =\n options?.expectedStreamVersion ??\n currentStreamVersion ??\n STREAM_DOES_NOT_EXIST;\n\n // 4. Append result to the stream\n const appendResult = await eventStore.appendToStream(\n streamName,\n newEvents,\n {\n expectedStreamVersion,\n },\n );\n\n // 5. Return result with updated state\n return { ...appendResult, newState: newEvents.reduce(evolve, state) };\n };\n// #endregion command-handler\n"]}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _handleCommand = require('./handleCommand');
|
|
2
|
+
const DeciderCommandHandler = ({
|
|
3
|
+
decide,
|
|
4
|
+
evolve,
|
|
5
|
+
getInitialState
|
|
6
|
+
}, mapToStreamId = (id) => id) => async (eventStore, id, command, options) => _handleCommand.CommandHandler.call(void 0,
|
|
7
|
+
evolve,
|
|
8
|
+
getInitialState,
|
|
9
|
+
mapToStreamId
|
|
10
|
+
)(eventStore, id, (state) => decide(command, state), options);
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
exports.DeciderCommandHandler = DeciderCommandHandler;
|
|
5
14
|
//# sourceMappingURL=handleCommandWithDecider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/commandHandling/handleCommandWithDecider.ts"],"names":[],"mappings":"AAOA,SAAS,sBAAsB;AAGxB,MAAM,wBACX,CAME;AAAA,EACE;AAAA,EACA;AAAA,EACA;AACF,GACA,gBAAwC,CAAC,OAAO,OAElD,OACE,YACA,IACA,SACA,YAIA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AACF,EAAE,YAAY,IAAI,CAAC,UAAU,OAAO,SAAS,KAAK,GAAG,OAAO","sourcesContent":["import type {\n DefaultStreamVersionType,\n EventStore,\n ExpectedStreamVersion,\n} from '../eventStore';\nimport type { Command, Event } from '../typing';\nimport type { Decider } from '../typing/decider';\nimport { CommandHandler } from './handleCommand';\n\n// #region command-handler\nexport const DeciderCommandHandler =\n <\n State,\n CommandType extends Command,\n StreamEvent extends Event,\n StreamVersion = DefaultStreamVersionType,\n >(\n {\n decide,\n evolve,\n getInitialState,\n }: Decider<State, CommandType, StreamEvent>,\n mapToStreamId: (id: string) => string = (id) => id,\n ) =>\n async (\n eventStore: EventStore<StreamVersion>,\n id: string,\n command: CommandType,\n options?: {\n expectedStreamVersion?: ExpectedStreamVersion<StreamVersion>;\n },\n ) =>\n CommandHandler<State, StreamEvent, StreamVersion>(\n evolve,\n getInitialState,\n mapToStreamId,\n )(eventStore, id, (state) => decide(command, state), options);\n// #endregion command-handler\n"]}
|
|
@@ -1,3 +1,3 @@
|
|
|
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 _handleCommand = require('./handleCommand'); _createStarExport(_handleCommand);
|
|
2
|
+
var _handleCommandWithDecider = require('./handleCommandWithDecider'); _createStarExport(_handleCommandWithDecider);
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/commandHandling/index.ts"],"names":[],"mappings":"AAAA,cAAc;AACd,cAAc","sourcesContent":["export * from './handleCommand';\nexport * from './handleCommandWithDecider';\n"]}
|
package/dist/errors/index.js
CHANGED
|
@@ -1,73 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
? options.message
|
|
12
|
-
: isString(options)
|
|
13
|
-
? options
|
|
14
|
-
: `Error with status code '${errorCode}' ocurred during Emmett processing`;
|
|
15
|
-
super(message);
|
|
16
|
-
this.errorCode = errorCode;
|
|
17
|
-
// 👇️ because we are extending a built-in class
|
|
18
|
-
Object.setPrototypeOf(this, EmmettError.prototype);
|
|
19
|
-
}
|
|
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; }var _validation = require('../validation');
|
|
2
|
+
class EmmettError extends Error {
|
|
3
|
+
|
|
4
|
+
constructor(options) {
|
|
5
|
+
const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : _validation.isNumber.call(void 0, options) ? options : 500;
|
|
6
|
+
const message = options && typeof options === "object" && "message" in options ? options.message : _validation.isString.call(void 0, options) ? options : `Error with status code '${errorCode}' ocurred during Emmett processing`;
|
|
7
|
+
super(message);
|
|
8
|
+
this.errorCode = errorCode;
|
|
9
|
+
Object.setPrototypeOf(this, EmmettError.prototype);
|
|
10
|
+
}
|
|
20
11
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
this.expected = expected;
|
|
32
|
-
// 👇️ because we are extending a built-in class
|
|
33
|
-
Object.setPrototypeOf(this, ConcurrencyError.prototype);
|
|
34
|
-
}
|
|
12
|
+
class ConcurrencyError extends EmmettError {
|
|
13
|
+
constructor(current, expected, message) {
|
|
14
|
+
super({
|
|
15
|
+
errorCode: 412,
|
|
16
|
+
message: _nullishCoalesce(message, () => ( `Expected version ${expected.toString()} does not match current ${_optionalChain([current, 'optionalAccess', _ => _.toString, 'call', _2 => _2()])}`))
|
|
17
|
+
});
|
|
18
|
+
this.current = current;
|
|
19
|
+
this.expected = expected;
|
|
20
|
+
Object.setPrototypeOf(this, ConcurrencyError.prototype);
|
|
21
|
+
}
|
|
35
22
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
23
|
+
class ValidationError extends EmmettError {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super({
|
|
26
|
+
errorCode: 400,
|
|
27
|
+
message: _nullishCoalesce(message, () => ( `Validation Error ocurred during Emmett processing`))
|
|
28
|
+
});
|
|
29
|
+
Object.setPrototypeOf(this, ValidationError.prototype);
|
|
30
|
+
}
|
|
45
31
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
32
|
+
class IllegalStateError extends EmmettError {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super({
|
|
35
|
+
errorCode: 403,
|
|
36
|
+
message: _nullishCoalesce(message, () => ( `Illegal State ocurred during Emmett processing`))
|
|
37
|
+
});
|
|
38
|
+
Object.setPrototypeOf(this, IllegalStateError.prototype);
|
|
39
|
+
}
|
|
55
40
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
: `State with ${options.id} was not found during Emmett processing`
|
|
65
|
-
: options?.type
|
|
66
|
-
? `${options.type} was not found during Emmett processing`
|
|
67
|
-
: 'State was not found during Emmett processing'),
|
|
68
|
-
});
|
|
69
|
-
// 👇️ because we are extending a built-in class
|
|
70
|
-
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
71
|
-
}
|
|
41
|
+
class NotFoundError extends EmmettError {
|
|
42
|
+
constructor(options) {
|
|
43
|
+
super({
|
|
44
|
+
errorCode: 404,
|
|
45
|
+
message: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.message]), () => ( (_optionalChain([options, 'optionalAccess', _4 => _4.id]) ? options.type ? `${options.type} with ${options.id} was not found during Emmett processing` : `State with ${options.id} was not found during Emmett processing` : _optionalChain([options, 'optionalAccess', _5 => _5.type]) ? `${options.type} was not found during Emmett processing` : "State was not found during Emmett processing")))
|
|
46
|
+
});
|
|
47
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
48
|
+
}
|
|
72
49
|
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
exports.ConcurrencyError = ConcurrencyError; exports.EmmettError = EmmettError; exports.IllegalStateError = IllegalStateError; exports.NotFoundError = NotFoundError; exports.ValidationError = ValidationError;
|
|
73
57
|
//# sourceMappingURL=index.js.map
|
package/dist/errors/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/errors/index.ts"],"names":[],"mappings":"AAAA,SAAS,UAAU,gBAAgB;AAE5B,MAAM,oBAAoB,MAAM;AAAA,EAC9B;AAAA,EAEP,YACE,SACA;AACA,UAAM,YACJ,WAAW,OAAO,YAAY,YAAY,eAAe,UACrD,QAAQ,YACR,SAAS,OAAO,IACd,UACA;AACR,UAAM,UACJ,WAAW,OAAO,YAAY,YAAY,aAAa,UACnD,QAAQ,UACR,SAAS,OAAO,IACd,UACA,2BAA2B,SAAS;AAE5C,UAAM,OAAO;AACb,SAAK,YAAY;AAGjB,WAAO,eAAe,MAAM,YAAY,SAAS;AAAA,EACnD;AACF;AAEO,MAAM,yBAAyB,YAAY;AAAA,EAChD,YACS,SACA,UACP,SACA;AACA,UAAM;AAAA,MACJ,WAAW;AAAA,MACX,SACE,WACA,oBAAoB,SAAS,SAAS,CAAC,2BAA2B,SAAS,SAAS,CAAC;AAAA,IACzF,CAAC;AATM;AACA;AAWP,WAAO,eAAe,MAAM,iBAAiB,SAAS;AAAA,EACxD;AACF;AAEO,MAAM,wBAAwB,YAAY;AAAA,EAC/C,YAAY,SAAkB;AAC5B,UAAM;AAAA,MACJ,WAAW;AAAA,MACX,SAAS,WAAW;AAAA,IACtB,CAAC;AAGD,WAAO,eAAe,MAAM,gBAAgB,SAAS;AAAA,EACvD;AACF;AAEO,MAAM,0BAA0B,YAAY;AAAA,EACjD,YAAY,SAAkB;AAC5B,UAAM;AAAA,MACJ,WAAW;AAAA,MACX,SAAS,WAAW;AAAA,IACtB,CAAC;AAGD,WAAO,eAAe,MAAM,kBAAkB,SAAS;AAAA,EACzD;AACF;AAEO,MAAM,sBAAsB,YAAY;AAAA,EAC7C,YAAY,SAA0D;AACpE,UAAM;AAAA,MACJ,WAAW;AAAA,MACX,SACE,SAAS,YACR,SAAS,KACN,QAAQ,OACN,GAAG,QAAQ,IAAI,SAAS,QAAQ,EAAE,4CAClC,cAAc,QAAQ,EAAE,4CAC1B,SAAS,OACP,GAAG,QAAQ,IAAI,4CACf;AAAA,IACV,CAAC;AAGD,WAAO,eAAe,MAAM,cAAc,SAAS;AAAA,EACrD;AACF","sourcesContent":["import { isNumber, isString } from '../validation';\n\nexport class EmmettError extends Error {\n public errorCode: number;\n\n constructor(\n options?: { errorCode: number; message?: string } | string | number,\n ) {\n const errorCode =\n options && typeof options === 'object' && 'errorCode' in options\n ? options.errorCode\n : isNumber(options)\n ? options\n : 500;\n const message =\n options && typeof options === 'object' && 'message' in options\n ? options.message\n : isString(options)\n ? options\n : `Error with status code '${errorCode}' ocurred during Emmett processing`;\n\n super(message);\n this.errorCode = errorCode;\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, EmmettError.prototype);\n }\n}\n\nexport class ConcurrencyError extends EmmettError {\n constructor(\n public current: string | undefined,\n public expected: string,\n message?: string,\n ) {\n super({\n errorCode: 412,\n message:\n message ??\n `Expected version ${expected.toString()} does not match current ${current?.toString()}`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, ConcurrencyError.prototype);\n }\n}\n\nexport class ValidationError extends EmmettError {\n constructor(message?: string) {\n super({\n errorCode: 400,\n message: message ?? `Validation Error ocurred during Emmett processing`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, ValidationError.prototype);\n }\n}\n\nexport class IllegalStateError extends EmmettError {\n constructor(message?: string) {\n super({\n errorCode: 403,\n message: message ?? `Illegal State ocurred during Emmett processing`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, IllegalStateError.prototype);\n }\n}\n\nexport class NotFoundError extends EmmettError {\n constructor(options?: { id: string; type: string; message?: string }) {\n super({\n errorCode: 404,\n message:\n options?.message ??\n (options?.id\n ? options.type\n ? `${options.type} with ${options.id} was not found during Emmett processing`\n : `State with ${options.id} was not found during Emmett processing`\n : options?.type\n ? `${options.type} was not found during Emmett processing`\n : 'State was not found during Emmett processing'),\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, NotFoundError.prototype);\n }\n}\n"]}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=eventStore.js.map
|
|
1
|
+
"use strict";//# sourceMappingURL=eventStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -1,26 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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; }var _errors = require('../errors');
|
|
2
|
+
const STREAM_EXISTS = "STREAM_EXISTS";
|
|
3
|
+
const STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
|
|
4
|
+
const NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
|
|
5
|
+
const matchesExpectedVersion = (current, expected) => {
|
|
6
|
+
if (expected === NO_CONCURRENCY_CHECK)
|
|
7
|
+
return true;
|
|
8
|
+
if (expected == STREAM_DOES_NOT_EXIST)
|
|
9
|
+
return current === void 0;
|
|
10
|
+
if (expected == STREAM_EXISTS)
|
|
11
|
+
return current !== void 0;
|
|
12
|
+
return current === expected;
|
|
13
13
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
const assertExpectedVersionMatchesCurrent = (current, expected) => {
|
|
15
|
+
expected ??= NO_CONCURRENCY_CHECK;
|
|
16
|
+
if (!matchesExpectedVersion(current, expected))
|
|
17
|
+
throw new ExpectedVersionConflictError(current, expected);
|
|
18
18
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
19
|
+
class ExpectedVersionConflictError extends _errors.ConcurrencyError {
|
|
20
|
+
constructor(current, expected) {
|
|
21
|
+
super(_optionalChain([current, 'optionalAccess', _ => _.toString, 'call', _2 => _2()]), _optionalChain([expected, 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]));
|
|
22
|
+
Object.setPrototypeOf(this, ExpectedVersionConflictError.prototype);
|
|
23
|
+
}
|
|
25
24
|
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
exports.ExpectedVersionConflictError = ExpectedVersionConflictError; exports.NO_CONCURRENCY_CHECK = NO_CONCURRENCY_CHECK; exports.STREAM_DOES_NOT_EXIST = STREAM_DOES_NOT_EXIST; exports.STREAM_EXISTS = STREAM_EXISTS; exports.assertExpectedVersionMatchesCurrent = assertExpectedVersionMatchesCurrent; exports.matchesExpectedVersion = matchesExpectedVersion;
|
|
26
33
|
//# sourceMappingURL=expectedVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/expectedVersion.ts"],"names":[],"mappings":"AAAA,SAAS,wBAAwB;AAiB1B,MAAM,gBAAgB;AACtB,MAAM,wBACX;AACK,MAAM,uBACX;AAEK,MAAM,yBAAyB,CAGpC,SACA,aACY;AACZ,MAAI,aAAa;AAAsB,WAAO;AAE9C,MAAI,YAAY;AAAuB,WAAO,YAAY;AAE1D,MAAI,YAAY;AAAe,WAAO,YAAY;AAElD,SAAO,YAAY;AACrB;AAEO,MAAM,sCAAsC,CAGjD,SACA,aACS;AACT,eAAa;AAEb,MAAI,CAAC,uBAAuB,SAAS,QAAQ;AAC3C,UAAM,IAAI,6BAA6B,SAAS,QAAQ;AAC5D;AAEO,MAAM,qCAEH,iBAAiB;AAAA,EACzB,YACE,SACA,UACA;AACA,UAAM,SAAS,SAAS,GAAG,UAAU,SAAS,CAAC;AAG/C,WAAO,eAAe,MAAM,6BAA6B,SAAS;AAAA,EACpE;AACF","sourcesContent":["import { ConcurrencyError } from '../errors';\nimport type { Flavour } from '../typing';\nimport type { DefaultStreamVersionType } from './eventStore';\n\nexport type ExpectedStreamVersion<VersionType = DefaultStreamVersionType> =\n | ExpectedStreamVersionWithValue<VersionType>\n | ExpectedStreamVersionGeneral;\n\nexport type ExpectedStreamVersionWithValue<\n VersionType = DefaultStreamVersionType,\n> = Flavour<VersionType, 'StreamVersion'>;\n\nexport type ExpectedStreamVersionGeneral = Flavour<\n 'STREAM_EXISTS' | 'STREAM_DOES_NOT_EXIST' | 'NO_CONCURRENCY_CHECK',\n 'StreamVersion'\n>;\n\nexport const STREAM_EXISTS = 'STREAM_EXISTS' as ExpectedStreamVersionGeneral;\nexport const STREAM_DOES_NOT_EXIST =\n 'STREAM_DOES_NOT_EXIST' as ExpectedStreamVersionGeneral;\nexport const NO_CONCURRENCY_CHECK =\n 'NO_CONCURRENCY_CHECK' as ExpectedStreamVersionGeneral;\n\nexport const matchesExpectedVersion = <\n StreamVersion = DefaultStreamVersionType,\n>(\n current: StreamVersion | undefined,\n expected: ExpectedStreamVersion<StreamVersion>,\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\nexport const assertExpectedVersionMatchesCurrent = <\n StreamVersion = DefaultStreamVersionType,\n>(\n current: StreamVersion | undefined,\n expected: ExpectedStreamVersion<StreamVersion> | undefined,\n): void => {\n expected ??= NO_CONCURRENCY_CHECK;\n\n if (!matchesExpectedVersion(current, expected))\n throw new ExpectedVersionConflictError(current, expected);\n};\n\nexport class ExpectedVersionConflictError<\n VersionType = DefaultStreamVersionType,\n> extends ConcurrencyError {\n constructor(\n current: VersionType | undefined,\n expected: ExpectedStreamVersion<VersionType>,\n ) {\n super(current?.toString(), expected?.toString());\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, ExpectedVersionConflictError.prototype);\n }\n}\n"]}
|
|
@@ -1,67 +1,71 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
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; }var _crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
require('./eventStore');
|
|
4
|
+
var _expectedVersion = require('./expectedVersion');
|
|
5
|
+
const getInMemoryEventStore = () => {
|
|
6
|
+
const streams = /* @__PURE__ */ new Map();
|
|
7
|
+
const getAllEventsCount = () => {
|
|
8
|
+
return Array.from(streams.values()).map((s) => s.length).reduce((p, c) => p + c, 0);
|
|
9
|
+
};
|
|
10
|
+
return {
|
|
11
|
+
async aggregateStream(streamName, options) {
|
|
12
|
+
const { evolve, getInitialState, read } = options;
|
|
13
|
+
const result = await this.readStream(streamName, read);
|
|
14
|
+
if (!result)
|
|
15
|
+
return null;
|
|
16
|
+
const events = _nullishCoalesce(_optionalChain([result, 'optionalAccess', _ => _.events]), () => ( []));
|
|
17
|
+
return {
|
|
18
|
+
currentStreamVersion: BigInt(events.length),
|
|
19
|
+
state: events.reduce(evolve, getInitialState())
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
readStream: (streamName, options) => {
|
|
23
|
+
const events = streams.get(streamName);
|
|
24
|
+
const currentStreamVersion = events ? BigInt(events.length) : void 0;
|
|
25
|
+
_expectedVersion.assertExpectedVersionMatchesCurrent.call(void 0,
|
|
26
|
+
currentStreamVersion,
|
|
27
|
+
_optionalChain([options, 'optionalAccess', _2 => _2.expectedStreamVersion])
|
|
28
|
+
);
|
|
29
|
+
const from = Number(options && "from" in options ? options.from : 0);
|
|
30
|
+
const to = Number(
|
|
31
|
+
options && "to" in options ? options.to : options && "maxCount" in options && options.maxCount ? options.from + options.maxCount : _nullishCoalesce(_optionalChain([events, 'optionalAccess', _3 => _3.length]), () => ( 1))
|
|
32
|
+
);
|
|
33
|
+
const resultEvents = events && events.length > 0 ? events.map((e) => e.event).slice(from, to) : [];
|
|
34
|
+
const result = events && events.length > 0 ? {
|
|
35
|
+
currentStreamVersion,
|
|
36
|
+
events: resultEvents
|
|
37
|
+
} : null;
|
|
38
|
+
return Promise.resolve(result);
|
|
39
|
+
},
|
|
40
|
+
appendToStream: (streamName, events, options) => {
|
|
41
|
+
const currentEvents = _nullishCoalesce(streams.get(streamName), () => ( []));
|
|
42
|
+
const currentStreamVersion = currentEvents.length > 0 ? BigInt(currentEvents.length) : void 0;
|
|
43
|
+
_expectedVersion.assertExpectedVersionMatchesCurrent.call(void 0,
|
|
44
|
+
currentStreamVersion,
|
|
45
|
+
_optionalChain([options, 'optionalAccess', _4 => _4.expectedStreamVersion])
|
|
46
|
+
);
|
|
47
|
+
const eventEnvelopes = events.map((event, index) => {
|
|
48
|
+
return {
|
|
49
|
+
event,
|
|
50
|
+
metadata: {
|
|
51
|
+
eventId: _crypto.randomUUID.call(void 0, ),
|
|
52
|
+
streamPosition: currentEvents.length + index + 1,
|
|
53
|
+
logPosition: BigInt(getAllEventsCount() + index + 1)
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
const positionOfLastEventInTheStream = BigInt(
|
|
58
|
+
eventEnvelopes.slice(-1)[0].metadata.streamPosition
|
|
59
|
+
);
|
|
60
|
+
streams.set(streamName, [...currentEvents, ...eventEnvelopes]);
|
|
61
|
+
const result = {
|
|
62
|
+
nextExpectedStreamVersion: positionOfLastEventInTheStream
|
|
63
|
+
};
|
|
64
|
+
return Promise.resolve(result);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
66
67
|
};
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
exports.getInMemoryEventStore = getInMemoryEventStore;
|
|
67
71
|
//# sourceMappingURL=inMemoryEventStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/inMemoryEventStore.ts"],"names":[],"mappings":"AAAA,SAAS,kBAAkB;AAE3B;AAAA,OAQO;AACP,SAAS,2CAA2C;AAiB7C,MAAM,wBAAwB,MAAkB;AACrD,QAAM,UAAU,oBAAI,IAA6B;AAEjD,QAAM,oBAAoB,MAAM;AAC9B,WAAO,MAAM,KAAsB,QAAQ,OAAO,CAAC,EAChD,IAAI,CAAC,MAAM,EAAE,MAAM,EACnB,OAAO,CAAC,GAAG,MAAM,IAAI,GAAG,CAAC;AAAA,EAC9B;AAEA,SAAO;AAAA,IACL,MAAM,gBACJ,YACA,SAC8C;AAC9C,YAAM,EAAE,QAAQ,iBAAiB,KAAK,IAAI;AAE1C,YAAM,SAAS,MAAM,KAAK,WAAsB,YAAY,IAAI;AAEhE,UAAI,CAAC;AAAQ,eAAO;AAEpB,YAAM,SAAS,QAAQ,UAAU,CAAC;AAElC,aAAO;AAAA,QACL,sBAAsB,OAAO,OAAO,MAAM;AAAA,QAC1C,OAAO,OAAO,OAAO,QAAQ,gBAAgB,CAAC;AAAA,MAChD;AAAA,IACF;AAAA,IAEA,YAAY,CACV,YACA,YACyC;AACzC,YAAM,SAAS,QAAQ,IAAI,UAAU;AACrC,YAAM,uBAAuB,SAAS,OAAO,OAAO,MAAM,IAAI;AAE9D;AAAA,QACE;AAAA,QACA,SAAS;AAAA,MACX;AAEA,YAAM,OAAO,OAAO,WAAW,UAAU,UAAU,QAAQ,OAAO,CAAC;AACnE,YAAM,KAAK;AAAA,QACT,WAAW,QAAQ,UACf,QAAQ,KACR,WAAW,cAAc,WAAW,QAAQ,WAC1C,QAAQ,OAAO,QAAQ,WACvB,QAAQ,UAAU;AAAA,MAC1B;AAEA,YAAM,eACJ,UAAU,OAAO,SAAS,IACtB,OAAO,IAAI,CAAC,MAAM,EAAE,KAAkB,EAAE,MAAM,MAAM,EAAE,IACtD,CAAC;AAEP,YAAM,SACJ,UAAU,OAAO,SAAS,IACtB;AAAA,QACE;AAAA,QACA,QAAQ;AAAA,MACV,IACA;AAEN,aAAO,QAAQ,QAAQ,MAAM;AAAA,IAC/B;AAAA,IAEA,gBAAgB,CACd,YACA,QACA,YACkC;AAClC,YAAM,gBAAgB,QAAQ,IAAI,UAAU,KAAK,CAAC;AAClD,YAAM,uBACJ,cAAc,SAAS,IAAI,OAAO,cAAc,MAAM,IAAI;AAE5D;AAAA,QACE;AAAA,QACA,SAAS;AAAA,MACX;AAEA,YAAM,iBAAkC,OAAO,IAAI,CAAC,OAAO,UAAU;AACnE,eAAO;AAAA,UACL;AAAA,UACA,UAAU;AAAA,YACR,SAAS,WAAW;AAAA,YACpB,gBAAgB,cAAc,SAAS,QAAQ;AAAA,YAC/C,aAAa,OAAO,kBAAkB,IAAI,QAAQ,CAAC;AAAA,UACrD;AAAA,QACF;AAAA,MACF,CAAC;AAED,YAAM,iCAAiC;AAAA,QACrC,eAAe,MAAM,EAAE,EAAE,CAAC,EAAG,SAAS;AAAA,MACxC;AAEA,cAAQ,IAAI,YAAY,CAAC,GAAG,eAAe,GAAG,cAAc,CAAC;AAE7D,YAAM,SAA+B;AAAA,QACnC,2BAA2B;AAAA,MAC7B;AAEA,aAAO,QAAQ,QAAQ,MAAM;AAAA,IAC/B;AAAA,EACF;AACF","sourcesContent":["import { randomUUID } from 'crypto';\nimport type { Event } from '../typing';\nimport {\n type AggregateStreamOptions,\n type AggregateStreamResult,\n type AppendToStreamOptions,\n type AppendToStreamResult,\n type EventStore,\n type ReadStreamOptions,\n type ReadStreamResult,\n} from './eventStore';\nimport { assertExpectedVersionMatchesCurrent } from './expectedVersion';\n\nexport type EventMetadata = Readonly<{\n eventId: string;\n streamPosition: number;\n logPosition: bigint;\n}>;\n\nexport type EventEnvelope<E extends Event = Event> = {\n event: E;\n metadata: EventMetadata;\n};\n\nexport type EventHandler<E extends Event = Event> = (\n eventEnvelope: EventEnvelope<E>,\n) => void;\n\nexport const getInMemoryEventStore = (): EventStore => {\n const streams = new Map<string, EventEnvelope[]>();\n\n const getAllEventsCount = () => {\n return Array.from<EventEnvelope[]>(streams.values())\n .map((s) => s.length)\n .reduce((p, c) => p + c, 0);\n };\n\n return {\n async aggregateStream<State, EventType extends Event>(\n streamName: string,\n options: AggregateStreamOptions<State, EventType>,\n ): Promise<AggregateStreamResult<State> | null> {\n const { evolve, getInitialState, read } = options;\n\n const result = await this.readStream<EventType>(streamName, read);\n\n if (!result) return null;\n\n const events = result?.events ?? [];\n\n return {\n currentStreamVersion: BigInt(events.length),\n state: events.reduce(evolve, getInitialState()),\n };\n },\n\n readStream: <EventType extends Event>(\n streamName: string,\n options?: ReadStreamOptions,\n ): Promise<ReadStreamResult<EventType>> => {\n const events = streams.get(streamName);\n const currentStreamVersion = events ? BigInt(events.length) : undefined;\n\n assertExpectedVersionMatchesCurrent(\n currentStreamVersion,\n options?.expectedStreamVersion,\n );\n\n const from = Number(options && 'from' in options ? options.from : 0);\n const to = Number(\n options && 'to' in options\n ? options.to\n : options && 'maxCount' in options && options.maxCount\n ? options.from + options.maxCount\n : events?.length ?? 1,\n );\n\n const resultEvents =\n events && events.length > 0\n ? events.map((e) => e.event as EventType).slice(from, to)\n : [];\n\n const result: ReadStreamResult<EventType> =\n events && events.length > 0\n ? {\n currentStreamVersion: currentStreamVersion!,\n events: resultEvents,\n }\n : null;\n\n return Promise.resolve(result);\n },\n\n appendToStream: <EventType extends Event>(\n streamName: string,\n events: EventType[],\n options?: AppendToStreamOptions,\n ): Promise<AppendToStreamResult> => {\n const currentEvents = streams.get(streamName) ?? [];\n const currentStreamVersion =\n currentEvents.length > 0 ? BigInt(currentEvents.length) : undefined;\n\n assertExpectedVersionMatchesCurrent(\n currentStreamVersion,\n options?.expectedStreamVersion,\n );\n\n const eventEnvelopes: EventEnvelope[] = events.map((event, index) => {\n return {\n event,\n metadata: {\n eventId: randomUUID(),\n streamPosition: currentEvents.length + index + 1,\n logPosition: BigInt(getAllEventsCount() + index + 1),\n },\n };\n });\n\n const positionOfLastEventInTheStream = BigInt(\n eventEnvelopes.slice(-1)[0]!.metadata.streamPosition,\n );\n\n streams.set(streamName, [...currentEvents, ...eventEnvelopes]);\n\n const result: AppendToStreamResult = {\n nextExpectedStreamVersion: positionOfLastEventInTheStream,\n };\n\n return Promise.resolve(result);\n },\n };\n};\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
2
|
import {
|
|
3
3
|
} from "./eventStore";
|
|
4
4
|
import { assertExpectedVersionMatchesCurrent } from "./expectedVersion";
|
|
@@ -48,7 +48,7 @@ const getInMemoryEventStore = () => {
|
|
|
48
48
|
return {
|
|
49
49
|
event,
|
|
50
50
|
metadata: {
|
|
51
|
-
eventId:
|
|
51
|
+
eventId: randomUUID(),
|
|
52
52
|
streamPosition: currentEvents.length + index + 1,
|
|
53
53
|
logPosition: BigInt(getAllEventsCount() + index + 1)
|
|
54
54
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/eventStore/inMemoryEventStore.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../src/eventStore/inMemoryEventStore.ts"],"sourcesContent":["import { randomUUID } from 'crypto';\nimport type { Event } from '../typing';\nimport {\n type AggregateStreamOptions,\n type AggregateStreamResult,\n type AppendToStreamOptions,\n type AppendToStreamResult,\n type EventStore,\n type ReadStreamOptions,\n type ReadStreamResult,\n} from './eventStore';\nimport { assertExpectedVersionMatchesCurrent } from './expectedVersion';\n\nexport type EventMetadata = Readonly<{\n eventId: string;\n streamPosition: number;\n logPosition: bigint;\n}>;\n\nexport type EventEnvelope<E extends Event = Event> = {\n event: E;\n metadata: EventMetadata;\n};\n\nexport type EventHandler<E extends Event = Event> = (\n eventEnvelope: EventEnvelope<E>,\n) => void;\n\nexport const getInMemoryEventStore = (): EventStore => {\n const streams = new Map<string, EventEnvelope[]>();\n\n const getAllEventsCount = () => {\n return Array.from<EventEnvelope[]>(streams.values())\n .map((s) => s.length)\n .reduce((p, c) => p + c, 0);\n };\n\n return {\n async aggregateStream<State, EventType extends Event>(\n streamName: string,\n options: AggregateStreamOptions<State, EventType>,\n ): Promise<AggregateStreamResult<State> | null> {\n const { evolve, getInitialState, read } = options;\n\n const result = await this.readStream<EventType>(streamName, read);\n\n if (!result) return null;\n\n const events = result?.events ?? [];\n\n return {\n currentStreamVersion: BigInt(events.length),\n state: events.reduce(evolve, getInitialState()),\n };\n },\n\n readStream: <EventType extends Event>(\n streamName: string,\n options?: ReadStreamOptions,\n ): Promise<ReadStreamResult<EventType>> => {\n const events = streams.get(streamName);\n const currentStreamVersion = events ? BigInt(events.length) : undefined;\n\n assertExpectedVersionMatchesCurrent(\n currentStreamVersion,\n options?.expectedStreamVersion,\n );\n\n const from = Number(options && 'from' in options ? options.from : 0);\n const to = Number(\n options && 'to' in options\n ? options.to\n : options && 'maxCount' in options && options.maxCount\n ? options.from + options.maxCount\n : events?.length ?? 1,\n );\n\n const resultEvents =\n events && events.length > 0\n ? events.map((e) => e.event as EventType).slice(from, to)\n : [];\n\n const result: ReadStreamResult<EventType> =\n events && events.length > 0\n ? {\n currentStreamVersion: currentStreamVersion!,\n events: resultEvents,\n }\n : null;\n\n return Promise.resolve(result);\n },\n\n appendToStream: <EventType extends Event>(\n streamName: string,\n events: EventType[],\n options?: AppendToStreamOptions,\n ): Promise<AppendToStreamResult> => {\n const currentEvents = streams.get(streamName) ?? [];\n const currentStreamVersion =\n currentEvents.length > 0 ? BigInt(currentEvents.length) : undefined;\n\n assertExpectedVersionMatchesCurrent(\n currentStreamVersion,\n options?.expectedStreamVersion,\n );\n\n const eventEnvelopes: EventEnvelope[] = events.map((event, index) => {\n return {\n event,\n metadata: {\n eventId: randomUUID(),\n streamPosition: currentEvents.length + index + 1,\n logPosition: BigInt(getAllEventsCount() + index + 1),\n },\n };\n });\n\n const positionOfLastEventInTheStream = BigInt(\n eventEnvelopes.slice(-1)[0]!.metadata.streamPosition,\n );\n\n streams.set(streamName, [...currentEvents, ...eventEnvelopes]);\n\n const result: AppendToStreamResult = {\n nextExpectedStreamVersion: positionOfLastEventInTheStream,\n };\n\n return Promise.resolve(result);\n },\n };\n};\n"],"mappings":"AAAA,SAAS,kBAAkB;AAE3B;AAAA,OAQO;AACP,SAAS,2CAA2C;AAiB7C,MAAM,wBAAwB,MAAkB;AACrD,QAAM,UAAU,oBAAI,IAA6B;AAEjD,QAAM,oBAAoB,MAAM;AAC9B,WAAO,MAAM,KAAsB,QAAQ,OAAO,CAAC,EAChD,IAAI,CAAC,MAAM,EAAE,MAAM,EACnB,OAAO,CAAC,GAAG,MAAM,IAAI,GAAG,CAAC;AAAA,EAC9B;AAEA,SAAO;AAAA,IACL,MAAM,gBACJ,YACA,SAC8C;AAC9C,YAAM,EAAE,QAAQ,iBAAiB,KAAK,IAAI;AAE1C,YAAM,SAAS,MAAM,KAAK,WAAsB,YAAY,IAAI;AAEhE,UAAI,CAAC;AAAQ,eAAO;AAEpB,YAAM,SAAS,QAAQ,UAAU,CAAC;AAElC,aAAO;AAAA,QACL,sBAAsB,OAAO,OAAO,MAAM;AAAA,QAC1C,OAAO,OAAO,OAAO,QAAQ,gBAAgB,CAAC;AAAA,MAChD;AAAA,IACF;AAAA,IAEA,YAAY,CACV,YACA,YACyC;AACzC,YAAM,SAAS,QAAQ,IAAI,UAAU;AACrC,YAAM,uBAAuB,SAAS,OAAO,OAAO,MAAM,IAAI;AAE9D;AAAA,QACE;AAAA,QACA,SAAS;AAAA,MACX;AAEA,YAAM,OAAO,OAAO,WAAW,UAAU,UAAU,QAAQ,OAAO,CAAC;AACnE,YAAM,KAAK;AAAA,QACT,WAAW,QAAQ,UACf,QAAQ,KACR,WAAW,cAAc,WAAW,QAAQ,WAC1C,QAAQ,OAAO,QAAQ,WACvB,QAAQ,UAAU;AAAA,MAC1B;AAEA,YAAM,eACJ,UAAU,OAAO,SAAS,IACtB,OAAO,IAAI,CAAC,MAAM,EAAE,KAAkB,EAAE,MAAM,MAAM,EAAE,IACtD,CAAC;AAEP,YAAM,SACJ,UAAU,OAAO,SAAS,IACtB;AAAA,QACE;AAAA,QACA,QAAQ;AAAA,MACV,IACA;AAEN,aAAO,QAAQ,QAAQ,MAAM;AAAA,IAC/B;AAAA,IAEA,gBAAgB,CACd,YACA,QACA,YACkC;AAClC,YAAM,gBAAgB,QAAQ,IAAI,UAAU,KAAK,CAAC;AAClD,YAAM,uBACJ,cAAc,SAAS,IAAI,OAAO,cAAc,MAAM,IAAI;AAE5D;AAAA,QACE;AAAA,QACA,SAAS;AAAA,MACX;AAEA,YAAM,iBAAkC,OAAO,IAAI,CAAC,OAAO,UAAU;AACnE,eAAO;AAAA,UACL;AAAA,UACA,UAAU;AAAA,YACR,SAAS,WAAW;AAAA,YACpB,gBAAgB,cAAc,SAAS,QAAQ;AAAA,YAC/C,aAAa,OAAO,kBAAkB,IAAI,QAAQ,CAAC;AAAA,UACrD;AAAA,QACF;AAAA,MACF,CAAC;AAED,YAAM,iCAAiC;AAAA,QACrC,eAAe,MAAM,EAAE,EAAE,CAAC,EAAG,SAAS;AAAA,MACxC;AAEA,cAAQ,IAAI,YAAY,CAAC,GAAG,eAAe,GAAG,cAAc,CAAC;AAE7D,YAAM,SAA+B;AAAA,QACnC,2BAA2B;AAAA,MAC7B;AAEA,aAAO,QAAQ,QAAQ,MAAM;AAAA,IAC/B;AAAA,EACF;AACF;","names":[]}
|
package/dist/eventStore/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
var _expectedVersion = require('./expectedVersion'); _createStarExport(_expectedVersion);
|
|
3
|
+
var _inMemoryEventStore = require('./inMemoryEventStore'); _createStarExport(_inMemoryEventStore);
|
|
4
4
|
//# sourceMappingURL=index.js.map
|