@tsed/mikro-orm 6.114.18 → 6.116.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +4 -4
- package/lib/cjs/MikroOrmModule.d.ts +7 -3
- package/lib/cjs/MikroOrmModule.js +21 -7
- package/lib/cjs/MikroOrmModule.js.map +1 -1
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interceptors/TransactionalInterceptor.d.ts +4 -4
- package/lib/cjs/interceptors/TransactionalInterceptor.js +13 -31
- package/lib/cjs/interceptors/TransactionalInterceptor.js.map +1 -1
- package/lib/cjs/services/MikroOrmContext.d.ts +9 -0
- package/lib/cjs/services/MikroOrmContext.js +26 -0
- package/lib/cjs/services/MikroOrmContext.js.map +1 -0
- package/lib/cjs/services/MikroOrmFactory.d.ts +3 -1
- package/lib/cjs/services/MikroOrmFactory.js +9 -8
- package/lib/cjs/services/MikroOrmFactory.js.map +1 -1
- package/lib/cjs/services/MikroOrmRegistry.d.ts +2 -0
- package/lib/cjs/services/MikroOrmRegistry.js +9 -5
- package/lib/cjs/services/MikroOrmRegistry.js.map +1 -1
- package/lib/esm/MikroOrmModule.js +22 -8
- package/lib/esm/MikroOrmModule.js.map +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interceptors/TransactionalInterceptor.js +13 -31
- package/lib/esm/interceptors/TransactionalInterceptor.js.map +1 -1
- package/lib/esm/services/MikroOrmContext.js +23 -0
- package/lib/esm/services/MikroOrmContext.js.map +1 -0
- package/lib/esm/services/MikroOrmFactory.js +10 -9
- package/lib/esm/services/MikroOrmFactory.js.map +1 -1
- package/lib/esm/services/MikroOrmRegistry.js +9 -5
- package/lib/esm/services/MikroOrmRegistry.js.map +1 -1
- package/lib/types/MikroOrmModule.d.ts +7 -3
- package/lib/types/index.d.ts +1 -1
- package/lib/types/interceptors/TransactionalInterceptor.d.ts +4 -4
- package/lib/types/services/MikroOrmContext.d.ts +9 -0
- package/lib/types/services/MikroOrmFactory.d.ts +3 -1
- package/lib/types/services/MikroOrmRegistry.d.ts +2 -0
- package/package.json +12 -12
- package/lib/cjs/services/DBContext.d.ts +0 -13
- package/lib/cjs/services/DBContext.js +0 -43
- package/lib/cjs/services/DBContext.js.map +0 -1
- package/lib/esm/services/DBContext.js +0 -40
- package/lib/esm/services/DBContext.js.map +0 -1
- package/lib/types/services/DBContext.d.ts +0 -13
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:
|
|
9
|
-
functions:
|
|
10
|
-
lines:
|
|
11
|
-
statements: 97.
|
|
8
|
+
branches: 95.83,
|
|
9
|
+
functions: 94.73,
|
|
10
|
+
lines: 97.43,
|
|
11
|
+
statements: 97.88
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./services/MikroOrmFactory";
|
|
2
|
+
import { AlterRunInContext, DIContext, OnDestroy, OnInit } from "@tsed/di";
|
|
2
3
|
import { Options } from "@mikro-orm/core";
|
|
3
4
|
declare global {
|
|
4
5
|
namespace TsED {
|
|
@@ -11,9 +12,12 @@ declare global {
|
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
|
-
export declare class MikroOrmModule implements OnDestroy, OnInit {
|
|
15
|
+
export declare class MikroOrmModule implements OnDestroy, OnInit, AlterRunInContext {
|
|
15
16
|
private readonly settings;
|
|
16
|
-
private readonly
|
|
17
|
+
private readonly registry;
|
|
18
|
+
private readonly context;
|
|
17
19
|
$onInit(): Promise<void>;
|
|
18
20
|
$onDestroy(): Promise<void>;
|
|
21
|
+
$alterRunInContext(next: (...args: unknown[]) => unknown, _: DIContext): () => unknown | Promise<() => unknown>;
|
|
22
|
+
private createContext;
|
|
19
23
|
}
|
|
@@ -2,19 +2,30 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MikroOrmModule = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
require("./services/MikroOrmFactory");
|
|
5
6
|
const di_1 = require("@tsed/di");
|
|
6
7
|
const MikroOrmRegistry_1 = require("./services/MikroOrmRegistry");
|
|
7
|
-
const DBContext_1 = require("./services/DBContext");
|
|
8
8
|
const RetryStrategy_1 = require("./services/RetryStrategy");
|
|
9
|
+
const OptimisticLockErrorFilter_1 = require("./filters/OptimisticLockErrorFilter");
|
|
10
|
+
const MikroOrmContext_1 = require("./services/MikroOrmContext");
|
|
9
11
|
let MikroOrmModule = class MikroOrmModule {
|
|
10
12
|
settings;
|
|
11
|
-
|
|
13
|
+
registry;
|
|
14
|
+
context;
|
|
12
15
|
async $onInit() {
|
|
13
|
-
const promises = this.settings.map((opts) => this.
|
|
16
|
+
const promises = this.settings.map((opts) => this.registry.register(opts));
|
|
14
17
|
await Promise.all(promises);
|
|
15
18
|
}
|
|
16
19
|
$onDestroy() {
|
|
17
|
-
return this.
|
|
20
|
+
return this.registry.clear();
|
|
21
|
+
}
|
|
22
|
+
$alterRunInContext(next, _) {
|
|
23
|
+
return () => this.createContext(next);
|
|
24
|
+
}
|
|
25
|
+
createContext(next) {
|
|
26
|
+
const instances = [...this.registry.values()];
|
|
27
|
+
const managers = instances.map((orm) => orm.em);
|
|
28
|
+
return this.context.run(managers, next);
|
|
18
29
|
}
|
|
19
30
|
};
|
|
20
31
|
tslib_1.__decorate([
|
|
@@ -24,11 +35,14 @@ tslib_1.__decorate([
|
|
|
24
35
|
tslib_1.__decorate([
|
|
25
36
|
(0, di_1.Inject)(),
|
|
26
37
|
tslib_1.__metadata("design:type", MikroOrmRegistry_1.MikroOrmRegistry)
|
|
27
|
-
], MikroOrmModule.prototype, "
|
|
38
|
+
], MikroOrmModule.prototype, "registry", void 0);
|
|
39
|
+
tslib_1.__decorate([
|
|
40
|
+
(0, di_1.Inject)(),
|
|
41
|
+
tslib_1.__metadata("design:type", MikroOrmContext_1.MikroOrmContext)
|
|
42
|
+
], MikroOrmModule.prototype, "context", void 0);
|
|
28
43
|
MikroOrmModule = tslib_1.__decorate([
|
|
29
44
|
(0, di_1.Module)({
|
|
30
|
-
|
|
31
|
-
deps: [di_1.Configuration, MikroOrmRegistry_1.MikroOrmRegistry]
|
|
45
|
+
responseFilters: [OptimisticLockErrorFilter_1.OptimisticLockErrorFilter]
|
|
32
46
|
})
|
|
33
47
|
], MikroOrmModule);
|
|
34
48
|
exports.MikroOrmModule = MikroOrmModule;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";;;;AAAA,sCAAoC;AACpC,iCAAqH;AAErH,kEAA6D;AAC7D,4DAAuD;AACvD,mFAA8E;AAC9E,gEAA2D;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,EAAE,CAAY;QAC3E,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,IAAA,aAAQ,EAAC,UAAU,EAAE,EAAE,CAAC;;gDACa;AAGtC;IADC,IAAA,WAAM,GAAE;sCACmB,mCAAgB;gDAAC;AAG7C;IADC,IAAA,WAAM,GAAE;sCACkB,iCAAe;+CAAC;AARhC,cAAc;IAH1B,IAAA,WAAM,EAAC;QACN,eAAe,EAAE,CAAC,qDAAyB,CAAC;KAC7C,CAAC;GACW,cAAc,CA8B1B;AA9BY,wCAAc;AAgC3B,uGAAuG;AACvG,IAAA,qBAAgB,EAAC,EAAC,OAAO,EAAE,6BAAa,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, _: DIContext): () => 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"]}
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export * from "./decorators/orm";
|
|
|
7
7
|
export * from "./decorators/transactional";
|
|
8
8
|
export * from "./filters/OptimisticLockErrorFilter";
|
|
9
9
|
export * from "./interceptors/TransactionalInterceptor";
|
|
10
|
-
export * from "./services/
|
|
10
|
+
export * from "./services/MikroOrmContext";
|
|
11
11
|
export * from "./services/MikroOrmFactory";
|
|
12
12
|
export * from "./services/MikroOrmRegistry";
|
|
13
13
|
export * from "./services/RetryStrategy";
|
package/lib/cjs/index.js
CHANGED
|
@@ -10,7 +10,7 @@ tslib_1.__exportStar(require("./decorators/orm"), exports);
|
|
|
10
10
|
tslib_1.__exportStar(require("./decorators/transactional"), exports);
|
|
11
11
|
tslib_1.__exportStar(require("./filters/OptimisticLockErrorFilter"), exports);
|
|
12
12
|
tslib_1.__exportStar(require("./interceptors/TransactionalInterceptor"), exports);
|
|
13
|
-
tslib_1.__exportStar(require("./services/
|
|
13
|
+
tslib_1.__exportStar(require("./services/MikroOrmContext"), exports);
|
|
14
14
|
tslib_1.__exportStar(require("./services/MikroOrmFactory"), exports);
|
|
15
15
|
tslib_1.__exportStar(require("./services/MikroOrmRegistry"), exports);
|
|
16
16
|
tslib_1.__exportStar(require("./services/RetryStrategy"), exports);
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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,qEAA2C;AAC3C,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/MikroOrmContext\";\nexport * from \"./services/MikroOrmFactory\";\nexport * from \"./services/MikroOrmRegistry\";\nexport * from \"./services/RetryStrategy\";\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { InterceptorContext, InterceptorMethods, InterceptorNext } from "@tsed/di";
|
|
2
2
|
import { Logger } from "@tsed/logger";
|
|
3
|
-
import { MikroOrmRegistry } from "../services/MikroOrmRegistry";
|
|
4
|
-
import { DBContext } from "../services/DBContext";
|
|
5
3
|
import { RetryStrategy } from "../services/RetryStrategy";
|
|
4
|
+
import { MikroOrmContext } from "../services/MikroOrmContext";
|
|
5
|
+
import { MikroOrmRegistry } from "../services/MikroOrmRegistry";
|
|
6
6
|
export interface TransactionOptions {
|
|
7
7
|
retry?: boolean;
|
|
8
8
|
contextName?: string;
|
|
@@ -16,10 +16,10 @@ export declare class TransactionalInterceptor implements InterceptorMethods {
|
|
|
16
16
|
private readonly context;
|
|
17
17
|
private readonly logger;
|
|
18
18
|
private readonly retryStrategy?;
|
|
19
|
-
constructor(registry: MikroOrmRegistry, context:
|
|
19
|
+
constructor(registry: MikroOrmRegistry, context: MikroOrmContext, logger: Logger, retryStrategy?: RetryStrategy | undefined);
|
|
20
20
|
intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown>;
|
|
21
|
+
private runWithinCtx;
|
|
21
22
|
private runWithinNewCtx;
|
|
22
|
-
private runUnderCtx;
|
|
23
23
|
private extractContextName;
|
|
24
24
|
private executeInTransaction;
|
|
25
25
|
}
|
|
@@ -4,9 +4,9 @@ exports.TransactionalInterceptor = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const di_1 = require("@tsed/di");
|
|
6
6
|
const logger_1 = require("@tsed/logger");
|
|
7
|
-
const MikroOrmRegistry_1 = require("../services/MikroOrmRegistry");
|
|
8
|
-
const DBContext_1 = require("../services/DBContext");
|
|
9
7
|
const RetryStrategy_1 = require("../services/RetryStrategy");
|
|
8
|
+
const MikroOrmContext_1 = require("../services/MikroOrmContext");
|
|
9
|
+
const MikroOrmRegistry_1 = require("../services/MikroOrmRegistry");
|
|
10
10
|
let TransactionalInterceptor = class TransactionalInterceptor {
|
|
11
11
|
registry;
|
|
12
12
|
context;
|
|
@@ -23,39 +23,21 @@ let TransactionalInterceptor = class TransactionalInterceptor {
|
|
|
23
23
|
if (options.retry && !this.retryStrategy) {
|
|
24
24
|
this.logger.warn(`To retry a transaction you have to implement a "${RetryStrategy_1.RetryStrategy.description}" interface`);
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return this.runWithinNewCtx(next, options);
|
|
29
|
-
}
|
|
30
|
-
if (ctx.has(options.contextName)) {
|
|
31
|
-
return next();
|
|
26
|
+
if (this.context.has(options.contextName)) {
|
|
27
|
+
return this.runWithinCtx(next, options);
|
|
32
28
|
}
|
|
33
|
-
return this.
|
|
29
|
+
return this.runWithinNewCtx(next, options);
|
|
34
30
|
}
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
return this.
|
|
31
|
+
runWithinCtx(next, options) {
|
|
32
|
+
const callback = () => this.executeInTransaction(options.contextName, next);
|
|
33
|
+
return options.retry && this.retryStrategy ? this.retryStrategy.acquire(callback) : callback();
|
|
38
34
|
}
|
|
39
|
-
|
|
35
|
+
runWithinNewCtx(next, options) {
|
|
40
36
|
const orm = this.registry.get(options.contextName);
|
|
41
37
|
if (!orm) {
|
|
42
38
|
throw new Error(`No such context: ${options.contextName}. Please register a corresponding MikroOrm instance using '@Configuration()' decorator.`);
|
|
43
39
|
}
|
|
44
|
-
|
|
45
|
-
* The fork method signature has been changed since v5.x,
|
|
46
|
-
* which might lead to unexpected behavior while using the @Transactional() decorator.
|
|
47
|
-
*
|
|
48
|
-
* ```diff
|
|
49
|
-
* - fork(clear = true, useContext = false): D[typeof EntityManagerType]
|
|
50
|
-
* + fork(options: ForkOptions = {}): D[typeof EntityManagerType] {
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* To ensure backward compatibility with v4.x and add support for v5.x, provided the following workaround:
|
|
54
|
-
*/
|
|
55
|
-
const em = orm.em.fork({ clear: true, useContext: true }, true);
|
|
56
|
-
ctx.set(em.name, em);
|
|
57
|
-
const runInTransaction = () => this.executeInTransaction(options.contextName, next);
|
|
58
|
-
return this.context.run(ctx, () => (options.retry ? this.retryStrategy?.acquire(runInTransaction) : runInTransaction()));
|
|
40
|
+
return this.context.run([orm.em], () => this.runWithinCtx(next, options));
|
|
59
41
|
}
|
|
60
42
|
extractContextName(context) {
|
|
61
43
|
const options = context.options || {};
|
|
@@ -77,9 +59,9 @@ let TransactionalInterceptor = class TransactionalInterceptor {
|
|
|
77
59
|
return { contextName, retry };
|
|
78
60
|
}
|
|
79
61
|
async executeInTransaction(contextName, next) {
|
|
80
|
-
const
|
|
62
|
+
const manager = this.context.get(contextName);
|
|
81
63
|
const result = await next();
|
|
82
|
-
await
|
|
64
|
+
await manager?.flush();
|
|
83
65
|
return result;
|
|
84
66
|
}
|
|
85
67
|
};
|
|
@@ -90,7 +72,7 @@ TransactionalInterceptor = tslib_1.__decorate([
|
|
|
90
72
|
tslib_1.__param(2, (0, di_1.Inject)()),
|
|
91
73
|
tslib_1.__param(3, (0, di_1.Inject)(RetryStrategy_1.RetryStrategy)),
|
|
92
74
|
tslib_1.__metadata("design:paramtypes", [MikroOrmRegistry_1.MikroOrmRegistry,
|
|
93
|
-
|
|
75
|
+
MikroOrmContext_1.MikroOrmContext,
|
|
94
76
|
logger_1.Logger, Object])
|
|
95
77
|
], TransactionalInterceptor);
|
|
96
78
|
exports.TransactionalInterceptor = TransactionalInterceptor;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";;;;AAAA,iCAAsG;
|
|
1
|
+
{"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";;;;AAAA,iCAAsG;AACtG,yCAAoC;AACpC,6DAAwD;AACxD,iEAA4D;AAC5D,mEAA8D;AAc9D,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IAEN;IACA;IACA;IAEV;IALnB,YAC6B,QAA0B,EAC1B,OAAwB,EACxB,MAAc,EAExB,aAA6B;QAJnB,aAAQ,GAAR,QAAQ,CAAkB;QAC1B,YAAO,GAAP,OAAO,CAAiB;QACxB,WAAM,GAAN,MAAM,CAAQ;QAExB,kBAAa,GAAb,aAAa,CAAgB;IAC7C,CAAC;IAEG,KAAK,CAAC,SAAS,C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kB,CAAC,OAAoC;QAC7D,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAK,EAAkC,CAAC;QAEvE,IAAI,WAA+B,CAAC;QACpC,IAAI,KAA0B,CAAC;QAE/B,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;YAC/B,WAAW,GAAG,OAAO,CAAC;SACvB;aAAM,IAAI,OAAO,EAAE;YAClB,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,cAAc,CAAC;YAC5D,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;SACvB;QAED,IAAI,CAAC,WAAW,EAAE;YAChB,WAAW,GAAG,SAAS,CAAC;SACzB;QAED,IAAI,CAAC,KAAK,EAAE;YACV,KAAK,GAAG,KAAK,CAAC;SACf;QAED,OAAO,EAAC,WAAW,EAAE,KAAK,EAAC,CAAC;IAC9B,CAAC;IAEO,KAAK,CAAC,oBAAoB,CAAC,WAAmB,EAAE,IAAqB;QAC3E,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAE9C,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAE5B,MAAM,OAAO,EAAE,KAAK,EAAE,CAAC;QAEvB,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAA;AA1EY,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,iCAAe;QAChB,eAAM;GAJhC,wBAAwB,CA0EpC;AA1EY,4DAAwB","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\";\n\nexport interface TransactionOptions {\n retry?: boolean;\n contextName?: string;\n /**\n * @deprecated Since 2022-02-01. Use {@link contextName} instead\n */\n connectionName?: string;\n}\n\ntype TransactionSettings = Required<Omit<TransactionOptions, \"connectionName\">>;\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(options.contextName, next);\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 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 manager = this.context.get(contextName);\n\n const result = await next();\n\n await manager?.flush();\n\n return result;\n }\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EntityManager } from "@mikro-orm/core";
|
|
2
|
+
/**
|
|
3
|
+
* @internal Since 2022-06-20.
|
|
4
|
+
*/
|
|
5
|
+
export declare class MikroOrmContext {
|
|
6
|
+
run(managers: EntityManager[], task: (...args: unknown[]) => unknown): Promise<unknown>;
|
|
7
|
+
get(contextName?: string): EntityManager | undefined;
|
|
8
|
+
has(contextName?: string): boolean;
|
|
9
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MikroOrmContext = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const di_1 = require("@tsed/di");
|
|
6
|
+
const core_1 = require("@mikro-orm/core");
|
|
7
|
+
/**
|
|
8
|
+
* @internal Since 2022-06-20.
|
|
9
|
+
*/
|
|
10
|
+
let MikroOrmContext = class MikroOrmContext {
|
|
11
|
+
run(managers, task) {
|
|
12
|
+
return core_1.RequestContext.createAsync(managers, task);
|
|
13
|
+
}
|
|
14
|
+
get(contextName) {
|
|
15
|
+
return core_1.RequestContext.getEntityManager(contextName);
|
|
16
|
+
}
|
|
17
|
+
has(contextName) {
|
|
18
|
+
const manager = this.get(contextName);
|
|
19
|
+
return !!manager;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
MikroOrmContext = tslib_1.__decorate([
|
|
23
|
+
(0, di_1.Injectable)()
|
|
24
|
+
], MikroOrmContext);
|
|
25
|
+
exports.MikroOrmContext = MikroOrmContext;
|
|
26
|
+
//# sourceMappingURL=MikroOrmContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MikroOrmContext.js","sourceRoot":"","sources":["../../../src/services/MikroOrmContext.ts"],"names":[],"mappings":";;;;AAAA,iCAAoC;AACpC,0CAA8D;AAE9D;;GAEG;AAEH,IAAa,eAAe,GAA5B,MAAa,eAAe;IACnB,GAAG,CAAC,QAAyB,EAAE,IAAqC;QACzE,OAAO,qBAAc,CAAC,WAAW,CAAC,QAAQ,EAAE,IAAW,CAAC,CAAC;IAC3D,CAAC;IAEM,GAAG,CAAC,WAAoB;QAC7B,OAAO,qBAAc,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,IAAA,eAAU,GAAE;GACA,eAAe,CAc3B;AAdY,0CAAe","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,5 +1,7 @@
|
|
|
1
1
|
import { MikroORM, Options } from "@mikro-orm/core";
|
|
2
|
+
import { MikroOrmContext } from "./MikroOrmContext";
|
|
2
3
|
export declare class MikroOrmFactory {
|
|
3
|
-
private readonly
|
|
4
|
+
private readonly context;
|
|
5
|
+
constructor(context: MikroOrmContext);
|
|
4
6
|
create(options: Options): Promise<MikroORM>;
|
|
5
7
|
}
|
|
@@ -2,24 +2,25 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MikroOrmFactory = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const DBContext_1 = require("./DBContext");
|
|
6
5
|
const core_1 = require("@mikro-orm/core");
|
|
7
6
|
const di_1 = require("@tsed/di");
|
|
7
|
+
const MikroOrmContext_1 = require("./MikroOrmContext");
|
|
8
8
|
let MikroOrmFactory = class MikroOrmFactory {
|
|
9
|
-
|
|
9
|
+
context;
|
|
10
|
+
constructor(context) {
|
|
11
|
+
this.context = context;
|
|
12
|
+
}
|
|
10
13
|
create(options) {
|
|
11
14
|
return core_1.MikroORM.init({
|
|
12
15
|
...options,
|
|
13
|
-
context: (name) => this.
|
|
16
|
+
context: (name) => this.context.get(name)
|
|
14
17
|
});
|
|
15
18
|
}
|
|
16
19
|
};
|
|
17
|
-
tslib_1.__decorate([
|
|
18
|
-
(0, di_1.Inject)(),
|
|
19
|
-
tslib_1.__metadata("design:type", DBContext_1.DBContext)
|
|
20
|
-
], MikroOrmFactory.prototype, "dbContext", void 0);
|
|
21
20
|
MikroOrmFactory = tslib_1.__decorate([
|
|
22
|
-
(0, di_1.Injectable)()
|
|
21
|
+
(0, di_1.Injectable)(),
|
|
22
|
+
tslib_1.__param(0, (0, di_1.Inject)()),
|
|
23
|
+
tslib_1.__metadata("design:paramtypes", [MikroOrmContext_1.MikroOrmContext])
|
|
23
24
|
], MikroOrmFactory);
|
|
24
25
|
exports.MikroOrmFactory = MikroOrmFactory;
|
|
25
26
|
//# sourceMappingURL=MikroOrmFactory.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";;;;AAAA,0CAAkD;AAClD,iCAA4C;AAC5C,uDAAkD;AAGlD,IAAa,eAAe,GAA5B,MAAa,eAAe;IACa;IAAvC,YAAuC,OAAwB;QAAxB,YAAO,GAAP,OAAO,CAAiB;IAAG,CAAC;IAE5D,MAAM,CAAC,OAAgB;QAC5B,OAAO,eAAQ,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,IAAA,eAAU,GAAE;IAEE,mBAAA,IAAA,WAAM,GAAE,CAAA;6CAA2B,iCAAe;GADpD,eAAe,CAS3B;AATY,0CAAe","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"]}
|
|
@@ -4,6 +4,7 @@ import { Logger } from "@tsed/logger";
|
|
|
4
4
|
export declare class MikroOrmRegistry {
|
|
5
5
|
private readonly logger;
|
|
6
6
|
private readonly mikroOrmFactory;
|
|
7
|
+
private readonly DEFAULT_CONTEXT_NAME;
|
|
7
8
|
private readonly instances;
|
|
8
9
|
constructor(logger: Logger, mikroOrmFactory: MikroOrmFactory);
|
|
9
10
|
register<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
|
|
@@ -13,6 +14,7 @@ export declare class MikroOrmRegistry {
|
|
|
13
14
|
createConnection<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
|
|
14
15
|
get(contextName?: string): MikroORM | undefined;
|
|
15
16
|
has(contextName?: string): boolean;
|
|
17
|
+
values(): IterableIterator<MikroORM>;
|
|
16
18
|
clear(): Promise<void>;
|
|
17
19
|
/**
|
|
18
20
|
* @deprecated Since 2022-02-01. Use {@link clear} instead
|
|
@@ -9,13 +9,14 @@ const logger_1 = require("@tsed/logger");
|
|
|
9
9
|
let MikroOrmRegistry = class MikroOrmRegistry {
|
|
10
10
|
logger;
|
|
11
11
|
mikroOrmFactory;
|
|
12
|
+
DEFAULT_CONTEXT_NAME = "default";
|
|
12
13
|
instances = new Map();
|
|
13
14
|
constructor(logger, mikroOrmFactory) {
|
|
14
15
|
this.logger = logger;
|
|
15
16
|
this.mikroOrmFactory = mikroOrmFactory;
|
|
16
17
|
}
|
|
17
18
|
async register(options) {
|
|
18
|
-
const contextName = (0, core_1.getValue)(options, "contextName",
|
|
19
|
+
const contextName = (0, core_1.getValue)(options, "contextName", this.DEFAULT_CONTEXT_NAME);
|
|
19
20
|
if (this.has(contextName)) {
|
|
20
21
|
return this.get(contextName);
|
|
21
22
|
}
|
|
@@ -32,11 +33,14 @@ let MikroOrmRegistry = class MikroOrmRegistry {
|
|
|
32
33
|
createConnection(options) {
|
|
33
34
|
return this.register(options);
|
|
34
35
|
}
|
|
35
|
-
get(contextName
|
|
36
|
-
return this.instances.get(contextName);
|
|
36
|
+
get(contextName) {
|
|
37
|
+
return this.instances.get(contextName ?? this.DEFAULT_CONTEXT_NAME);
|
|
37
38
|
}
|
|
38
|
-
has(contextName
|
|
39
|
-
return this.instances.has(contextName);
|
|
39
|
+
has(contextName) {
|
|
40
|
+
return this.instances.has(contextName ?? this.DEFAULT_CONTEXT_NAME);
|
|
41
|
+
}
|
|
42
|
+
values() {
|
|
43
|
+
return this.instances.values();
|
|
40
44
|
}
|
|
41
45
|
async clear() {
|
|
42
46
|
const instances = [...this.instances.values()];
|
|
@@ -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;
|
|
1
|
+
{"version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AC,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;IAED;;OAEG;IACI,gBAAgB;QACrB,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,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;AAhEY,gBAAgB;IAD5B,IAAA,eAAU,GAAE;IAKE,mBAAA,IAAA,WAAM,GAAE,CAAA;IAAmC,mBAAA,IAAA,WAAM,GAAE,CAAA;6CAAjB,eAAM,EAA8C,iCAAe;GAJvG,gBAAgB,CAgE5B;AAhEY,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 /**\n * @deprecated Since 2022-02-01. Use {@link register} instead\n */\n public createConnection<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM> {\n return this.register(options);\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 /**\n * @deprecated Since 2022-02-01. Use {@link clear} instead\n */\n public closeConnections(): Promise<void> {\n return this.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,17 +1,28 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
|
-
import
|
|
2
|
+
import "./services/MikroOrmFactory.js";
|
|
3
|
+
import { Constant, Inject, Module, registerProvider } from "@tsed/di";
|
|
3
4
|
import { MikroOrmRegistry } from "./services/MikroOrmRegistry.js";
|
|
4
|
-
import { DBContext } from "./services/DBContext.js";
|
|
5
5
|
import { RetryStrategy } from "./services/RetryStrategy.js";
|
|
6
|
+
import { OptimisticLockErrorFilter } from "./filters/OptimisticLockErrorFilter.js";
|
|
7
|
+
import { MikroOrmContext } from "./services/MikroOrmContext.js";
|
|
6
8
|
let MikroOrmModule = class MikroOrmModule {
|
|
7
9
|
settings;
|
|
8
|
-
|
|
10
|
+
registry;
|
|
11
|
+
context;
|
|
9
12
|
async $onInit() {
|
|
10
|
-
const promises = this.settings.map((opts) => this.
|
|
13
|
+
const promises = this.settings.map((opts) => this.registry.register(opts));
|
|
11
14
|
await Promise.all(promises);
|
|
12
15
|
}
|
|
13
16
|
$onDestroy() {
|
|
14
|
-
return this.
|
|
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);
|
|
15
26
|
}
|
|
16
27
|
};
|
|
17
28
|
__decorate([
|
|
@@ -21,11 +32,14 @@ __decorate([
|
|
|
21
32
|
__decorate([
|
|
22
33
|
Inject(),
|
|
23
34
|
__metadata("design:type", MikroOrmRegistry)
|
|
24
|
-
], MikroOrmModule.prototype, "
|
|
35
|
+
], MikroOrmModule.prototype, "registry", void 0);
|
|
36
|
+
__decorate([
|
|
37
|
+
Inject(),
|
|
38
|
+
__metadata("design:type", MikroOrmContext)
|
|
39
|
+
], MikroOrmModule.prototype, "context", void 0);
|
|
25
40
|
MikroOrmModule = __decorate([
|
|
26
41
|
Module({
|
|
27
|
-
|
|
28
|
-
deps: [Configuration, MikroOrmRegistry]
|
|
42
|
+
responseFilters: [OptimisticLockErrorFilter]
|
|
29
43
|
})
|
|
30
44
|
], MikroOrmModule);
|
|
31
45
|
export { MikroOrmModule };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";AAAA,OAAO,
|
|
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,EAAE,CAAY;QAC3E,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, _: DIContext): () => 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"]}
|
package/lib/esm/index.js
CHANGED
|
@@ -7,7 +7,7 @@ export * from "./decorators/orm.js";
|
|
|
7
7
|
export * from "./decorators/transactional.js";
|
|
8
8
|
export * from "./filters/OptimisticLockErrorFilter.js";
|
|
9
9
|
export * from "./interceptors/TransactionalInterceptor.js";
|
|
10
|
-
export * from "./services/
|
|
10
|
+
export * from "./services/MikroOrmContext.js";
|
|
11
11
|
export * from "./services/MikroOrmFactory.js";
|
|
12
12
|
export * from "./services/MikroOrmRegistry.js";
|
|
13
13
|
export * from "./services/RetryStrategy.js";
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,9 +1,9 @@
|
|
|
1
1
|
import { __decorate, __metadata, __param } from "tslib";
|
|
2
2
|
import { Inject, Interceptor } from "@tsed/di";
|
|
3
3
|
import { Logger } from "@tsed/logger";
|
|
4
|
-
import { MikroOrmRegistry } from "../services/MikroOrmRegistry.js";
|
|
5
|
-
import { DBContext } from "../services/DBContext.js";
|
|
6
4
|
import { RetryStrategy } from "../services/RetryStrategy.js";
|
|
5
|
+
import { MikroOrmContext } from "../services/MikroOrmContext.js";
|
|
6
|
+
import { MikroOrmRegistry } from "../services/MikroOrmRegistry.js";
|
|
7
7
|
let TransactionalInterceptor = class TransactionalInterceptor {
|
|
8
8
|
registry;
|
|
9
9
|
context;
|
|
@@ -20,39 +20,21 @@ let TransactionalInterceptor = class TransactionalInterceptor {
|
|
|
20
20
|
if (options.retry && !this.retryStrategy) {
|
|
21
21
|
this.logger.warn(`To retry a transaction you have to implement a "${RetryStrategy.description}" interface`);
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return this.runWithinNewCtx(next, options);
|
|
26
|
-
}
|
|
27
|
-
if (ctx.has(options.contextName)) {
|
|
28
|
-
return next();
|
|
23
|
+
if (this.context.has(options.contextName)) {
|
|
24
|
+
return this.runWithinCtx(next, options);
|
|
29
25
|
}
|
|
30
|
-
return this.
|
|
26
|
+
return this.runWithinNewCtx(next, options);
|
|
31
27
|
}
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
return this.
|
|
28
|
+
runWithinCtx(next, options) {
|
|
29
|
+
const callback = () => this.executeInTransaction(options.contextName, next);
|
|
30
|
+
return options.retry && this.retryStrategy ? this.retryStrategy.acquire(callback) : callback();
|
|
35
31
|
}
|
|
36
|
-
|
|
32
|
+
runWithinNewCtx(next, options) {
|
|
37
33
|
const orm = this.registry.get(options.contextName);
|
|
38
34
|
if (!orm) {
|
|
39
35
|
throw new Error(`No such context: ${options.contextName}. Please register a corresponding MikroOrm instance using '@Configuration()' decorator.`);
|
|
40
36
|
}
|
|
41
|
-
|
|
42
|
-
* The fork method signature has been changed since v5.x,
|
|
43
|
-
* which might lead to unexpected behavior while using the @Transactional() decorator.
|
|
44
|
-
*
|
|
45
|
-
* ```diff
|
|
46
|
-
* - fork(clear = true, useContext = false): D[typeof EntityManagerType]
|
|
47
|
-
* + fork(options: ForkOptions = {}): D[typeof EntityManagerType] {
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* To ensure backward compatibility with v4.x and add support for v5.x, provided the following workaround:
|
|
51
|
-
*/
|
|
52
|
-
const em = orm.em.fork({ clear: true, useContext: true }, true);
|
|
53
|
-
ctx.set(em.name, em);
|
|
54
|
-
const runInTransaction = () => this.executeInTransaction(options.contextName, next);
|
|
55
|
-
return this.context.run(ctx, () => (options.retry ? this.retryStrategy?.acquire(runInTransaction) : runInTransaction()));
|
|
37
|
+
return this.context.run([orm.em], () => this.runWithinCtx(next, options));
|
|
56
38
|
}
|
|
57
39
|
extractContextName(context) {
|
|
58
40
|
const options = context.options || {};
|
|
@@ -74,9 +56,9 @@ let TransactionalInterceptor = class TransactionalInterceptor {
|
|
|
74
56
|
return { contextName, retry };
|
|
75
57
|
}
|
|
76
58
|
async executeInTransaction(contextName, next) {
|
|
77
|
-
const
|
|
59
|
+
const manager = this.context.get(contextName);
|
|
78
60
|
const result = await next();
|
|
79
|
-
await
|
|
61
|
+
await manager?.flush();
|
|
80
62
|
return result;
|
|
81
63
|
}
|
|
82
64
|
};
|
|
@@ -87,7 +69,7 @@ TransactionalInterceptor = __decorate([
|
|
|
87
69
|
__param(2, Inject()),
|
|
88
70
|
__param(3, Inject(RetryStrategy)),
|
|
89
71
|
__metadata("design:paramtypes", [MikroOrmRegistry,
|
|
90
|
-
|
|
72
|
+
MikroOrmContext,
|
|
91
73
|
Logger, Object])
|
|
92
74
|
], TransactionalInterceptor);
|
|
93
75
|
export { TransactionalInterceptor };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,WAAW,EAA0D,MAAM,UAAU,CAAC;
|
|
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,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IAEN;IACA;IACA;IAEV;IALnB,YAC6B,QAA0B,EAC1B,OAAwB,EACxB,MAAc,EAExB,aAA6B;QAJnB,aAAQ,GAAR,QAAQ,CAAkB;QAC1B,YAAO,GAAP,OAAO,CAAiB;QACxB,WAAM,GAAN,MAAM,CAAQ;QAExB,kBAAa,GAAb,aAAa,CAAgB;IAC7C,CAAC;IAEG,KAAK,CAAC,SAAS,CAAC,O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C,OAAoC;QAC7D,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAK,EAAkC,CAAC;QAEvE,IAAI,WAA+B,CAAC;QACpC,IAAI,KAA0B,CAAC;QAE/B,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;YAC/B,WAAW,GAAG,OAAO,CAAC;SACvB;aAAM,IAAI,OAAO,EAAE;YAClB,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,cAAc,CAAC;YAC5D,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;SACvB;QAED,IAAI,CAAC,WAAW,EAAE;YAChB,WAAW,GAAG,SAAS,CAAC;SACzB;QAED,IAAI,CAAC,KAAK,EAAE;YACV,KAAK,GAAG,KAAK,CAAC;SACf;QAED,OAAO,EAAC,WAAW,EAAE,KAAK,EAAC,CAAC;IAC9B,CAAC;IAEO,KAAK,CAAC,oBAAoB,CAAC,WAAmB,EAAE,IAAqB;QAC3E,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAE9C,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAE5B,MAAM,OAAO,EAAE,KAAK,EAAE,CAAC;QAEvB,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAA;AA1EY,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,CA0EpC;SA1EY,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\";\n\nexport interface TransactionOptions {\n retry?: boolean;\n contextName?: string;\n /**\n * @deprecated Since 2022-02-01. Use {@link contextName} instead\n */\n connectionName?: string;\n}\n\ntype TransactionSettings = Required<Omit<TransactionOptions, \"connectionName\">>;\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(options.contextName, next);\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 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 manager = this.context.get(contextName);\n\n const result = await next();\n\n await manager?.flush();\n\n return result;\n }\n}\n"]}
|
|
@@ -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,22 +1,23 @@
|
|
|
1
|
-
import { __decorate, __metadata } from "tslib";
|
|
2
|
-
import { DBContext } from "./DBContext.js";
|
|
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
|
-
|
|
6
|
+
context;
|
|
7
|
+
constructor(context) {
|
|
8
|
+
this.context = context;
|
|
9
|
+
}
|
|
7
10
|
create(options) {
|
|
8
11
|
return MikroORM.init({
|
|
9
12
|
...options,
|
|
10
|
-
context: (name) => this.
|
|
13
|
+
context: (name) => this.context.get(name)
|
|
11
14
|
});
|
|
12
15
|
}
|
|
13
16
|
};
|
|
14
|
-
__decorate([
|
|
15
|
-
Inject(),
|
|
16
|
-
__metadata("design:type", DBContext)
|
|
17
|
-
], MikroOrmFactory.prototype, "dbContext", void 0);
|
|
18
17
|
MikroOrmFactory = __decorate([
|
|
19
|
-
Injectable()
|
|
18
|
+
Injectable(),
|
|
19
|
+
__param(0, Inject()),
|
|
20
|
+
__metadata("design:paramtypes", [MikroOrmContext])
|
|
20
21
|
], MikroOrmFactory);
|
|
21
22
|
export { MikroOrmFactory };
|
|
22
23
|
//# sourceMappingURL=MikroOrmFactory.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,
|
|
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"]}
|
|
@@ -6,13 +6,14 @@ import { Logger } from "@tsed/logger";
|
|
|
6
6
|
let MikroOrmRegistry = class MikroOrmRegistry {
|
|
7
7
|
logger;
|
|
8
8
|
mikroOrmFactory;
|
|
9
|
+
DEFAULT_CONTEXT_NAME = "default";
|
|
9
10
|
instances = new Map();
|
|
10
11
|
constructor(logger, mikroOrmFactory) {
|
|
11
12
|
this.logger = logger;
|
|
12
13
|
this.mikroOrmFactory = mikroOrmFactory;
|
|
13
14
|
}
|
|
14
15
|
async register(options) {
|
|
15
|
-
const contextName = getValue(options, "contextName",
|
|
16
|
+
const contextName = getValue(options, "contextName", this.DEFAULT_CONTEXT_NAME);
|
|
16
17
|
if (this.has(contextName)) {
|
|
17
18
|
return this.get(contextName);
|
|
18
19
|
}
|
|
@@ -29,11 +30,14 @@ let MikroOrmRegistry = class MikroOrmRegistry {
|
|
|
29
30
|
createConnection(options) {
|
|
30
31
|
return this.register(options);
|
|
31
32
|
}
|
|
32
|
-
get(contextName
|
|
33
|
-
return this.instances.get(contextName);
|
|
33
|
+
get(contextName) {
|
|
34
|
+
return this.instances.get(contextName ?? this.DEFAULT_CONTEXT_NAME);
|
|
34
35
|
}
|
|
35
|
-
has(contextName
|
|
36
|
-
return this.instances.has(contextName);
|
|
36
|
+
has(contextName) {
|
|
37
|
+
return this.instances.has(contextName ?? this.DEFAULT_CONTEXT_NAME);
|
|
38
|
+
}
|
|
39
|
+
values() {
|
|
40
|
+
return this.instances.values();
|
|
37
41
|
}
|
|
38
42
|
async clear() {
|
|
39
43
|
const instances = [...this.instances.values()];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MikroOrmRegistry.js","sourceRoot":"","sources":["../../../src/services/MikroOrmRegistry.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAE5C,OAAO,EAAC,eAAe,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACrD,OAAO,EAAC,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AAM,EAA8C,eAAe;GAJvG,gBAAgB,CAgE5B;SAhEY,gBAAgB","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 /**\n * @deprecated Since 2022-02-01. Use {@link register} instead\n */\n public createConnection<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM> {\n return this.register(options);\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 /**\n * @deprecated Since 2022-02-01. Use {@link clear} instead\n */\n public closeConnections(): Promise<void> {\n return this.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,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./services/MikroOrmFactory";
|
|
2
|
+
import { AlterRunInContext, DIContext, OnDestroy, OnInit } from "@tsed/di";
|
|
2
3
|
import { Options } from "@mikro-orm/core";
|
|
3
4
|
declare global {
|
|
4
5
|
namespace TsED {
|
|
@@ -11,9 +12,12 @@ declare global {
|
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
|
-
export declare class MikroOrmModule implements OnDestroy, OnInit {
|
|
15
|
+
export declare class MikroOrmModule implements OnDestroy, OnInit, AlterRunInContext {
|
|
15
16
|
private readonly settings;
|
|
16
|
-
private readonly
|
|
17
|
+
private readonly registry;
|
|
18
|
+
private readonly context;
|
|
17
19
|
$onInit(): Promise<void>;
|
|
18
20
|
$onDestroy(): Promise<void>;
|
|
21
|
+
$alterRunInContext(next: (...args: unknown[]) => unknown, _: DIContext): () => unknown | Promise<() => unknown>;
|
|
22
|
+
private createContext;
|
|
19
23
|
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export * from "./decorators/orm";
|
|
|
7
7
|
export * from "./decorators/transactional";
|
|
8
8
|
export * from "./filters/OptimisticLockErrorFilter";
|
|
9
9
|
export * from "./interceptors/TransactionalInterceptor";
|
|
10
|
-
export * from "./services/
|
|
10
|
+
export * from "./services/MikroOrmContext";
|
|
11
11
|
export * from "./services/MikroOrmFactory";
|
|
12
12
|
export * from "./services/MikroOrmRegistry";
|
|
13
13
|
export * from "./services/RetryStrategy";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { InterceptorContext, InterceptorMethods, InterceptorNext } from "@tsed/di";
|
|
2
2
|
import { Logger } from "@tsed/logger";
|
|
3
|
-
import { MikroOrmRegistry } from "../services/MikroOrmRegistry";
|
|
4
|
-
import { DBContext } from "../services/DBContext";
|
|
5
3
|
import { RetryStrategy } from "../services/RetryStrategy";
|
|
4
|
+
import { MikroOrmContext } from "../services/MikroOrmContext";
|
|
5
|
+
import { MikroOrmRegistry } from "../services/MikroOrmRegistry";
|
|
6
6
|
export interface TransactionOptions {
|
|
7
7
|
retry?: boolean;
|
|
8
8
|
contextName?: string;
|
|
@@ -16,10 +16,10 @@ export declare class TransactionalInterceptor implements InterceptorMethods {
|
|
|
16
16
|
private readonly context;
|
|
17
17
|
private readonly logger;
|
|
18
18
|
private readonly retryStrategy?;
|
|
19
|
-
constructor(registry: MikroOrmRegistry, context:
|
|
19
|
+
constructor(registry: MikroOrmRegistry, context: MikroOrmContext, logger: Logger, retryStrategy?: RetryStrategy | undefined);
|
|
20
20
|
intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown>;
|
|
21
|
+
private runWithinCtx;
|
|
21
22
|
private runWithinNewCtx;
|
|
22
|
-
private runUnderCtx;
|
|
23
23
|
private extractContextName;
|
|
24
24
|
private executeInTransaction;
|
|
25
25
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EntityManager } from "@mikro-orm/core";
|
|
2
|
+
/**
|
|
3
|
+
* @internal Since 2022-06-20.
|
|
4
|
+
*/
|
|
5
|
+
export declare class MikroOrmContext {
|
|
6
|
+
run(managers: EntityManager[], task: (...args: unknown[]) => unknown): Promise<unknown>;
|
|
7
|
+
get(contextName?: string): EntityManager | undefined;
|
|
8
|
+
has(contextName?: string): boolean;
|
|
9
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { MikroORM, Options } from "@mikro-orm/core";
|
|
2
|
+
import { MikroOrmContext } from "./MikroOrmContext";
|
|
2
3
|
export declare class MikroOrmFactory {
|
|
3
|
-
private readonly
|
|
4
|
+
private readonly context;
|
|
5
|
+
constructor(context: MikroOrmContext);
|
|
4
6
|
create(options: Options): Promise<MikroORM>;
|
|
5
7
|
}
|
|
@@ -4,6 +4,7 @@ import { Logger } from "@tsed/logger";
|
|
|
4
4
|
export declare class MikroOrmRegistry {
|
|
5
5
|
private readonly logger;
|
|
6
6
|
private readonly mikroOrmFactory;
|
|
7
|
+
private readonly DEFAULT_CONTEXT_NAME;
|
|
7
8
|
private readonly instances;
|
|
8
9
|
constructor(logger: Logger, mikroOrmFactory: MikroOrmFactory);
|
|
9
10
|
register<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
|
|
@@ -13,6 +14,7 @@ export declare class MikroOrmRegistry {
|
|
|
13
14
|
createConnection<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
|
|
14
15
|
get(contextName?: string): MikroORM | undefined;
|
|
15
16
|
has(contextName?: string): boolean;
|
|
17
|
+
values(): IterableIterator<MikroORM>;
|
|
16
18
|
clear(): Promise<void>;
|
|
17
19
|
/**
|
|
18
20
|
* @deprecated Since 2022-02-01. Use {@link clear} instead
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/mikro-orm",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.116.0",
|
|
4
4
|
"description": "MikroORM package for Ts.ED framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -24,22 +24,22 @@
|
|
|
24
24
|
"tslib": "2.4.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@mikro-orm/core": "^
|
|
28
|
-
"@mikro-orm/mongodb": "^
|
|
29
|
-
"@tsed/common": "6.
|
|
30
|
-
"@tsed/core": "6.
|
|
31
|
-
"@tsed/di": "6.
|
|
32
|
-
"@tsed/json-mapper": "6.
|
|
27
|
+
"@mikro-orm/core": "^5.1.5",
|
|
28
|
+
"@mikro-orm/mongodb": "^5.1.5",
|
|
29
|
+
"@tsed/common": "6.116.0",
|
|
30
|
+
"@tsed/core": "6.116.0",
|
|
31
|
+
"@tsed/di": "6.116.0",
|
|
32
|
+
"@tsed/json-mapper": "6.116.0",
|
|
33
33
|
"@tsed/logger": ">=6.1.4",
|
|
34
|
-
"@tsed/schema": "6.
|
|
35
|
-
"@tsed/testing-mongoose": "6.
|
|
34
|
+
"@tsed/schema": "6.116.0",
|
|
35
|
+
"@tsed/testing-mongoose": "6.116.0",
|
|
36
36
|
"ts-mockito": "^2.6.1"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"@mikro-orm/core": ">=4.5.9",
|
|
40
|
-
"@tsed/common": "^6.
|
|
41
|
-
"@tsed/core": "^6.
|
|
42
|
-
"@tsed/di": "^6.
|
|
40
|
+
"@tsed/common": "^6.116.0",
|
|
41
|
+
"@tsed/core": "^6.116.0",
|
|
42
|
+
"@tsed/di": "^6.116.0",
|
|
43
43
|
"@tsed/logger": ">=6.1.4"
|
|
44
44
|
},
|
|
45
45
|
"repository": "https://github.com/tsedio/tsed",
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { EntityManager } from "@mikro-orm/core";
|
|
2
|
-
export declare class DBContext {
|
|
3
|
-
private readonly storage;
|
|
4
|
-
constructor();
|
|
5
|
-
run<R>(ctx: Map<string, EntityManager>, callback: (...args: any[]) => R): R;
|
|
6
|
-
entries(): Map<string, EntityManager> | undefined;
|
|
7
|
-
/**
|
|
8
|
-
* @deprecated Since 2022-02-01. Use {@link entries} instead
|
|
9
|
-
*/
|
|
10
|
-
getContext(): Map<string, EntityManager> | undefined;
|
|
11
|
-
get(contextName: string): EntityManager | undefined;
|
|
12
|
-
has(contextName: string): boolean;
|
|
13
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DBContext = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const di_1 = require("@tsed/di");
|
|
6
|
-
const async_hooks_1 = require("async_hooks");
|
|
7
|
-
let DBContext = class DBContext {
|
|
8
|
-
storage = new async_hooks_1.AsyncLocalStorage();
|
|
9
|
-
constructor() {
|
|
10
|
-
if (async_hooks_1.AsyncLocalStorage) {
|
|
11
|
-
this.storage = new async_hooks_1.AsyncLocalStorage();
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
throw new Error(`AsyncLocalStorage is not available for your Node.js version (${process.versions.node}).`);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
run(ctx, callback) {
|
|
18
|
-
return this.storage.run(ctx, callback);
|
|
19
|
-
}
|
|
20
|
-
entries() {
|
|
21
|
-
return this.storage.getStore();
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* @deprecated Since 2022-02-01. Use {@link entries} instead
|
|
25
|
-
*/
|
|
26
|
-
getContext() {
|
|
27
|
-
return this.entries();
|
|
28
|
-
}
|
|
29
|
-
get(contextName) {
|
|
30
|
-
const context = this.entries();
|
|
31
|
-
return context?.get(contextName);
|
|
32
|
-
}
|
|
33
|
-
has(contextName) {
|
|
34
|
-
const context = this.entries();
|
|
35
|
-
return !!context?.has(contextName);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
DBContext = tslib_1.__decorate([
|
|
39
|
-
(0, di_1.Injectable)(),
|
|
40
|
-
tslib_1.__metadata("design:paramtypes", [])
|
|
41
|
-
], DBContext);
|
|
42
|
-
exports.DBContext = DBContext;
|
|
43
|
-
//# sourceMappingURL=DBContext.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DBContext.js","sourceRoot":"","sources":["../../../src/services/DBContext.ts"],"names":[],"mappings":";;;;AAAA,iCAAoC;AAEpC,6CAA8C;AAG9C,IAAa,SAAS,GAAtB,MAAa,SAAS;IACH,OAAO,GAAG,IAAI,+BAAiB,EAA8B,CAAC;IAE/E;QACE,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;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAEM,GAAG,CAAC,WAAmB;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAE/B,OAAO,OAAO,EAAE,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,OAAO,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACrC,CAAC;CACF,CAAA;AArCY,SAAS;IADrB,IAAA,eAAU,GAAE;;GACA,SAAS,CAqCrB;AArCY,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 /**\n * @deprecated Since 2022-02-01. Use {@link entries} instead\n */\n public getContext(): Map<string, EntityManager> | undefined {\n return this.entries();\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,40 +0,0 @@
|
|
|
1
|
-
import { __decorate, __metadata } from "tslib";
|
|
2
|
-
import { Injectable } from "@tsed/di";
|
|
3
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
4
|
-
let DBContext = class DBContext {
|
|
5
|
-
storage = new AsyncLocalStorage();
|
|
6
|
-
constructor() {
|
|
7
|
-
if (AsyncLocalStorage) {
|
|
8
|
-
this.storage = new AsyncLocalStorage();
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
throw new Error(`AsyncLocalStorage is not available for your Node.js version (${process.versions.node}).`);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
run(ctx, callback) {
|
|
15
|
-
return this.storage.run(ctx, callback);
|
|
16
|
-
}
|
|
17
|
-
entries() {
|
|
18
|
-
return this.storage.getStore();
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* @deprecated Since 2022-02-01. Use {@link entries} instead
|
|
22
|
-
*/
|
|
23
|
-
getContext() {
|
|
24
|
-
return this.entries();
|
|
25
|
-
}
|
|
26
|
-
get(contextName) {
|
|
27
|
-
const context = this.entries();
|
|
28
|
-
return context?.get(contextName);
|
|
29
|
-
}
|
|
30
|
-
has(contextName) {
|
|
31
|
-
const context = this.entries();
|
|
32
|
-
return !!context?.has(contextName);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
DBContext = __decorate([
|
|
36
|
-
Injectable(),
|
|
37
|
-
__metadata("design:paramtypes", [])
|
|
38
|
-
], DBContext);
|
|
39
|
-
export { DBContext };
|
|
40
|
-
//# sourceMappingURL=DBContext.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DBContext.js","sourceRoot":"","sources":["../../../src/services/DBContext.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAC,iBAAiB,EAAC,MAAM,aAAa,CAAC;AAG9C,IAAa,SAAS,GAAtB,MAAa,SAAS;IACH,OAAO,GAAG,IAAI,iBAAiB,EAA8B,CAAC;IAE/E;QACE,IAAI,iBAAiB,EAAE;YACrB,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAiB,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;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAEM,GAAG,CAAC,WAAmB;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAE/B,OAAO,OAAO,EAAE,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,OAAO,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACrC,CAAC;CACF,CAAA;AArCY,SAAS;IADrB,UAAU,EAAE;;GACA,SAAS,CAqCrB;SArCY,SAAS","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 /**\n * @deprecated Since 2022-02-01. Use {@link entries} instead\n */\n public getContext(): Map<string, EntityManager> | undefined {\n return this.entries();\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,13 +0,0 @@
|
|
|
1
|
-
import { EntityManager } from "@mikro-orm/core";
|
|
2
|
-
export declare class DBContext {
|
|
3
|
-
private readonly storage;
|
|
4
|
-
constructor();
|
|
5
|
-
run<R>(ctx: Map<string, EntityManager>, callback: (...args: any[]) => R): R;
|
|
6
|
-
entries(): Map<string, EntityManager> | undefined;
|
|
7
|
-
/**
|
|
8
|
-
* @deprecated Since 2022-02-01. Use {@link entries} instead
|
|
9
|
-
*/
|
|
10
|
-
getContext(): Map<string, EntityManager> | undefined;
|
|
11
|
-
get(contextName: string): EntityManager | undefined;
|
|
12
|
-
has(contextName: string): boolean;
|
|
13
|
-
}
|