@tsed/mikro-orm 7.0.0-beta.2 → 7.0.0-beta.20

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 (97) hide show
  1. package/jest.config.js +4 -4
  2. package/lib/cjs/MikroOrmModule.d.ts +7 -3
  3. package/lib/cjs/MikroOrmModule.js +30 -12
  4. package/lib/cjs/MikroOrmModule.js.map +1 -1
  5. package/lib/cjs/decorators/entityManager.d.ts +6 -6
  6. package/lib/cjs/decorators/entityManager.js +6 -6
  7. package/lib/cjs/decorators/entityManager.js.map +1 -1
  8. package/lib/cjs/decorators/orm.d.ts +5 -0
  9. package/lib/cjs/decorators/orm.js +12 -0
  10. package/lib/cjs/decorators/orm.js.map +1 -0
  11. package/lib/cjs/decorators/transactional.d.ts +6 -2
  12. package/lib/cjs/decorators/transactional.js +6 -2
  13. package/lib/cjs/decorators/transactional.js.map +1 -1
  14. package/lib/cjs/filters/OptimisticLockErrorFilter.js +1 -1
  15. package/lib/cjs/index.d.ts +12 -4
  16. package/lib/cjs/index.js +13 -5
  17. package/lib/cjs/index.js.map +1 -1
  18. package/lib/cjs/interceptors/TransactionalInterceptor.d.ts +10 -4
  19. package/lib/cjs/interceptors/TransactionalInterceptor.js +56 -40
  20. package/lib/cjs/interceptors/TransactionalInterceptor.js.map +1 -1
  21. package/lib/cjs/package.json +3 -0
  22. package/lib/cjs/services/MikroOrmContext.d.ts +9 -0
  23. package/lib/cjs/services/MikroOrmContext.js +26 -0
  24. package/lib/cjs/services/MikroOrmContext.js.map +1 -0
  25. package/lib/cjs/services/MikroOrmFactory.d.ts +4 -2
  26. package/lib/cjs/services/MikroOrmFactory.js +12 -10
  27. package/lib/cjs/services/MikroOrmFactory.js.map +1 -1
  28. package/lib/cjs/services/MikroOrmRegistry.d.ts +7 -5
  29. package/lib/cjs/services/MikroOrmRegistry.js +32 -27
  30. package/lib/cjs/services/MikroOrmRegistry.js.map +1 -1
  31. package/lib/esm/MikroOrmModule.js +25 -7
  32. package/lib/esm/MikroOrmModule.js.map +1 -1
  33. package/lib/esm/decorators/entityManager.js +6 -6
  34. package/lib/esm/decorators/entityManager.js.map +1 -1
  35. package/lib/esm/decorators/orm.js +8 -0
  36. package/lib/esm/decorators/orm.js.map +1 -0
  37. package/lib/esm/decorators/transactional.js +6 -2
  38. package/lib/esm/decorators/transactional.js.map +1 -1
  39. package/lib/esm/index.js +13 -5
  40. package/lib/esm/index.js.map +1 -1
  41. package/lib/esm/interceptors/TransactionalInterceptor.js +49 -33
  42. package/lib/esm/interceptors/TransactionalInterceptor.js.map +1 -1
  43. package/lib/esm/package.json +3 -0
  44. package/lib/esm/services/MikroOrmContext.js +23 -0
  45. package/lib/esm/services/MikroOrmContext.js.map +1 -0
  46. package/lib/esm/services/MikroOrmFactory.js +12 -10
  47. package/lib/esm/services/MikroOrmFactory.js.map +1 -1
  48. package/lib/esm/services/MikroOrmRegistry.js +29 -24
  49. package/lib/esm/services/MikroOrmRegistry.js.map +1 -1
  50. package/lib/types/MikroOrmModule.d.ts +7 -3
  51. package/lib/types/decorators/entityManager.d.ts +6 -6
  52. package/lib/types/decorators/orm.d.ts +5 -0
  53. package/lib/types/decorators/transactional.d.ts +6 -2
  54. package/lib/types/index.d.ts +12 -4
  55. package/lib/types/interceptors/TransactionalInterceptor.d.ts +10 -4
  56. package/lib/types/services/MikroOrmContext.d.ts +9 -0
  57. package/lib/types/services/MikroOrmFactory.d.ts +4 -2
  58. package/lib/types/services/MikroOrmRegistry.d.ts +7 -5
  59. package/package.json +23 -18
  60. package/readme.md +75 -46
  61. package/tsconfig.compile.esm.json +1 -12
  62. package/lib/cjs/decorators/connection.d.ts +0 -1
  63. package/lib/cjs/decorators/connection.js +0 -8
  64. package/lib/cjs/decorators/connection.js.map +0 -1
  65. package/lib/cjs/decorators/index.d.ts +0 -3
  66. package/lib/cjs/decorators/index.js +0 -7
  67. package/lib/cjs/decorators/index.js.map +0 -1
  68. package/lib/cjs/filters/index.d.ts +0 -1
  69. package/lib/cjs/filters/index.js +0 -5
  70. package/lib/cjs/filters/index.js.map +0 -1
  71. package/lib/cjs/interceptors/index.d.ts +0 -1
  72. package/lib/cjs/interceptors/index.js +0 -5
  73. package/lib/cjs/interceptors/index.js.map +0 -1
  74. package/lib/cjs/services/DBContext.d.ts +0 -9
  75. package/lib/cjs/services/DBContext.js +0 -37
  76. package/lib/cjs/services/DBContext.js.map +0 -1
  77. package/lib/cjs/services/index.d.ts +0 -4
  78. package/lib/cjs/services/index.js +0 -8
  79. package/lib/cjs/services/index.js.map +0 -1
  80. package/lib/esm/decorators/connection.js +0 -4
  81. package/lib/esm/decorators/connection.js.map +0 -1
  82. package/lib/esm/decorators/index.js +0 -4
  83. package/lib/esm/decorators/index.js.map +0 -1
  84. package/lib/esm/filters/index.js +0 -2
  85. package/lib/esm/filters/index.js.map +0 -1
  86. package/lib/esm/interceptors/index.js +0 -2
  87. package/lib/esm/interceptors/index.js.map +0 -1
  88. package/lib/esm/services/DBContext.js +0 -34
  89. package/lib/esm/services/DBContext.js.map +0 -1
  90. package/lib/esm/services/index.js +0 -5
  91. package/lib/esm/services/index.js.map +0 -1
  92. package/lib/types/decorators/connection.d.ts +0 -1
  93. package/lib/types/decorators/index.d.ts +0 -3
  94. package/lib/types/filters/index.d.ts +0 -1
  95. package/lib/types/interceptors/index.d.ts +0 -1
  96. package/lib/types/services/DBContext.d.ts +0 -9
  97. package/lib/types/services/index.d.ts +0 -4
@@ -4,11 +4,13 @@ import { Logger } from "@tsed/logger";
4
4
  export declare class MikroOrmRegistry {
5
5
  private readonly logger;
6
6
  private readonly mikroOrmFactory;
7
- private readonly connections;
7
+ private readonly DEFAULT_CONTEXT_NAME;
8
+ private readonly instances;
8
9
  constructor(logger: Logger, mikroOrmFactory: MikroOrmFactory);
9
- createConnection<T extends DatabaseDriver>(connectionOptions: Options<T>): Promise<MikroORM>;
10
- get(id?: string): MikroORM;
11
- has(id?: string): boolean;
12
- closeConnections(): Promise<void>;
10
+ register<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
11
+ get(contextName?: string): MikroORM | undefined;
12
+ has(contextName?: string): boolean;
13
+ values(): IterableIterator<MikroORM>;
14
+ clear(): Promise<void>;
13
15
  private dispose;
14
16
  }
@@ -7,46 +7,51 @@ const di_1 = require("@tsed/di");
7
7
  const core_1 = require("@tsed/core");
8
8
  const logger_1 = require("@tsed/logger");
9
9
  let MikroOrmRegistry = class MikroOrmRegistry {
10
+ logger;
11
+ mikroOrmFactory;
12
+ DEFAULT_CONTEXT_NAME = "default";
13
+ instances = new Map();
10
14
  constructor(logger, mikroOrmFactory) {
11
15
  this.logger = logger;
12
16
  this.mikroOrmFactory = mikroOrmFactory;
13
- this.connections = new Map();
14
17
  }
15
- async createConnection(connectionOptions) {
16
- const connectionName = (0, core_1.getValue)(connectionOptions, "contextName", "default");
17
- if (this.has(connectionName)) {
18
- return this.get(connectionName);
18
+ async register(options) {
19
+ const contextName = (0, core_1.getValue)(options, "contextName", this.DEFAULT_CONTEXT_NAME);
20
+ if (this.has(contextName)) {
21
+ return this.get(contextName);
19
22
  }
20
- this.logger.info(`Create connection with MikroOrm to database: %s`, connectionName);
21
- this.logger.debug(`options: %j`, connectionOptions);
22
- const connection = await this.mikroOrmFactory.create(connectionOptions);
23
- this.connections.set(connectionName, connection);
24
- this.logger.info(`Connected with MikroOrm to database: %s`, connectionName);
25
- return connection;
23
+ this.logger.info(`Create connection with MikroOrm to database: %s`, contextName);
24
+ this.logger.debug(`options: %j`, options);
25
+ const instance = await this.mikroOrmFactory.create(options);
26
+ this.instances.set(contextName, instance);
27
+ this.logger.info(`Connected with MikroOrm to database: %s`, contextName);
28
+ return instance;
26
29
  }
27
- get(id = "default") {
28
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
29
- return this.connections.get(id);
30
+ get(contextName) {
31
+ return this.instances.get(contextName ?? this.DEFAULT_CONTEXT_NAME);
30
32
  }
31
- has(id = "default") {
32
- return this.connections.has(id);
33
+ has(contextName) {
34
+ return this.instances.has(contextName ?? this.DEFAULT_CONTEXT_NAME);
33
35
  }
34
- async closeConnections() {
35
- const connections = [...this.connections.values()];
36
- await Promise.all(connections.map((connection) => (0, core_1.catchAsyncError)(() => this.dispose(connection))));
37
- this.connections.clear();
36
+ values() {
37
+ return this.instances.values();
38
38
  }
39
- async dispose(connection) {
40
- if (await connection.isConnected()) {
41
- await connection.close(false);
39
+ async clear() {
40
+ const instances = [...this.instances.values()];
41
+ await Promise.all(instances.map((instance) => (0, core_1.catchAsyncError)(() => this.dispose(instance))));
42
+ this.instances.clear();
43
+ }
44
+ async dispose(instance) {
45
+ if (await instance.isConnected()) {
46
+ await instance.close(false);
42
47
  }
43
48
  }
44
49
  };
45
- MikroOrmRegistry = (0, tslib_1.__decorate)([
50
+ MikroOrmRegistry = tslib_1.__decorate([
46
51
  (0, di_1.Injectable)(),
47
- (0, tslib_1.__param)(0, (0, di_1.Inject)()),
48
- (0, tslib_1.__param)(1, (0, di_1.Inject)()),
49
- (0, tslib_1.__metadata)("design:paramtypes", [logger_1.Logger, MikroOrmFactory_1.MikroOrmFactory])
52
+ tslib_1.__param(0, (0, di_1.Inject)()),
53
+ tslib_1.__param(1, (0, di_1.Inject)()),
54
+ tslib_1.__metadata("design:paramtypes", [logger_1.Logger, MikroOrmFactory_1.MikroOrmFactory])
50
55
  ], MikroOrmRegistry);
51
56
  exports.MikroOrmRegistry = MikroOrmRegistry;
52
57
  //# sourceMappingURL=MikroOrmRegistry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"MikroOrmRegistry.js","sourceRoot":"","sources":["../../../src/services/MikroOrmRegistry.ts"],"names":[],"mappings":";;;;AAAA,uDAAkD;AAClD,iCAA4C;AAE5C,qCAAqD;AACrD,yCAAoC;AAGpC,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAG3B,YAAuC,MAAc,EAA6B,eAAgC;QAA3E,WAAM,GAAN,MAAM,CAAQ;QAA6B,oBAAe,GAAf,eAAe,CA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AAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAE9G,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC;IAEO,KAAK,CAAC,OAAO,CAAC,UAAoB;QACxC,IAAI,MAAM,UAAU,CAAC,WAAW,EAAE,EAAE;YAClC,MAAM,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC/B;IACH,CAAC;CACF,CAAA;AA9CY,gBAAgB;IAD5B,IAAA,eAAU,GAAE;IAIE,wBAAA,IAAA,WAAM,GAAE,CAAA;IAAmC,wBAAA,IAAA,WAAM,GAAE,CAAA;kDAAjB,eAAM,EAA8C,iCAAe;GAHvG,gBAAgB,CA8C5B;AA9CY,4CAAgB","sourcesContent":["import {MikroOrmFactory} from \"./MikroOrmFactory\";\nimport {Inject, Injectable} from \"@tsed/di\";\nimport {IDatabaseDriver as DatabaseDriver, MikroORM, Options} from \"@mikro-orm/core\";\nimport {catchAsyncError, getValue} from \"@tsed/core\";\nimport {Logger} from \"@tsed/logger\";\n\n@Injectable()\nexport class MikroOrmRegistry {\n private readonly connections = new Map<string, MikroORM>();\n\n constructor(@Inject() private readonly logger: Logger, @Inject() private readonly mikroOrmFactory: MikroOrmFactory) {}\n\n public async createConnection<T extends DatabaseDriver>(connectionOptions: Options<T>): Promise<MikroORM> {\n const connectionName = getValue<string>(connectionOptions, \"contextName\", \"default\");\n\n if (this.has(connectionName)) {\n return this.get(connectionName);\n }\n\n this.logger.info(`Create connection with MikroOrm to database: %s`, connectionName);\n this.logger.debug(`options: %j`, connectionOptions);\n\n const connection = await this.mikroOrmFactory.create(connectionOptions);\n\n this.connections.set(connectionName, connection);\n\n this.logger.info(`Connected with MikroOrm to database: %s`, connectionName);\n\n return connection;\n }\n\n public get(id: string = \"default\"): MikroORM {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return this.connections.get(id)!;\n }\n\n public has(id: string = \"default\"): boolean {\n return this.connections.has(id);\n }\n\n public async closeConnections(): Promise<void> {\n const connections = [...this.connections.values()];\n\n await Promise.all(connections.map((connection: MikroORM) => catchAsyncError(() => this.dispose(connection))));\n\n this.connections.clear();\n }\n\n private async dispose(connection: MikroORM): Promise<void> {\n if (await connection.isConnected()) {\n await connection.close(false);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"MikroOrmRegistry.js","sourceRoot":"","sources":["../../../src/services/MikroOrmRegistry.ts"],"names":[],"mappings":";;;;AAAA,uDAAkD;AAClD,iCAA4C;AAE5C,qCAAqD;AACrD,yCAAoC;AAGpC,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAIY;IAA2C;IAHjE,oBAAoB,GAAG,SAAS,CAAC;IACjC,SAAS,GAAG,IAAI,GAAG,EAAoB,CAAC;IAEzD,YAAuC,MAAc,EAA6B,eAAgC;QAA3E,WAAM,GAAN,MAAM,CAAQ;QAA6B,oBAAe,GAAf,eAAe,CAAiB;IAAG,CAAC;IAE/G,KAAK,CAAC,QAAQ,CAA2B,OAAmB;QACjE,MAAM,WAAW,GAAG,IAAA,eAAQ,EAAS,OAAO,EAAE,aAAa,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;QAExF,IAAI,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;YACzB,OAAO,IAAI,CAAC,GAAG,CAAC,WAAW,CAAE,CAAC;SAC/B;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,iDAAiD,EAAE,WAAW,CAAC,CAAC;QACjF,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;QAE1C,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAE5D,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,yCAAyC,EAAE,WAAW,CAAC,CAAC;QAEzE,OAAO,QAAQ,CAAC;IAClB,CAAC;IAEM,GAAG,CAAC,WAAoB;QAC7B,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,IAAI,IAAI,CAAC,oBAAoB,CAAC,CAAC;IACtE,CAAC;IAEM,GAAG,CAAC,WAAoB;QAC7B,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,IAAI,IAAI,CAAC,oBAAoB,CAAC,CAAC;IACtE,CAAC;IAEM,MAAM;QACX,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IACjC,CAAC;IAEM,KAAK,CAAC,KAAK;QAChB,MAAM,SAAS,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;QAE/C,MAAM,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,QAAkB,EAAE,EAAE,CAAC,IAAA,sBAAe,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QAExG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;IAEO,KAAK,CAAC,OAAO,CAAC,QAAkB;QACtC,IAAI,MAAM,QAAQ,CAAC,WAAW,EAAE,EAAE;YAChC,MAAM,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC7B;IACH,CAAC;CACF,CAAA;AAlDY,gBAAgB;IAD5B,IAAA,eAAU,GAAE;IAKE,mBAAA,IAAA,WAAM,GAAE,CAAA;IAAmC,mBAAA,IAAA,WAAM,GAAE,CAAA;6CAAjB,eAAM,EAA8C,iCAAe;GAJvG,gBAAgB,CAkD5B;AAlDY,4CAAgB","sourcesContent":["import {MikroOrmFactory} from \"./MikroOrmFactory\";\nimport {Inject, Injectable} from \"@tsed/di\";\nimport {IDatabaseDriver as DatabaseDriver, MikroORM, Options} from \"@mikro-orm/core\";\nimport {catchAsyncError, getValue} from \"@tsed/core\";\nimport {Logger} from \"@tsed/logger\";\n\n@Injectable()\nexport class MikroOrmRegistry {\n private readonly DEFAULT_CONTEXT_NAME = \"default\";\n private readonly instances = new Map<string, MikroORM>();\n\n constructor(@Inject() private readonly logger: Logger, @Inject() private readonly mikroOrmFactory: MikroOrmFactory) {}\n\n public async register<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM> {\n const contextName = getValue<string>(options, \"contextName\", this.DEFAULT_CONTEXT_NAME);\n\n if (this.has(contextName)) {\n return this.get(contextName)!;\n }\n\n this.logger.info(`Create connection with MikroOrm to database: %s`, contextName);\n this.logger.debug(`options: %j`, options);\n\n const instance = await this.mikroOrmFactory.create(options);\n\n this.instances.set(contextName, instance);\n\n this.logger.info(`Connected with MikroOrm to database: %s`, contextName);\n\n return instance;\n }\n\n public get(contextName?: string): MikroORM | undefined {\n return this.instances.get(contextName ?? this.DEFAULT_CONTEXT_NAME);\n }\n\n public has(contextName?: string): boolean {\n return this.instances.has(contextName ?? this.DEFAULT_CONTEXT_NAME);\n }\n\n public values(): IterableIterator<MikroORM> {\n return this.instances.values();\n }\n\n public async clear(): Promise<void> {\n const instances = [...this.instances.values()];\n\n await Promise.all(instances.map((instance: MikroORM) => catchAsyncError(() => this.dispose(instance))));\n\n this.instances.clear();\n }\n\n private async dispose(instance: MikroORM): Promise<void> {\n if (await instance.isConnected()) {\n await instance.close(false);\n }\n }\n}\n"]}
@@ -1,13 +1,28 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { DBContext, MikroOrmRegistry, RetryStrategy } from "./services";
3
- import { Configuration, Constant, Inject, Module, registerProvider } from "@tsed/di";
2
+ import "./services/MikroOrmFactory.js";
3
+ import { Constant, Inject, Module, registerProvider } from "@tsed/di";
4
+ import { MikroOrmRegistry } from "./services/MikroOrmRegistry.js";
5
+ import { RetryStrategy } from "./services/RetryStrategy.js";
6
+ import { OptimisticLockErrorFilter } from "./filters/OptimisticLockErrorFilter.js";
7
+ import { MikroOrmContext } from "./services/MikroOrmContext.js";
4
8
  let MikroOrmModule = class MikroOrmModule {
9
+ settings;
10
+ registry;
11
+ context;
5
12
  async $onInit() {
6
- const promises = this.settings.map((opts) => this.mikroOrmRegistry.createConnection(opts));
13
+ const promises = this.settings.map((opts) => this.registry.register(opts));
7
14
  await Promise.all(promises);
8
15
  }
9
16
  $onDestroy() {
10
- return this.mikroOrmRegistry.closeConnections();
17
+ return this.registry.clear();
18
+ }
19
+ $alterRunInContext(next) {
20
+ return () => this.createContext(next);
21
+ }
22
+ createContext(next) {
23
+ const instances = [...this.registry.values()];
24
+ const managers = instances.map((orm) => orm.em);
25
+ return this.context.run(managers, next);
11
26
  }
12
27
  };
13
28
  __decorate([
@@ -17,11 +32,14 @@ __decorate([
17
32
  __decorate([
18
33
  Inject(),
19
34
  __metadata("design:type", MikroOrmRegistry)
20
- ], MikroOrmModule.prototype, "mikroOrmRegistry", void 0);
35
+ ], MikroOrmModule.prototype, "registry", void 0);
36
+ __decorate([
37
+ Inject(),
38
+ __metadata("design:type", MikroOrmContext)
39
+ ], MikroOrmModule.prototype, "context", void 0);
21
40
  MikroOrmModule = __decorate([
22
41
  Module({
23
- imports: [DBContext, MikroOrmRegistry],
24
- deps: [Configuration, MikroOrmRegistry]
42
+ responseFilters: [OptimisticLockErrorFilter]
25
43
  })
26
44
  ], MikroOrmModule);
27
45
  export { MikroOrmModule };
@@ -1 +1 @@
1
- {"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,gBAAgB,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACtE,OAAO,EAAC,aAAa,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAqB,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAoBtG,IAAa,cAAc,GAA3B,MAAa,cAAc;IAOlB,KAAK,CAAC,OAAO;QAClB,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;QAE3F,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAEM,UAAU;QACf,OAAO,IAAI,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,CAAC;IAClD,CAAC;CACF,CAAA;AAdC;IADC,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC;;gDACa;AAGtC;IADC,MAAM,EAAE;8BAC2B,gBAAgB;wDAAC;AAL1C,cAAc;IAJ1B,MAAM,CAAC;QACN,OAAO,EAAE,CAAC,SAAS,EAAE,gBAAgB,CAAC;QACtC,IAAI,EAAE,CAAC,aAAa,EAAE,gBAAgB,CAAC;KACxC,CAAC;GACW,cAAc,CAgB1B;SAhBY,cAAc;AAkB3B,uGAAuG;AACvG,gBAAgB,CAAC,EAAC,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,IAAI,EAAC,CAAC,CAAC","sourcesContent":["import {DBContext, MikroOrmRegistry, RetryStrategy} from \"./services\";\nimport {Configuration, Constant, Inject, Module, OnDestroy, OnInit, registerProvider} from \"@tsed/di\";\nimport {Options} from \"@mikro-orm/core\";\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace TsED {\n interface Configuration {\n /**\n * The the ORM configuration, entity metadata.\n * If you omit the `options` parameter, your CLI config will be used.\n */\n mikroOrm?: Options[];\n }\n }\n}\n\n@Module({\n imports: [DBContext, MikroOrmRegistry],\n deps: [Configuration, MikroOrmRegistry]\n})\nexport class MikroOrmModule implements OnDestroy, OnInit {\n @Constant(\"mikroOrm\", [])\n private readonly settings!: Options[];\n\n @Inject()\n private readonly mikroOrmRegistry!: MikroOrmRegistry;\n\n public async $onInit(): Promise<void> {\n const promises = this.settings.map((opts) => this.mikroOrmRegistry.createConnection(opts));\n\n await Promise.all(promises);\n }\n\n public $onDestroy(): Promise<void> {\n return this.mikroOrmRegistry.closeConnections();\n }\n}\n\n// TODO: the IoC container should provide null or undefined by default until tsedio/tsed#1694 is closed\nregisterProvider({provide: RetryStrategy, useValue: null});\n"]}
1
+ {"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";AAAA,OAAO,4BAA4B,CAAC;AACpC,OAAO,EAAoB,QAAQ,EAAa,MAAM,EAAE,MAAM,EAAqB,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAErH,OAAO,EAAC,gBAAgB,EAAC,MAAM,6BAA6B,CAAC;AAC7D,OAAO,EAAC,aAAa,EAAC,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAC,yBAAyB,EAAC,MAAM,qCAAqC,CAAC;AAC9E,OAAO,EAAC,eAAe,EAAC,MAAM,4BAA4B,CAAC;AAkB3D,IAAa,cAAc,GAA3B,MAAa,cAAc;IAER,QAAQ,CAAa;IAGrB,QAAQ,CAAoB;IAG5B,OAAO,CAAmB;IAEpC,KAAK,CAAC,OAAO;QAClB,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QAE3E,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAEM,UAAU;QACf,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC/B,CAAC;IAEM,kBAAkB,CAAC,IAAqC;QAC7D,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;IAEO,aAAa,CAAC,IAAqC;QACzD,MAAM,SAAS,GAAG,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;QAC9C,MAAM,QAAQ,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAEhD,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC;CACF,CAAA;AA5BC;IADC,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC;;gDACa;AAGtC;IADC,MAAM,EAAE;8BACmB,gBAAgB;gDAAC;AAG7C;IADC,MAAM,EAAE;8BACkB,eAAe;+CAAC;AARhC,cAAc;IAH1B,MAAM,CAAC;QACN,eAAe,EAAE,CAAC,yBAAyB,CAAC;KAC7C,CAAC;GACW,cAAc,CA8B1B;SA9BY,cAAc;AAgC3B,uGAAuG;AACvG,gBAAgB,CAAC,EAAC,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,IAAI,EAAC,CAAC,CAAC","sourcesContent":["import \"./services/MikroOrmFactory\";\nimport {AlterRunInContext, Constant, DIContext, Inject, Module, OnDestroy, OnInit, registerProvider} from \"@tsed/di\";\nimport {Options} from \"@mikro-orm/core\";\nimport {MikroOrmRegistry} from \"./services/MikroOrmRegistry\";\nimport {RetryStrategy} from \"./services/RetryStrategy\";\nimport {OptimisticLockErrorFilter} from \"./filters/OptimisticLockErrorFilter\";\nimport {MikroOrmContext} from \"./services/MikroOrmContext\";\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace TsED {\n interface Configuration {\n /**\n * The the ORM configuration, entity metadata.\n * If you omit the `options` parameter, your CLI config will be used.\n */\n mikroOrm?: Options[];\n }\n }\n}\n\n@Module({\n responseFilters: [OptimisticLockErrorFilter]\n})\nexport class MikroOrmModule implements OnDestroy, OnInit, AlterRunInContext {\n @Constant(\"mikroOrm\", [])\n private readonly settings!: Options[];\n\n @Inject()\n private readonly registry!: MikroOrmRegistry;\n\n @Inject()\n private readonly context!: MikroOrmContext;\n\n public async $onInit(): Promise<void> {\n const promises = this.settings.map((opts) => this.registry.register(opts));\n\n await Promise.all(promises);\n }\n\n public $onDestroy(): Promise<void> {\n return this.registry.clear();\n }\n\n public $alterRunInContext(next: (...args: unknown[]) => unknown): () => unknown | Promise<() => unknown> {\n return () => this.createContext(next);\n }\n\n private createContext(next: (...args: unknown[]) => unknown): unknown {\n const instances = [...this.registry.values()];\n const managers = instances.map((orm) => orm.em);\n\n return this.context.run(managers, next);\n }\n}\n\n// TODO: the IoC container should provide null or undefined by default until tsedio/tsed#1694 is closed\nregisterProvider({provide: RetryStrategy, useValue: null});\n"]}
@@ -1,15 +1,15 @@
1
1
  import { Inject } from "@tsed/di";
2
- import { MikroOrmRegistry } from "../services";
2
+ import { MikroOrmRegistry } from "../services/MikroOrmRegistry.js";
3
3
  /**
4
- * Get the entity manager for the given connection name.
5
- * @param connectionName
4
+ * Get the entity manager for the given context name.
5
+ * @param {String} contextName
6
6
  * @decorator
7
7
  * @mikroOrm
8
8
  */
9
- export const EntityManager = (connectionName) => Inject(MikroOrmRegistry, (registry) => { var _a; return (_a = registry.get(connectionName)) === null || _a === void 0 ? void 0 : _a.em; });
9
+ export const EntityManager = (contextName) => Inject(MikroOrmRegistry, (registry) => registry.get(contextName)?.em);
10
10
  /**
11
- * Get the entity manager for the given connection name.
12
- * @param connectionName
11
+ * Get the entity manager for the given context name.
12
+ * @param {String} connectionName
13
13
  * @decorator
14
14
  * @mikroOrm
15
15
  */
@@ -1 +1 @@
1
- {"version":3,"file":"entityManager.js","sourceRoot":"","sources":["../../../src/decorators/entityManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,UAAU,CAAC;AAChC,OAAO,EAAC,gBAAgB,EAAC,MAAM,aAAa,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,cAAuB,EAAqB,EAAE,CAC1E,MAAM,CAAC,gBAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,WAAC,OAAA,MAAA,QAAQ,CAAC,GAAG,CAAC,cAAc,CAAC,0CAAE,EAAE,CAAA,EAAA,CAAsB,CAAC;AAElH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,aAAa,CAAC","sourcesContent":["import {Inject} from \"@tsed/di\";\nimport {MikroOrmRegistry} from \"../services\";\n\n/**\n * Get the entity manager for the given connection name.\n * @param connectionName\n * @decorator\n * @mikroOrm\n */\nexport const EntityManager = (connectionName?: string): PropertyDecorator =>\n Inject(MikroOrmRegistry, (registry: MikroOrmRegistry) => registry.get(connectionName)?.em) as PropertyDecorator;\n\n/**\n * Get the entity manager for the given connection name.\n * @param connectionName\n * @decorator\n * @mikroOrm\n */\nexport const Em = EntityManager;\n"]}
1
+ {"version":3,"file":"entityManager.js","sourceRoot":"","sources":["../../../src/decorators/entityManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,UAAU,CAAC;AAChC,OAAO,EAAC,gBAAgB,EAAC,MAAM,8BAA8B,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,WAAoB,EAAqB,EAAE,CACvE,MAAM,CAAC,gBAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,EAAE,CAAsB,CAAC;AAE/G;;;;;GAKG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,aAAa,CAAC","sourcesContent":["import {Inject} from \"@tsed/di\";\nimport {MikroOrmRegistry} from \"../services/MikroOrmRegistry\";\n\n/**\n * Get the entity manager for the given context name.\n * @param {String} contextName\n * @decorator\n * @mikroOrm\n */\nexport const EntityManager = (contextName?: string): PropertyDecorator =>\n Inject(MikroOrmRegistry, (registry: MikroOrmRegistry) => registry.get(contextName)?.em) as PropertyDecorator;\n\n/**\n * Get the entity manager for the given context name.\n * @param {String} connectionName\n * @decorator\n * @mikroOrm\n */\nexport const Em = EntityManager;\n"]}
@@ -0,0 +1,8 @@
1
+ import { MikroOrmRegistry } from "../services/MikroOrmRegistry.js";
2
+ import { Inject } from "@tsed/di";
3
+ /**
4
+ * Get the ORM for the given context name.
5
+ * @param {String} contextName
6
+ */
7
+ export const Orm = (contextName) => Inject(MikroOrmRegistry, (registry) => registry.get(contextName));
8
+ //# sourceMappingURL=orm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orm.js","sourceRoot":"","sources":["../../../src/decorators/orm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAC,MAAM,EAAC,MAAM,UAAU,CAAC;AAEhC;;;GAGG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,WAAoB,EAAqB,EAAE,CAC7D,MAAM,CAAC,gBAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAsB,CAAC","sourcesContent":["import {MikroOrmRegistry} from \"../services/MikroOrmRegistry\";\nimport {Inject} from \"@tsed/di\";\n\n/**\n * Get the ORM for the given context name.\n * @param {String} contextName\n */\nexport const Orm = (contextName?: string): PropertyDecorator =>\n Inject(MikroOrmRegistry, (registry: MikroOrmRegistry) => registry.get(contextName)) as PropertyDecorator;\n"]}
@@ -1,4 +1,8 @@
1
- import { TransactionalInterceptor } from "../interceptors";
2
1
  import { Intercept } from "@tsed/di";
3
- export const Transactional = (connectionOrOptions) => Intercept(TransactionalInterceptor, connectionOrOptions);
2
+ import { TransactionalInterceptor } from "../interceptors/TransactionalInterceptor.js";
3
+ /**
4
+ * Register a new request context for your method and execute it inside the context.
5
+ * @param {String | TransactionOptions} contextNameOrOptions
6
+ */
7
+ export const Transactional = (contextNameOrOptions) => Intercept(TransactionalInterceptor, contextNameOrOptions);
4
8
  //# sourceMappingURL=transactional.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"transactional.js","sourceRoot":"","sources":["../../../src/decorators/transactional.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,wBAAwB,EAAqB,MAAM,iBAAiB,CAAC;AAC7E,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,mBAAiD,EAAmB,EAAE,CAClG,SAAS,CAAC,wBAAwB,EAAE,mBAAmB,CAAC,CAAC","sourcesContent":["import {TransactionalInterceptor, TransactionOptions} from \"../interceptors\";\nimport {Intercept} from \"@tsed/di\";\n\nexport const Transactional = (connectionOrOptions?: string | TransactionOptions): MethodDecorator =>\n Intercept(TransactionalInterceptor, connectionOrOptions);\n"]}
1
+ {"version":3,"file":"transactional.js","sourceRoot":"","sources":["../../../src/decorators/transactional.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AACnC,OAAO,EAAC,wBAAwB,EAAqB,MAAM,0CAA0C,CAAC;AAEtG;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,oBAAkD,EAAmB,EAAE,CACnG,SAAS,CAAC,wBAAwB,EAAE,oBAAoB,CAAC,CAAC","sourcesContent":["import {Intercept} from \"@tsed/di\";\nimport {TransactionalInterceptor, TransactionOptions} from \"../interceptors/TransactionalInterceptor\";\n\n/**\n * Register a new request context for your method and execute it inside the context.\n * @param {String | TransactionOptions} contextNameOrOptions\n */\nexport const Transactional = (contextNameOrOptions?: string | TransactionOptions): MethodDecorator =>\n Intercept(TransactionalInterceptor, contextNameOrOptions);\n"]}
package/lib/esm/index.js CHANGED
@@ -1,6 +1,14 @@
1
- export * from "./decorators";
2
- export * from "./filters";
3
- export * from "./interceptors";
4
- export * from "./services";
5
- export * from "./MikroOrmModule";
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./MikroOrmModule.js";
5
+ export * from "./decorators/entityManager.js";
6
+ export * from "./decorators/orm.js";
7
+ export * from "./decorators/transactional.js";
8
+ export * from "./filters/OptimisticLockErrorFilter.js";
9
+ export * from "./interceptors/TransactionalInterceptor.js";
10
+ export * from "./services/MikroOrmContext.js";
11
+ export * from "./services/MikroOrmFactory.js";
12
+ export * from "./services/MikroOrmRegistry.js";
13
+ export * from "./services/RetryStrategy.js";
6
14
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC","sourcesContent":["export * from \"./decorators\";\nexport * from \"./filters\";\nexport * from \"./interceptors\";\nexport * from \"./services\";\nexport * from \"./MikroOrmModule\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,kBAAkB,CAAC;AACjC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kBAAkB,CAAC;AACjC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,yCAAyC,CAAC;AACxD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./MikroOrmModule\";\nexport * from \"./decorators/entityManager\";\nexport * from \"./decorators/orm\";\nexport * from \"./decorators/transactional\";\nexport * from \"./filters/OptimisticLockErrorFilter\";\nexport * from \"./interceptors/TransactionalInterceptor\";\nexport * from \"./services/MikroOrmContext\";\nexport * from \"./services/MikroOrmFactory\";\nexport * from \"./services/MikroOrmRegistry\";\nexport * from \"./services/RetryStrategy\";\n"]}
@@ -1,8 +1,15 @@
1
1
  import { __decorate, __metadata, __param } from "tslib";
2
- import { DBContext, MikroOrmRegistry, RetryStrategy } from "../services";
3
2
  import { Inject, Interceptor } from "@tsed/di";
4
3
  import { Logger } from "@tsed/logger";
4
+ import { RetryStrategy } from "../services/RetryStrategy.js";
5
+ import { MikroOrmContext } from "../services/MikroOrmContext.js";
6
+ import { MikroOrmRegistry } from "../services/MikroOrmRegistry.js";
7
+ import { IsolationLevel } from "@mikro-orm/core";
5
8
  let TransactionalInterceptor = class TransactionalInterceptor {
9
+ registry;
10
+ context;
11
+ logger;
12
+ retryStrategy;
6
13
  constructor(registry, context, logger, retryStrategy) {
7
14
  this.registry = registry;
8
15
  this.context = context;
@@ -14,50 +21,59 @@ let TransactionalInterceptor = class TransactionalInterceptor {
14
21
  if (options.retry && !this.retryStrategy) {
15
22
  this.logger.warn(`To retry a transaction you have to implement a "${RetryStrategy.description}" interface`);
16
23
  }
17
- const ctx = this.context.getContext();
18
- if (!ctx) {
19
- return this.runWithinNewCtx(next, options);
24
+ if (this.context.has(options.contextName)) {
25
+ return this.runWithinCtx(next, options);
20
26
  }
21
- if (ctx.has(options.connectionName)) {
22
- return next();
23
- }
24
- return this.runUnderCtx(next, ctx, options);
27
+ return this.runWithinNewCtx(next, options);
25
28
  }
26
- runWithinNewCtx(next, options) {
27
- const ctx = new Map();
28
- return this.runUnderCtx(next, ctx, options);
29
+ runWithinCtx(next, options) {
30
+ const callback = () => this.executeInTransaction(next, options);
31
+ return options.retry && this.retryStrategy ? this.retryStrategy.acquire(callback) : callback();
29
32
  }
30
- runUnderCtx(next, ctx, options) {
31
- const orm = this.registry.get(options.connectionName);
32
- const em = orm.em.fork(true, true);
33
- ctx.set(em.name, em);
34
- const runInTransaction = () => this.executeInTransaction(options.connectionName, next);
35
- return this.context.run(ctx, () => { var _a; return (options.retry ? (_a = this.retryStrategy) === null || _a === void 0 ? void 0 : _a.acquire(runInTransaction) : runInTransaction()); });
33
+ runWithinNewCtx(next, options) {
34
+ const orm = this.registry.get(options.contextName);
35
+ if (!orm) {
36
+ throw new Error(`No such context: ${options.contextName}. Please register a corresponding MikroOrm instance using '@Configuration()' decorator.`);
37
+ }
38
+ return this.context.run([orm.em], () => this.runWithinCtx(next, options));
36
39
  }
37
40
  extractContextName(context) {
38
- const options = context.options || {};
39
- let connectionName;
41
+ const options = (context.options || {});
42
+ let isolationLevel;
43
+ let disabled;
44
+ let contextName;
45
+ let flushMode;
40
46
  let retry;
41
47
  if (typeof options === "string") {
42
- connectionName = options;
48
+ contextName = options;
43
49
  }
44
50
  else if (options) {
45
- connectionName = options.connectionName;
51
+ contextName = options.contextName;
52
+ isolationLevel = options.isolationLevel;
46
53
  retry = options.retry;
54
+ disabled = options.disabled;
55
+ flushMode = options.flushMode;
47
56
  }
48
- if (!connectionName) {
49
- connectionName = "default";
57
+ return {
58
+ flushMode,
59
+ retry: retry ?? false,
60
+ disabled: disabled ?? false,
61
+ contextName: contextName ?? "default",
62
+ isolationLevel: isolationLevel ?? IsolationLevel.READ_COMMITTED
63
+ };
64
+ }
65
+ async executeInTransaction(next, options) {
66
+ const manager = this.context.get(options.contextName);
67
+ if (!manager) {
68
+ throw new Error(`No such context: ${options.contextName}. Please check if the async context is lost in one of the asynchronous operations.`);
50
69
  }
51
- if (!retry) {
52
- retry = false;
70
+ if (options.disabled) {
71
+ return next();
53
72
  }
54
- return { connectionName, retry };
55
- }
56
- async executeInTransaction(connectionName, next) {
57
- const em = this.context.get(connectionName);
58
- const result = await next();
59
- await (em === null || em === void 0 ? void 0 : em.flush());
60
- return result;
73
+ return manager.transactional(() => next(), {
74
+ flushMode: options.flushMode,
75
+ isolationLevel: options.isolationLevel
76
+ });
61
77
  }
62
78
  };
63
79
  TransactionalInterceptor = __decorate([
@@ -67,7 +83,7 @@ TransactionalInterceptor = __decorate([
67
83
  __param(2, Inject()),
68
84
  __param(3, Inject(RetryStrategy)),
69
85
  __metadata("design:paramtypes", [MikroOrmRegistry,
70
- DBContext,
86
+ MikroOrmContext,
71
87
  Logger, Object])
72
88
  ], TransactionalInterceptor);
73
89
  export { TransactionalInterceptor };
@@ -1 +1 @@
1
- {"version":3,"file":"TransactionalInterceptor.js","sour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,EAAC,cAAc,EAAE,KAAK,EAAC,CAAC;IACjC,CAAC;IAEO,KAAK,CAAC,oBAAoB,CAAC,cAAsB,EAAE,IAAqB;QAC9E,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QAE5C,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAE5B,MAAM,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,KAAK,EAAE,CAAA,CAAC;QAElB,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAA;AA/EY,wBAAwB;IADpC,WAAW,EAAE;IAGT,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,CAAC,aAAa,CAAC,CAAA;qCAHe,gBAAgB;QACjB,SAAS;QACV,MAAM;GAJhC,wBAAwB,CA+EpC;SA/EY,wBAAwB","sourcesContent":["import {DBContext, MikroOrmRegistry, RetryStrategy} from \"../services\";\nimport {Inject, Interceptor, InterceptorContext, InterceptorMethods, InterceptorNext} from \"@tsed/di\";\nimport {EntityManager} from \"@mikro-orm/core\";\nimport {Logger} from \"@tsed/logger\";\n\nexport interface TransactionOptions {\n connectionName?: string;\n retry?: boolean;\n}\n\n@Interceptor()\nexport class TransactionalInterceptor implements InterceptorMethods {\n constructor(\n @Inject() private readonly registry: MikroOrmRegistry,\n @Inject() private readonly context: DBContext,\n @Inject() private readonly logger: Logger,\n @Inject(RetryStrategy)\n private readonly retryStrategy?: RetryStrategy\n ) {}\n\n public async intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown> {\n const options = this.extractContextName(context);\n\n if (options.retry && !this.retryStrategy) {\n this.logger.warn(`To retry a transaction you have to implement a \"${RetryStrategy.description}\" interface`);\n }\n\n const ctx = this.context.getContext();\n\n if (!ctx) {\n return this.runWithinNewCtx(next, options);\n }\n\n if (ctx.has(options.connectionName!)) {\n return next();\n }\n\n return this.runUnderCtx(next, ctx, options);\n }\n\n private runWithinNewCtx(next: InterceptorNext, options: TransactionOptions): Promise<unknown> | unknown {\n const ctx = new Map<string, EntityManager>();\n\n return this.runUnderCtx(next, ctx, options);\n }\n\n private runUnderCtx(next: InterceptorNext, ctx: Map<string, EntityManager>, options: TransactionOptions): Promise<unknown> | unknown {\n const orm = this.registry.get(options.connectionName);\n const em = orm.em.fork(true, true);\n\n ctx.set(em.name, em);\n\n const runInTransaction = () => this.executeInTransaction(options.connectionName!, next);\n\n return this.context.run(ctx, () => (options.retry ? this.retryStrategy?.acquire(runInTransaction) : runInTransaction()));\n }\n\n private extractContextName(context: InterceptorContext<unknown>): TransactionOptions {\n const options = context.options || ({} as Partial<TransactionOptions> | string);\n\n let connectionName: string | undefined;\n let retry: boolean | undefined;\n\n if (typeof options === \"string\") {\n connectionName = options;\n } else if (options) {\n connectionName = options.connectionName;\n retry = options.retry;\n }\n\n if (!connectionName) {\n connectionName = \"default\";\n }\n\n if (!retry) {\n retry = false;\n }\n\n return {connectionName, retry};\n }\n\n private async executeInTransaction(connectionName: string, next: InterceptorNext): Promise<unknown> {\n const em = this.context.get(connectionName);\n\n const result = await next();\n\n await em?.flush();\n\n return result;\n }\n}\n"]}
1
+ {"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,WAAW,EAA0D,MAAM,UAAU,CAAC;AACtG,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAC,gBAAgB,EAAC,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAY,cAAc,EAAC,MAAM,iBAAiB,CAAC;AAa1D,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IAE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CAAC;SACH;QAED,IAAI,OAAO,CAAC,QAAQ,EAAE;YACpB,OAAO,IAAI,EAAE,CAAC;SACf;QAED,OAAO,OAAO,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE;YACzC,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,cAAc,EAAE,OAAO,CAAC,cAAc;SACvC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAvFY,wBAAwB;IADpC,WAAW,EAAE;IAGT,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,CAAC,aAAa,CAAC,CAAA;qCAHe,gBAAgB;QACjB,eAAe;QAChB,MAAM;GAJhC,wBAAwB,CAuFpC;SAvFY,wBAAwB","sourcesContent":["import {Inject, Interceptor, InterceptorContext, InterceptorMethods, InterceptorNext} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {RetryStrategy} from \"../services/RetryStrategy\";\nimport {MikroOrmContext} from \"../services/MikroOrmContext\";\nimport {MikroOrmRegistry} from \"../services/MikroOrmRegistry\";\nimport {FlushMode, IsolationLevel} from \"@mikro-orm/core\";\n\nexport interface TransactionOptions {\n retry?: boolean;\n disabled?: boolean;\n isolationLevel?: IsolationLevel;\n flushMode?: FlushMode;\n contextName?: string;\n}\n\ntype TransactionSettings = Required<Omit<TransactionOptions, \"connectionName\" | \"flushMode\">> & {flushMode?: FlushMode};\n\n@Interceptor()\nexport class TransactionalInterceptor implements InterceptorMethods {\n constructor(\n @Inject() private readonly registry: MikroOrmRegistry,\n @Inject() private readonly context: MikroOrmContext,\n @Inject() private readonly logger: Logger,\n @Inject(RetryStrategy)\n private readonly retryStrategy?: RetryStrategy\n ) {}\n\n public async intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown> {\n const options = this.extractContextName(context);\n\n if (options.retry && !this.retryStrategy) {\n this.logger.warn(`To retry a transaction you have to implement a \"${RetryStrategy.description}\" interface`);\n }\n\n if (this.context.has(options.contextName)) {\n return this.runWithinCtx(next, options);\n }\n\n return this.runWithinNewCtx(next, options);\n }\n\n private runWithinCtx(next: InterceptorNext, options: TransactionSettings): Promise<unknown> | unknown {\n const callback = () => this.executeInTransaction(next, options);\n\n return options.retry && this.retryStrategy ? this.retryStrategy.acquire(callback) : callback();\n }\n\n private runWithinNewCtx(next: InterceptorNext, options: TransactionSettings): Promise<unknown> | unknown {\n const orm = this.registry.get(options.contextName);\n\n if (!orm) {\n throw new Error(\n `No such context: ${options.contextName}. Please register a corresponding MikroOrm instance using '@Configuration()' decorator.`\n );\n }\n\n return this.context.run([orm.em], () => this.runWithinCtx(next, options));\n }\n\n private extractContextName(context: InterceptorContext<unknown>): TransactionSettings {\n const options = (context.options || {}) as TransactionOptions | string;\n\n let isolationLevel: IsolationLevel | undefined;\n let disabled: boolean | undefined;\n let contextName: string | undefined;\n let flushMode: FlushMode | undefined;\n let retry: boolean | undefined;\n\n if (typeof options === \"string\") {\n contextName = options;\n } else if (options) {\n contextName = options.contextName;\n isolationLevel = options.isolationLevel;\n retry = options.retry;\n disabled = options.disabled;\n flushMode = options.flushMode;\n }\n\n return {\n flushMode,\n retry: retry ?? false,\n disabled: disabled ?? false,\n contextName: contextName ?? \"default\",\n isolationLevel: isolationLevel ?? IsolationLevel.READ_COMMITTED\n };\n }\n\n private async executeInTransaction(next: InterceptorNext, options: TransactionSettings): Promise<unknown> {\n const manager = this.context.get(options.contextName);\n\n if (!manager) {\n throw new Error(\n `No such context: ${options.contextName}. Please check if the async context is lost in one of the asynchronous operations.`\n );\n }\n\n if (options.disabled) {\n return next();\n }\n\n return manager.transactional(() => next(), {\n flushMode: options.flushMode,\n isolationLevel: options.isolationLevel\n });\n }\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,23 @@
1
+ import { __decorate } from "tslib";
2
+ import { Injectable } from "@tsed/di";
3
+ import { RequestContext } from "@mikro-orm/core";
4
+ /**
5
+ * @internal Since 2022-06-20.
6
+ */
7
+ let MikroOrmContext = class MikroOrmContext {
8
+ run(managers, task) {
9
+ return RequestContext.createAsync(managers, task);
10
+ }
11
+ get(contextName) {
12
+ return RequestContext.getEntityManager(contextName);
13
+ }
14
+ has(contextName) {
15
+ const manager = this.get(contextName);
16
+ return !!manager;
17
+ }
18
+ };
19
+ MikroOrmContext = __decorate([
20
+ Injectable()
21
+ ], MikroOrmContext);
22
+ export { MikroOrmContext };
23
+ //# sourceMappingURL=MikroOrmContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MikroOrmContext.js","sourceRoot":"","sources":["../../../src/services/MikroOrmContext.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AACpC,OAAO,EAAgB,cAAc,EAAC,MAAM,iBAAiB,CAAC;AAE9D;;GAEG;AAEH,IAAa,eAAe,GAA5B,MAAa,eAAe;IACnB,GAAG,CAAC,QAAyB,EAAE,IAAqC;QACzE,OAAO,cAAc,CAAC,WAAW,CAAC,QAAQ,EAAE,IAAW,CAAC,CAAC;IAC3D,CAAC;IAEM,GAAG,CAAC,WAAoB;QAC7B,OAAO,cAAc,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;IACtD,CAAC;IAEM,GAAG,CAAC,WAAoB;QAC7B,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAEtC,OAAO,CAAC,CAAC,OAAO,CAAC;IACnB,CAAC;CACF,CAAA;AAdY,eAAe;IAD3B,UAAU,EAAE;GACA,eAAe,CAc3B;SAdY,eAAe","sourcesContent":["import {Injectable} from \"@tsed/di\";\nimport {EntityManager, RequestContext} from \"@mikro-orm/core\";\n\n/**\n * @internal Since 2022-06-20.\n */\n@Injectable()\nexport class MikroOrmContext {\n public run(managers: EntityManager[], task: (...args: unknown[]) => unknown): Promise<unknown> {\n return RequestContext.createAsync(managers, task as any);\n }\n\n public get(contextName?: string): EntityManager | undefined {\n return RequestContext.getEntityManager(contextName);\n }\n\n public has(contextName?: string): boolean {\n const manager = this.get(contextName);\n\n return !!manager;\n }\n}\n"]}
@@ -1,21 +1,23 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { DBContext } from "./DBContext";
1
+ import { __decorate, __metadata, __param } from "tslib";
3
2
  import { MikroORM } from "@mikro-orm/core";
4
3
  import { Inject, Injectable } from "@tsed/di";
4
+ import { MikroOrmContext } from "./MikroOrmContext.js";
5
5
  let MikroOrmFactory = class MikroOrmFactory {
6
- create(connectionOptions) {
6
+ context;
7
+ constructor(context) {
8
+ this.context = context;
9
+ }
10
+ create(options) {
7
11
  return MikroORM.init({
8
- ...connectionOptions,
9
- context: (name) => this.dbContext.get(name)
12
+ ...options,
13
+ context: (name) => this.context.get(name)
10
14
  });
11
15
  }
12
16
  };
13
- __decorate([
14
- Inject(),
15
- __metadata("design:type", DBContext)
16
- ], MikroOrmFactory.prototype, "dbContext", void 0);
17
17
  MikroOrmFactory = __decorate([
18
- Injectable()
18
+ Injectable(),
19
+ __param(0, Inject()),
20
+ __metadata("design:paramtypes", [MikroOrmContext])
19
21
  ], MikroOrmFactory);
20
22
  export { MikroOrmFactory };
21
23
  //# sourceMappingURL=MikroOrmFactory.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,EAAC,QAAQ,EAAU,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAG5C,IAAa,eAAe,GAA5B,MAAa,eAAe;IAInB,MAAM,CAAC,iBAA0B;QACtC,OAAO,QAAQ,CAAC,IAAI,CAAC;YACnB,GAAG,iBAAiB;YACpB,OAAO,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC;SACpD,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AARC;IADC,MAAM,EAAE;8BACoB,SAAS;kDAAC;AAF5B,eAAe;IAD3B,UAAU,EAAE;GACA,eAAe,CAU3B;SAVY,eAAe","sourcesContent":["import {DBContext} from \"./DBContext\";\nimport {MikroORM, Options} from \"@mikro-orm/core\";\nimport {Inject, Injectable} from \"@tsed/di\";\n\n@Injectable()\nexport class MikroOrmFactory {\n @Inject()\n private readonly dbContext!: DBContext;\n\n public create(connectionOptions: Options): Promise<MikroORM> {\n return MikroORM.init({\n ...connectionOptions,\n context: (name: string) => this.dbContext.get(name)\n });\n }\n}\n"]}
1
+ {"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAU,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAGlD,IAAa,eAAe,GAA5B,MAAa,eAAe;IACa;IAAvC,YAAuC,OAAwB;QAAxB,YAAO,GAAP,OAAO,CAAiB;IAAG,CAAC;IAE5D,MAAM,CAAC,OAAgB;QAC5B,OAAO,QAAQ,CAAC,IAAI,CAAC;YACnB,GAAG,OAAO;YACV,OAAO,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;SAClD,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AATY,eAAe;IAD3B,UAAU,EAAE;IAEE,WAAA,MAAM,EAAE,CAAA;qCAA2B,eAAe;GADpD,eAAe,CAS3B;SATY,eAAe","sourcesContent":["import {MikroORM, Options} from \"@mikro-orm/core\";\nimport {Inject, Injectable} from \"@tsed/di\";\nimport {MikroOrmContext} from \"./MikroOrmContext\";\n\n@Injectable()\nexport class MikroOrmFactory {\n constructor(@Inject() private readonly context: MikroOrmContext) {}\n\n public create(options: Options): Promise<MikroORM> {\n return MikroORM.init({\n ...options,\n context: (name: string) => this.context.get(name)\n });\n }\n}\n"]}
@@ -1,41 +1,46 @@
1
1
  import { __decorate, __metadata, __param } from "tslib";
2
- import { MikroOrmFactory } from "./MikroOrmFactory";
2
+ import { MikroOrmFactory } from "./MikroOrmFactory.js";
3
3
  import { Inject, Injectable } from "@tsed/di";
4
4
  import { catchAsyncError, getValue } from "@tsed/core";
5
5
  import { Logger } from "@tsed/logger";
6
6
  let MikroOrmRegistry = class MikroOrmRegistry {
7
+ logger;
8
+ mikroOrmFactory;
9
+ DEFAULT_CONTEXT_NAME = "default";
10
+ instances = new Map();
7
11
  constructor(logger, mikroOrmFactory) {
8
12
  this.logger = logger;
9
13
  this.mikroOrmFactory = mikroOrmFactory;
10
- this.connections = new Map();
11
14
  }
12
- async createConnection(connectionOptions) {
13
- const connectionName = getValue(connectionOptions, "contextName", "default");
14
- if (this.has(connectionName)) {
15
- return this.get(connectionName);
15
+ async register(options) {
16
+ const contextName = getValue(options, "contextName", this.DEFAULT_CONTEXT_NAME);
17
+ if (this.has(contextName)) {
18
+ return this.get(contextName);
16
19
  }
17
- this.logger.info(`Create connection with MikroOrm to database: %s`, connectionName);
18
- this.logger.debug(`options: %j`, connectionOptions);
19
- const connection = await this.mikroOrmFactory.create(connectionOptions);
20
- this.connections.set(connectionName, connection);
21
- this.logger.info(`Connected with MikroOrm to database: %s`, connectionName);
22
- return connection;
20
+ this.logger.info(`Create connection with MikroOrm to database: %s`, contextName);
21
+ this.logger.debug(`options: %j`, options);
22
+ const instance = await this.mikroOrmFactory.create(options);
23
+ this.instances.set(contextName, instance);
24
+ this.logger.info(`Connected with MikroOrm to database: %s`, contextName);
25
+ return instance;
23
26
  }
24
- get(id = "default") {
25
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
26
- return this.connections.get(id);
27
+ get(contextName) {
28
+ return this.instances.get(contextName ?? this.DEFAULT_CONTEXT_NAME);
27
29
  }
28
- has(id = "default") {
29
- return this.connections.has(id);
30
+ has(contextName) {
31
+ return this.instances.has(contextName ?? this.DEFAULT_CONTEXT_NAME);
30
32
  }
31
- async closeConnections() {
32
- const connections = [...this.connections.values()];
33
- await Promise.all(connections.map((connection) => catchAsyncError(() => this.dispose(connection))));
34
- this.connections.clear();
33
+ values() {
34
+ return this.instances.values();
35
35
  }
36
- async dispose(connection) {
37
- if (await connection.isConnected()) {
38
- await connection.close(false);
36
+ async clear() {
37
+ const instances = [...this.instances.values()];
38
+ await Promise.all(instances.map((instance) => catchAsyncError(() => this.dispose(instance))));
39
+ this.instances.clear();
40
+ }
41
+ async dispose(instance) {
42
+ if (await instance.isConnected()) {
43
+ await instance.close(false);
39
44
  }
40
45
  }
41
46
  };