@common-stack/store-mongo 7.2.1-alpha.16 → 7.2.1-alpha.18

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.
@@ -0,0 +1,118 @@
1
+ /* eslint-disable @typescript-eslint/no-namespace */
2
+ /**
3
+ * @file moleculer-event-handler.ts
4
+ * @description Decorator for marking service methods as Moleculer event handlers
5
+ *
6
+ * This allows event handlers to be defined directly in the service class
7
+ * and automatically registered as Moleculer events.
8
+ */
9
+
10
+ import 'reflect-metadata';
11
+
12
+ /**
13
+ * Moleculer namespace containing event handler utilities
14
+ */
15
+ export namespace Moleculer {
16
+ /**
17
+ * Metadata key for storing event handler information
18
+ */
19
+ export const EVENT_HANDLER_METADATA_KEY = 'moleculer:eventHandler';
20
+
21
+ /**
22
+ * Event handler metadata interface
23
+ */
24
+ export interface EventHandlerMetadata {
25
+ eventName: string;
26
+ methodName: string;
27
+ group?: string;
28
+ }
29
+
30
+ /**
31
+ * Decorator for marking a service method as a Moleculer event handler
32
+ *
33
+ * @param eventName - The event name to listen for
34
+ * @param options - Optional event configuration
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * class OrganizationService implements IOrganizationService {
39
+ * @Moleculer.EventHandler(UserBroadcasterAction.OnUserCreated)
40
+ * async onUserCreated(event: IUserCreationEvent): Promise<void> {
41
+ * await this.createDefaultOrganization(event.user);
42
+ * }
43
+ *
44
+ * @Moleculer.EventHandler(OrganizationServiceAction.OnOrganizationCreated, { group: 'org-setup' })
45
+ * async onOrganizationCreated(event: IOrganizationCreatedEvent): Promise<void> {
46
+ * // Handle organization creation
47
+ * }
48
+ * }
49
+ * ```
50
+ */
51
+ export function EventHandler(
52
+ eventName: string,
53
+ options?: { group?: string }
54
+ ): MethodDecorator {
55
+ return function moleculerEventHandlerDecorator(
56
+ target: object,
57
+ propertyKey: string | symbol,
58
+ descriptor: PropertyDescriptor
59
+ ): PropertyDescriptor {
60
+ // Get existing event handlers for this class
61
+ const existingHandlers: EventHandlerMetadata[] =
62
+ Reflect.getMetadata(EVENT_HANDLER_METADATA_KEY, target.constructor) || [];
63
+
64
+ // Add this handler to the list
65
+ const metadata: EventHandlerMetadata = {
66
+ eventName,
67
+ methodName: propertyKey.toString(),
68
+ group: options?.group,
69
+ };
70
+
71
+ existingHandlers.push(metadata);
72
+
73
+ // Store the updated list
74
+ Reflect.defineMetadata(EVENT_HANDLER_METADATA_KEY, existingHandlers, target.constructor);
75
+
76
+ return descriptor;
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Get all Moleculer event handlers defined on a service class
82
+ *
83
+ * @param serviceClassOrInstance - The service class constructor or instance
84
+ * @returns Array of event handler metadata
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const handlers = Moleculer.getEventHandlers(OrganizationService);
89
+ * // Returns: [
90
+ * // { eventName: 'user.created', methodName: 'onUserCreated' },
91
+ * // { eventName: 'org.created', methodName: 'onOrganizationCreated' }
92
+ * // ]
93
+ * ```
94
+ */
95
+ export function getEventHandlers(
96
+ serviceClassOrInstance: (new (...args: unknown[]) => unknown) | object
97
+ ): EventHandlerMetadata[] {
98
+ const target = typeof serviceClassOrInstance === 'function'
99
+ ? serviceClassOrInstance
100
+ : (serviceClassOrInstance as { constructor: new (...args: unknown[]) => unknown }).constructor;
101
+ return Reflect.getMetadata(EVENT_HANDLER_METADATA_KEY, target) || [];
102
+ }
103
+
104
+ /**
105
+ * Check if a method is marked as a Moleculer event handler
106
+ *
107
+ * @param serviceClassOrInstance - The service class constructor or instance
108
+ * @param methodName - The method name to check
109
+ * @returns Event handler metadata if found, undefined otherwise
110
+ */
111
+ export function isEventHandler(
112
+ serviceClassOrInstance: (new (...args: unknown[]) => unknown) | object,
113
+ methodName: string
114
+ ): EventHandlerMetadata | undefined {
115
+ const handlers = getEventHandlers(serviceClassOrInstance);
116
+ return handlers.find(h => h.methodName === methodName);
117
+ }
118
+ }