truxie 0.1.0-test.7 → 0.1.0-test.8
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/cqrs.d.ts +127 -30
- package/dist/cqrs.js +1 -1
- package/package.json +1 -1
package/dist/cqrs.d.ts
CHANGED
|
@@ -1,44 +1,25 @@
|
|
|
1
1
|
import { D as DynamicModule, a as Type } from './types-D-An5cXP.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Enables CQRS with CommandBus, QueryBus, and auto-discovered handlers.
|
|
4
|
+
* Enables CQRS with CommandBus, QueryBus, CqrsEventBus, and auto-discovered handlers.
|
|
5
5
|
*
|
|
6
6
|
* Import with `CqrsModule.forRoot()` in your root module.
|
|
7
7
|
*
|
|
8
|
+
* **Three buses, three purposes:**
|
|
9
|
+
* - `CommandBus` — dispatches commands to `@CommandHandler` (1:1 mapping)
|
|
10
|
+
* - `QueryBus` — dispatches queries to `@QueryHandler` (1:1 mapping)
|
|
11
|
+
* - `CqrsEventBus` — publishes domain events to `@EventsHandler` (1:N fan-out)
|
|
12
|
+
*
|
|
13
|
+
* **vs core EventBus:**
|
|
14
|
+
* - Core `EventBus` (`@OnEvent('string')`) — string-based, fire-and-forget app events
|
|
15
|
+
* - CQRS `CqrsEventBus` (`@EventsHandler(Class)`) — class-based domain events
|
|
16
|
+
*
|
|
8
17
|
* @example
|
|
9
18
|
* ```typescript
|
|
10
19
|
* @Module({
|
|
11
20
|
* imports: [CqrsModule.forRoot()],
|
|
12
21
|
* })
|
|
13
22
|
* export class AppModule {}
|
|
14
|
-
*
|
|
15
|
-
* // Define a command:
|
|
16
|
-
* class CreateUserCommand {
|
|
17
|
-
* constructor(public readonly email: string, public readonly name: string) {}
|
|
18
|
-
* }
|
|
19
|
-
*
|
|
20
|
-
* // Register handler in a module's providers:
|
|
21
|
-
* @CommandHandler(CreateUserCommand)
|
|
22
|
-
* @Injectable()
|
|
23
|
-
* @Inject(UserService)
|
|
24
|
-
* class CreateUserHandler implements ICommandHandler<CreateUserCommand, IUser> {
|
|
25
|
-
* constructor(private readonly userService: UserService) {}
|
|
26
|
-
* async execute(command: CreateUserCommand) {
|
|
27
|
-
* return this.userService.create(command)
|
|
28
|
-
* }
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* // Dispatch from controller:
|
|
32
|
-
* @Injectable()
|
|
33
|
-
* @Inject(CommandBus)
|
|
34
|
-
* class AuthController {
|
|
35
|
-
* constructor(private readonly commandBus: CommandBus) {}
|
|
36
|
-
*
|
|
37
|
-
* @Post('/register')
|
|
38
|
-
* async register(@Body() body: RegisterInput) {
|
|
39
|
-
* return this.commandBus.execute(new CreateUserCommand(body.email, body.name))
|
|
40
|
-
* }
|
|
41
|
-
* }
|
|
42
23
|
* ```
|
|
43
24
|
*/
|
|
44
25
|
declare class CqrsModule {
|
|
@@ -51,6 +32,9 @@ interface ICommand {
|
|
|
51
32
|
/** Marker interface for queries. */
|
|
52
33
|
interface IQuery {
|
|
53
34
|
}
|
|
35
|
+
/** Base class for CQRS domain events. Class-based, not string-based. */
|
|
36
|
+
interface IEvent {
|
|
37
|
+
}
|
|
54
38
|
/** Handler that executes a command. */
|
|
55
39
|
interface ICommandHandler<TCommand extends ICommand = ICommand, TResult = void> {
|
|
56
40
|
execute(command: TCommand): Promise<TResult>;
|
|
@@ -59,6 +43,10 @@ interface ICommandHandler<TCommand extends ICommand = ICommand, TResult = void>
|
|
|
59
43
|
interface IQueryHandler<TQuery extends IQuery = IQuery, TResult = unknown> {
|
|
60
44
|
execute(query: TQuery): Promise<TResult>;
|
|
61
45
|
}
|
|
46
|
+
/** Handler that reacts to a domain event. */
|
|
47
|
+
interface IEventHandler<TEvent extends IEvent = IEvent> {
|
|
48
|
+
handle(event: TEvent): Promise<void> | void;
|
|
49
|
+
}
|
|
62
50
|
|
|
63
51
|
/**
|
|
64
52
|
* Dispatches commands to their registered handlers.
|
|
@@ -116,6 +104,92 @@ declare class QueryBus {
|
|
|
116
104
|
execute<TResult = unknown>(query: IQuery): Promise<TResult>;
|
|
117
105
|
}
|
|
118
106
|
|
|
107
|
+
/**
|
|
108
|
+
* CQRS EventBus — dispatches class-based domain events to registered handlers.
|
|
109
|
+
*
|
|
110
|
+
* Different from the core EventBus:
|
|
111
|
+
* - Core EventBus: string-based (`@OnEvent('user.created')`), fire-and-forget app events
|
|
112
|
+
* - CQRS EventBus: class-based (`@EventsHandler(UserCreatedEvent)`), domain events after commands
|
|
113
|
+
*
|
|
114
|
+
* Multiple handlers can subscribe to the same event type (fan-out).
|
|
115
|
+
* Handlers run concurrently via Promise.allSettled — one failure doesn't block others.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* // Publish from a command handler or aggregate root:
|
|
120
|
+
* await this.eventBus.publish(new UserCreatedEvent(user.id, user.email))
|
|
121
|
+
*
|
|
122
|
+
* // Publish multiple events at once:
|
|
123
|
+
* await this.eventBus.publishAll([
|
|
124
|
+
* new UserCreatedEvent(user.id, user.email),
|
|
125
|
+
* new WelcomeEmailQueuedEvent(user.email),
|
|
126
|
+
* ])
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
declare class CqrsEventBus {
|
|
130
|
+
private handlers;
|
|
131
|
+
/** @internal — called by CqrsExplorer during bootstrap. */
|
|
132
|
+
register(eventClass: Type, handler: IEventHandler): void;
|
|
133
|
+
/** Publish a single domain event to all registered handlers. */
|
|
134
|
+
publish(event: IEvent): Promise<void>;
|
|
135
|
+
/** Publish multiple domain events in order. */
|
|
136
|
+
publishAll(events: IEvent[]): Promise<void>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Base class for aggregate roots that accumulate domain events.
|
|
141
|
+
*
|
|
142
|
+
* Events are collected via `apply()` and committed (published) later
|
|
143
|
+
* by the command handler or service via `commit()`.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* class UserAggregate extends AggregateRoot {
|
|
148
|
+
* create(email: string, name: string) {
|
|
149
|
+
* // ... business logic ...
|
|
150
|
+
* this.apply(new UserCreatedEvent(this.id, email))
|
|
151
|
+
* }
|
|
152
|
+
*
|
|
153
|
+
* deactivate(reason: string) {
|
|
154
|
+
* // ... validation ...
|
|
155
|
+
* this.apply(new UserDeactivatedEvent(this.id, reason))
|
|
156
|
+
* }
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* // In command handler:
|
|
160
|
+
* @CommandHandler(CreateUserCommand)
|
|
161
|
+
* @Injectable()
|
|
162
|
+
* @Inject(CqrsEventBus)
|
|
163
|
+
* class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
|
|
164
|
+
* constructor(private readonly eventBus: CqrsEventBus) {}
|
|
165
|
+
*
|
|
166
|
+
* async execute(command: CreateUserCommand) {
|
|
167
|
+
* const user = new UserAggregate()
|
|
168
|
+
* user.create(command.email, command.name)
|
|
169
|
+
* // ... persist ...
|
|
170
|
+
* await user.commit(this.eventBus)
|
|
171
|
+
* }
|
|
172
|
+
* }
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare abstract class AggregateRoot {
|
|
176
|
+
private uncommittedEvents;
|
|
177
|
+
/** Apply a domain event (stage it for publishing). */
|
|
178
|
+
protected apply(event: IEvent): void;
|
|
179
|
+
/** Get all uncommitted events. */
|
|
180
|
+
getUncommittedEvents(): IEvent[];
|
|
181
|
+
/**
|
|
182
|
+
* Publish all uncommitted events via the event bus and clear the list.
|
|
183
|
+
*
|
|
184
|
+
* @param eventBus - The CQRS event bus to publish through
|
|
185
|
+
*/
|
|
186
|
+
commit(eventBus: {
|
|
187
|
+
publishAll(events: IEvent[]): Promise<void>;
|
|
188
|
+
}): Promise<void>;
|
|
189
|
+
/** Clear uncommitted events without publishing. */
|
|
190
|
+
clearEvents(): void;
|
|
191
|
+
}
|
|
192
|
+
|
|
119
193
|
/**
|
|
120
194
|
* Marks a class as a command handler for a specific command type.
|
|
121
195
|
*
|
|
@@ -160,5 +234,28 @@ declare function CommandHandler(command: Type): ClassDecorator;
|
|
|
160
234
|
* ```
|
|
161
235
|
*/
|
|
162
236
|
declare function QueryHandler(query: Type): ClassDecorator;
|
|
237
|
+
/**
|
|
238
|
+
* Marks a class as a domain event handler for one or more event types.
|
|
239
|
+
* Unlike @OnEvent (string-based), this is class-based — matched by event constructor.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* class UserCreatedEvent {
|
|
244
|
+
* constructor(public readonly userId: string, public readonly email: string) {}
|
|
245
|
+
* }
|
|
246
|
+
*
|
|
247
|
+
* @EventsHandler(UserCreatedEvent)
|
|
248
|
+
* @Injectable()
|
|
249
|
+
* @Inject(EmailService)
|
|
250
|
+
* class UserCreatedHandler implements IEventHandler<UserCreatedEvent> {
|
|
251
|
+
* constructor(private readonly email: EmailService) {}
|
|
252
|
+
*
|
|
253
|
+
* async handle(event: UserCreatedEvent) {
|
|
254
|
+
* await this.email.sendWelcome(event.email)
|
|
255
|
+
* }
|
|
256
|
+
* }
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
declare function EventsHandler(...events: Type[]): ClassDecorator;
|
|
163
260
|
|
|
164
|
-
export { CommandBus, CommandHandler, CqrsModule, type ICommand, type ICommandHandler, type IQuery, type IQueryHandler, QueryBus, QueryHandler };
|
|
261
|
+
export { AggregateRoot, CommandBus, CommandHandler, CqrsEventBus, CqrsModule, EventsHandler, type ICommand, type ICommandHandler, type IEvent, type IEventHandler, type IQuery, type IQueryHandler, QueryBus, QueryHandler };
|
package/dist/cqrs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {a as a$1}from'./chunk-NXAGO5NO.js';import {b
|
|
1
|
+
import {a as a$1}from'./chunk-NXAGO5NO.js';import {b,a as a$2}from'./chunk-XZNMJJJU.js';import {c,d}from'./chunk-N277L4OB.js';import {a}from'./chunk-EI7MMDWY.js';import'reflect-metadata';function A(o,e,t,n){var s=arguments.length,r=s<3?e:n===null?n=Object.getOwnPropertyDescriptor(e,t):n,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(o,e,t,n);else for(var i=o.length-1;i>=0;i--)(c=o[i])&&(r=(s<3?c(r):s>3?c(e,t,r):c(e,t))||r);return s>3&&r&&Object.defineProperty(e,t,r),r}a(A,"_ts_decorate");var f=class{static{a(this,"CommandBus");}handlers=new Map;register(e,t){if(this.handlers.has(e))throw new Error(`Duplicate command handler for ${e.name}. Each command must have exactly one handler.`);this.handlers.set(e,t);}async execute(e){let t=e.constructor,n=this.handlers.get(t);if(!n)throw new Error(`No handler registered for command ${t.name}`);return n.execute(e)}};f=A([c()],f);function H(o,e,t,n){var s=arguments.length,r=s<3?e:n===null?n=Object.getOwnPropertyDescriptor(e,t):n,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(o,e,t,n);else for(var i=o.length-1;i>=0;i--)(c=o[i])&&(r=(s<3?c(r):s>3?c(e,t,r):c(e,t))||r);return s>3&&r&&Object.defineProperty(e,t,r),r}a(H,"_ts_decorate");var l=class{static{a(this,"QueryBus");}handlers=new Map;register(e,t){if(this.handlers.has(e))throw new Error(`Duplicate query handler for ${e.name}. Each query must have exactly one handler.`);this.handlers.set(e,t);}async execute(e){let t=e.constructor,n=this.handlers.get(t);if(!n)throw new Error(`No handler registered for query ${t.name}`);return n.execute(e)}};l=H([c()],l);function D(o,e,t,n){var s=arguments.length,r=s<3?e:n===null?n=Object.getOwnPropertyDescriptor(e,t):n,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(o,e,t,n);else for(var i=o.length-1;i>=0;i--)(c=o[i])&&(r=(s<3?c(r):s>3?c(e,t,r):c(e,t))||r);return s>3&&r&&Object.defineProperty(e,t,r),r}a(D,"_ts_decorate");var u=class{static{a(this,"CqrsEventBus");}handlers=new Map;register(e,t){let n=this.handlers.get(e)??[];n.push(t),this.handlers.set(e,n);}async publish(e){let t=e.constructor,n=this.handlers.get(t);if(!n||n.length===0)return;let s=await Promise.allSettled(n.map(r=>r.handle(e)));for(let r of s)r.status==="rejected"&&console.error(`[CQRS EventBus] Handler failed for ${t.name}:`,r.reason);}async publishAll(e){for(let t of e)await this.publish(t);}};u=D([c()],u);var j=Symbol("cqrs:command-handler"),x=Symbol("cqrs:query-handler"),g=Symbol("cqrs:events-handler");function P(o){return e=>{Reflect.defineMetadata(j,o,e);}}a(P,"CommandHandler");function q(o){return e=>{Reflect.defineMetadata(x,o,e);}}a(q,"QueryHandler");function T(...o){return e=>{Reflect.defineMetadata(g,o,e);}}a(T,"EventsHandler");function w(o){return Reflect.getOwnMetadata(j,o)}a(w,"getCommandHandlerMetadata");function O(o){return Reflect.getOwnMetadata(x,o)}a(O,"getQueryHandlerMetadata");function M(o){return Reflect.getOwnMetadata(g,o)}a(M,"getEventsHandlerMetadata");function C(o,e,t,n){var s=arguments.length,r=s<3?e:n===null?n=Object.getOwnPropertyDescriptor(e,t):n,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(o,e,t,n);else for(var i=o.length-1;i>=0;i--)(c=o[i])&&(r=(s<3?c(r):s>3?c(e,t,r):c(e,t))||r);return s>3&&r&&Object.defineProperty(e,t,r),r}a(C,"_ts_decorate");function B(o,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(o,e)}a(B,"_ts_metadata");var m=class{static{a(this,"CqrsExplorer");}discovery;commandBus;queryBus;eventBus;constructor(e,t,n,s){this.discovery=e,this.commandBus=t,this.queryBus=n,this.eventBus=s;}async onApplicationBootstrap(){let e=this.discovery.getProviders();for(let t of e){let n=this.discovery.getProviderClass(t);if(!n)continue;let s=w(n),r=O(n),c=M(n);if(!s&&!r&&!c)continue;let i=typeof t=="function"?t:t.provide,h;try{h=await this.discovery.resolve(i);}catch{continue}if(s&&this.commandBus.register(s,h),r&&this.queryBus.register(r,h),c)for(let _ of c)this.eventBus.register(_,h);}}};m=C([c(),d(a$1,f,l,u),B("design:type",Function),B("design:paramtypes",[typeof a$1>"u"?Object:a$1,typeof f>"u"?Object:f,typeof l>"u"?Object:l,typeof u>"u"?Object:u])],m);function Q(o,e,t,n){var s=arguments.length,r=s<3?e:n===null?n=Object.getOwnPropertyDescriptor(e,t):n,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(o,e,t,n);else for(var i=o.length-1;i>=0;i--)(c=o[i])&&(r=(s<3?c(r):s>3?c(e,t,r):c(e,t))||r);return s>3&&r&&Object.defineProperty(e,t,r),r}a(Q,"_ts_decorate");var p=class o{static{a(this,"CqrsModule");}static forRoot(){return {module:o,global:true,providers:[f,l,u,m],exports:[f,l,u]}}};p=Q([b(),a$2({})],p);var y=class{static{a(this,"AggregateRoot");}uncommittedEvents=[];apply(e){this.uncommittedEvents.push(e);}getUncommittedEvents(){return [...this.uncommittedEvents]}async commit(e){let t=this.uncommittedEvents;this.uncommittedEvents=[],await e.publishAll(t);}clearEvents(){this.uncommittedEvents=[];}};export{y as AggregateRoot,f as CommandBus,P as CommandHandler,u as CqrsEventBus,p as CqrsModule,T as EventsHandler,l as QueryBus,q as QueryHandler};
|
package/package.json
CHANGED