@robinmalfait/event-source 0.0.3 → 0.0.4
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/index.d.mts +18 -18
- package/dist/index.d.ts +18 -18
- package/dist/index.js +9 -9
- package/dist/index.mjs +10 -10
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
interface EventType<T,
|
|
1
|
+
interface EventType<T extends string, P> {
|
|
2
2
|
aggregateId: string;
|
|
3
3
|
eventId: string;
|
|
4
|
-
eventName:
|
|
5
|
-
payload:
|
|
4
|
+
eventName: T;
|
|
5
|
+
payload: P;
|
|
6
6
|
recordedAt: Date;
|
|
7
7
|
version: number;
|
|
8
8
|
}
|
|
9
|
-
declare function Event<T,
|
|
9
|
+
declare function Event<const T extends string, P = null>(eventName: T, aggregateId: string, payload?: P): EventType<T, P>;
|
|
10
10
|
|
|
11
11
|
declare class Aggregate {
|
|
12
12
|
private version;
|
|
13
13
|
private recordedEvents;
|
|
14
|
-
replayEvents<T>(events?: EventType<T, any>[]): this;
|
|
14
|
+
replayEvents<T extends string>(events?: EventType<T, any>[]): this;
|
|
15
15
|
private applyAnEvent;
|
|
16
|
-
protected recordThat<T>(event: EventType<T, any>): this;
|
|
17
|
-
releaseEvents(): EventType<
|
|
16
|
+
protected recordThat<const T extends string>(event: EventType<T, any>): this;
|
|
17
|
+
releaseEvents(): EventType<string, any>[];
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
interface CommandType<T> {
|
|
21
21
|
type: string;
|
|
22
22
|
payload: T;
|
|
23
23
|
}
|
|
24
|
-
declare function Command<T>(type:
|
|
24
|
+
declare function Command<const T extends string, P>(type: T, payload: P): CommandType<P>;
|
|
25
25
|
|
|
26
26
|
type PayloadOf<T extends (...args: any) => {
|
|
27
27
|
payload: unknown;
|
|
@@ -36,9 +36,9 @@ declare function objectToYaml<T extends Object>(object: T): string;
|
|
|
36
36
|
|
|
37
37
|
type MaybePromise<T> = T | Promise<T>;
|
|
38
38
|
interface EventStore {
|
|
39
|
-
persist<T>(events: EventType<T, unknown>[]): MaybePromise<void>;
|
|
40
|
-
load<T>(aggregateId: string): MaybePromise<EventType<T, string>[]>;
|
|
41
|
-
loadEvents<T>(): MaybePromise<EventType<T, string>[]>;
|
|
39
|
+
persist<const T extends string>(events: EventType<T, unknown>[]): MaybePromise<void>;
|
|
40
|
+
load<const T extends string>(aggregateId: string): MaybePromise<EventType<T, string>[]>;
|
|
41
|
+
loadEvents<const T extends string>(): MaybePromise<EventType<T, string>[]>;
|
|
42
42
|
}
|
|
43
43
|
interface EventSourceConfig<Handlers = any> {
|
|
44
44
|
store: EventStore | Promise<EventStore>;
|
|
@@ -47,12 +47,12 @@ interface EventSourceConfig<Handlers = any> {
|
|
|
47
47
|
eventHandlers?: EventHandler[];
|
|
48
48
|
}
|
|
49
49
|
interface EventHandler {
|
|
50
|
-
(event: EventType<
|
|
50
|
+
(event: EventType<string, string>, es: EventSource): MaybePromise<unknown>;
|
|
51
51
|
}
|
|
52
|
-
interface Projector<
|
|
52
|
+
interface Projector<T extends string = any, P = any> {
|
|
53
53
|
name: string;
|
|
54
54
|
init: (es: EventSource) => MaybePromise<void>;
|
|
55
|
-
update: (event: EventType<
|
|
55
|
+
update: (event: EventType<T, P>) => MaybePromise<unknown>;
|
|
56
56
|
}
|
|
57
57
|
interface CommandHandler<T> {
|
|
58
58
|
(command: CommandType<T>, es: EventSource): MaybePromise<void>;
|
|
@@ -61,7 +61,7 @@ type EventSource = ReturnType<typeof createEventSource>;
|
|
|
61
61
|
declare function createEventSource(config: EventSourceConfig): {
|
|
62
62
|
resetProjections(): Promise<void>;
|
|
63
63
|
dispatch<T>(command: CommandType<T>): Promise<CommandType<T>>;
|
|
64
|
-
loadEvents<T_1>(): Promise<EventType<T_1, string>[]>;
|
|
64
|
+
loadEvents<const T_1 extends string>(): Promise<EventType<T_1, string>[]>;
|
|
65
65
|
load<T_2 extends Aggregate>(aggregate: T_2, aggregateId: string): Promise<T_2>;
|
|
66
66
|
persist(aggregate: Aggregate): Promise<void>;
|
|
67
67
|
loadPersist<T_3 extends Aggregate>(aggregate: T_3, aggregateId: string, handle: (aggregate: T_3) => Promise<void> | void): Promise<void>;
|
|
@@ -70,14 +70,14 @@ declare function createEventSource(config: EventSourceConfig): {
|
|
|
70
70
|
type EventMapper$1 = Record<string, (event: EventType<any, any>, es: EventSource) => void>;
|
|
71
71
|
declare function createEventMapper(mapper: EventMapper$1): (event: EventType<any, any>, es: EventSource) => void;
|
|
72
72
|
|
|
73
|
-
type EventMapper<Ctx,
|
|
74
|
-
declare function createProjector<Ctx,
|
|
73
|
+
type EventMapper<Ctx, T extends string, P> = Record<string, (event: EventType<T, P>, ctx?: Ctx) => void>;
|
|
74
|
+
declare function createProjector<Ctx, T extends string, P>(name: string, mapper: EventMapper<Ctx, T, P>, initializer?: (ctx?: Ctx) => Promise<void> | void, createContext?: () => Ctx): Projector;
|
|
75
75
|
|
|
76
76
|
declare function createTestEventStore(commandHandlers: Record<string, CommandHandler<any>>, projectors?: Projector[]): {
|
|
77
77
|
___: any;
|
|
78
78
|
given(events?: EventType<any, any>[]): Promise<void>;
|
|
79
79
|
when<T>(command: CommandType<T> | (() => CommandType<T>)): Promise<CommandType<T>>;
|
|
80
|
-
then<T_1>(expectation: Error | EventType<T_1, any>[]): Promise<void>;
|
|
80
|
+
then<const T_1 extends string>(expectation: Error | EventType<T_1, any>[]): Promise<void>;
|
|
81
81
|
};
|
|
82
82
|
|
|
83
83
|
export { Aggregate, Command, type CommandHandler, type CommandType, Event, type EventHandler, type EventSource, type EventSourceConfig, type EventStore, type EventType, type PayloadOf, type Projector, type TypeOf, abort, createEventMapper, createEventSource, createProjector, createTestEventStore, objectToYaml };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
interface EventType<T,
|
|
1
|
+
interface EventType<T extends string, P> {
|
|
2
2
|
aggregateId: string;
|
|
3
3
|
eventId: string;
|
|
4
|
-
eventName:
|
|
5
|
-
payload:
|
|
4
|
+
eventName: T;
|
|
5
|
+
payload: P;
|
|
6
6
|
recordedAt: Date;
|
|
7
7
|
version: number;
|
|
8
8
|
}
|
|
9
|
-
declare function Event<T,
|
|
9
|
+
declare function Event<const T extends string, P = null>(eventName: T, aggregateId: string, payload?: P): EventType<T, P>;
|
|
10
10
|
|
|
11
11
|
declare class Aggregate {
|
|
12
12
|
private version;
|
|
13
13
|
private recordedEvents;
|
|
14
|
-
replayEvents<T>(events?: EventType<T, any>[]): this;
|
|
14
|
+
replayEvents<T extends string>(events?: EventType<T, any>[]): this;
|
|
15
15
|
private applyAnEvent;
|
|
16
|
-
protected recordThat<T>(event: EventType<T, any>): this;
|
|
17
|
-
releaseEvents(): EventType<
|
|
16
|
+
protected recordThat<const T extends string>(event: EventType<T, any>): this;
|
|
17
|
+
releaseEvents(): EventType<string, any>[];
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
interface CommandType<T> {
|
|
21
21
|
type: string;
|
|
22
22
|
payload: T;
|
|
23
23
|
}
|
|
24
|
-
declare function Command<T>(type:
|
|
24
|
+
declare function Command<const T extends string, P>(type: T, payload: P): CommandType<P>;
|
|
25
25
|
|
|
26
26
|
type PayloadOf<T extends (...args: any) => {
|
|
27
27
|
payload: unknown;
|
|
@@ -36,9 +36,9 @@ declare function objectToYaml<T extends Object>(object: T): string;
|
|
|
36
36
|
|
|
37
37
|
type MaybePromise<T> = T | Promise<T>;
|
|
38
38
|
interface EventStore {
|
|
39
|
-
persist<T>(events: EventType<T, unknown>[]): MaybePromise<void>;
|
|
40
|
-
load<T>(aggregateId: string): MaybePromise<EventType<T, string>[]>;
|
|
41
|
-
loadEvents<T>(): MaybePromise<EventType<T, string>[]>;
|
|
39
|
+
persist<const T extends string>(events: EventType<T, unknown>[]): MaybePromise<void>;
|
|
40
|
+
load<const T extends string>(aggregateId: string): MaybePromise<EventType<T, string>[]>;
|
|
41
|
+
loadEvents<const T extends string>(): MaybePromise<EventType<T, string>[]>;
|
|
42
42
|
}
|
|
43
43
|
interface EventSourceConfig<Handlers = any> {
|
|
44
44
|
store: EventStore | Promise<EventStore>;
|
|
@@ -47,12 +47,12 @@ interface EventSourceConfig<Handlers = any> {
|
|
|
47
47
|
eventHandlers?: EventHandler[];
|
|
48
48
|
}
|
|
49
49
|
interface EventHandler {
|
|
50
|
-
(event: EventType<
|
|
50
|
+
(event: EventType<string, string>, es: EventSource): MaybePromise<unknown>;
|
|
51
51
|
}
|
|
52
|
-
interface Projector<
|
|
52
|
+
interface Projector<T extends string = any, P = any> {
|
|
53
53
|
name: string;
|
|
54
54
|
init: (es: EventSource) => MaybePromise<void>;
|
|
55
|
-
update: (event: EventType<
|
|
55
|
+
update: (event: EventType<T, P>) => MaybePromise<unknown>;
|
|
56
56
|
}
|
|
57
57
|
interface CommandHandler<T> {
|
|
58
58
|
(command: CommandType<T>, es: EventSource): MaybePromise<void>;
|
|
@@ -61,7 +61,7 @@ type EventSource = ReturnType<typeof createEventSource>;
|
|
|
61
61
|
declare function createEventSource(config: EventSourceConfig): {
|
|
62
62
|
resetProjections(): Promise<void>;
|
|
63
63
|
dispatch<T>(command: CommandType<T>): Promise<CommandType<T>>;
|
|
64
|
-
loadEvents<T_1>(): Promise<EventType<T_1, string>[]>;
|
|
64
|
+
loadEvents<const T_1 extends string>(): Promise<EventType<T_1, string>[]>;
|
|
65
65
|
load<T_2 extends Aggregate>(aggregate: T_2, aggregateId: string): Promise<T_2>;
|
|
66
66
|
persist(aggregate: Aggregate): Promise<void>;
|
|
67
67
|
loadPersist<T_3 extends Aggregate>(aggregate: T_3, aggregateId: string, handle: (aggregate: T_3) => Promise<void> | void): Promise<void>;
|
|
@@ -70,14 +70,14 @@ declare function createEventSource(config: EventSourceConfig): {
|
|
|
70
70
|
type EventMapper$1 = Record<string, (event: EventType<any, any>, es: EventSource) => void>;
|
|
71
71
|
declare function createEventMapper(mapper: EventMapper$1): (event: EventType<any, any>, es: EventSource) => void;
|
|
72
72
|
|
|
73
|
-
type EventMapper<Ctx,
|
|
74
|
-
declare function createProjector<Ctx,
|
|
73
|
+
type EventMapper<Ctx, T extends string, P> = Record<string, (event: EventType<T, P>, ctx?: Ctx) => void>;
|
|
74
|
+
declare function createProjector<Ctx, T extends string, P>(name: string, mapper: EventMapper<Ctx, T, P>, initializer?: (ctx?: Ctx) => Promise<void> | void, createContext?: () => Ctx): Projector;
|
|
75
75
|
|
|
76
76
|
declare function createTestEventStore(commandHandlers: Record<string, CommandHandler<any>>, projectors?: Projector[]): {
|
|
77
77
|
___: any;
|
|
78
78
|
given(events?: EventType<any, any>[]): Promise<void>;
|
|
79
79
|
when<T>(command: CommandType<T> | (() => CommandType<T>)): Promise<CommandType<T>>;
|
|
80
|
-
then<T_1>(expectation: Error | EventType<T_1, any>[]): Promise<void>;
|
|
80
|
+
then<const T_1 extends string>(expectation: Error | EventType<T_1, any>[]): Promise<void>;
|
|
81
81
|
};
|
|
82
82
|
|
|
83
83
|
export { Aggregate, Command, type CommandHandler, type CommandType, Event, type EventHandler, type EventSource, type EventSourceConfig, type EventStore, type EventType, type PayloadOf, type Projector, type TypeOf, abort, createEventMapper, createEventSource, createProjector, createTestEventStore, objectToYaml };
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
"use strict";var $=Object.create;var f=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var q=(t,e)=>{for(var r in e)f(t,r,{get:e[r],enumerable:!0})},A=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of R(e))!Q.call(t,o)&&o!==r&&f(t,o,{get:()=>e[o],enumerable:!(s=O(e,o))||s.enumerable});return t};var F=(t,e,r)=>(r=t!=null?$(L(t)):{},A(e||!t||!t.__esModule?f(r,"default",{value:t,enumerable:!0}):r,t)),V=t=>A(f({},"__esModule",{value:!0}),t);var B={};q(B,{Aggregate:()=>S,Command:()=>Y,Event:()=>J,abort:()=>
|
|
1
|
+
"use strict";var $=Object.create;var f=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty;var q=(t,e)=>{for(var r in e)f(t,r,{get:e[r],enumerable:!0})},A=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of R(e))!Q.call(t,o)&&o!==r&&f(t,o,{get:()=>e[o],enumerable:!(s=O(e,o))||s.enumerable});return t};var F=(t,e,r)=>(r=t!=null?$(L(t)):{},A(e||!t||!t.__esModule?f(r,"default",{value:t,enumerable:!0}):r,t)),V=t=>A(f({},"__esModule",{value:!0}),t);var B={};q(B,{Aggregate:()=>S,Command:()=>Y,Event:()=>J,abort:()=>d,createEventMapper:()=>_,createEventSource:()=>j,createProjector:()=>D,createTestEventStore:()=>Z,objectToYaml:()=>E});module.exports=V(B);function w(t,e){let r=Object.assign(new Error(t),e);return r.stack,Error.captureStackTrace&&Error.captureStackTrace(r,w),r}function d(t,e){let r=w(t,e);throw Error.captureStackTrace&&Error.captureStackTrace(r,d),r}function b(t){Object.freeze(t);for(let e of Object.getOwnPropertyNames(t))t.hasOwnProperty(e)&&t[e]!==null&&(typeof t[e]=="object"||typeof t[e]=="function")&&!Object.isFrozen(t[e])&&b(t[e]);return t}var z={NODE_ENV:process.env.NODE_ENV},S=class{version=0;recordedEvents=[];replayEvents(e=[]){for(let r of e)this.applyAnEvent(r);return this}applyAnEvent(e){z.NODE_ENV==="test"&&b(e),this[e.eventName]===void 0&&(e.eventName.match(/^[$A-Z_][0-9A-Z_$]*$/i)?d(`Aggregate "${this.constructor.name}" has no method:
|
|
2
2
|
|
|
3
3
|
${e.eventName}(event) {
|
|
4
4
|
// Code goes here...
|
|
5
|
-
}`):
|
|
5
|
+
}`):d(`Aggregate "${this.constructor.name}" has no method:
|
|
6
6
|
|
|
7
7
|
['${e.eventName}'](event) {
|
|
8
8
|
// Code goes here...
|
|
9
9
|
}`));try{this[e.eventName](e)}catch(r){r instanceof Error&&console.error(`An error occurred inside your "%s" function:
|
|
10
10
|
`,e.eventName,r.stack?.split(`
|
|
11
11
|
`).map(s=>` ${s}`).join(`
|
|
12
|
-
`))}finally{this.version++}return this}recordThat(e){let r={...e,version:this.version};return this.applyAnEvent(r),this.recordedEvents.push(r),this}releaseEvents(){return this.recordedEvents.splice(0)}};function Y(t,e){return{type:t,payload:e}}var I=require("crypto");function J(t,e,r){return{aggregateId:e,eventId:(0,I.randomUUID)(),eventName:t,payload:r,recordedAt:new Date,version:-1}}var H=F(require("yamlify-object")),U={indent:" ",colors:{date:m,error:m,symbol:m,string:m,number:m,boolean:m,null:m,undefined:m}};function m(t){return t}function E(t){return t instanceof Error?E({...t}):(0,H.default)(t,U).split(`
|
|
12
|
+
`))}finally{this.version++}return this}recordThat(e){let r={...e,version:this.version};return this.applyAnEvent(r),this.recordedEvents.push(r),this}releaseEvents(){return this.recordedEvents.splice(0)}};function Y(t,e){return{type:t,payload:e}}var I=require("crypto");function J(t,e,r=null){return{aggregateId:e,eventId:(0,I.randomUUID)(),eventName:t,payload:r,recordedAt:new Date,version:-1}}var H=F(require("yamlify-object")),U={indent:" ",colors:{date:m,error:m,symbol:m,string:m,number:m,boolean:m,null:m,undefined:m}};function m(t){return t}function E(t){return t instanceof Error?E({...t}):(0,H.default)(t,U).split(`
|
|
13
13
|
`).slice(1).join(`
|
|
14
|
-
`)}function
|
|
15
|
-
`).map(
|
|
16
|
-
`)),
|
|
17
|
-
`).map(
|
|
18
|
-
`)),
|
|
14
|
+
`)}function j(t){let{commandHandlers:e,eventHandlers:r=[],projectors:s=[],store:o}=t,l=[],a={async resetProjections(){await Promise.all(l),await Promise.all(s.map(n=>n.init(a)))},async dispatch(n){e[n.type]===void 0&&d(`There is no command handler for the "${n.type}" command`);let i=e[n.type];return await i(n,a),n},async loadEvents(){return await(await o).loadEvents()},async load(n,i){let v=await(await o).load(i);return v.length<=0&&d(`Aggregate(${n.constructor.name}) with ID(${i}) does not exist.`,{aggregate:n.constructor.name,aggregateId:i}),n.replayEvents(v)},async persist(n){await Promise.all(l);let i=await o,c=n.releaseEvents();await i.persist(c);for(let v of c)await Promise.all(s.map(async T=>{try{await T.update(v)}catch(p){throw p instanceof Error&&console.error(`An error occurred in one of your projections: ${T.name}, given an event`,p.stack?.split(`
|
|
15
|
+
`).map(u=>` ${u}`).join(`
|
|
16
|
+
`)),p}})),await Promise.all(r.map(async T=>{try{await T(v,a)}catch(p){throw p instanceof Error&&console.error(`An error occurred in one of your event handlers: ${T.name}, given an event`,p.stack?.split(`
|
|
17
|
+
`).map(u=>` ${u}`).join(`
|
|
18
|
+
`)),p}}))},async loadPersist(n,i,c){return await this.load(n,i),await c(n),this.persist(n)}};for(let n of s)l.push(n.init(a));return a}function _(t){return(e,r)=>t[e.eventName]?.(e,r)}var h=new WeakMap;function g(t,e){if(h.has(t)){let r=h.get(t);for(let s in e)r[s]=e[s]}else h.set(t,e)}function x(t){return h.get(t)}var P=class{constructor(){g(this,{jobs:[],state:0})}get length(){return x(this).jobs.length}async start(){let{state:e,jobs:r}=x(this);if(!(e===1||r.length<=0)){for(g(this,{state:1});r.length>0;){let s=r.shift();await Promise.resolve().then(s.handle).then(s.resolve,s.reject)}g(this,{state:0})}}push(e){return new Promise((r,s)=>{let{jobs:o}=x(this);o.push({handle:e,resolve:r,reject:s}),setImmediate(()=>this.start())})}};function W(){}function D(t,e,r=W,s){let o=new P,l=s?.();return{name:t,async init(a){await o.push(()=>r(l));let n=await a.loadEvents();await Promise.all(n.map(i=>o.push(()=>e[i.eventName]?.(i,l))))},update(a){return o.push(()=>e[a.eventName]?.(a,l))}}}var C=Symbol("__placeholder__"),y={usedTestEventStoreInTest:!1,calledThenHandler:!1};process.env.NODE_ENV==="test"&&(beforeEach(()=>{y.usedTestEventStoreInTest=!1,y.calledThenHandler=!1}),afterEach(()=>{y.usedTestEventStoreInTest&&!y.calledThenHandler&&d("It seems like you used `createTestEventStore()`\nwithout using the `await then([expected, events, go, here, ...])`")}));function M(t,e){try{return t()}catch(r){throw Error.captureStackTrace&&r instanceof Error&&Error.captureStackTrace(r,e),r}}var k=class{constructor(e=[],r=[]){this.db=e;this.producedEvents=r}name="test-recording-projector";init(){this.db.splice(0)}update(e){this.producedEvents.push(e)}};function Z(t,e=[]){y.usedTestEventStoreInTest=!0;let r=new k,s=j({store:{async load(a){return r.db.filter(n=>n.aggregateId===a)},loadEvents(){return r.db},persist(a){r.db.push(...a)}},commandHandlers:t,projectors:[...e,r]}),o,l={___:C,async given(a=[]){r.db.push(...a)},async when(a){try{return await s.dispatch(typeof a=="function"?a():a)}catch(n){return n instanceof Error&&(o=n),n}},async then(a){if(y.calledThenHandler=!0,a instanceof Error){let i=a;M(()=>expect(o).toEqual(i),l.then);return}let n=a;if(o)throw Object.keys(o).length>0&&(o.message=["With properties:",`
|
|
19
19
|
${E(o)}
|
|
20
20
|
|
|
21
21
|
---
|
|
22
22
|
|
|
23
23
|
`].join(`
|
|
24
|
-
`)),o;M(()=>{expect(n).toHaveLength(r.producedEvents.length);for(let[i,
|
|
24
|
+
`)),o;M(()=>{expect(n).toHaveLength(r.producedEvents.length);for(let[i,c]of n.entries()){let{aggregateId:v,eventName:T,payload:p}=r.producedEvents[i];if(c.aggregateId===C)throw new Error("Expected an `aggregateId`, but got `___` instead.");expect(c.aggregateId).toEqual(v),expect(c.eventName).toEqual(T),(c.payload===null||c.payload===void 0)&&expect(c.payload).toEqual(p);for(let u in c.payload){let N=c.payload[u];N===C?(expect(p).toHaveProperty(u),expect(p[u]).toBeDefined()):expect(p[u]).toEqual(N)}}},l.then)}};return l}0&&(module.exports={Aggregate,Command,Event,abort,createEventMapper,createEventSource,createProjector,createTestEventStore,objectToYaml});
|
package/dist/index.mjs
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
function x(r,e){let t=Object.assign(new Error(r),e);return t.stack,Error.captureStackTrace&&Error.captureStackTrace(t,x),t}function
|
|
1
|
+
function x(r,e){let t=Object.assign(new Error(r),e);return t.stack,Error.captureStackTrace&&Error.captureStackTrace(t,x),t}function d(r,e){let t=x(r,e);throw Error.captureStackTrace&&Error.captureStackTrace(t,d),t}function P(r){Object.freeze(r);for(let e of Object.getOwnPropertyNames(r))r.hasOwnProperty(e)&&r[e]!==null&&(typeof r[e]=="object"||typeof r[e]=="function")&&!Object.isFrozen(r[e])&&P(r[e]);return r}var A={NODE_ENV:process.env.NODE_ENV},C=class{version=0;recordedEvents=[];replayEvents(e=[]){for(let t of e)this.applyAnEvent(t);return this}applyAnEvent(e){A.NODE_ENV==="test"&&P(e),this[e.eventName]===void 0&&(e.eventName.match(/^[$A-Z_][0-9A-Z_$]*$/i)?d(`Aggregate "${this.constructor.name}" has no method:
|
|
2
2
|
|
|
3
3
|
${e.eventName}(event) {
|
|
4
4
|
// Code goes here...
|
|
5
|
-
}`):
|
|
5
|
+
}`):d(`Aggregate "${this.constructor.name}" has no method:
|
|
6
6
|
|
|
7
7
|
['${e.eventName}'](event) {
|
|
8
8
|
// Code goes here...
|
|
9
9
|
}`));try{this[e.eventName](e)}catch(t){t instanceof Error&&console.error(`An error occurred inside your "%s" function:
|
|
10
10
|
`,e.eventName,t.stack?.split(`
|
|
11
11
|
`).map(a=>` ${a}`).join(`
|
|
12
|
-
`))}finally{this.version++}return this}recordThat(e){let t={...e,version:this.version};return this.applyAnEvent(t),this.recordedEvents.push(t),this}releaseEvents(){return this.recordedEvents.splice(0)}};function z(r,e){return{type:r,payload:e}}import{randomUUID as I}from"crypto";function U(r,e,t){return{aggregateId:e,eventId:I(),eventName:r,payload:t,recordedAt:new Date,version:-1}}import H from"yamlify-object";var _={indent:" ",colors:{date:m,error:m,symbol:m,string:m,number:m,boolean:m,null:m,undefined:m}};function m(r){return r}function
|
|
12
|
+
`))}finally{this.version++}return this}recordThat(e){let t={...e,version:this.version};return this.applyAnEvent(t),this.recordedEvents.push(t),this}releaseEvents(){return this.recordedEvents.splice(0)}};function z(r,e){return{type:r,payload:e}}import{randomUUID as I}from"crypto";function U(r,e,t=null){return{aggregateId:e,eventId:I(),eventName:r,payload:t,recordedAt:new Date,version:-1}}import H from"yamlify-object";var _={indent:" ",colors:{date:m,error:m,symbol:m,string:m,number:m,boolean:m,null:m,undefined:m}};function m(r){return r}function w(r){return r instanceof Error?w({...r}):H(r,_).split(`
|
|
13
13
|
`).slice(1).join(`
|
|
14
|
-
`)}function
|
|
15
|
-
`).map(
|
|
16
|
-
`)),
|
|
17
|
-
`).map(
|
|
18
|
-
`)),
|
|
19
|
-
${
|
|
14
|
+
`)}function k(r){let{commandHandlers:e,eventHandlers:t=[],projectors:a=[],store:s}=r,l=[],o={async resetProjections(){await Promise.all(l),await Promise.all(a.map(n=>n.init(o)))},async dispatch(n){e[n.type]===void 0&&d(`There is no command handler for the "${n.type}" command`);let i=e[n.type];return await i(n,o),n},async loadEvents(){return await(await s).loadEvents()},async load(n,i){let v=await(await s).load(i);return v.length<=0&&d(`Aggregate(${n.constructor.name}) with ID(${i}) does not exist.`,{aggregate:n.constructor.name,aggregateId:i}),n.replayEvents(v)},async persist(n){await Promise.all(l);let i=await s,c=n.releaseEvents();await i.persist(c);for(let v of c)await Promise.all(a.map(async T=>{try{await T.update(v)}catch(p){throw p instanceof Error&&console.error(`An error occurred in one of your projections: ${T.name}, given an event`,p.stack?.split(`
|
|
15
|
+
`).map(u=>` ${u}`).join(`
|
|
16
|
+
`)),p}})),await Promise.all(t.map(async T=>{try{await T(v,o)}catch(p){throw p instanceof Error&&console.error(`An error occurred in one of your event handlers: ${T.name}, given an event`,p.stack?.split(`
|
|
17
|
+
`).map(u=>` ${u}`).join(`
|
|
18
|
+
`)),p}}))},async loadPersist(n,i,c){return await this.load(n,i),await c(n),this.persist(n)}};for(let n of a)l.push(n.init(o));return o}function D(r){return(e,t)=>r[e.eventName]?.(e,t)}var f=new WeakMap;function E(r,e){if(f.has(r)){let t=f.get(r);for(let a in e)t[a]=e[a]}else f.set(r,e)}function h(r){return f.get(r)}var g=class{constructor(){E(this,{jobs:[],state:0})}get length(){return h(this).jobs.length}async start(){let{state:e,jobs:t}=h(this);if(!(e===1||t.length<=0)){for(E(this,{state:1});t.length>0;){let a=t.shift();await Promise.resolve().then(a.handle).then(a.resolve,a.reject)}E(this,{state:0})}}push(e){return new Promise((t,a)=>{let{jobs:s}=h(this);s.push({handle:e,resolve:t,reject:a}),setImmediate(()=>this.start())})}};function M(){}function $(r,e,t=M,a){let s=new g,l=a?.();return{name:r,async init(o){await s.push(()=>t(l));let n=await o.loadEvents();await Promise.all(n.map(i=>s.push(()=>e[i.eventName]?.(i,l))))},update(o){return s.push(()=>e[o.eventName]?.(o,l))}}}var b=Symbol("__placeholder__"),y={usedTestEventStoreInTest:!1,calledThenHandler:!1};process.env.NODE_ENV==="test"&&(beforeEach(()=>{y.usedTestEventStoreInTest=!1,y.calledThenHandler=!1}),afterEach(()=>{y.usedTestEventStoreInTest&&!y.calledThenHandler&&d("It seems like you used `createTestEventStore()`\nwithout using the `await then([expected, events, go, here, ...])`")}));function N(r,e){try{return r()}catch(t){throw Error.captureStackTrace&&t instanceof Error&&Error.captureStackTrace(t,e),t}}var S=class{constructor(e=[],t=[]){this.db=e;this.producedEvents=t}name="test-recording-projector";init(){this.db.splice(0)}update(e){this.producedEvents.push(e)}};function le(r,e=[]){y.usedTestEventStoreInTest=!0;let t=new S,a=k({store:{async load(o){return t.db.filter(n=>n.aggregateId===o)},loadEvents(){return t.db},persist(o){t.db.push(...o)}},commandHandlers:r,projectors:[...e,t]}),s,l={___:b,async given(o=[]){t.db.push(...o)},async when(o){try{return await a.dispatch(typeof o=="function"?o():o)}catch(n){return n instanceof Error&&(s=n),n}},async then(o){if(y.calledThenHandler=!0,o instanceof Error){let i=o;N(()=>expect(s).toEqual(i),l.then);return}let n=o;if(s)throw Object.keys(s).length>0&&(s.message=["With properties:",`
|
|
19
|
+
${w(s)}
|
|
20
20
|
|
|
21
21
|
---
|
|
22
22
|
|
|
23
23
|
`].join(`
|
|
24
|
-
`)),s;
|
|
24
|
+
`)),s;N(()=>{expect(n).toHaveLength(t.producedEvents.length);for(let[i,c]of n.entries()){let{aggregateId:v,eventName:T,payload:p}=t.producedEvents[i];if(c.aggregateId===b)throw new Error("Expected an `aggregateId`, but got `___` instead.");expect(c.aggregateId).toEqual(v),expect(c.eventName).toEqual(T),(c.payload===null||c.payload===void 0)&&expect(c.payload).toEqual(p);for(let u in c.payload){let j=c.payload[u];j===b?(expect(p).toHaveProperty(u),expect(p[u]).toBeDefined()):expect(p[u]).toEqual(j)}}},l.then)}};return l}export{C as Aggregate,z as Command,U as Event,d as abort,D as createEventMapper,k as createEventSource,$ as createProjector,le as createTestEventStore,w as objectToYaml};
|
package/package.json
CHANGED