@tsed/mikro-orm 7.33.4 → 7.34.1

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.
Files changed (35) hide show
  1. package/lib/cjs/MikroOrmModule.js +1 -1
  2. package/lib/cjs/MikroOrmModule.js.map +1 -1
  3. package/lib/cjs/index.js +3 -1
  4. package/lib/cjs/index.js.map +1 -1
  5. package/lib/cjs/interceptors/TransactionalInterceptor.js +56 -39
  6. package/lib/cjs/interceptors/TransactionalInterceptor.js.map +1 -1
  7. package/lib/cjs/interfaces/AfterTransactionCommit.js +3 -0
  8. package/lib/cjs/interfaces/AfterTransactionCommit.js.map +1 -0
  9. package/lib/cjs/interfaces/BeforeTransactionCommit.js +3 -0
  10. package/lib/cjs/interfaces/BeforeTransactionCommit.js.map +1 -0
  11. package/lib/cjs/interfaces/RetryStrategy.js.map +1 -0
  12. package/lib/esm/MikroOrmModule.js +1 -1
  13. package/lib/esm/MikroOrmModule.js.map +1 -1
  14. package/lib/esm/index.js +3 -1
  15. package/lib/esm/index.js.map +1 -1
  16. package/lib/esm/interceptors/TransactionalInterceptor.js +57 -40
  17. package/lib/esm/interceptors/TransactionalInterceptor.js.map +1 -1
  18. package/lib/esm/interfaces/AfterTransactionCommit.js +2 -0
  19. package/lib/esm/interfaces/AfterTransactionCommit.js.map +1 -0
  20. package/lib/esm/interfaces/BeforeTransactionCommit.js +2 -0
  21. package/lib/esm/interfaces/BeforeTransactionCommit.js.map +1 -0
  22. package/lib/esm/interfaces/RetryStrategy.js.map +1 -0
  23. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  24. package/lib/tsconfig.tsbuildinfo +1 -1
  25. package/lib/types/index.d.ts +3 -1
  26. package/lib/types/interceptors/TransactionalInterceptor.d.ts +10 -4
  27. package/lib/types/interfaces/AfterTransactionCommit.d.ts +4 -0
  28. package/lib/types/interfaces/BeforeTransactionCommit.d.ts +4 -0
  29. package/lib/types/{services → interfaces}/RetryStrategy.d.ts +1 -1
  30. package/package.json +12 -12
  31. package/readme.md +53 -18
  32. package/lib/cjs/services/RetryStrategy.js.map +0 -1
  33. package/lib/esm/services/RetryStrategy.js.map +0 -1
  34. /package/lib/cjs/{services → interfaces}/RetryStrategy.js +0 -0
  35. /package/lib/esm/{services → interfaces}/RetryStrategy.js +0 -0
@@ -9,7 +9,9 @@ export * from "./decorators/subscriber";
9
9
  export * from "./decorators/transactional";
10
10
  export * from "./filters/OptimisticLockErrorFilter";
11
11
  export * from "./interceptors/TransactionalInterceptor";
12
+ export * from "./interfaces/AfterTransactionCommit";
13
+ export * from "./interfaces/BeforeTransactionCommit";
14
+ export * from "./interfaces/RetryStrategy";
12
15
  export * from "./services/MikroOrmContext";
13
16
  export * from "./services/MikroOrmFactory";
14
17
  export * from "./services/MikroOrmRegistry";
15
- export * from "./services/RetryStrategy";
@@ -1,6 +1,6 @@
1
- import { InterceptorContext, InterceptorMethods, InterceptorNext } from "@tsed/di";
1
+ import { InjectorService, InterceptorContext, InterceptorMethods, InterceptorNext } from "@tsed/di";
2
2
  import { Logger } from "@tsed/logger";
3
- import { RetryStrategy } from "../services/RetryStrategy";
3
+ import { RetryStrategy } from "../interfaces/RetryStrategy";
4
4
  import { MikroOrmContext } from "../services/MikroOrmContext";
5
5
  import { MikroOrmRegistry } from "../services/MikroOrmRegistry";
6
6
  import { FlushMode, IsolationLevel } from "@mikro-orm/core";
@@ -12,14 +12,20 @@ export interface TransactionOptions {
12
12
  contextName?: string;
13
13
  }
14
14
  export declare class TransactionalInterceptor implements InterceptorMethods {
15
+ private readonly injector;
15
16
  private readonly registry;
16
17
  private readonly context;
17
18
  private readonly logger;
18
19
  private readonly retryStrategy?;
19
- constructor(registry: MikroOrmRegistry, context: MikroOrmContext, logger: Logger, retryStrategy?: RetryStrategy | undefined);
20
- intercept(context: InterceptorContext<unknown>, next: InterceptorNext): unknown;
20
+ constructor(injector: InjectorService, registry: MikroOrmRegistry, context: MikroOrmContext, logger: Logger, retryStrategy?: RetryStrategy | undefined);
21
+ intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown>;
21
22
  private runWithinCtx;
22
23
  private runWithinNewCtx;
24
+ private getGlobalEntityManager;
23
25
  private extractContextName;
24
26
  private executeInTransaction;
27
+ private executeHandlerDelegate;
28
+ private beforeTransactionCommit;
29
+ private afterTransactionCommit;
30
+ private getEntityManager;
25
31
  }
@@ -0,0 +1,4 @@
1
+ import { EntityManager } from "@mikro-orm/core";
2
+ export interface AfterTransactionCommit {
3
+ $afterTransactionCommit(em: EntityManager): Promise<unknown> | unknown;
4
+ }
@@ -0,0 +1,4 @@
1
+ import { EntityManager } from "@mikro-orm/core";
2
+ export interface BeforeTransactionCommit {
3
+ $beforeTransactionCommit(em: EntityManager): Promise<unknown> | unknown;
4
+ }
@@ -1,4 +1,4 @@
1
1
  export interface RetryStrategy {
2
- acquire<T extends (...args: unknown[]) => unknown>(task: T): Promise<ReturnType<T>>;
2
+ acquire<T extends (...args: unknown[]) => unknown>(task: T): Promise<ReturnType<T> extends Promise<infer V> ? V : ReturnType<T>>;
3
3
  }
4
4
  export declare const RetryStrategy: unique symbol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/mikro-orm",
3
- "version": "7.33.4",
3
+ "version": "7.34.1",
4
4
  "description": "MikroORM package for Ts.ED framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
@@ -27,23 +27,23 @@
27
27
  "devDependencies": {
28
28
  "@mikro-orm/core": "^5.2.2",
29
29
  "@mikro-orm/mongodb": "^5.2.2",
30
- "@tsed/common": "7.33.4",
31
- "@tsed/core": "7.33.4",
32
- "@tsed/di": "7.33.4",
33
- "@tsed/eslint": "7.33.4",
34
- "@tsed/json-mapper": "7.33.4",
30
+ "@tsed/common": "7.34.1",
31
+ "@tsed/core": "7.34.1",
32
+ "@tsed/di": "7.34.1",
33
+ "@tsed/eslint": "7.34.1",
34
+ "@tsed/json-mapper": "7.34.1",
35
35
  "@tsed/logger": ">=6.1.1",
36
- "@tsed/schema": "7.33.4",
37
- "@tsed/testing-mongoose": "7.33.4",
38
- "@tsed/typescript": "7.33.4",
36
+ "@tsed/schema": "7.34.1",
37
+ "@tsed/testing-mongoose": "7.34.1",
38
+ "@tsed/typescript": "7.34.1",
39
39
  "eslint": "^8.12.0",
40
40
  "ts-mockito": "^2.6.1"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@mikro-orm/core": ">=4.5.9",
44
- "@tsed/common": "^7.33.4",
45
- "@tsed/core": "^7.33.4",
46
- "@tsed/di": "^7.33.4",
44
+ "@tsed/common": "^7.34.1",
45
+ "@tsed/core": "^7.34.1",
46
+ "@tsed/di": "^7.34.1",
47
47
  "@tsed/logger": ">=6.2.2"
48
48
  },
49
49
  "repository": "https://github.com/tsedio/tsed",
package/readme.md CHANGED
@@ -316,16 +316,14 @@ The `@Transactional()` decorator allows you to enable a retry policy for the par
316
316
  ```typescript
317
317
  import {OptimisticLockError} from "@mikro-orm/core";
318
318
  import {RetryStrategy} from "@tsed/mikro-orm";
319
+ import {OverrideProvider} from "@tsed/di";
320
+ import {setTimeout} from "timers/promises";
319
321
 
320
- export interface ExponentialBackoffOptions {
321
- maxDepth: number;
322
- }
323
-
322
+ @OverrideProvider(RetryStrategy)
324
323
  export class ExponentialBackoff implements RetryStrategy {
324
+ private readonly maxDepth = 3;
325
325
  private depth = 0;
326
326
 
327
- constructor(private readonly options: ExponentialBackoffOptions) {}
328
-
329
327
  public async acquire<T extends (...args: unknown[]) => unknown>(task: T): Promise<ReturnType<T>> {
330
328
  try {
331
329
  return (await task()) as ReturnType<T>;
@@ -343,24 +341,13 @@ export class ExponentialBackoff implements RetryStrategy {
343
341
  }
344
342
 
345
343
  private async retry<T extends (...args: unknown[]) => unknown>(task: T): Promise<ReturnType<T>> {
346
- await this.sleep(2 ** this.depth * 50);
344
+ await setTimeout(2 ** this.depth * 50);
347
345
 
348
346
  this.depth += 1;
349
347
 
350
348
  return this.acquire(task);
351
349
  }
352
-
353
- private sleep(milliseconds: number): Promise<void> {
354
- return new Promise((resolve) => setTimeout(resolve, milliseconds));
355
- }
356
350
  }
357
-
358
- registerProvider({
359
- provide: RetryStrategy,
360
- useFactory(): ExponentialBackoff {
361
- return new ExponentialBackoff({maxDepth: 3});
362
- }
363
- });
364
351
  ```
365
352
 
366
353
  `ExponentialBackoff` invokes passed function recursively is contained in a try/catch block. The method returns control to the interceptor if the call to the `task` function succeeds without throwing an exception. If the `task` method fails, the catch block examines the reason for the failure. If it's optimistic locking the code waits for a short delay before retrying the operation.
@@ -417,6 +404,54 @@ export class SomeSubscriber implements EventSubscriber {
417
404
  }
418
405
  ```
419
406
 
407
+ ## Transaction Hooks
408
+
409
+ The transaction hooks allow you to customize the default transaction behavior. These hooks enable you to execute custom code before and after committing data to the database. These transaction hooks provide a flexible way to extend the default transaction behavior and implement advanced patterns such as the Inbox pattern or domain event dispatching.
410
+
411
+ ### BeforeTransactionCommit Hook
412
+
413
+ The `BeforeTransactionCommit` interface allows you to define hooks that are executed right before committing data to the database. This hook provides a way to modify data within the same transaction context and perform additional operations before the transaction is committed.
414
+
415
+ To use the `BeforeTransactionCommit` hook, first, you have to implement the `BeforeTransactionCommit` interface:
416
+
417
+ ```typescript
418
+ import {BeforeTransactionCommit} from "@tsed/mikro-orm";
419
+ import {EntityManager} from "@mikro-orm/core";
420
+ import {Injectable} from "@tsed/di";
421
+
422
+ @Injectable()
423
+ export class Hooks implements BeforeTransactionCommit {
424
+ $beforeTransactionCommit(em: EntityManager): Promise<unknown> | unknown {
425
+ // Custom code executed before committing data
426
+ }
427
+ }
428
+ ```
429
+
430
+ Then just write your code inside the `$beforeTransactionCommit` method. This code will be executed before the transaction is committed.
431
+
432
+ ### AfterTransactionCommit Hook
433
+
434
+ The `AfterTransactionCommit` interface allows you to define hooks that are executed right after committing data to the database. This hook enables you to execute code after the data is committed, making multiple transactions.
435
+
436
+ To use the `AfterTransactionCommit` hook, you have to implement the `AfterTransactionCommit` interface:
437
+
438
+ ```typescript
439
+ import {AfterTransactionCommit} from "@tsed/mikro-orm";
440
+ import {EntityManager} from "@mikro-orm/core";
441
+ import {Injectable} from "@tsed/di";
442
+
443
+ @Injectable()
444
+ export class Hooks implements AfterTransactionCommit {
445
+ $afterTransactionCommit(em: EntityManager): Promise<unknown> | unknown {
446
+ // Custom code executed after committing data
447
+ }
448
+ }
449
+ ```
450
+
451
+ ::: tip Note
452
+ When using the `AfterTransactionCommit` hook, you need to handle eventual consistency and compensatory actions in case of failures on your own.
453
+ :::
454
+
420
455
  ## Contributors
421
456
 
422
457
  Please read [contributing guidelines here](https://tsed.io/contributing.html)
@@ -1 +0,0 @@
1
- {"version":3,"file":"RetryStrategy.js","sourceRoot":"","sources":["../../../src/services/RetryStrategy.ts"],"names":[],"mappings":";;;AAIa,QAAA,aAAa,GAAkB,MAAM,CAAC,eAAe,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"RetryStrategy.js","sourceRoot":"","sources":["../../../src/services/RetryStrategy.ts"],"names":[],"mappings":"AAIA,MAAM,CAAC,MAAM,aAAa,GAAkB,MAAM,CAAC,eAAe,CAAC,CAAC"}