truxie 0.1.0-test.6 → 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 +261 -0
- package/dist/cqrs.js +1 -0
- package/package.json +6 -1
package/dist/cqrs.d.ts
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { D as DynamicModule, a as Type } from './types-D-An5cXP.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Enables CQRS with CommandBus, QueryBus, CqrsEventBus, and auto-discovered handlers.
|
|
5
|
+
*
|
|
6
|
+
* Import with `CqrsModule.forRoot()` in your root module.
|
|
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
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* @Module({
|
|
20
|
+
* imports: [CqrsModule.forRoot()],
|
|
21
|
+
* })
|
|
22
|
+
* export class AppModule {}
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare class CqrsModule {
|
|
26
|
+
static forRoot(): DynamicModule;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Marker interface for commands. */
|
|
30
|
+
interface ICommand {
|
|
31
|
+
}
|
|
32
|
+
/** Marker interface for queries. */
|
|
33
|
+
interface IQuery {
|
|
34
|
+
}
|
|
35
|
+
/** Base class for CQRS domain events. Class-based, not string-based. */
|
|
36
|
+
interface IEvent {
|
|
37
|
+
}
|
|
38
|
+
/** Handler that executes a command. */
|
|
39
|
+
interface ICommandHandler<TCommand extends ICommand = ICommand, TResult = void> {
|
|
40
|
+
execute(command: TCommand): Promise<TResult>;
|
|
41
|
+
}
|
|
42
|
+
/** Handler that executes a query. */
|
|
43
|
+
interface IQueryHandler<TQuery extends IQuery = IQuery, TResult = unknown> {
|
|
44
|
+
execute(query: TQuery): Promise<TResult>;
|
|
45
|
+
}
|
|
46
|
+
/** Handler that reacts to a domain event. */
|
|
47
|
+
interface IEventHandler<TEvent extends IEvent = IEvent> {
|
|
48
|
+
handle(event: TEvent): Promise<void> | void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Dispatches commands to their registered handlers.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* @Injectable()
|
|
57
|
+
* @Inject(CommandBus)
|
|
58
|
+
* class AuthController {
|
|
59
|
+
* constructor(private readonly commandBus: CommandBus) {}
|
|
60
|
+
*
|
|
61
|
+
* @Post('/register')
|
|
62
|
+
* async register(@Body() body: RegisterInput) {
|
|
63
|
+
* return this.commandBus.execute(new CreateUserCommand(body.email, body.name))
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare class CommandBus {
|
|
69
|
+
private handlers;
|
|
70
|
+
/** @internal — called by CqrsExplorer during bootstrap. */
|
|
71
|
+
register(commandClass: Type, handler: ICommandHandler): void;
|
|
72
|
+
/**
|
|
73
|
+
* Execute a command by dispatching it to the registered handler.
|
|
74
|
+
* Throws if no handler is registered for the command type.
|
|
75
|
+
*/
|
|
76
|
+
execute<TResult = void>(command: ICommand): Promise<TResult>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Dispatches queries to their registered handlers.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* @Injectable()
|
|
85
|
+
* @Inject(QueryBus)
|
|
86
|
+
* class UserController {
|
|
87
|
+
* constructor(private readonly queryBus: QueryBus) {}
|
|
88
|
+
*
|
|
89
|
+
* @Get('/:id')
|
|
90
|
+
* async getUser(@Param('id') id: string) {
|
|
91
|
+
* return this.queryBus.execute(new GetUserQuery(id))
|
|
92
|
+
* }
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
declare class QueryBus {
|
|
97
|
+
private handlers;
|
|
98
|
+
/** @internal — called by CqrsExplorer during bootstrap. */
|
|
99
|
+
register(queryClass: Type, handler: IQueryHandler): void;
|
|
100
|
+
/**
|
|
101
|
+
* Execute a query by dispatching it to the registered handler.
|
|
102
|
+
* Throws if no handler is registered for the query type.
|
|
103
|
+
*/
|
|
104
|
+
execute<TResult = unknown>(query: IQuery): Promise<TResult>;
|
|
105
|
+
}
|
|
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
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Marks a class as a command handler for a specific command type.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* class CreateUserCommand {
|
|
199
|
+
* constructor(public readonly email: string, public readonly name: string) {}
|
|
200
|
+
* }
|
|
201
|
+
*
|
|
202
|
+
* @CommandHandler(CreateUserCommand)
|
|
203
|
+
* @Injectable()
|
|
204
|
+
* @Inject(UserService)
|
|
205
|
+
* class CreateUserHandler implements ICommandHandler<CreateUserCommand, IUser> {
|
|
206
|
+
* constructor(private readonly userService: UserService) {}
|
|
207
|
+
*
|
|
208
|
+
* async execute(command: CreateUserCommand): Promise<IUser> {
|
|
209
|
+
* return this.userService.create(command)
|
|
210
|
+
* }
|
|
211
|
+
* }
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
declare function CommandHandler(command: Type): ClassDecorator;
|
|
215
|
+
/**
|
|
216
|
+
* Marks a class as a query handler for a specific query type.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* class GetUserQuery {
|
|
221
|
+
* constructor(public readonly id: string) {}
|
|
222
|
+
* }
|
|
223
|
+
*
|
|
224
|
+
* @QueryHandler(GetUserQuery)
|
|
225
|
+
* @Injectable()
|
|
226
|
+
* @Inject(UserService)
|
|
227
|
+
* class GetUserHandler implements IQueryHandler<GetUserQuery, IUser> {
|
|
228
|
+
* constructor(private readonly userService: UserService) {}
|
|
229
|
+
*
|
|
230
|
+
* async execute(query: GetUserQuery): Promise<IUser> {
|
|
231
|
+
* return this.userService.findById(query.id)
|
|
232
|
+
* }
|
|
233
|
+
* }
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
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;
|
|
260
|
+
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "truxie",
|
|
3
|
-
"version": "0.1.0-test.
|
|
3
|
+
"version": "0.1.0-test.8",
|
|
4
4
|
"description": "NestJS-like structural backend library with DI, modules, guards, interceptors, pipes, and exception filters",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -51,6 +51,11 @@
|
|
|
51
51
|
"types": "./dist/ws.d.ts",
|
|
52
52
|
"import": "./dist/ws.js",
|
|
53
53
|
"default": "./dist/ws.js"
|
|
54
|
+
},
|
|
55
|
+
"./cqrs": {
|
|
56
|
+
"types": "./dist/cqrs.d.ts",
|
|
57
|
+
"import": "./dist/cqrs.js",
|
|
58
|
+
"default": "./dist/cqrs.js"
|
|
54
59
|
}
|
|
55
60
|
},
|
|
56
61
|
"files": [
|