@tsed/mikro-orm 7.0.0-beta.3 → 7.0.0-beta.6

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 (89) hide show
  1. package/jest.config.js +4 -4
  2. package/lib/cjs/MikroOrmModule.js +13 -11
  3. package/lib/cjs/MikroOrmModule.js.map +1 -1
  4. package/lib/cjs/decorators/entityManager.d.ts +6 -6
  5. package/lib/cjs/decorators/entityManager.js +6 -6
  6. package/lib/cjs/decorators/entityManager.js.map +1 -1
  7. package/lib/cjs/decorators/orm.d.ts +5 -0
  8. package/lib/cjs/decorators/orm.js +12 -0
  9. package/lib/cjs/decorators/orm.js.map +1 -0
  10. package/lib/cjs/decorators/transactional.d.ts +6 -2
  11. package/lib/cjs/decorators/transactional.js +6 -2
  12. package/lib/cjs/decorators/transactional.js.map +1 -1
  13. package/lib/cjs/filters/OptimisticLockErrorFilter.js +1 -1
  14. package/lib/cjs/index.d.ts +12 -4
  15. package/lib/cjs/index.js +13 -5
  16. package/lib/cjs/index.js.map +1 -1
  17. package/lib/cjs/interceptors/TransactionalInterceptor.d.ts +4 -2
  18. package/lib/cjs/interceptors/TransactionalInterceptor.js +39 -22
  19. package/lib/cjs/interceptors/TransactionalInterceptor.js.map +1 -1
  20. package/lib/cjs/package.json +3 -0
  21. package/lib/cjs/services/DBContext.d.ts +1 -1
  22. package/lib/cjs/services/DBContext.js +5 -5
  23. package/lib/cjs/services/DBContext.js.map +1 -1
  24. package/lib/cjs/services/MikroOrmFactory.d.ts +1 -1
  25. package/lib/cjs/services/MikroOrmFactory.js +5 -5
  26. package/lib/cjs/services/MikroOrmFactory.js.map +1 -1
  27. package/lib/cjs/services/MikroOrmRegistry.d.ts +5 -5
  28. package/lib/cjs/services/MikroOrmRegistry.js +26 -27
  29. package/lib/cjs/services/MikroOrmRegistry.js.map +1 -1
  30. package/lib/esm/MikroOrmModule.js +5 -3
  31. package/lib/esm/MikroOrmModule.js.map +1 -1
  32. package/lib/esm/decorators/entityManager.js +6 -6
  33. package/lib/esm/decorators/entityManager.js.map +1 -1
  34. package/lib/esm/decorators/orm.js +8 -0
  35. package/lib/esm/decorators/orm.js.map +1 -0
  36. package/lib/esm/decorators/transactional.js +6 -2
  37. package/lib/esm/decorators/transactional.js.map +1 -1
  38. package/lib/esm/index.js +13 -5
  39. package/lib/esm/index.js.map +1 -1
  40. package/lib/esm/interceptors/TransactionalInterceptor.js +31 -14
  41. package/lib/esm/interceptors/TransactionalInterceptor.js.map +1 -1
  42. package/lib/esm/package.json +3 -0
  43. package/lib/esm/services/DBContext.js +3 -3
  44. package/lib/esm/services/DBContext.js.map +1 -1
  45. package/lib/esm/services/MikroOrmFactory.js +3 -3
  46. package/lib/esm/services/MikroOrmFactory.js.map +1 -1
  47. package/lib/esm/services/MikroOrmRegistry.js +23 -24
  48. package/lib/esm/services/MikroOrmRegistry.js.map +1 -1
  49. package/lib/types/decorators/entityManager.d.ts +6 -6
  50. package/lib/types/decorators/orm.d.ts +5 -0
  51. package/lib/types/decorators/transactional.d.ts +6 -2
  52. package/lib/types/index.d.ts +12 -4
  53. package/lib/types/interceptors/TransactionalInterceptor.d.ts +4 -2
  54. package/lib/types/services/DBContext.d.ts +1 -1
  55. package/lib/types/services/MikroOrmFactory.d.ts +1 -1
  56. package/lib/types/services/MikroOrmRegistry.d.ts +5 -5
  57. package/package.json +17 -15
  58. package/readme.md +34 -42
  59. package/tsconfig.compile.esm.json +1 -12
  60. package/lib/cjs/decorators/connection.d.ts +0 -1
  61. package/lib/cjs/decorators/connection.js +0 -8
  62. package/lib/cjs/decorators/connection.js.map +0 -1
  63. package/lib/cjs/decorators/index.d.ts +0 -3
  64. package/lib/cjs/decorators/index.js +0 -7
  65. package/lib/cjs/decorators/index.js.map +0 -1
  66. package/lib/cjs/filters/index.d.ts +0 -1
  67. package/lib/cjs/filters/index.js +0 -5
  68. package/lib/cjs/filters/index.js.map +0 -1
  69. package/lib/cjs/interceptors/index.d.ts +0 -1
  70. package/lib/cjs/interceptors/index.js +0 -5
  71. package/lib/cjs/interceptors/index.js.map +0 -1
  72. package/lib/cjs/services/index.d.ts +0 -4
  73. package/lib/cjs/services/index.js +0 -8
  74. package/lib/cjs/services/index.js.map +0 -1
  75. package/lib/esm/decorators/connection.js +0 -4
  76. package/lib/esm/decorators/connection.js.map +0 -1
  77. package/lib/esm/decorators/index.js +0 -4
  78. package/lib/esm/decorators/index.js.map +0 -1
  79. package/lib/esm/filters/index.js +0 -2
  80. package/lib/esm/filters/index.js.map +0 -1
  81. package/lib/esm/interceptors/index.js +0 -2
  82. package/lib/esm/interceptors/index.js.map +0 -1
  83. package/lib/esm/services/index.js +0 -5
  84. package/lib/esm/services/index.js.map +0 -1
  85. package/lib/types/decorators/connection.d.ts +0 -1
  86. package/lib/types/decorators/index.d.ts +0 -3
  87. package/lib/types/filters/index.d.ts +0 -1
  88. package/lib/types/interceptors/index.d.ts +0 -1
  89. package/lib/types/services/index.d.ts +0 -4
package/jest.config.js CHANGED
@@ -5,10 +5,10 @@ module.exports = {
5
5
  ...require("@tsed/jest-config")(__dirname, "mikro-orm"),
6
6
  coverageThreshold: {
7
7
  global: {
8
- branches: 90.91,
9
- functions: 100,
10
- lines: 99.06,
11
- statements: 99.21
8
+ branches: 90.38,
9
+ functions: 91.42,
10
+ lines: 96.52,
11
+ statements: 97.1
12
12
  }
13
13
  }
14
14
  };
@@ -2,32 +2,34 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MikroOrmModule = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const services_1 = require("./services");
6
5
  const di_1 = require("@tsed/di");
6
+ const MikroOrmRegistry_1 = require("./services/MikroOrmRegistry");
7
+ const DBContext_1 = require("./services/DBContext");
8
+ const RetryStrategy_1 = require("./services/RetryStrategy");
7
9
  let MikroOrmModule = class MikroOrmModule {
8
10
  async $onInit() {
9
- const promises = this.settings.map((opts) => this.mikroOrmRegistry.createConnection(opts));
11
+ const promises = this.settings.map((opts) => this.mikroOrmRegistry.register(opts));
10
12
  await Promise.all(promises);
11
13
  }
12
14
  $onDestroy() {
13
- return this.mikroOrmRegistry.closeConnections();
15
+ return this.mikroOrmRegistry.clear();
14
16
  }
15
17
  };
16
- (0, tslib_1.__decorate)([
18
+ tslib_1.__decorate([
17
19
  (0, di_1.Constant)("mikroOrm", []),
18
- (0, tslib_1.__metadata)("design:type", Array)
20
+ tslib_1.__metadata("design:type", Array)
19
21
  ], MikroOrmModule.prototype, "settings", void 0);
20
- (0, tslib_1.__decorate)([
22
+ tslib_1.__decorate([
21
23
  (0, di_1.Inject)(),
22
- (0, tslib_1.__metadata)("design:type", services_1.MikroOrmRegistry)
24
+ tslib_1.__metadata("design:type", MikroOrmRegistry_1.MikroOrmRegistry)
23
25
  ], MikroOrmModule.prototype, "mikroOrmRegistry", void 0);
24
- MikroOrmModule = (0, tslib_1.__decorate)([
26
+ MikroOrmModule = tslib_1.__decorate([
25
27
  (0, di_1.Module)({
26
- imports: [services_1.DBContext, services_1.MikroOrmRegistry],
27
- deps: [di_1.Configuration, services_1.MikroOrmRegistry]
28
+ imports: [DBContext_1.DBContext, MikroOrmRegistry_1.MikroOrmRegistry],
29
+ deps: [di_1.Configuration, MikroOrmRegistry_1.MikroOrmRegistry]
28
30
  })
29
31
  ], MikroOrmModule);
30
32
  exports.MikroOrmModule = MikroOrmModule;
31
33
  // TODO: the IoC container should provide null or undefined by default until tsedio/tsed#1694 is closed
32
- (0, di_1.registerProvider)({ provide: services_1.RetryStrategy, useValue: null });
34
+ (0, di_1.registerProvider)({ provide: RetryStrategy_1.RetryStrategy, useValue: null });
33
35
  //# sourceMappingURL=MikroOrmModule.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";;;;AAAA,yCAAsE;AACtE,iCAAsG;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,IAAA,aAAQ,EAAC,UAAU,EAAE,EAAE,CAAC;;gDACa;AAGtC;IADC,IAAA,WAAM,GAAE;2CAC2B,2BAAgB;wDAAC;AAL1C,cAAc;IAJ1B,IAAA,WAAM,EAAC;QACN,OAAO,EAAE,CAAC,oBAAS,EAAE,2BAAgB,CAAC;QACtC,IAAI,EAAE,CAAC,kBAAa,EAAE,2BAAgB,CAAC;KACxC,CAAC;GACW,cAAc,CAgB1B;AAhBY,wCAAc;AAkB3B,uGAAuG;AACvG,IAAA,qBAAgB,EAAC,EAAC,OAAO,EAAE,wBAAa,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,iCAAsG;AAEtG,kEAA6D;AAC7D,oDAA+C;AAC/C,4DAAuD;AAmBvD,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,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QAEnF,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAEM,UAAU;QACf,OAAO,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;IACvC,CAAC;CACF,CAAA;AAdC;IADC,IAAA,aAAQ,EAAC,UAAU,EAAE,EAAE,CAAC;;gDACa;AAGtC;IADC,IAAA,WAAM,GAAE;sCAC2B,mCAAgB;wDAAC;AAL1C,cAAc;IAJ1B,IAAA,WAAM,EAAC;QACN,OAAO,EAAE,CAAC,qBAAS,EAAE,mCAAgB,CAAC;QACtC,IAAI,EAAE,CAAC,kBAAa,EAAE,mCAAgB,CAAC;KACxC,CAAC;GACW,cAAc,CAgB1B;AAhBY,wCAAc;AAkB3B,uGAAuG;AACvG,IAAA,qBAAgB,EAAC,EAAC,OAAO,EAAE,6BAAa,EAAE,QAAQ,EAAE,IAAI,EAAC,CAAC,CAAC","sourcesContent":["import {Configuration, Constant, Inject, Module, OnDestroy, OnInit, registerProvider} from \"@tsed/di\";\nimport {Options} from \"@mikro-orm/core\";\nimport {MikroOrmRegistry} from \"./services/MikroOrmRegistry\";\nimport {DBContext} from \"./services/DBContext\";\nimport {RetryStrategy} from \"./services/RetryStrategy\";\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.register(opts));\n\n await Promise.all(promises);\n }\n\n public $onDestroy(): Promise<void> {\n return this.mikroOrmRegistry.clear();\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,14 +1,14 @@
1
1
  /**
2
- * Get the entity manager for the given connection name.
3
- * @param connectionName
2
+ * Get the entity manager for the given context name.
3
+ * @param {String} contextName
4
4
  * @decorator
5
5
  * @mikroOrm
6
6
  */
7
- export declare const EntityManager: (connectionName?: string | undefined) => PropertyDecorator;
7
+ export declare const EntityManager: (contextName?: string | undefined) => PropertyDecorator;
8
8
  /**
9
- * Get the entity manager for the given connection name.
10
- * @param connectionName
9
+ * Get the entity manager for the given context name.
10
+ * @param {String} connectionName
11
11
  * @decorator
12
12
  * @mikroOrm
13
13
  */
14
- export declare const Em: (connectionName?: string | undefined) => PropertyDecorator;
14
+ export declare const Em: (contextName?: string | undefined) => PropertyDecorator;
@@ -2,18 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Em = exports.EntityManager = void 0;
4
4
  const di_1 = require("@tsed/di");
5
- const services_1 = require("../services");
5
+ const MikroOrmRegistry_1 = require("../services/MikroOrmRegistry");
6
6
  /**
7
- * Get the entity manager for the given connection name.
8
- * @param connectionName
7
+ * Get the entity manager for the given context name.
8
+ * @param {String} contextName
9
9
  * @decorator
10
10
  * @mikroOrm
11
11
  */
12
- const EntityManager = (connectionName) => (0, di_1.Inject)(services_1.MikroOrmRegistry, (registry) => { var _a; return (_a = registry.get(connectionName)) === null || _a === void 0 ? void 0 : _a.em; });
12
+ const EntityManager = (contextName) => (0, di_1.Inject)(MikroOrmRegistry_1.MikroOrmRegistry, (registry) => { var _a; return (_a = registry.get(contextName)) === null || _a === void 0 ? void 0 : _a.em; });
13
13
  exports.EntityManager = EntityManager;
14
14
  /**
15
- * Get the entity manager for the given connection name.
16
- * @param connectionName
15
+ * Get the entity manager for the given context name.
16
+ * @param {String} connectionName
17
17
  * @decorator
18
18
  * @mikroOrm
19
19
  */
@@ -1 +1 @@
1
- {"version":3,"file":"entityManager.js","sourceRoot":"","sources":["../../../src/decorators/entityManager.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,0CAA6C;AAE7C;;;;;GAKG;AACI,MAAM,aAAa,GAAG,CAAC,cAAuB,EAAqB,EAAE,CAC1E,IAAA,WAAM,EAAC,2BAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,WAAC,OAAA,MAAA,QAAQ,CAAC,GAAG,CAAC,cAAc,CAAC,0CAAE,EAAE,CAAA,EAAA,CAAsB,CAAC;AADrG,QAAA,aAAa,iBACwF;AAElH;;;;;GAKG;AACU,QAAA,EAAE,GAAG,qBAAa,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,iCAAgC;AAChC,mEAA8D;AAE9D;;;;;GAKG;AACI,MAAM,aAAa,GAAG,CAAC,WAAoB,EAAqB,EAAE,CACvE,IAAA,WAAM,EAAC,mCAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,WAAC,OAAA,MAAA,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,0CAAE,EAAE,CAAA,EAAA,CAAsB,CAAC;AADlG,QAAA,aAAa,iBACqF;AAE/G;;;;;GAKG;AACU,QAAA,EAAE,GAAG,qBAAa,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,5 @@
1
+ /**
2
+ * Get the ORM for the given context name.
3
+ * @param {String} contextName
4
+ */
5
+ export declare const Orm: (contextName?: string | undefined) => PropertyDecorator;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Orm = void 0;
4
+ const MikroOrmRegistry_1 = require("../services/MikroOrmRegistry");
5
+ const di_1 = require("@tsed/di");
6
+ /**
7
+ * Get the ORM for the given context name.
8
+ * @param {String} contextName
9
+ */
10
+ const Orm = (contextName) => (0, di_1.Inject)(MikroOrmRegistry_1.MikroOrmRegistry, (registry) => registry.get(contextName));
11
+ exports.Orm = Orm;
12
+ //# sourceMappingURL=orm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orm.js","sourceRoot":"","sources":["../../../src/decorators/orm.ts"],"names":[],"mappings":";;;AAAA,mEAA8D;AAC9D,iCAAgC;AAEhC;;;GAGG;AACI,MAAM,GAAG,GAAG,CAAC,WAAoB,EAAqB,EAAE,CAC7D,IAAA,WAAM,EAAC,mCAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAsB,CAAC;AAD9F,QAAA,GAAG,OAC2F","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,2 +1,6 @@
1
- import { TransactionOptions } from "../interceptors";
2
- export declare const Transactional: (connectionOrOptions?: string | TransactionOptions | undefined) => MethodDecorator;
1
+ import { TransactionOptions } from "../interceptors/TransactionalInterceptor";
2
+ /**
3
+ * Register a new request context for your method and execute it inside the context.
4
+ * @param {String | TransactionOptions} contextNameOrOptions
5
+ */
6
+ export declare const Transactional: (contextNameOrOptions?: string | TransactionOptions | undefined) => MethodDecorator;
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Transactional = void 0;
4
- const interceptors_1 = require("../interceptors");
5
4
  const di_1 = require("@tsed/di");
6
- const Transactional = (connectionOrOptions) => (0, di_1.Intercept)(interceptors_1.TransactionalInterceptor, connectionOrOptions);
5
+ const TransactionalInterceptor_1 = require("../interceptors/TransactionalInterceptor");
6
+ /**
7
+ * Register a new request context for your method and execute it inside the context.
8
+ * @param {String | TransactionOptions} contextNameOrOptions
9
+ */
10
+ const Transactional = (contextNameOrOptions) => (0, di_1.Intercept)(TransactionalInterceptor_1.TransactionalInterceptor, contextNameOrOptions);
7
11
  exports.Transactional = Transactional;
8
12
  //# sourceMappingURL=transactional.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"transactional.js","sourceRoot":"","sources":["../../../src/decorators/transactional.ts"],"names":[],"mappings":";;;AAAA,kDAA6E;AAC7E,iCAAmC;AAE5B,MAAM,aAAa,GAAG,CAAC,mBAAiD,EAAmB,EAAE,CAClG,IAAA,cAAS,EAAC,uCAAwB,EAAE,mBAAmB,CAAC,CAAC;AAD9C,QAAA,aAAa,iBACiC","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,iCAAmC;AACnC,uFAAsG;AAEtG;;;GAGG;AACI,MAAM,aAAa,GAAG,CAAC,oBAAkD,EAAmB,EAAE,CACnG,IAAA,cAAS,EAAC,mDAAwB,EAAE,oBAAoB,CAAC,CAAC;AAD/C,QAAA,aAAa,iBACkC","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"]}
@@ -13,7 +13,7 @@ let OptimisticLockErrorFilter = class OptimisticLockErrorFilter {
13
13
  response.status(409).body(`Update refused. The resource has changed on the server. Please try again later`);
14
14
  }
15
15
  };
16
- OptimisticLockErrorFilter = (0, tslib_1.__decorate)([
16
+ OptimisticLockErrorFilter = tslib_1.__decorate([
17
17
  (0, common_1.Catch)(core_1.OptimisticLockError)
18
18
  ], OptimisticLockErrorFilter);
19
19
  exports.OptimisticLockErrorFilter = OptimisticLockErrorFilter;
@@ -1,5 +1,13 @@
1
- export * from "./decorators";
2
- export * from "./filters";
3
- export * from "./interceptors";
4
- export * from "./services";
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
5
4
  export * from "./MikroOrmModule";
5
+ export * from "./decorators/entityManager";
6
+ export * from "./decorators/orm";
7
+ export * from "./decorators/transactional";
8
+ export * from "./filters/OptimisticLockErrorFilter";
9
+ export * from "./interceptors/TransactionalInterceptor";
10
+ export * from "./services/DBContext";
11
+ export * from "./services/MikroOrmFactory";
12
+ export * from "./services/MikroOrmRegistry";
13
+ export * from "./services/RetryStrategy";
package/lib/cjs/index.js CHANGED
@@ -1,9 +1,17 @@
1
1
  "use strict";
2
+ /**
3
+ * @file Automatically generated by barrelsby.
4
+ */
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const tslib_1 = require("tslib");
4
- (0, tslib_1.__exportStar)(require("./decorators"), exports);
5
- (0, tslib_1.__exportStar)(require("./filters"), exports);
6
- (0, tslib_1.__exportStar)(require("./interceptors"), exports);
7
- (0, tslib_1.__exportStar)(require("./services"), exports);
8
- (0, tslib_1.__exportStar)(require("./MikroOrmModule"), exports);
7
+ tslib_1.__exportStar(require("./MikroOrmModule"), exports);
8
+ tslib_1.__exportStar(require("./decorators/entityManager"), exports);
9
+ tslib_1.__exportStar(require("./decorators/orm"), exports);
10
+ tslib_1.__exportStar(require("./decorators/transactional"), exports);
11
+ tslib_1.__exportStar(require("./filters/OptimisticLockErrorFilter"), exports);
12
+ tslib_1.__exportStar(require("./interceptors/TransactionalInterceptor"), exports);
13
+ tslib_1.__exportStar(require("./services/DBContext"), exports);
14
+ tslib_1.__exportStar(require("./services/MikroOrmFactory"), exports);
15
+ tslib_1.__exportStar(require("./services/MikroOrmRegistry"), exports);
16
+ tslib_1.__exportStar(require("./services/RetryStrategy"), exports);
9
17
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,4DAA6B;AAC7B,yDAA0B;AAC1B,8DAA+B;AAC/B,0DAA2B;AAC3B,gEAAiC","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,2DAAiC;AACjC,qEAA2C;AAC3C,2DAAiC;AACjC,qEAA2C;AAC3C,8EAAoD;AACpD,kFAAwD;AACxD,+DAAqC;AACrC,qEAA2C;AAC3C,sEAA4C;AAC5C,mEAAyC","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/DBContext\";\nexport * from \"./services/MikroOrmFactory\";\nexport * from \"./services/MikroOrmRegistry\";\nexport * from \"./services/RetryStrategy\";\n"]}
@@ -1,9 +1,11 @@
1
- import { DBContext, MikroOrmRegistry, RetryStrategy } from "../services";
2
1
  import { InterceptorContext, InterceptorMethods, InterceptorNext } from "@tsed/di";
3
2
  import { Logger } from "@tsed/logger";
3
+ import { MikroOrmRegistry } from "../services/MikroOrmRegistry";
4
+ import { DBContext } from "../services/DBContext";
5
+ import { RetryStrategy } from "../services/RetryStrategy";
4
6
  export interface TransactionOptions {
5
- connectionName?: string;
6
7
  retry?: boolean;
8
+ contextName?: string;
7
9
  }
8
10
  export declare class TransactionalInterceptor implements InterceptorMethods {
9
11
  private readonly registry;
@@ -2,9 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TransactionalInterceptor = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const services_1 = require("../services");
6
5
  const di_1 = require("@tsed/di");
7
6
  const logger_1 = require("@tsed/logger");
7
+ const MikroOrmRegistry_1 = require("../services/MikroOrmRegistry");
8
+ const DBContext_1 = require("../services/DBContext");
9
+ const RetryStrategy_1 = require("../services/RetryStrategy");
8
10
  let TransactionalInterceptor = class TransactionalInterceptor {
9
11
  constructor(registry, context, logger, retryStrategy) {
10
12
  this.registry = registry;
@@ -15,13 +17,13 @@ let TransactionalInterceptor = class TransactionalInterceptor {
15
17
  async intercept(context, next) {
16
18
  const options = this.extractContextName(context);
17
19
  if (options.retry && !this.retryStrategy) {
18
- this.logger.warn(`To retry a transaction you have to implement a "${services_1.RetryStrategy.description}" interface`);
20
+ this.logger.warn(`To retry a transaction you have to implement a "${RetryStrategy_1.RetryStrategy.description}" interface`);
19
21
  }
20
- const ctx = this.context.getContext();
22
+ const ctx = this.context.entries();
21
23
  if (!ctx) {
22
24
  return this.runWithinNewCtx(next, options);
23
25
  }
24
- if (ctx.has(options.connectionName)) {
26
+ if (ctx.has(options.contextName)) {
25
27
  return next();
26
28
  }
27
29
  return this.runUnderCtx(next, ctx, options);
@@ -31,46 +33,61 @@ let TransactionalInterceptor = class TransactionalInterceptor {
31
33
  return this.runUnderCtx(next, ctx, options);
32
34
  }
33
35
  runUnderCtx(next, ctx, options) {
34
- const orm = this.registry.get(options.connectionName);
35
- const em = orm.em.fork(true, true);
36
+ const orm = this.registry.get(options.contextName);
37
+ if (!orm) {
38
+ throw new Error(`No such context: ${options.contextName}. Please register a corresponding MikroOrm instance using '@Configuration()' decorator.`);
39
+ }
40
+ /**
41
+ * The fork method signature has been changed since v5.x,
42
+ * which might lead to unexpected behavior while using the @Transactional() decorator.
43
+ *
44
+ * ```diff
45
+ * - fork(clear = true, useContext = false): D[typeof EntityManagerType]
46
+ * + fork(options: ForkOptions = {}): D[typeof EntityManagerType] {
47
+ * ```
48
+ *
49
+ * To ensure backward compatibility with v4.x and add support for v5.x, provided the following workaround:
50
+ */
51
+ const em = orm.em.fork({ clear: true, useContext: true }, true);
36
52
  ctx.set(em.name, em);
37
- const runInTransaction = () => this.executeInTransaction(options.connectionName, next);
53
+ const runInTransaction = () => this.executeInTransaction(options.contextName, next);
38
54
  return this.context.run(ctx, () => { var _a; return (options.retry ? (_a = this.retryStrategy) === null || _a === void 0 ? void 0 : _a.acquire(runInTransaction) : runInTransaction()); });
39
55
  }
40
56
  extractContextName(context) {
57
+ var _a;
41
58
  const options = context.options || {};
42
- let connectionName;
59
+ let contextName;
43
60
  let retry;
44
61
  if (typeof options === "string") {
45
- connectionName = options;
62
+ contextName = options;
46
63
  }
47
64
  else if (options) {
48
- connectionName = options.connectionName;
65
+ contextName = (_a = options.contextName) !== null && _a !== void 0 ? _a : options.connectionName;
49
66
  retry = options.retry;
50
67
  }
51
- if (!connectionName) {
52
- connectionName = "default";
68
+ if (!contextName) {
69
+ contextName = "default";
53
70
  }
54
71
  if (!retry) {
55
72
  retry = false;
56
73
  }
57
- return { connectionName, retry };
74
+ return { contextName, retry };
58
75
  }
59
- async executeInTransaction(connectionName, next) {
60
- const em = this.context.get(connectionName);
76
+ async executeInTransaction(contextName, next) {
77
+ const em = this.context.get(contextName);
61
78
  const result = await next();
62
79
  await (em === null || em === void 0 ? void 0 : em.flush());
63
80
  return result;
64
81
  }
65
82
  };
66
- TransactionalInterceptor = (0, tslib_1.__decorate)([
83
+ TransactionalInterceptor = tslib_1.__decorate([
67
84
  (0, di_1.Interceptor)(),
68
- (0, tslib_1.__param)(0, (0, di_1.Inject)()),
69
- (0, tslib_1.__param)(1, (0, di_1.Inject)()),
70
- (0, tslib_1.__param)(2, (0, di_1.Inject)()),
71
- (0, tslib_1.__param)(3, (0, di_1.Inject)(services_1.RetryStrategy)),
72
- (0, tslib_1.__metadata)("design:paramtypes", [services_1.MikroOrmRegistry,
73
- services_1.DBContext,
85
+ tslib_1.__param(0, (0, di_1.Inject)()),
86
+ tslib_1.__param(1, (0, di_1.Inject)()),
87
+ tslib_1.__param(2, (0, di_1.Inject)()),
88
+ tslib_1.__param(3, (0, di_1.Inject)(RetryStrategy_1.RetryStrategy)),
89
+ tslib_1.__metadata("design:paramtypes", [MikroOrmRegistry_1.MikroOrmRegistry,
90
+ DBContext_1.DBContext,
74
91
  logger_1.Logger, Object])
75
92
  ], TransactionalInterceptor);
76
93
  exports.TransactionalInterceptor = TransactionalInterceptor;
@@ -1 +1 @@
1
- {"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";;;;AAAA,0CAAuE;AACvE,iCAAsG;AAEtG,yCAAoC;AAQpC,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IACnC,YAC6B,QAA0B,EAC1B,OAAkB,EAClB,MAAc,EAExB,aAA6B;QAJnB,aAAQ,GAAR,QAAQ,CAAkB;QAC1B,YAAO,GAAP,OAAO,CAAW;QAClB,WAAM,GAAN,MAAM,CAAQ;QAExB,kBAAa,GAAb,aAAa,CAAgB;IAC7C,CAAC;IAEG,KAAK,CAAC,SAAS,CAAC,OAAoC,EAAE,IAAqB;QAChF,MAAM,OAAO,GAAG,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC;QAEjD,IAAI,OAAO,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE;YACxC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,mDAAmD,wBAAa,CAAC,WAAW,aAAa,CAAC,CAAC;SAC7G;QAED,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;QAEtC,IAAI,CAAC,GAAG,EAAE;YACR,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;SAC5C;QAED,IAAI,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,cAAe,CAAC,EAAE;YACpC,OAAO,IAAI,EAAE,CAAC;SACf;QAED,OAAO,I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xC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;SACvB;QAED,IAAI,CAAC,cAAc,EAAE;YACnB,cAAc,GAAG,SAAS,CAAC;SAC5B;QAED,IAAI,CAAC,KAAK,EAAE;YACV,KAAK,GAAG,KAAK,CAAC;SACf;QAED,OAAO,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,IAAA,gBAAW,GAAE;IAGT,wBAAA,IAAA,WAAM,GAAE,CAAA;IACR,wBAAA,IAAA,WAAM,GAAE,CAAA;IACR,wBAAA,IAAA,WAAM,GAAE,CAAA;IACR,wBAAA,IAAA,WAAM,EAAC,wBAAa,CAAC,CAAA;kDAHe,2BAAgB;QACjB,oBAAS;QACV,eAAM;GAJhC,wBAAwB,CA+EpC;AA/EY,4DAAwB","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,iCAAsG;AAEtG,yCAAoC;AACpC,mEAA8D;AAC9D,qDAAgD;AAChD,6DAAwD;AAQxD,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IACnC,YAC6B,QAA0B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E,IAAqB;QAC3E,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAEzC,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;AAnGY,wBAAwB;IADpC,IAAA,gBAAW,GAAE;IAGT,mBAAA,IAAA,WAAM,GAAE,CAAA;IACR,mBAAA,IAAA,WAAM,GAAE,CAAA;IACR,mBAAA,IAAA,WAAM,GAAE,CAAA;IACR,mBAAA,IAAA,WAAM,EAAC,6BAAa,CAAC,CAAA;6CAHe,mCAAgB;QACjB,qBAAS;QACV,eAAM;GAJhC,wBAAwB,CAmGpC;AAnGY,4DAAwB","sourcesContent":["import {Inject, Interceptor, InterceptorContext, InterceptorMethods, InterceptorNext} from \"@tsed/di\";\nimport {EntityManager} from \"@mikro-orm/core\";\nimport {Logger} from \"@tsed/logger\";\nimport {MikroOrmRegistry} from \"../services/MikroOrmRegistry\";\nimport {DBContext} from \"../services/DBContext\";\nimport {RetryStrategy} from \"../services/RetryStrategy\";\n\nexport interface TransactionOptions {\n retry?: boolean;\n contextName?: string;\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.entries();\n\n if (!ctx) {\n return this.runWithinNewCtx(next, options);\n }\n\n if (ctx.has(options.contextName!)) {\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.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 /**\n * The fork method signature has been changed since v5.x,\n * which might lead to unexpected behavior while using the @Transactional() decorator.\n *\n * ```diff\n * - fork(clear = true, useContext = false): D[typeof EntityManagerType]\n * + fork(options: ForkOptions = {}): D[typeof EntityManagerType] {\n * ```\n *\n * To ensure backward compatibility with v4.x and add support for v5.x, provided the following workaround:\n */\n const em = (\n orm.em.fork as (clearOrForkOptions?: boolean | {clear?: boolean; useContext?: boolean}, useContext?: boolean) => EntityManager\n )({clear: true, useContext: true}, true);\n\n ctx.set(em.name, em);\n\n const runInTransaction = () => this.executeInTransaction(options.contextName!, 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 contextName: string | undefined;\n let retry: boolean | undefined;\n\n if (typeof options === \"string\") {\n contextName = options;\n } else if (options) {\n contextName = options.contextName ?? options.connectionName;\n retry = options.retry;\n }\n\n if (!contextName) {\n contextName = \"default\";\n }\n\n if (!retry) {\n retry = false;\n }\n\n return {contextName, retry};\n }\n\n private async executeInTransaction(contextName: string, next: InterceptorNext): Promise<unknown> {\n const em = this.context.get(contextName);\n\n const result = await next();\n\n await em?.flush();\n\n return result;\n }\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -3,7 +3,7 @@ export declare class DBContext {
3
3
  private readonly storage;
4
4
  constructor();
5
5
  run<R>(ctx: Map<string, EntityManager>, callback: (...args: any[]) => R): R;
6
- getContext(): Map<string, EntityManager> | undefined;
6
+ entries(): Map<string, EntityManager> | undefined;
7
7
  get(contextName: string): EntityManager | undefined;
8
8
  has(contextName: string): boolean;
9
9
  }
@@ -17,21 +17,21 @@ let DBContext = class DBContext {
17
17
  run(ctx, callback) {
18
18
  return this.storage.run(ctx, callback);
19
19
  }
20
- getContext() {
20
+ entries() {
21
21
  return this.storage.getStore();
22
22
  }
23
23
  get(contextName) {
24
- const context = this.getContext();
24
+ const context = this.entries();
25
25
  return context === null || context === void 0 ? void 0 : context.get(contextName);
26
26
  }
27
27
  has(contextName) {
28
- const context = this.getContext();
28
+ const context = this.entries();
29
29
  return !!(context === null || context === void 0 ? void 0 : context.has(contextName));
30
30
  }
31
31
  };
32
- DBContext = (0, tslib_1.__decorate)([
32
+ DBContext = tslib_1.__decorate([
33
33
  (0, di_1.Injectable)(),
34
- (0, tslib_1.__metadata)("design:paramtypes", [])
34
+ tslib_1.__metadata("design:paramtypes", [])
35
35
  ], DBContext);
36
36
  exports.DBContext = DBContext;
37
37
  //# sourceMappingURL=DBContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"DBContext.js","sourceRoot":"","sources":["../../../src/services/DBContext.ts"],"names":[],"mappings":";;;;AAAA,iCAAoC;AAEpC,6CAA8C;AAG9C,IAAa,SAAS,GAAtB,MAAa,SAAS;IAGpB;QAFiB,YAAO,GAAG,IAAI,+BAAiB,EAA8B,CAAC;QAG7E,IAAI,+BAAiB,EAAE;YACrB,IAAI,CAAC,OAAO,GAAG,IAAI,+BAAiB,EAAE,CAAC;SACxC;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,gEAAgE,OAAO,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,CAAC;SAC5G;IACH,CAAC;IAEM,GAAG,CAAI,GAA+B,EAAE,QAA+B;QAC5E,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IACzC,CAAC;IAEM,UAAU;QACf,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAEM,GAAG,CAAC,WAAmB;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;QAElC,OAAO,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACnC,CAAC;IAEM,GAAG,CAAC,WAAmB;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;QAElC,OAAO,CAAC,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,CAAC,WAAW,CAAC,CAAA,CAAC;IACrC,CAAC;CACF,CAAA;AA9BY,SAAS;IADrB,IAAA,eAAU,GAAE;;GACA,SAAS,CA8BrB;AA9BY,8BAAS","sourcesContent":["import {Injectable} from \"@tsed/di\";\nimport {EntityManager} from \"@mikro-orm/core\";\nimport {AsyncLocalStorage} from \"async_hooks\";\n\n@Injectable()\nexport class DBContext {\n private readonly storage = new AsyncLocalStorage<Map<string, EntityManager>>();\n\n constructor() {\n if (AsyncLocalStorage) {\n this.storage = new AsyncLocalStorage();\n } else {\n throw new Error(`AsyncLocalStorage is not available for your Node.js version (${process.versions.node}).`);\n }\n }\n\n public run<R>(ctx: Map<string, EntityManager>, callback: (...args: any[]) => R): R {\n return this.storage.run(ctx, callback);\n }\n\n public getContext(): Map<string, EntityManager> | undefined {\n return this.storage.getStore();\n }\n\n public get(contextName: string): EntityManager | undefined {\n const context = this.getContext();\n\n return context?.get(contextName);\n }\n\n public has(contextName: string): boolean {\n const context = this.getContext();\n\n return !!context?.has(contextName);\n }\n}\n"]}
1
+ {"version":3,"file":"DBContext.js","sourceRoot":"","sources":["../../../src/services/DBContext.ts"],"names":[],"mappings":";;;;AAAA,iCAAoC;AAEpC,6CAA8C;AAG9C,IAAa,SAAS,GAAtB,MAAa,SAAS;IAGpB;QAFiB,YAAO,GAAG,IAAI,+BAAiB,EAA8B,CAAC;QAG7E,IAAI,+BAAiB,EAAE;YACrB,IAAI,CAAC,OAAO,GAAG,IAAI,+BAAiB,EAAE,CAAC;SACxC;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,gEAAgE,OAAO,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,CAAC;SAC5G;IACH,CAAC;IAEM,GAAG,CAAI,GAA+B,EAAE,QAA+B;QAC5E,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IACzC,CAAC;IAEM,OAAO;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAEM,GAAG,CAAC,WAAmB;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAE/B,OAAO,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACnC,CAAC;IAEM,GAAG,CAAC,WAAmB;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAE/B,OAAO,CAAC,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,CAAC,WAAW,CAAC,CAAA,CAAC;IACrC,CAAC;CACF,CAAA;AA9BY,SAAS;IADrB,IAAA,eAAU,GAAE;;GACA,SAAS,CA8BrB;AA9BY,8BAAS","sourcesContent":["import {Injectable} from \"@tsed/di\";\nimport {EntityManager} from \"@mikro-orm/core\";\nimport {AsyncLocalStorage} from \"async_hooks\";\n\n@Injectable()\nexport class DBContext {\n private readonly storage = new AsyncLocalStorage<Map<string, EntityManager>>();\n\n constructor() {\n if (AsyncLocalStorage) {\n this.storage = new AsyncLocalStorage();\n } else {\n throw new Error(`AsyncLocalStorage is not available for your Node.js version (${process.versions.node}).`);\n }\n }\n\n public run<R>(ctx: Map<string, EntityManager>, callback: (...args: any[]) => R): R {\n return this.storage.run(ctx, callback);\n }\n\n public entries(): Map<string, EntityManager> | undefined {\n return this.storage.getStore();\n }\n\n public get(contextName: string): EntityManager | undefined {\n const context = this.entries();\n\n return context?.get(contextName);\n }\n\n public has(contextName: string): boolean {\n const context = this.entries();\n\n return !!context?.has(contextName);\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { MikroORM, Options } from "@mikro-orm/core";
2
2
  export declare class MikroOrmFactory {
3
3
  private readonly dbContext;
4
- create(connectionOptions: Options): Promise<MikroORM>;
4
+ create(options: Options): Promise<MikroORM>;
5
5
  }
@@ -6,18 +6,18 @@ const DBContext_1 = require("./DBContext");
6
6
  const core_1 = require("@mikro-orm/core");
7
7
  const di_1 = require("@tsed/di");
8
8
  let MikroOrmFactory = class MikroOrmFactory {
9
- create(connectionOptions) {
9
+ create(options) {
10
10
  return core_1.MikroORM.init({
11
- ...connectionOptions,
11
+ ...options,
12
12
  context: (name) => this.dbContext.get(name)
13
13
  });
14
14
  }
15
15
  };
16
- (0, tslib_1.__decorate)([
16
+ tslib_1.__decorate([
17
17
  (0, di_1.Inject)(),
18
- (0, tslib_1.__metadata)("design:type", DBContext_1.DBContext)
18
+ tslib_1.__metadata("design:type", DBContext_1.DBContext)
19
19
  ], MikroOrmFactory.prototype, "dbContext", void 0);
20
- MikroOrmFactory = (0, tslib_1.__decorate)([
20
+ MikroOrmFactory = tslib_1.__decorate([
21
21
  (0, di_1.Injectable)()
22
22
  ], MikroOrmFactory);
23
23
  exports.MikroOrmFactory = MikroOrmFactory;
@@ -1 +1 @@
1
- {"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";;;;AAAA,2CAAsC;AACtC,0CAAkD;AAClD,iCAA4C;AAG5C,IAAa,eAAe,GAA5B,MAAa,eAAe;IAInB,MAAM,CAAC,iBAA0B;QACtC,OAAO,eAAQ,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,IAAA,WAAM,GAAE;2CACoB,qBAAS;kDAAC;AAF5B,eAAe;IAD3B,IAAA,eAAU,GAAE;GACA,eAAe,CAU3B;AAVY,0CAAe","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,2CAAsC;AACtC,0CAAkD;AAClD,iCAA4C;AAG5C,IAAa,eAAe,GAA5B,MAAa,eAAe;IAInB,MAAM,CAAC,OAAgB;QAC5B,OAAO,eAAQ,CAAC,IAAI,CAAC;YACnB,GAAG,OAAO;YACV,OAAO,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC;SACpD,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AARC;IADC,IAAA,WAAM,GAAE;sCACoB,qBAAS;kDAAC;AAF5B,eAAe;IAD3B,IAAA,eAAU,GAAE;GACA,eAAe,CAU3B;AAVY,0CAAe","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(options: Options): Promise<MikroORM> {\n return MikroORM.init({\n ...options,\n context: (name: string) => this.dbContext.get(name)\n });\n }\n}\n"]}
@@ -4,11 +4,11 @@ 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 instances;
8
8
  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>;
9
+ register<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
10
+ get(contextName?: string): MikroORM | undefined;
11
+ has(contextName?: string): boolean;
12
+ clear(): Promise<void>;
13
13
  private dispose;
14
14
  }
@@ -10,43 +10,42 @@ let MikroOrmRegistry = class MikroOrmRegistry {
10
10
  constructor(logger, mikroOrmFactory) {
11
11
  this.logger = logger;
12
12
  this.mikroOrmFactory = mikroOrmFactory;
13
- this.connections = new Map();
13
+ this.instances = new Map();
14
14
  }
15
- async createConnection(connectionOptions) {
16
- const connectionName = (0, core_1.getValue)(connectionOptions, "contextName", "default");
17
- if (this.has(connectionName)) {
18
- return this.get(connectionName);
15
+ async register(options) {
16
+ const contextName = (0, core_1.getValue)(options, "contextName", "default");
17
+ if (this.has(contextName)) {
18
+ return this.get(contextName);
19
19
  }
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;
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;
26
26
  }
27
- get(id = "default") {
28
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
29
- return this.connections.get(id);
27
+ get(contextName = "default") {
28
+ return this.instances.get(contextName);
30
29
  }
31
- has(id = "default") {
32
- return this.connections.has(id);
30
+ has(contextName = "default") {
31
+ return this.instances.has(contextName);
33
32
  }
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();
33
+ async clear() {
34
+ const instances = [...this.instances.values()];
35
+ await Promise.all(instances.map((instance) => (0, core_1.catchAsyncError)(() => this.dispose(instance))));
36
+ this.instances.clear();
38
37
  }
39
- async dispose(connection) {
40
- if (await connection.isConnected()) {
41
- await connection.close(false);
38
+ async dispose(instance) {
39
+ if (await instance.isConnected()) {
40
+ await instance.close(false);
42
41
  }
43
42
  }
44
43
  };
45
- MikroOrmRegistry = (0, tslib_1.__decorate)([
44
+ MikroOrmRegistry = tslib_1.__decorate([
46
45
  (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])
46
+ tslib_1.__param(0, (0, di_1.Inject)()),
47
+ tslib_1.__param(1, (0, di_1.Inject)()),
48
+ tslib_1.__metadata("design:paramtypes", [logger_1.Logger, MikroOrmFactory_1.MikroOrmFactory])
50
49
  ], MikroOrmRegistry);
51
50
  exports.MikroOrmRegistry = MikroOrmRegistry;
52
51
  //# sourceMappingURL=MikroOrmRegistry.js.map