wsp-ms-core 1.0.78-beta-1 → 1.0.78-beta-3

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 CHANGED
@@ -84,53 +84,12 @@ declare abstract class BaseEvent {
84
84
  get ocurredAt(): DateTime;
85
85
  }
86
86
 
87
- declare class ProcessStatus extends ValueObject<string> {
88
- static readonly SUPPORTED: readonly string[];
89
- static readonly PENDING: ProcessStatus;
90
- static readonly PROCESSING: ProcessStatus;
91
- static readonly PROCESSED: ProcessStatus;
92
- static readonly FAILED: ProcessStatus;
93
- static readonly DEAD: ProcessStatus;
94
- private constructor();
95
- protected validate(value: string): void;
96
- toPrimitives(): Record<string, unknown>;
97
- static create(status: string): ProcessStatus;
98
- }
99
-
100
- declare class OutboxRecord {
101
- private readonly _eventUuid;
102
- private readonly _eventType;
103
- private readonly _tenantUuid;
104
- private readonly _aggregateUuid;
105
- private readonly _aggregateType;
106
- private readonly _topic;
107
- private readonly _payload;
108
- private _status;
109
- private _attempts;
110
- private _errorMessage;
111
- private _publishedAt;
112
- private _lastAttempt;
113
- private readonly _createdAt;
114
- protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt: DateTime);
115
- get eventUuid(): UUID;
116
- get tenantUuid(): UUID;
87
+ declare class DomainEvent extends BaseEvent {
88
+ protected readonly _aggregateUuid: UUID;
89
+ protected readonly _aggregateType: string;
90
+ protected constructor(tenantUuid: UUID, version: string, type: string, payload: Record<string, unknown>, aggregateUuid: UUID, aggregateType: string);
117
91
  get aggregateUuid(): UUID;
118
92
  get aggregateType(): string;
119
- get eventType(): string;
120
- get topic(): string;
121
- get payload(): string;
122
- get status(): ProcessStatus;
123
- get attempts(): number;
124
- get errorMessage(): string | undefined;
125
- get publishedAt(): DateTime | undefined;
126
- get lastAttempt(): DateTime | undefined;
127
- get createdAt(): DateTime;
128
- incrementAttempts(): void;
129
- markProcessed(): void;
130
- markProcessing(): void;
131
- markWithError(error: string): void;
132
- toPrimitives(): Record<string, unknown>;
133
- static reconstitute(data: Record<string, any>): OutboxRecord;
134
93
  }
135
94
 
136
95
  interface BaseEntity {
@@ -141,16 +100,16 @@ interface BaseEntity {
141
100
  }
142
101
  declare abstract class DomainEntity<T extends BaseEntity> {
143
102
  protected readonly props: T;
144
- protected _events: OutboxRecord[];
103
+ protected _events: DomainEvent[];
145
104
  protected constructor(props: T);
146
- protected recordEvent(event: OutboxRecord): void;
105
+ protected recordEvent(event: DomainEvent): void;
147
106
  protected touch(): void;
148
107
  get uuid(): UUID;
149
108
  get createdAt(): DateTime;
150
109
  get updatedAt(): DateTime;
151
110
  get deletedAt(): DateTime | undefined;
152
111
  get isDeleted(): boolean;
153
- pullEvents(): OutboxRecord[];
112
+ pullEvents(): DomainEvent[];
154
113
  abstract equals(entity?: DomainEntity<T>): boolean;
155
114
  abstract toProps(): T;
156
115
  abstract toPrimitives(): Record<string, unknown>;
@@ -181,14 +140,6 @@ declare class UsageError extends DomainError {
181
140
  constructor(type: string, vars?: Record<string, any>);
182
141
  }
183
142
 
184
- declare class DomainEvent extends BaseEvent {
185
- protected readonly _aggregateUuid: UUID;
186
- protected readonly _aggregateType: string;
187
- protected constructor(tenantUuid: UUID, version: string, type: string, payload: Record<string, unknown>, aggregateUuid: UUID, aggregateType: string);
188
- get aggregateUuid(): UUID;
189
- get aggregateType(): string;
190
- }
191
-
192
143
  declare class PaymentGateway extends ValueObject<string> {
193
144
  static readonly SUPPORTED: readonly string[];
194
145
  private static readonly EXTERNALS;
@@ -369,6 +320,55 @@ declare class Price extends ValueObject<PriceProps> {
369
320
  static createFromPrimitives(data: Record<string, unknown>): Price;
370
321
  }
371
322
 
323
+ declare class ProcessStatus extends ValueObject<string> {
324
+ static readonly SUPPORTED: readonly string[];
325
+ static readonly PENDING: ProcessStatus;
326
+ static readonly PROCESSING: ProcessStatus;
327
+ static readonly PROCESSED: ProcessStatus;
328
+ static readonly FAILED: ProcessStatus;
329
+ static readonly DEAD: ProcessStatus;
330
+ private constructor();
331
+ protected validate(value: string): void;
332
+ toPrimitives(): Record<string, unknown>;
333
+ static create(status: string): ProcessStatus;
334
+ }
335
+
336
+ declare class OutboxRecord {
337
+ private readonly _eventUuid;
338
+ private readonly _eventType;
339
+ private readonly _tenantUuid;
340
+ private readonly _aggregateUuid;
341
+ private readonly _aggregateType;
342
+ private readonly _topic;
343
+ private readonly _payload;
344
+ private _status;
345
+ private _attempts;
346
+ private _errorMessage;
347
+ private _publishedAt;
348
+ private _lastAttempt;
349
+ private readonly _createdAt;
350
+ protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt: DateTime);
351
+ get eventUuid(): UUID;
352
+ get tenantUuid(): UUID;
353
+ get aggregateUuid(): UUID;
354
+ get aggregateType(): string;
355
+ get eventType(): string;
356
+ get topic(): string;
357
+ get payload(): string;
358
+ get status(): ProcessStatus;
359
+ get attempts(): number;
360
+ get errorMessage(): string | undefined;
361
+ get publishedAt(): DateTime | undefined;
362
+ get lastAttempt(): DateTime | undefined;
363
+ get createdAt(): DateTime;
364
+ incrementAttempts(): void;
365
+ markProcessed(): void;
366
+ markProcessing(): void;
367
+ markWithError(error: string): void;
368
+ toPrimitives(): Record<string, unknown>;
369
+ static reconstitute(data: Record<string, any>): OutboxRecord;
370
+ }
371
+
372
372
  interface EventBusRepository {
373
373
  create(event: OutboxRecord): Promise<void>;
374
374
  update(event: OutboxRecord): Promise<void>;
@@ -579,6 +579,22 @@ declare class MysqlConnection implements DatabaseConnection<string, any[], RowDa
579
579
  close(): Promise<void>;
580
580
  }
581
581
 
582
+ declare class DefaultOutboxRunner {
583
+ private readonly uowFactory;
584
+ private readonly eventBusRepository;
585
+ private readonly eventManager;
586
+ private readonly interval;
587
+ private readonly maxEvents;
588
+ private errors;
589
+ constructor(uowFactory: BasicUnitOfWorkFactory, eventBusRepository: EventBusRepository, eventManager: EventManager);
590
+ private addError;
591
+ private logErrors;
592
+ private sleep;
593
+ private processOutboxRecord;
594
+ private process;
595
+ start(): Promise<void>;
596
+ }
597
+
582
598
  interface ExchangeRatesProps {
583
599
  base: Currency;
584
600
  rates: Record<string, number>;
@@ -603,4 +619,4 @@ declare class StringVars {
603
619
  }): string;
604
620
  }
605
621
 
606
- export { BaseEntity, BaseEvent, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Country, Currency, DatabaseConnection, DatabaseConnector, DateTime, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, EventManager, EventManagerConnection, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, IntegrationEvent, InternalError, KafkaManager, Language, Logger, MysqlConnection, MysqlConnector, OutboxRecord, PaymentGateway, PaymentStatus, Price, ProcessStatus, RouteCallback, RoutesCallbackList, StringVars, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
622
+ export { BaseEntity, BaseEvent, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Country, Currency, DatabaseConnection, DatabaseConnector, DateTime, DefaultOutboxRunner, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, EventManager, EventManagerConnection, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, IntegrationEvent, InternalError, KafkaManager, Language, Logger, MysqlConnection, MysqlConnector, OutboxRecord, PaymentGateway, PaymentStatus, Price, ProcessStatus, RouteCallback, RoutesCallbackList, StringVars, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
package/dist/index.d.ts CHANGED
@@ -84,53 +84,12 @@ declare abstract class BaseEvent {
84
84
  get ocurredAt(): DateTime;
85
85
  }
86
86
 
87
- declare class ProcessStatus extends ValueObject<string> {
88
- static readonly SUPPORTED: readonly string[];
89
- static readonly PENDING: ProcessStatus;
90
- static readonly PROCESSING: ProcessStatus;
91
- static readonly PROCESSED: ProcessStatus;
92
- static readonly FAILED: ProcessStatus;
93
- static readonly DEAD: ProcessStatus;
94
- private constructor();
95
- protected validate(value: string): void;
96
- toPrimitives(): Record<string, unknown>;
97
- static create(status: string): ProcessStatus;
98
- }
99
-
100
- declare class OutboxRecord {
101
- private readonly _eventUuid;
102
- private readonly _eventType;
103
- private readonly _tenantUuid;
104
- private readonly _aggregateUuid;
105
- private readonly _aggregateType;
106
- private readonly _topic;
107
- private readonly _payload;
108
- private _status;
109
- private _attempts;
110
- private _errorMessage;
111
- private _publishedAt;
112
- private _lastAttempt;
113
- private readonly _createdAt;
114
- protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt: DateTime);
115
- get eventUuid(): UUID;
116
- get tenantUuid(): UUID;
87
+ declare class DomainEvent extends BaseEvent {
88
+ protected readonly _aggregateUuid: UUID;
89
+ protected readonly _aggregateType: string;
90
+ protected constructor(tenantUuid: UUID, version: string, type: string, payload: Record<string, unknown>, aggregateUuid: UUID, aggregateType: string);
117
91
  get aggregateUuid(): UUID;
118
92
  get aggregateType(): string;
119
- get eventType(): string;
120
- get topic(): string;
121
- get payload(): string;
122
- get status(): ProcessStatus;
123
- get attempts(): number;
124
- get errorMessage(): string | undefined;
125
- get publishedAt(): DateTime | undefined;
126
- get lastAttempt(): DateTime | undefined;
127
- get createdAt(): DateTime;
128
- incrementAttempts(): void;
129
- markProcessed(): void;
130
- markProcessing(): void;
131
- markWithError(error: string): void;
132
- toPrimitives(): Record<string, unknown>;
133
- static reconstitute(data: Record<string, any>): OutboxRecord;
134
93
  }
135
94
 
136
95
  interface BaseEntity {
@@ -141,16 +100,16 @@ interface BaseEntity {
141
100
  }
142
101
  declare abstract class DomainEntity<T extends BaseEntity> {
143
102
  protected readonly props: T;
144
- protected _events: OutboxRecord[];
103
+ protected _events: DomainEvent[];
145
104
  protected constructor(props: T);
146
- protected recordEvent(event: OutboxRecord): void;
105
+ protected recordEvent(event: DomainEvent): void;
147
106
  protected touch(): void;
148
107
  get uuid(): UUID;
149
108
  get createdAt(): DateTime;
150
109
  get updatedAt(): DateTime;
151
110
  get deletedAt(): DateTime | undefined;
152
111
  get isDeleted(): boolean;
153
- pullEvents(): OutboxRecord[];
112
+ pullEvents(): DomainEvent[];
154
113
  abstract equals(entity?: DomainEntity<T>): boolean;
155
114
  abstract toProps(): T;
156
115
  abstract toPrimitives(): Record<string, unknown>;
@@ -181,14 +140,6 @@ declare class UsageError extends DomainError {
181
140
  constructor(type: string, vars?: Record<string, any>);
182
141
  }
183
142
 
184
- declare class DomainEvent extends BaseEvent {
185
- protected readonly _aggregateUuid: UUID;
186
- protected readonly _aggregateType: string;
187
- protected constructor(tenantUuid: UUID, version: string, type: string, payload: Record<string, unknown>, aggregateUuid: UUID, aggregateType: string);
188
- get aggregateUuid(): UUID;
189
- get aggregateType(): string;
190
- }
191
-
192
143
  declare class PaymentGateway extends ValueObject<string> {
193
144
  static readonly SUPPORTED: readonly string[];
194
145
  private static readonly EXTERNALS;
@@ -369,6 +320,55 @@ declare class Price extends ValueObject<PriceProps> {
369
320
  static createFromPrimitives(data: Record<string, unknown>): Price;
370
321
  }
371
322
 
323
+ declare class ProcessStatus extends ValueObject<string> {
324
+ static readonly SUPPORTED: readonly string[];
325
+ static readonly PENDING: ProcessStatus;
326
+ static readonly PROCESSING: ProcessStatus;
327
+ static readonly PROCESSED: ProcessStatus;
328
+ static readonly FAILED: ProcessStatus;
329
+ static readonly DEAD: ProcessStatus;
330
+ private constructor();
331
+ protected validate(value: string): void;
332
+ toPrimitives(): Record<string, unknown>;
333
+ static create(status: string): ProcessStatus;
334
+ }
335
+
336
+ declare class OutboxRecord {
337
+ private readonly _eventUuid;
338
+ private readonly _eventType;
339
+ private readonly _tenantUuid;
340
+ private readonly _aggregateUuid;
341
+ private readonly _aggregateType;
342
+ private readonly _topic;
343
+ private readonly _payload;
344
+ private _status;
345
+ private _attempts;
346
+ private _errorMessage;
347
+ private _publishedAt;
348
+ private _lastAttempt;
349
+ private readonly _createdAt;
350
+ protected constructor(eventUuid: UUID, eventType: string, tenantUuid: UUID, aggregateUuid: UUID, aggregateType: string, topic: string, payload: string, status: ProcessStatus, attempts: number, errorMessage: string | undefined, publishedAt: DateTime | undefined, lastAttempt: DateTime | undefined, createdAt: DateTime);
351
+ get eventUuid(): UUID;
352
+ get tenantUuid(): UUID;
353
+ get aggregateUuid(): UUID;
354
+ get aggregateType(): string;
355
+ get eventType(): string;
356
+ get topic(): string;
357
+ get payload(): string;
358
+ get status(): ProcessStatus;
359
+ get attempts(): number;
360
+ get errorMessage(): string | undefined;
361
+ get publishedAt(): DateTime | undefined;
362
+ get lastAttempt(): DateTime | undefined;
363
+ get createdAt(): DateTime;
364
+ incrementAttempts(): void;
365
+ markProcessed(): void;
366
+ markProcessing(): void;
367
+ markWithError(error: string): void;
368
+ toPrimitives(): Record<string, unknown>;
369
+ static reconstitute(data: Record<string, any>): OutboxRecord;
370
+ }
371
+
372
372
  interface EventBusRepository {
373
373
  create(event: OutboxRecord): Promise<void>;
374
374
  update(event: OutboxRecord): Promise<void>;
@@ -579,6 +579,22 @@ declare class MysqlConnection implements DatabaseConnection<string, any[], RowDa
579
579
  close(): Promise<void>;
580
580
  }
581
581
 
582
+ declare class DefaultOutboxRunner {
583
+ private readonly uowFactory;
584
+ private readonly eventBusRepository;
585
+ private readonly eventManager;
586
+ private readonly interval;
587
+ private readonly maxEvents;
588
+ private errors;
589
+ constructor(uowFactory: BasicUnitOfWorkFactory, eventBusRepository: EventBusRepository, eventManager: EventManager);
590
+ private addError;
591
+ private logErrors;
592
+ private sleep;
593
+ private processOutboxRecord;
594
+ private process;
595
+ start(): Promise<void>;
596
+ }
597
+
582
598
  interface ExchangeRatesProps {
583
599
  base: Currency;
584
600
  rates: Record<string, number>;
@@ -603,4 +619,4 @@ declare class StringVars {
603
619
  }): string;
604
620
  }
605
621
 
606
- export { BaseEntity, BaseEvent, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Country, Currency, DatabaseConnection, DatabaseConnector, DateTime, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, EventManager, EventManagerConnection, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, IntegrationEvent, InternalError, KafkaManager, Language, Logger, MysqlConnection, MysqlConnector, OutboxRecord, PaymentGateway, PaymentStatus, Price, ProcessStatus, RouteCallback, RoutesCallbackList, StringVars, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
622
+ export { BaseEntity, BaseEvent, BaseObject, BasicUnitOfWork, BasicUnitOfWorkFactory, Country, Currency, DatabaseConnection, DatabaseConnector, DateTime, DefaultOutboxRunner, DomainEntity, DomainError, DomainEvent, Email, ErrorManager, ErrorManagerHandleResult, ErrorTemplate, EventBus, EventBusMysqlRepository, EventBusRepository, EventManager, EventManagerConnection, ExchangeRates, FatalError, HttpController, HttpHealthCheckController, HttpNotFoundController, HttpRequest, HttpResponse, IntegrationEvent, InternalError, KafkaManager, Language, Logger, MysqlConnection, MysqlConnector, OutboxRecord, PaymentGateway, PaymentStatus, Price, ProcessStatus, RouteCallback, RoutesCallbackList, StringVars, UUID, UnitOfWork, UploadedFile, UsageError, ValueObject, adaptExpressErrorHandler, adaptExpressRoute };
package/dist/index.js CHANGED
@@ -1524,7 +1524,7 @@ var _MysqlConnector = class _MysqlConnector {
1524
1524
  password: process.env.DB_PASSWORD,
1525
1525
  database: process.env.DB_DATABASE,
1526
1526
  dateStrings: true,
1527
- connectionLimit: Number(process.env.DB_POOL_SIZE) || _MysqlConnector.DEFAULT_POOL_SIZE,
1527
+ connectionLimit: Number(process.env.DB_POOL_SIZE ?? _MysqlConnector.DEFAULT_POOL_SIZE),
1528
1528
  decimalNumbers: true
1529
1529
  });
1530
1530
  }
@@ -1591,6 +1591,64 @@ var MysqlConnection = class {
1591
1591
  }
1592
1592
  };
1593
1593
 
1594
+ // src/infrastructure/runners/default-outbox-runner.ts
1595
+ var DefaultOutboxRunner = class {
1596
+ constructor(uowFactory, eventBusRepository, eventManager) {
1597
+ this.uowFactory = uowFactory;
1598
+ this.eventBusRepository = eventBusRepository;
1599
+ this.eventManager = eventManager;
1600
+ this.interval = Number(process.env.OUTBOX_RUNNER_INTERVAL_MS || 5e3);
1601
+ this.maxEvents = Number(process.env.OUTBOX_RUNNER_MAX_EVENTS || 20);
1602
+ }
1603
+ addError(error) {
1604
+ this.errors.push(error);
1605
+ }
1606
+ logErrors() {
1607
+ if (this.errors.length > 0) {
1608
+ console.error(this.errors);
1609
+ this.errors = [];
1610
+ }
1611
+ }
1612
+ async sleep() {
1613
+ return new Promise((r) => setTimeout(r, this.maxEvents));
1614
+ }
1615
+ async processOutboxRecord(e) {
1616
+ try {
1617
+ e.markProcessing();
1618
+ await this.eventBusRepository.update(e);
1619
+ await this.eventManager.send(e.topic, e.payload);
1620
+ e.markProcessed();
1621
+ } catch (error) {
1622
+ const type = String(error.type);
1623
+ e.markWithError(type);
1624
+ throw new Error(type);
1625
+ } finally {
1626
+ await this.eventBusRepository.update(e);
1627
+ }
1628
+ }
1629
+ async process() {
1630
+ const uow = await this.uowFactory.create();
1631
+ await uow.execute(async () => {
1632
+ const records = await this.eventBusRepository.listPending(this.maxEvents);
1633
+ for (let record of records) {
1634
+ try {
1635
+ await this.processOutboxRecord(record);
1636
+ } catch (error) {
1637
+ this.addError({ eventUuid: record.eventUuid.value, error: error.toString() });
1638
+ }
1639
+ }
1640
+ this.logErrors();
1641
+ });
1642
+ }
1643
+ async start() {
1644
+ console.log("[outbox-runner]: start");
1645
+ for (; ; ) {
1646
+ await this.process();
1647
+ await this.sleep();
1648
+ }
1649
+ }
1650
+ };
1651
+
1594
1652
  // src/utils/ExchangeRates.ts
1595
1653
  var ExchangeRates = class _ExchangeRates extends BaseObject {
1596
1654
  constructor(props) {
@@ -1650,6 +1708,7 @@ export {
1650
1708
  Country,
1651
1709
  Currency,
1652
1710
  DateTime,
1711
+ DefaultOutboxRunner,
1653
1712
  DomainEntity,
1654
1713
  DomainError,
1655
1714
  DomainEvent,