@tsed/mikro-orm 6.96.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 +14 -0
- package/lib/cjs/MikroOrmModule.d.ts +19 -0
- package/lib/cjs/MikroOrmModule.js +31 -0
- package/lib/cjs/MikroOrmModule.js.map +1 -0
- package/lib/cjs/decorators/connection.d.ts +1 -0
- package/lib/cjs/decorators/connection.js +8 -0
- package/lib/cjs/decorators/connection.js.map +1 -0
- package/lib/cjs/decorators/index.d.ts +2 -0
- package/lib/cjs/decorators/index.js +6 -0
- package/lib/cjs/decorators/index.js.map +1 -0
- package/lib/cjs/decorators/transactional.d.ts +2 -0
- package/lib/cjs/decorators/transactional.js +8 -0
- package/lib/cjs/decorators/transactional.js.map +1 -0
- package/lib/cjs/filters/OptimisticLockErrorFilter.d.ts +5 -0
- package/lib/cjs/filters/OptimisticLockErrorFilter.js +20 -0
- package/lib/cjs/filters/OptimisticLockErrorFilter.js.map +1 -0
- package/lib/cjs/filters/index.d.ts +1 -0
- package/lib/cjs/filters/index.js +5 -0
- package/lib/cjs/filters/index.js.map +1 -0
- package/lib/cjs/index.d.ts +5 -0
- package/lib/cjs/index.js +9 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interceptors/TransactionalInterceptor.d.ts +16 -0
- package/lib/cjs/interceptors/TransactionalInterceptor.js +64 -0
- package/lib/cjs/interceptors/TransactionalInterceptor.js.map +1 -0
- package/lib/cjs/interceptors/index.d.ts +1 -0
- package/lib/cjs/interceptors/index.js +5 -0
- package/lib/cjs/interceptors/index.js.map +1 -0
- package/lib/cjs/services/DBContext.d.ts +9 -0
- package/lib/cjs/services/DBContext.js +37 -0
- package/lib/cjs/services/DBContext.js.map +1 -0
- package/lib/cjs/services/MikroOrmFactory.d.ts +5 -0
- package/lib/cjs/services/MikroOrmFactory.js +24 -0
- package/lib/cjs/services/MikroOrmFactory.js.map +1 -0
- package/lib/cjs/services/MikroOrmRegistry.d.ts +14 -0
- package/lib/cjs/services/MikroOrmRegistry.js +51 -0
- package/lib/cjs/services/MikroOrmRegistry.js.map +1 -0
- package/lib/cjs/services/RetryStrategy.d.ts +3 -0
- package/lib/cjs/services/RetryStrategy.js +3 -0
- package/lib/cjs/services/RetryStrategy.js.map +1 -0
- package/lib/cjs/services/index.d.ts +4 -0
- package/lib/cjs/services/index.js +8 -0
- package/lib/cjs/services/index.js.map +1 -0
- package/lib/esm/MikroOrmModule.js +28 -0
- package/lib/esm/MikroOrmModule.js.map +1 -0
- package/lib/esm/decorators/connection.js +4 -0
- package/lib/esm/decorators/connection.js.map +1 -0
- package/lib/esm/decorators/index.js +3 -0
- package/lib/esm/decorators/index.js.map +1 -0
- package/lib/esm/decorators/transactional.js +4 -0
- package/lib/esm/decorators/transactional.js.map +1 -0
- package/lib/esm/filters/OptimisticLockErrorFilter.js +17 -0
- package/lib/esm/filters/OptimisticLockErrorFilter.js.map +1 -0
- package/lib/esm/filters/index.js +2 -0
- package/lib/esm/filters/index.js.map +1 -0
- package/lib/esm/index.js +6 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interceptors/TransactionalInterceptor.js +61 -0
- package/lib/esm/interceptors/TransactionalInterceptor.js.map +1 -0
- package/lib/esm/interceptors/index.js +2 -0
- package/lib/esm/interceptors/index.js.map +1 -0
- package/lib/esm/services/DBContext.js +34 -0
- package/lib/esm/services/DBContext.js.map +1 -0
- package/lib/esm/services/MikroOrmFactory.js +21 -0
- package/lib/esm/services/MikroOrmFactory.js.map +1 -0
- package/lib/esm/services/MikroOrmRegistry.js +48 -0
- package/lib/esm/services/MikroOrmRegistry.js.map +1 -0
- package/lib/esm/services/RetryStrategy.js +2 -0
- package/lib/esm/services/RetryStrategy.js.map +1 -0
- package/lib/esm/services/index.js +5 -0
- package/lib/esm/services/index.js.map +1 -0
- package/lib/types/MikroOrmModule.d.ts +19 -0
- package/lib/types/decorators/connection.d.ts +1 -0
- package/lib/types/decorators/index.d.ts +2 -0
- package/lib/types/decorators/transactional.d.ts +2 -0
- package/lib/types/filters/OptimisticLockErrorFilter.d.ts +5 -0
- package/lib/types/filters/index.d.ts +1 -0
- package/lib/types/index.d.ts +5 -0
- package/lib/types/interceptors/TransactionalInterceptor.d.ts +16 -0
- package/lib/types/interceptors/index.d.ts +1 -0
- package/lib/types/services/DBContext.d.ts +9 -0
- package/lib/types/services/MikroOrmFactory.d.ts +5 -0
- package/lib/types/services/MikroOrmRegistry.d.ts +14 -0
- package/lib/types/services/RetryStrategy.d.ts +3 -0
- package/lib/types/services/index.d.ts +4 -0
- package/package.json +49 -0
- package/readme.md +237 -0
- package/tsconfig.compile.esm.json +23 -0
package/jest.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "mikro-orm"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: {
|
|
8
|
+
branches: 85.29,
|
|
9
|
+
functions: 100,
|
|
10
|
+
lines: 98.95,
|
|
11
|
+
statements: 99.15
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { OnDestroy, OnInit } from "@tsed/di";
|
|
2
|
+
import { Options } from "@mikro-orm/core";
|
|
3
|
+
declare global {
|
|
4
|
+
namespace TsED {
|
|
5
|
+
interface Configuration {
|
|
6
|
+
/**
|
|
7
|
+
* The the ORM configuration, entity metadata.
|
|
8
|
+
* If you omit the `options` parameter, your CLI config will be used.
|
|
9
|
+
*/
|
|
10
|
+
mikroOrm?: Options[];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export declare class MikroOrmModule implements OnDestroy, OnInit {
|
|
15
|
+
private readonly settings;
|
|
16
|
+
private readonly mikroOrmRegistry;
|
|
17
|
+
$onInit(): Promise<void>;
|
|
18
|
+
$onDestroy(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MikroOrmModule = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const services_1 = require("./services");
|
|
6
|
+
const di_1 = require("@tsed/di");
|
|
7
|
+
let MikroOrmModule = class MikroOrmModule {
|
|
8
|
+
async $onInit() {
|
|
9
|
+
const promises = this.settings.map((opts) => this.mikroOrmRegistry.createConnection(opts));
|
|
10
|
+
await Promise.all(promises);
|
|
11
|
+
}
|
|
12
|
+
$onDestroy() {
|
|
13
|
+
return this.mikroOrmRegistry.closeConnections();
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
(0, tslib_1.__decorate)([
|
|
17
|
+
(0, di_1.Constant)("mikroOrm", []),
|
|
18
|
+
(0, tslib_1.__metadata)("design:type", Array)
|
|
19
|
+
], MikroOrmModule.prototype, "settings", void 0);
|
|
20
|
+
(0, tslib_1.__decorate)([
|
|
21
|
+
(0, di_1.Inject)(),
|
|
22
|
+
(0, tslib_1.__metadata)("design:type", services_1.MikroOrmRegistry)
|
|
23
|
+
], MikroOrmModule.prototype, "mikroOrmRegistry", void 0);
|
|
24
|
+
MikroOrmModule = (0, tslib_1.__decorate)([
|
|
25
|
+
(0, di_1.Module)({
|
|
26
|
+
imports: [services_1.DBContext, services_1.MikroOrmRegistry],
|
|
27
|
+
deps: [di_1.Configuration, services_1.MikroOrmRegistry]
|
|
28
|
+
})
|
|
29
|
+
], MikroOrmModule);
|
|
30
|
+
exports.MikroOrmModule = MikroOrmModule;
|
|
31
|
+
//# sourceMappingURL=MikroOrmModule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";;;;AAAA,yCAAuD;AACvD,iCAAoF;AAoBpF,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","sourcesContent":["import {DBContext, MikroOrmRegistry} from \"./services\";\nimport {Configuration, Constant, Inject, Module, OnDestroy, OnInit} 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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const Connection: (connectionName?: string | undefined) => PropertyDecorator;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Connection = void 0;
|
|
4
|
+
const services_1 = require("../services");
|
|
5
|
+
const di_1 = require("@tsed/di");
|
|
6
|
+
const Connection = (connectionName) => (0, di_1.Inject)(services_1.MikroOrmRegistry, (registry) => registry.get(connectionName));
|
|
7
|
+
exports.Connection = Connection;
|
|
8
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../../src/decorators/connection.ts"],"names":[],"mappings":";;;AAAA,0CAA6C;AAC7C,iCAAgC;AAEzB,MAAM,UAAU,GAAG,CAAC,cAAuB,EAAqB,EAAE,CACvE,IAAA,WAAM,EAAC,2BAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,cAAc,CAAC,CAAsB,CAAC;AADjG,QAAA,UAAU,cACuF","sourcesContent":["import {MikroOrmRegistry} from \"../services\";\nimport {Inject} from \"@tsed/di\";\n\nexport const Connection = (connectionName?: string): PropertyDecorator =>\n Inject(MikroOrmRegistry, (registry: MikroOrmRegistry) => registry.get(connectionName)) as PropertyDecorator;\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./transactional"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./connection"), exports);
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decorators/index.ts"],"names":[],"mappings":";;;AAAA,+DAAgC;AAChC,4DAA6B","sourcesContent":["export * from \"./transactional\";\nexport * from \"./connection\";\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Transactional = void 0;
|
|
4
|
+
const interceptors_1 = require("../interceptors");
|
|
5
|
+
const di_1 = require("@tsed/di");
|
|
6
|
+
const Transactional = (connectionOrOptions) => (0, di_1.Intercept)(interceptors_1.TransactionalInterceptor, connectionOrOptions);
|
|
7
|
+
exports.Transactional = Transactional;
|
|
8
|
+
//# sourceMappingURL=transactional.js.map
|
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ExceptionFilterMethods, PlatformContext } from "@tsed/common";
|
|
2
|
+
import { OptimisticLockError } from "@mikro-orm/core";
|
|
3
|
+
export declare class OptimisticLockErrorFilter implements ExceptionFilterMethods<OptimisticLockError> {
|
|
4
|
+
catch(exception: OptimisticLockError, ctx: PlatformContext): void;
|
|
5
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OptimisticLockErrorFilter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@tsed/common");
|
|
6
|
+
const core_1 = require("@mikro-orm/core");
|
|
7
|
+
let OptimisticLockErrorFilter = class OptimisticLockErrorFilter {
|
|
8
|
+
catch(exception, ctx) {
|
|
9
|
+
const { response, logger } = ctx;
|
|
10
|
+
logger.error({
|
|
11
|
+
exception
|
|
12
|
+
});
|
|
13
|
+
response.status(409).body(`Update refused. The resource has changed on the server. Please try again later`);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
OptimisticLockErrorFilter = (0, tslib_1.__decorate)([
|
|
17
|
+
(0, common_1.Catch)(core_1.OptimisticLockError)
|
|
18
|
+
], OptimisticLockErrorFilter);
|
|
19
|
+
exports.OptimisticLockErrorFilter = OptimisticLockErrorFilter;
|
|
20
|
+
//# sourceMappingURL=OptimisticLockErrorFilter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OptimisticLockErrorFilter.js","sourceRoot":"","sources":["../../../src/filters/OptimisticLockErrorFilter.ts"],"names":[],"mappings":";;;;AAAA,yCAA4E;AAC5E,0CAAoD;AAGpD,IAAa,yBAAyB,GAAtC,MAAa,yBAAyB;IAC7B,KAAK,CAAC,SAA8B,EAAE,GAAoB;QAC/D,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAC,GAAG,GAAG,CAAC;QAE/B,MAAM,CAAC,KAAK,CAAC;YACX,SAAS;SACV,CAAC,CAAC;QAEH,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,gFAAgF,CAAC,CAAC;IAC9G,CAAC;CACF,CAAA;AAVY,yBAAyB;IADrC,IAAA,cAAK,EAAC,0BAAmB,CAAC;GACd,yBAAyB,CAUrC;AAVY,8DAAyB","sourcesContent":["import {Catch, ExceptionFilterMethods, PlatformContext} from \"@tsed/common\";\nimport {OptimisticLockError} from \"@mikro-orm/core\";\n\n@Catch(OptimisticLockError)\nexport class OptimisticLockErrorFilter implements ExceptionFilterMethods<OptimisticLockError> {\n public catch(exception: OptimisticLockError, ctx: PlatformContext): void {\n const {response, logger} = ctx;\n\n logger.error({\n exception\n });\n\n response.status(409).body(`Update refused. The resource has changed on the server. Please try again later`);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./OptimisticLockErrorFilter";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/filters/index.ts"],"names":[],"mappings":";;;AAAA,2EAA4C","sourcesContent":["export * from \"./OptimisticLockErrorFilter\";\n"]}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
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);
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DBContext, MikroOrmRegistry, RetryStrategy } from "../services";
|
|
2
|
+
import { InterceptorContext, InterceptorMethods, InterceptorNext } from "@tsed/di";
|
|
3
|
+
export interface TransactionOptions {
|
|
4
|
+
connectionName?: string;
|
|
5
|
+
retryStrategy?: RetryStrategy;
|
|
6
|
+
}
|
|
7
|
+
export declare class TransactionalInterceptor implements InterceptorMethods {
|
|
8
|
+
private readonly registry;
|
|
9
|
+
private readonly context;
|
|
10
|
+
constructor(registry: MikroOrmRegistry, context: DBContext);
|
|
11
|
+
intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown>;
|
|
12
|
+
private runWithinNewCtx;
|
|
13
|
+
private runUnderCtx;
|
|
14
|
+
private extractContextName;
|
|
15
|
+
private executeInTransaction;
|
|
16
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TransactionalInterceptor = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const services_1 = require("../services");
|
|
6
|
+
const di_1 = require("@tsed/di");
|
|
7
|
+
let TransactionalInterceptor = class TransactionalInterceptor {
|
|
8
|
+
constructor(registry, context) {
|
|
9
|
+
this.registry = registry;
|
|
10
|
+
this.context = context;
|
|
11
|
+
}
|
|
12
|
+
async intercept(context, next) {
|
|
13
|
+
const options = this.extractContextName(context);
|
|
14
|
+
const ctx = this.context.getContext();
|
|
15
|
+
if (!ctx) {
|
|
16
|
+
return this.runWithinNewCtx(next, options);
|
|
17
|
+
}
|
|
18
|
+
if (ctx.has(options.connectionName)) {
|
|
19
|
+
return next();
|
|
20
|
+
}
|
|
21
|
+
return this.runUnderCtx(next, ctx, options);
|
|
22
|
+
}
|
|
23
|
+
runWithinNewCtx(next, options) {
|
|
24
|
+
const ctx = new Map();
|
|
25
|
+
return this.runUnderCtx(next, ctx, options);
|
|
26
|
+
}
|
|
27
|
+
runUnderCtx(next, ctx, options) {
|
|
28
|
+
const orm = this.registry.get(options.connectionName);
|
|
29
|
+
const em = orm.em.fork(true, true);
|
|
30
|
+
ctx.set(em.name, em);
|
|
31
|
+
const runInTransaction = () => this.executeInTransaction(options.connectionName, next);
|
|
32
|
+
return this.context.run(ctx, () => (options.retryStrategy ? options.retryStrategy.acquire(runInTransaction) : runInTransaction()));
|
|
33
|
+
}
|
|
34
|
+
extractContextName(context) {
|
|
35
|
+
const options = context.options || {};
|
|
36
|
+
let connectionName;
|
|
37
|
+
let retryStrategy;
|
|
38
|
+
if (typeof options === "string") {
|
|
39
|
+
connectionName = options;
|
|
40
|
+
}
|
|
41
|
+
else if (options) {
|
|
42
|
+
connectionName = options.connectionName;
|
|
43
|
+
retryStrategy = options.retryStrategy;
|
|
44
|
+
}
|
|
45
|
+
if (!connectionName) {
|
|
46
|
+
connectionName = "default";
|
|
47
|
+
}
|
|
48
|
+
return { connectionName, retryStrategy };
|
|
49
|
+
}
|
|
50
|
+
async executeInTransaction(connectionName, next) {
|
|
51
|
+
const em = this.context.get(connectionName);
|
|
52
|
+
const result = await next();
|
|
53
|
+
await (em === null || em === void 0 ? void 0 : em.flush());
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
TransactionalInterceptor = (0, tslib_1.__decorate)([
|
|
58
|
+
(0, di_1.Interceptor)(),
|
|
59
|
+
(0, tslib_1.__param)(0, (0, di_1.Inject)()),
|
|
60
|
+
(0, tslib_1.__param)(1, (0, di_1.Inject)()),
|
|
61
|
+
(0, tslib_1.__metadata)("design:paramtypes", [services_1.MikroOrmRegistry, services_1.DBContext])
|
|
62
|
+
], TransactionalInterceptor);
|
|
63
|
+
exports.TransactionalInterceptor = TransactionalInterceptor;
|
|
64
|
+
//# sourceMappingURL=TransactionalInterceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";;;;AAAA,0CAAuE;AACvE,iCAAsG;AAStG,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;I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InterceptorMethods, InterceptorNext} from \"@tsed/di\";\nimport {EntityManager} from \"@mikro-orm/core\";\n\nexport interface TransactionOptions {\n connectionName?: string;\n retryStrategy?: RetryStrategy;\n}\n\n@Interceptor()\nexport class TransactionalInterceptor implements InterceptorMethods {\n constructor(@Inject() private readonly registry: MikroOrmRegistry, @Inject() private readonly context: DBContext) {}\n\n public async intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown> {\n const options = this.extractContextName(context);\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.retryStrategy ? options.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 retryStrategy: RetryStrategy | undefined;\n\n if (typeof options === \"string\") {\n connectionName = options;\n } else if (options) {\n connectionName = options.connectionName;\n retryStrategy = options.retryStrategy;\n }\n\n if (!connectionName) {\n connectionName = \"default\";\n }\n\n return {connectionName, retryStrategy};\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./TransactionalInterceptor";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interceptors/index.ts"],"names":[],"mappings":";;;AAAA,0EAA2C","sourcesContent":["export * from \"./TransactionalInterceptor\";\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
getContext(): Map<string, EntityManager> | undefined;
|
|
7
|
+
get(contextName: string): EntityManager | undefined;
|
|
8
|
+
has(contextName: string): boolean;
|
|
9
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
constructor() {
|
|
9
|
+
this.storage = new async_hooks_1.AsyncLocalStorage();
|
|
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
|
+
getContext() {
|
|
21
|
+
return this.storage.getStore();
|
|
22
|
+
}
|
|
23
|
+
get(contextName) {
|
|
24
|
+
const context = this.getContext();
|
|
25
|
+
return context === null || context === void 0 ? void 0 : context.get(contextName);
|
|
26
|
+
}
|
|
27
|
+
has(contextName) {
|
|
28
|
+
const context = this.getContext();
|
|
29
|
+
return !!(context === null || context === void 0 ? void 0 : context.has(contextName));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
DBContext = (0, tslib_1.__decorate)([
|
|
33
|
+
(0, di_1.Injectable)(),
|
|
34
|
+
(0, tslib_1.__metadata)("design:paramtypes", [])
|
|
35
|
+
], DBContext);
|
|
36
|
+
exports.DBContext = DBContext;
|
|
37
|
+
//# sourceMappingURL=DBContext.js.map
|
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MikroOrmFactory = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const DBContext_1 = require("./DBContext");
|
|
6
|
+
const core_1 = require("@mikro-orm/core");
|
|
7
|
+
const di_1 = require("@tsed/di");
|
|
8
|
+
let MikroOrmFactory = class MikroOrmFactory {
|
|
9
|
+
create(connectionOptions) {
|
|
10
|
+
return core_1.MikroORM.init({
|
|
11
|
+
...connectionOptions,
|
|
12
|
+
context: (name) => this.dbContext.get(name)
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
(0, tslib_1.__decorate)([
|
|
17
|
+
(0, di_1.Inject)(),
|
|
18
|
+
(0, tslib_1.__metadata)("design:type", DBContext_1.DBContext)
|
|
19
|
+
], MikroOrmFactory.prototype, "dbContext", void 0);
|
|
20
|
+
MikroOrmFactory = (0, tslib_1.__decorate)([
|
|
21
|
+
(0, di_1.Injectable)()
|
|
22
|
+
], MikroOrmFactory);
|
|
23
|
+
exports.MikroOrmFactory = MikroOrmFactory;
|
|
24
|
+
//# sourceMappingURL=MikroOrmFactory.js.map
|
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MikroOrmFactory } from "./MikroOrmFactory";
|
|
2
|
+
import { InjectorService } from "@tsed/di";
|
|
3
|
+
import { IDatabaseDriver as DatabaseDriver, MikroORM, Options } from "@mikro-orm/core";
|
|
4
|
+
export declare class MikroOrmRegistry {
|
|
5
|
+
private readonly injector;
|
|
6
|
+
private readonly mikroOrmFactory;
|
|
7
|
+
private readonly connections;
|
|
8
|
+
constructor(injector: InjectorService, 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>;
|
|
13
|
+
private dispose;
|
|
14
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MikroOrmRegistry = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const MikroOrmFactory_1 = require("./MikroOrmFactory");
|
|
6
|
+
const di_1 = require("@tsed/di");
|
|
7
|
+
const core_1 = require("@tsed/core");
|
|
8
|
+
let MikroOrmRegistry = class MikroOrmRegistry {
|
|
9
|
+
constructor(injector, mikroOrmFactory) {
|
|
10
|
+
this.injector = injector;
|
|
11
|
+
this.mikroOrmFactory = mikroOrmFactory;
|
|
12
|
+
this.connections = new Map();
|
|
13
|
+
}
|
|
14
|
+
async createConnection(connectionOptions) {
|
|
15
|
+
const connectionName = (0, core_1.getValue)(connectionOptions, "contextName", "default");
|
|
16
|
+
if (this.has(connectionName)) {
|
|
17
|
+
return this.get(connectionName);
|
|
18
|
+
}
|
|
19
|
+
this.injector.logger.info(`Create connection with MikroOrm to database: %s`, connectionName);
|
|
20
|
+
this.injector.logger.debug(`options: %j`, connectionOptions);
|
|
21
|
+
const connection = await this.mikroOrmFactory.create(connectionOptions);
|
|
22
|
+
this.connections.set(connectionName, connection);
|
|
23
|
+
this.injector.logger.info(`Connected with MikroOrm to database: %s`, connectionName);
|
|
24
|
+
return connection;
|
|
25
|
+
}
|
|
26
|
+
get(id = "default") {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
28
|
+
return this.connections.get(id);
|
|
29
|
+
}
|
|
30
|
+
has(id = "default") {
|
|
31
|
+
return this.connections.has(id);
|
|
32
|
+
}
|
|
33
|
+
async closeConnections() {
|
|
34
|
+
const connections = [...this.connections.values()];
|
|
35
|
+
await Promise.all(connections.map((connection) => (0, core_1.catchAsyncError)(() => this.dispose(connection))));
|
|
36
|
+
this.connections.clear();
|
|
37
|
+
}
|
|
38
|
+
async dispose(connection) {
|
|
39
|
+
if (await connection.isConnected()) {
|
|
40
|
+
await connection.close(false);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
MikroOrmRegistry = (0, tslib_1.__decorate)([
|
|
45
|
+
(0, di_1.Injectable)(),
|
|
46
|
+
(0, tslib_1.__param)(0, (0, di_1.Inject)()),
|
|
47
|
+
(0, tslib_1.__param)(1, (0, di_1.Inject)()),
|
|
48
|
+
(0, tslib_1.__metadata)("design:paramtypes", [di_1.InjectorService, MikroOrmFactory_1.MikroOrmFactory])
|
|
49
|
+
], MikroOrmRegistry);
|
|
50
|
+
exports.MikroOrmRegistry = MikroOrmRegistry;
|
|
51
|
+
//# sourceMappingURL=MikroOrmRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MikroOrmRegistry.js","sourceRoot":"","sources":["../../../src/services/MikroOrmRegistry.ts"],"names":[],"mappings":";;;;AAAA,uDAAkD;AAClD,iCAA6D;AAE7D,qCAAqD;AAGrD,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAG3B,YAAuC,QAAyB,EAA6B,eAAgC;QAAtF,aAAQ,GAAR,QAAQ,CAAiB;QAA6B,oBAAe,GAAf,eAAe,CAAiB;QAF5G,gBAAW,GAAG,IAAI,GAAG,EAAoB,CAAC;IAEqE,CAAC;IAE1H,KAAK,CAAC,gBAAgB,CAA2B,iBAA6B;QACnF,MAAM,cAAc,GAAG,IAAA,eAAQ,EAAS,iBAAiB,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC;QAErF,IAAI,IAAI,CA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,EAAE;YAClC,MAAM,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC/B;IACH,CAAC;CACF,CAAA;AA9CY,gBAAgB;IAD5B,IAAA,eAAU,GAAE;IAIE,wBAAA,IAAA,WAAM,GAAE,CAAA;IAA8C,wBAAA,IAAA,WAAM,GAAE,CAAA;kDAA1B,oBAAe,EAA8C,iCAAe;GAHlH,gBAAgB,CA8C5B;AA9CY,4CAAgB","sourcesContent":["import {MikroOrmFactory} from \"./MikroOrmFactory\";\nimport {Inject, Injectable, InjectorService} from \"@tsed/di\";\nimport {IDatabaseDriver as DatabaseDriver, MikroORM, Options} from \"@mikro-orm/core\";\nimport {catchAsyncError, getValue} from \"@tsed/core\";\n\n@Injectable()\nexport class MikroOrmRegistry {\n private readonly connections = new Map<string, MikroORM>();\n\n constructor(@Inject() private readonly injector: InjectorService, @Inject() private readonly mikroOrmFactory: MikroOrmFactory) {}\n\n public async createConnection<T extends DatabaseDriver>(connectionOptions: Options<T>): Promise<MikroORM> {\n const connectionName = getValue<string>(connectionOptions, \"contextName\", \"default\");\n\n if (this.has(connectionName)) {\n return this.get(connectionName);\n }\n\n this.injector.logger.info(`Create connection with MikroOrm to database: %s`, connectionName);\n this.injector.logger.debug(`options: %j`, connectionOptions);\n\n const connection = await this.mikroOrmFactory.create(connectionOptions);\n\n this.connections.set(connectionName, connection);\n\n this.injector.logger.info(`Connected with MikroOrm to database: %s`, connectionName);\n\n return connection;\n }\n\n public get(id: string = \"default\"): MikroORM {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return this.connections.get(id)!;\n }\n\n public has(id: string = \"default\"): boolean {\n return this.connections.has(id);\n }\n\n public async closeConnections(): Promise<void> {\n const connections = [...this.connections.values()];\n\n await Promise.all(connections.map((connection: MikroORM) => catchAsyncError(() => this.dispose(connection))));\n\n this.connections.clear();\n }\n\n private async dispose(connection: MikroORM): Promise<void> {\n if (await connection.isConnected()) {\n await connection.close(false);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RetryStrategy.js","sourceRoot":"","sources":["../../../src/services/RetryStrategy.ts"],"names":[],"mappings":"","sourcesContent":["export interface RetryStrategy {\n acquire<T extends (...args: unknown[]) => unknown>(task: T): Promise<ReturnType<T>>;\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./DBContext"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./MikroOrmFactory"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./MikroOrmRegistry"), exports);
|
|
7
|
+
(0, tslib_1.__exportStar)(require("./RetryStrategy"), exports);
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/services/index.ts"],"names":[],"mappings":";;;AAAA,2DAA4B;AAC5B,iEAAkC;AAClC,kEAAmC;AACnC,+DAAgC","sourcesContent":["export * from \"./DBContext\";\nexport * from \"./MikroOrmFactory\";\nexport * from \"./MikroOrmRegistry\";\nexport * from \"./RetryStrategy\";\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { DBContext, MikroOrmRegistry } from "./services";
|
|
3
|
+
import { Configuration, Constant, Inject, Module } from "@tsed/di";
|
|
4
|
+
let MikroOrmModule = class MikroOrmModule {
|
|
5
|
+
async $onInit() {
|
|
6
|
+
const promises = this.settings.map((opts) => this.mikroOrmRegistry.createConnection(opts));
|
|
7
|
+
await Promise.all(promises);
|
|
8
|
+
}
|
|
9
|
+
$onDestroy() {
|
|
10
|
+
return this.mikroOrmRegistry.closeConnections();
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
__decorate([
|
|
14
|
+
Constant("mikroOrm", []),
|
|
15
|
+
__metadata("design:type", Array)
|
|
16
|
+
], MikroOrmModule.prototype, "settings", void 0);
|
|
17
|
+
__decorate([
|
|
18
|
+
Inject(),
|
|
19
|
+
__metadata("design:type", MikroOrmRegistry)
|
|
20
|
+
], MikroOrmModule.prototype, "mikroOrmRegistry", void 0);
|
|
21
|
+
MikroOrmModule = __decorate([
|
|
22
|
+
Module({
|
|
23
|
+
imports: [DBContext, MikroOrmRegistry],
|
|
24
|
+
deps: [Configuration, MikroOrmRegistry]
|
|
25
|
+
})
|
|
26
|
+
], MikroOrmModule);
|
|
27
|
+
export { MikroOrmModule };
|
|
28
|
+
//# sourceMappingURL=MikroOrmModule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MikroOrmModule.js","sourceRoot":"","sources":["../../src/MikroOrmModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,gBAAgB,EAAC,MAAM,YAAY,CAAC;AACvD,OAAO,EAAC,aAAa,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAoB,MAAM,UAAU,CAAC;AAoBpF,IAAa,cAAc,GAA3B,MAAa,cAAc;IAOlB,KAAK,CAAC,OAAO;QAClB,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;QAE3F,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAEM,UAAU;QACf,OAAO,IAAI,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,CAAC;IAClD,CAAC;CACF,CAAA;AAdC;IADC,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC;;gDACa;AAGtC;IADC,MAAM,EAAE;8BAC2B,gBAAgB;wDAAC;AAL1C,cAAc;IAJ1B,MAAM,CAAC;QACN,OAAO,EAAE,CAAC,SAAS,EAAE,gBAAgB,CAAC;QACtC,IAAI,EAAE,CAAC,aAAa,EAAE,gBAAgB,CAAC;KACxC,CAAC;GACW,cAAc,CAgB1B;SAhBY,cAAc","sourcesContent":["import {DBContext, MikroOrmRegistry} from \"./services\";\nimport {Configuration, Constant, Inject, Module, OnDestroy, OnInit} 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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../../src/decorators/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAC,MAAM,EAAC,MAAM,UAAU,CAAC;AAEhC,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,cAAuB,EAAqB,EAAE,CACvE,MAAM,CAAC,gBAAgB,EAAE,CAAC,QAA0B,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,cAAc,CAAC,CAAsB,CAAC","sourcesContent":["import {MikroOrmRegistry} from \"../services\";\nimport {Inject} from \"@tsed/di\";\n\nexport const Connection = (connectionName?: string): PropertyDecorator =>\n Inject(MikroOrmRegistry, (registry: MikroOrmRegistry) => registry.get(connectionName)) as PropertyDecorator;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC","sourcesContent":["export * from \"./transactional\";\nexport * from \"./connection\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transactional.js","sourceRoot":"","sources":["../../../src/decorators/transactional.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,wBAAwB,EAAqB,MAAM,iBAAiB,CAAC;AAC7E,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,mBAAiD,EAAmB,EAAE,CAClG,SAAS,CAAC,wBAAwB,EAAE,mBAAmB,CAAC,CAAC","sourcesContent":["import {TransactionalInterceptor, TransactionOptions} from \"../interceptors\";\nimport {Intercept} from \"@tsed/di\";\n\nexport const Transactional = (connectionOrOptions?: string | TransactionOptions): MethodDecorator =>\n Intercept(TransactionalInterceptor, connectionOrOptions);\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { Catch } from "@tsed/common";
|
|
3
|
+
import { OptimisticLockError } from "@mikro-orm/core";
|
|
4
|
+
let OptimisticLockErrorFilter = class OptimisticLockErrorFilter {
|
|
5
|
+
catch(exception, ctx) {
|
|
6
|
+
const { response, logger } = ctx;
|
|
7
|
+
logger.error({
|
|
8
|
+
exception
|
|
9
|
+
});
|
|
10
|
+
response.status(409).body(`Update refused. The resource has changed on the server. Please try again later`);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
OptimisticLockErrorFilter = __decorate([
|
|
14
|
+
Catch(OptimisticLockError)
|
|
15
|
+
], OptimisticLockErrorFilter);
|
|
16
|
+
export { OptimisticLockErrorFilter };
|
|
17
|
+
//# sourceMappingURL=OptimisticLockErrorFilter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OptimisticLockErrorFilter.js","sourceRoot":"","sources":["../../../src/filters/OptimisticLockErrorFilter.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,KAAK,EAA0C,MAAM,cAAc,CAAC;AAC5E,OAAO,EAAC,mBAAmB,EAAC,MAAM,iBAAiB,CAAC;AAGpD,IAAa,yBAAyB,GAAtC,MAAa,yBAAyB;IAC7B,KAAK,CAAC,SAA8B,EAAE,GAAoB;QAC/D,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAC,GAAG,GAAG,CAAC;QAE/B,MAAM,CAAC,KAAK,CAAC;YACX,SAAS;SACV,CAAC,CAAC;QAEH,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,gFAAgF,CAAC,CAAC;IAC9G,CAAC;CACF,CAAA;AAVY,yBAAyB;IADrC,KAAK,CAAC,mBAAmB,CAAC;GACd,yBAAyB,CAUrC;SAVY,yBAAyB","sourcesContent":["import {Catch, ExceptionFilterMethods, PlatformContext} from \"@tsed/common\";\nimport {OptimisticLockError} from \"@mikro-orm/core\";\n\n@Catch(OptimisticLockError)\nexport class OptimisticLockErrorFilter implements ExceptionFilterMethods<OptimisticLockError> {\n public catch(exception: OptimisticLockError, ctx: PlatformContext): void {\n const {response, logger} = ctx;\n\n logger.error({\n exception\n });\n\n response.status(409).body(`Update refused. The resource has changed on the server. Please try again later`);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/filters/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC","sourcesContent":["export * from \"./OptimisticLockErrorFilter\";\n"]}
|