@tsed/mikro-orm 6.98.3 → 6.100.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.js +2 -2
- package/lib/cjs/MikroOrmModule.js.map +1 -1
- package/lib/cjs/decorators/entityManager.d.ts +6 -6
- package/lib/cjs/decorators/entityManager.js +5 -5
- package/lib/cjs/decorators/entityManager.js.map +1 -1
- package/lib/cjs/decorators/index.d.ts +1 -1
- package/lib/cjs/decorators/index.js +1 -1
- package/lib/cjs/decorators/index.js.map +1 -1
- package/lib/cjs/decorators/orm.d.ts +11 -0
- package/lib/cjs/decorators/orm.js +18 -0
- package/lib/cjs/decorators/orm.js.map +1 -0
- package/lib/cjs/decorators/transactional.d.ts +5 -1
- package/lib/cjs/decorators/transactional.js +5 -1
- package/lib/cjs/decorators/transactional.js.map +1 -1
- package/lib/cjs/interceptors/TransactionalInterceptor.d.ts +5 -1
- package/lib/cjs/interceptors/TransactionalInterceptor.js +28 -13
- package/lib/cjs/interceptors/TransactionalInterceptor.js.map +1 -1
- package/lib/cjs/services/DBContext.d.ts +4 -0
- package/lib/cjs/services/DBContext.js +9 -3
- package/lib/cjs/services/DBContext.js.map +1 -1
- package/lib/cjs/services/MikroOrmFactory.d.ts +1 -1
- package/lib/cjs/services/MikroOrmFactory.js +2 -2
- package/lib/cjs/services/MikroOrmFactory.js.map +1 -1
- package/lib/cjs/services/MikroOrmRegistry.d.ts +12 -4
- package/lib/cjs/services/MikroOrmRegistry.js +38 -27
- package/lib/cjs/services/MikroOrmRegistry.js.map +1 -1
- package/lib/esm/MikroOrmModule.js +2 -2
- package/lib/esm/MikroOrmModule.js.map +1 -1
- package/lib/esm/decorators/entityManager.js +5 -5
- package/lib/esm/decorators/entityManager.js.map +1 -1
- package/lib/esm/decorators/index.js +1 -1
- package/lib/esm/decorators/index.js.map +1 -1
- package/lib/esm/decorators/orm.js +14 -0
- package/lib/esm/decorators/orm.js.map +1 -0
- package/lib/esm/decorators/transactional.js +5 -1
- package/lib/esm/decorators/transactional.js.map +1 -1
- package/lib/esm/interceptors/TransactionalInterceptor.js +28 -13
- package/lib/esm/interceptors/TransactionalInterceptor.js.map +1 -1
- package/lib/esm/services/DBContext.js +9 -3
- package/lib/esm/services/DBContext.js.map +1 -1
- package/lib/esm/services/MikroOrmFactory.js +2 -2
- package/lib/esm/services/MikroOrmFactory.js.map +1 -1
- package/lib/esm/services/MikroOrmRegistry.js +38 -27
- package/lib/esm/services/MikroOrmRegistry.js.map +1 -1
- package/lib/types/decorators/entityManager.d.ts +6 -6
- package/lib/types/decorators/index.d.ts +1 -1
- package/lib/types/decorators/orm.d.ts +11 -0
- package/lib/types/decorators/transactional.d.ts +5 -1
- package/lib/types/interceptors/TransactionalInterceptor.d.ts +5 -1
- package/lib/types/services/DBContext.d.ts +4 -0
- package/lib/types/services/MikroOrmFactory.d.ts +1 -1
- package/lib/types/services/MikroOrmRegistry.d.ts +12 -4
- package/package.json +10 -10
- package/readme.md +20 -21
- package/lib/cjs/decorators/connection.d.ts +0 -1
- package/lib/cjs/decorators/connection.js +0 -8
- package/lib/cjs/decorators/connection.js.map +0 -1
- package/lib/esm/decorators/connection.js +0 -4
- package/lib/esm/decorators/connection.js.map +0 -1
- package/lib/types/decorators/connection.d.ts +0 -1
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { TransactionalInterceptor } from "../interceptors";
|
|
2
2
|
import { Intercept } from "@tsed/di";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Register a new request context for your method and execute it inside the context.
|
|
5
|
+
* @param {String | TransactionOptions} contextNameOrOptions
|
|
6
|
+
*/
|
|
7
|
+
export const Transactional = (contextNameOrOptions) => Intercept(TransactionalInterceptor, contextNameOrOptions);
|
|
4
8
|
//# sourceMappingURL=transactional.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transactional.js","sourceRoot":"","sources":["../../../src/decorators/transactional.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,wBAAwB,EAAqB,MAAM,iBAAiB,CAAC;AAC7E,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,
|
|
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;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,oBAAkD,EAAmB,EAAE,CACnG,SAAS,CAAC,wBAAwB,EAAE,oBAAoB,CAAC,CAAC","sourcesContent":["import {TransactionalInterceptor, TransactionOptions} from \"../interceptors\";\nimport {Intercept} from \"@tsed/di\";\n\n/**\n * Register a new request context for your method and execute it inside the context.\n * @param {String | TransactionOptions} contextNameOrOptions\n */\nexport const Transactional = (contextNameOrOptions?: string | TransactionOptions): MethodDecorator =>\n Intercept(TransactionalInterceptor, contextNameOrOptions);\n"]}
|
|
@@ -14,11 +14,11 @@ let TransactionalInterceptor = class TransactionalInterceptor {
|
|
|
14
14
|
if (options.retry && !this.retryStrategy) {
|
|
15
15
|
this.logger.warn(`To retry a transaction you have to implement a "${RetryStrategy.description}" interface`);
|
|
16
16
|
}
|
|
17
|
-
const ctx = this.context.
|
|
17
|
+
const ctx = this.context.entries();
|
|
18
18
|
if (!ctx) {
|
|
19
19
|
return this.runWithinNewCtx(next, options);
|
|
20
20
|
}
|
|
21
|
-
if (ctx.has(options.
|
|
21
|
+
if (ctx.has(options.contextName)) {
|
|
22
22
|
return next();
|
|
23
23
|
}
|
|
24
24
|
return this.runUnderCtx(next, ctx, options);
|
|
@@ -28,33 +28,48 @@ let TransactionalInterceptor = class TransactionalInterceptor {
|
|
|
28
28
|
return this.runUnderCtx(next, ctx, options);
|
|
29
29
|
}
|
|
30
30
|
runUnderCtx(next, ctx, options) {
|
|
31
|
-
const orm = this.registry.get(options.
|
|
32
|
-
|
|
31
|
+
const orm = this.registry.get(options.contextName);
|
|
32
|
+
if (!orm) {
|
|
33
|
+
throw new Error(`No such context: ${options.contextName}. Please register a corresponding MikroOrm instance using '@Configuration()' decorator.`);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The fork method signature has been changed since v5.x,
|
|
37
|
+
* which might lead to unexpected behavior while using the @Transactional() decorator.
|
|
38
|
+
*
|
|
39
|
+
* ```diff
|
|
40
|
+
* - fork(clear = true, useContext = false): D[typeof EntityManagerType]
|
|
41
|
+
* + fork(options: ForkOptions = {}): D[typeof EntityManagerType] {
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* To ensure backward compatibility with v4.x and add support for v5.x, provided the following workaround:
|
|
45
|
+
*/
|
|
46
|
+
const em = orm.em.fork({ clear: true, useContext: true }, true);
|
|
33
47
|
ctx.set(em.name, em);
|
|
34
|
-
const runInTransaction = () => this.executeInTransaction(options.
|
|
48
|
+
const runInTransaction = () => this.executeInTransaction(options.contextName, next);
|
|
35
49
|
return this.context.run(ctx, () => { var _a; return (options.retry ? (_a = this.retryStrategy) === null || _a === void 0 ? void 0 : _a.acquire(runInTransaction) : runInTransaction()); });
|
|
36
50
|
}
|
|
37
51
|
extractContextName(context) {
|
|
52
|
+
var _a;
|
|
38
53
|
const options = context.options || {};
|
|
39
|
-
let
|
|
54
|
+
let contextName;
|
|
40
55
|
let retry;
|
|
41
56
|
if (typeof options === "string") {
|
|
42
|
-
|
|
57
|
+
contextName = options;
|
|
43
58
|
}
|
|
44
59
|
else if (options) {
|
|
45
|
-
|
|
60
|
+
contextName = (_a = options.contextName) !== null && _a !== void 0 ? _a : options.connectionName;
|
|
46
61
|
retry = options.retry;
|
|
47
62
|
}
|
|
48
|
-
if (!
|
|
49
|
-
|
|
63
|
+
if (!contextName) {
|
|
64
|
+
contextName = "default";
|
|
50
65
|
}
|
|
51
66
|
if (!retry) {
|
|
52
67
|
retry = false;
|
|
53
68
|
}
|
|
54
|
-
return {
|
|
69
|
+
return { contextName, retry };
|
|
55
70
|
}
|
|
56
|
-
async executeInTransaction(
|
|
57
|
-
const em = this.context.get(
|
|
71
|
+
async executeInTransaction(contextName, next) {
|
|
72
|
+
const em = this.context.get(contextName);
|
|
58
73
|
const result = await next();
|
|
59
74
|
await (em === null || em === void 0 ? void 0 : em.flush());
|
|
60
75
|
return result;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,gBAAgB,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AACvE,OAAO,EAAC,MAAM,EAAE,WAAW,EAA0D,MAAM,UAAU,CAAC;AAEtG,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"TransactionalInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/TransactionalInterceptor.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,gBAAgB,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AACvE,OAAO,EAAC,MAAM,EAAE,WAAW,EAA0D,MAAM,UAAU,CAAC;AAEtG,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AAYpC,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IACnC,YAC6B,QAA0B,EAC1B,OAAkB,EAClB,MAAc,EAExB,aAA6B;QAJnB,aAAQ,GAAR,QAAQ,CAAkB;QAC1B,YAAO,GAAP,OAAO,CAAW;QAClB,WAAM,GAAN,MAAM,CAAQ;QAExB,kBAAa,GAAb,aAAa,CAAgB;IAC7C,CAAC;IAEG,KAAK,CAAC,SAAS,CAAC,OAAoC,EAAE,IAAqB;QAChF,MAAM,OAAO,GAAG,IAAI,CAA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ApGY,wBAAwB;IADpC,WAAW,EAAE;IAGT,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,EAAE,CAAA;IACR,WAAA,MAAM,CAAC,aAAa,CAAC,CAAA;qCAHe,gBAAgB;QACjB,SAAS;QACV,MAAM;GAJhC,wBAAwB,CAoGpC;SApGY,wBAAwB","sourcesContent":["import {DBContext, MikroOrmRegistry, RetryStrategy} from \"../services\";\nimport {Inject, Interceptor, InterceptorContext, InterceptorMethods, InterceptorNext} from \"@tsed/di\";\nimport {EntityManager} from \"@mikro-orm/core\";\nimport {Logger} from \"@tsed/logger\";\n\nexport interface TransactionOptions {\n retry?: boolean;\n contextName?: string;\n /**\n * @deprecated Since 2022-02-01. Use {@link contextName} instead\n */\n connectionName?: string;\n}\n\n@Interceptor()\nexport class TransactionalInterceptor implements InterceptorMethods {\n constructor(\n @Inject() private readonly registry: MikroOrmRegistry,\n @Inject() private readonly context: DBContext,\n @Inject() private readonly logger: Logger,\n @Inject(RetryStrategy)\n private readonly retryStrategy?: RetryStrategy\n ) {}\n\n public async intercept(context: InterceptorContext<unknown>, next: InterceptorNext): Promise<unknown> {\n const options = this.extractContextName(context);\n\n if (options.retry && !this.retryStrategy) {\n this.logger.warn(`To retry a transaction you have to implement a \"${RetryStrategy.description}\" interface`);\n }\n\n const ctx = this.context.entries();\n\n if (!ctx) {\n return this.runWithinNewCtx(next, options);\n }\n\n if (ctx.has(options.contextName!)) {\n return next();\n }\n\n return this.runUnderCtx(next, ctx, options);\n }\n\n private runWithinNewCtx(next: InterceptorNext, options: TransactionOptions): Promise<unknown> | unknown {\n const ctx = new Map<string, EntityManager>();\n\n return this.runUnderCtx(next, ctx, options);\n }\n\n private runUnderCtx(next: InterceptorNext, ctx: Map<string, EntityManager>, options: TransactionOptions): Promise<unknown> | unknown {\n const orm = this.registry.get(options.contextName);\n\n if (!orm) {\n throw new Error(\n `No such context: ${options.contextName}. Please register a corresponding MikroOrm instance using '@Configuration()' decorator.`\n );\n }\n\n /**\n * The fork method signature has been changed since v5.x,\n * which might lead to unexpected behavior while using the @Transactional() decorator.\n *\n * ```diff\n * - fork(clear = true, useContext = false): D[typeof EntityManagerType]\n * + fork(options: ForkOptions = {}): D[typeof EntityManagerType] {\n * ```\n *\n * To ensure backward compatibility with v4.x and add support for v5.x, provided the following workaround:\n */\n const em = (orm.em.fork as (\n clearOrForkOptions?: boolean | {clear?: boolean; useContext?: boolean},\n useContext?: boolean\n ) => EntityManager)({clear: true, useContext: true}, true);\n\n ctx.set(em.name, em);\n\n const runInTransaction = () => this.executeInTransaction(options.contextName!, next);\n\n return this.context.run(ctx, () => (options.retry ? this.retryStrategy?.acquire(runInTransaction) : runInTransaction()));\n }\n\n private extractContextName(context: InterceptorContext<unknown>): TransactionOptions {\n const options = context.options || ({} as Partial<TransactionOptions> | string);\n\n let contextName: string | undefined;\n let retry: boolean | undefined;\n\n if (typeof options === \"string\") {\n contextName = options;\n } else if (options) {\n contextName = options.contextName ?? options.connectionName;\n retry = options.retry;\n }\n\n if (!contextName) {\n contextName = \"default\";\n }\n\n if (!retry) {\n retry = false;\n }\n\n return {contextName, retry};\n }\n\n private async executeInTransaction(contextName: string, next: InterceptorNext): Promise<unknown> {\n const em = this.context.get(contextName);\n\n const result = await next();\n\n await em?.flush();\n\n return result;\n }\n}\n"]}
|
|
@@ -14,15 +14,21 @@ let DBContext = class DBContext {
|
|
|
14
14
|
run(ctx, callback) {
|
|
15
15
|
return this.storage.run(ctx, callback);
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
entries() {
|
|
18
18
|
return this.storage.getStore();
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated Since 2022-02-01. Use {@link entries} instead
|
|
22
|
+
*/
|
|
23
|
+
getContext() {
|
|
24
|
+
return this.entries();
|
|
25
|
+
}
|
|
20
26
|
get(contextName) {
|
|
21
|
-
const context = this.
|
|
27
|
+
const context = this.entries();
|
|
22
28
|
return context === null || context === void 0 ? void 0 : context.get(contextName);
|
|
23
29
|
}
|
|
24
30
|
has(contextName) {
|
|
25
|
-
const context = this.
|
|
31
|
+
const context = this.entries();
|
|
26
32
|
return !!(context === null || context === void 0 ? void 0 : context.has(contextName));
|
|
27
33
|
}
|
|
28
34
|
};
|
|
@@ -1 +1 @@
|
|
|
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;IAGpB;QAFiB,YAAO,GAAG,IAAI,iBAAiB,EAA8B,CAAC;QAG7E,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,
|
|
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;IAGpB;QAFiB,YAAO,GAAG,IAAI,iBAAiB,EAA8B,CAAC;QAG7E,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,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACnC,CAAC;IAEM,GAAG,CAAC,WAAmB;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAE/B,OAAO,CAAC,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,CAAC,WAAW,CAAC,CAAA,CAAC;IACrC,CAAC;CACF,CAAA;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"]}
|
|
@@ -3,9 +3,9 @@ import { DBContext } from "./DBContext";
|
|
|
3
3
|
import { MikroORM } from "@mikro-orm/core";
|
|
4
4
|
import { Inject, Injectable } from "@tsed/di";
|
|
5
5
|
let MikroOrmFactory = class MikroOrmFactory {
|
|
6
|
-
create(
|
|
6
|
+
create(options) {
|
|
7
7
|
return MikroORM.init({
|
|
8
|
-
...
|
|
8
|
+
...options,
|
|
9
9
|
context: (name) => this.dbContext.get(name)
|
|
10
10
|
});
|
|
11
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,EAAC,QAAQ,EAAU,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAG5C,IAAa,eAAe,GAA5B,MAAa,eAAe;IAInB,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"MikroOrmFactory.js","sourceRoot":"","sources":["../../../src/services/MikroOrmFactory.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,EAAC,QAAQ,EAAU,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAG5C,IAAa,eAAe,GAA5B,MAAa,eAAe;IAInB,MAAM,CAAC,OAAgB;QAC5B,OAAO,QAAQ,CAAC,IAAI,CAAC;YACnB,GAAG,OAAO;YACV,OAAO,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC;SACpD,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AARC;IADC,MAAM,EAAE;8BACoB,SAAS;kDAAC;AAF5B,eAAe;IAD3B,UAAU,EAAE;GACA,eAAe,CAU3B;SAVY,eAAe","sourcesContent":["import {DBContext} from \"./DBContext\";\nimport {MikroORM, Options} from \"@mikro-orm/core\";\nimport {Inject, Injectable} from \"@tsed/di\";\n\n@Injectable()\nexport class MikroOrmFactory {\n @Inject()\n private readonly dbContext!: DBContext;\n\n public create(options: Options): Promise<MikroORM> {\n return MikroORM.init({\n ...options,\n context: (name: string) => this.dbContext.get(name)\n });\n }\n}\n"]}
|
|
@@ -7,35 +7,46 @@ let MikroOrmRegistry = class MikroOrmRegistry {
|
|
|
7
7
|
constructor(logger, mikroOrmFactory) {
|
|
8
8
|
this.logger = logger;
|
|
9
9
|
this.mikroOrmFactory = mikroOrmFactory;
|
|
10
|
-
this.
|
|
10
|
+
this.instances = new Map();
|
|
11
11
|
}
|
|
12
|
-
async
|
|
13
|
-
const
|
|
14
|
-
if (this.has(
|
|
15
|
-
return this.get(
|
|
12
|
+
async register(options) {
|
|
13
|
+
const contextName = getValue(options, "contextName", "default");
|
|
14
|
+
if (this.has(contextName)) {
|
|
15
|
+
return this.get(contextName);
|
|
16
16
|
}
|
|
17
|
-
this.logger.info(`Create connection with MikroOrm to database: %s`,
|
|
18
|
-
this.logger.debug(`options: %j`,
|
|
19
|
-
const
|
|
20
|
-
this.
|
|
21
|
-
this.logger.info(`Connected with MikroOrm to database: %s`,
|
|
22
|
-
return
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
}
|
|
36
|
-
async
|
|
37
|
-
|
|
38
|
-
|
|
17
|
+
this.logger.info(`Create connection with MikroOrm to database: %s`, contextName);
|
|
18
|
+
this.logger.debug(`options: %j`, options);
|
|
19
|
+
const instance = await this.mikroOrmFactory.create(options);
|
|
20
|
+
this.instances.set(contextName, instance);
|
|
21
|
+
this.logger.info(`Connected with MikroOrm to database: %s`, contextName);
|
|
22
|
+
return instance;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated Since 2022-02-01. Use {@link register} instead
|
|
26
|
+
*/
|
|
27
|
+
createConnection(options) {
|
|
28
|
+
return this.register(options);
|
|
29
|
+
}
|
|
30
|
+
get(contextName = "default") {
|
|
31
|
+
return this.instances.get(contextName);
|
|
32
|
+
}
|
|
33
|
+
has(contextName = "default") {
|
|
34
|
+
return this.instances.has(contextName);
|
|
35
|
+
}
|
|
36
|
+
async clear() {
|
|
37
|
+
const instances = [...this.instances.values()];
|
|
38
|
+
await Promise.all(instances.map((instance) => catchAsyncError(() => this.dispose(instance))));
|
|
39
|
+
this.instances.clear();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @deprecated Since 2022-02-01. Use {@link clear} instead
|
|
43
|
+
*/
|
|
44
|
+
closeConnections() {
|
|
45
|
+
return this.clear();
|
|
46
|
+
}
|
|
47
|
+
async dispose(instance) {
|
|
48
|
+
if (await instance.isConnected()) {
|
|
49
|
+
await instance.close(false);
|
|
39
50
|
}
|
|
40
51
|
}
|
|
41
52
|
};
|
|
@@ -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,MAAM,EAAC,MAAM,cAAc,CAAC;AAGpC,IAAa,gBAAgB,GAA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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 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\", \"default\");\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 = \"default\"): MikroORM | undefined {\n return this.instances.get(contextName);\n }\n\n public has(contextName: string = \"default\"): boolean {\n return this.instances.has(contextName);\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,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Get the entity manager for the given
|
|
3
|
-
* @param
|
|
2
|
+
* Get the entity manager for the given context name.
|
|
3
|
+
* @param {String} contextName
|
|
4
4
|
* @decorator
|
|
5
5
|
* @mikroOrm
|
|
6
6
|
*/
|
|
7
|
-
export declare const EntityManager: (
|
|
7
|
+
export declare const EntityManager: (contextName?: string | undefined) => PropertyDecorator;
|
|
8
8
|
/**
|
|
9
|
-
* Get the entity manager for the given
|
|
10
|
-
* @param connectionName
|
|
9
|
+
* Get the entity manager for the given context name.
|
|
10
|
+
* @param {String} connectionName
|
|
11
11
|
* @decorator
|
|
12
12
|
* @mikroOrm
|
|
13
13
|
*/
|
|
14
|
-
export declare const Em: (
|
|
14
|
+
export declare const Em: (contextName?: string | undefined) => PropertyDecorator;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the ORM for the given context name.
|
|
3
|
+
* @param {String} contextName
|
|
4
|
+
*/
|
|
5
|
+
export declare const Orm: (contextName?: string | undefined) => PropertyDecorator;
|
|
6
|
+
/**
|
|
7
|
+
* Get the ORM for the given context name.
|
|
8
|
+
* @param {String} contextName
|
|
9
|
+
* @deprecated Since 2022-02-01. Use {@link Orm} instead
|
|
10
|
+
*/
|
|
11
|
+
export declare const Connection: (contextName?: string | undefined) => PropertyDecorator;
|
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import { TransactionOptions } from "../interceptors";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Register a new request context for your method and execute it inside the context.
|
|
4
|
+
* @param {String | TransactionOptions} contextNameOrOptions
|
|
5
|
+
*/
|
|
6
|
+
export declare const Transactional: (contextNameOrOptions?: string | TransactionOptions | undefined) => MethodDecorator;
|
|
@@ -2,8 +2,12 @@ import { DBContext, MikroOrmRegistry, RetryStrategy } from "../services";
|
|
|
2
2
|
import { InterceptorContext, InterceptorMethods, InterceptorNext } from "@tsed/di";
|
|
3
3
|
import { Logger } from "@tsed/logger";
|
|
4
4
|
export interface TransactionOptions {
|
|
5
|
-
connectionName?: string;
|
|
6
5
|
retry?: boolean;
|
|
6
|
+
contextName?: string;
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated Since 2022-02-01. Use {@link contextName} instead
|
|
9
|
+
*/
|
|
10
|
+
connectionName?: string;
|
|
7
11
|
}
|
|
8
12
|
export declare class TransactionalInterceptor implements InterceptorMethods {
|
|
9
13
|
private readonly registry;
|
|
@@ -3,6 +3,10 @@ export declare class DBContext {
|
|
|
3
3
|
private readonly storage;
|
|
4
4
|
constructor();
|
|
5
5
|
run<R>(ctx: Map<string, EntityManager>, callback: (...args: any[]) => R): R;
|
|
6
|
+
entries(): Map<string, EntityManager> | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated Since 2022-02-01. Use {@link entries} instead
|
|
9
|
+
*/
|
|
6
10
|
getContext(): Map<string, EntityManager> | undefined;
|
|
7
11
|
get(contextName: string): EntityManager | undefined;
|
|
8
12
|
has(contextName: string): boolean;
|
|
@@ -4,11 +4,19 @@ import { Logger } from "@tsed/logger";
|
|
|
4
4
|
export declare class MikroOrmRegistry {
|
|
5
5
|
private readonly logger;
|
|
6
6
|
private readonly mikroOrmFactory;
|
|
7
|
-
private readonly
|
|
7
|
+
private readonly instances;
|
|
8
8
|
constructor(logger: Logger, mikroOrmFactory: MikroOrmFactory);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
register<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated Since 2022-02-01. Use {@link register} instead
|
|
12
|
+
*/
|
|
13
|
+
createConnection<T extends DatabaseDriver>(options: Options<T>): Promise<MikroORM>;
|
|
14
|
+
get(contextName?: string): MikroORM | undefined;
|
|
15
|
+
has(contextName?: string): boolean;
|
|
16
|
+
clear(): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Since 2022-02-01. Use {@link clear} instead
|
|
19
|
+
*/
|
|
12
20
|
closeConnections(): Promise<void>;
|
|
13
21
|
private dispose;
|
|
14
22
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/mikro-orm",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.100.0",
|
|
4
4
|
"description": "MikroORM package for Ts.ED framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -24,20 +24,20 @@
|
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@mikro-orm/core": "^4.5.9",
|
|
26
26
|
"@mikro-orm/mongodb": "^4.5.9",
|
|
27
|
-
"@tsed/common": "6.
|
|
28
|
-
"@tsed/core": "6.
|
|
29
|
-
"@tsed/di": "6.
|
|
30
|
-
"@tsed/json-mapper": "6.
|
|
27
|
+
"@tsed/common": "6.100.0",
|
|
28
|
+
"@tsed/core": "6.100.0",
|
|
29
|
+
"@tsed/di": "6.100.0",
|
|
30
|
+
"@tsed/json-mapper": "6.100.0",
|
|
31
31
|
"@tsed/logger": "^6.0.0",
|
|
32
|
-
"@tsed/schema": "6.
|
|
33
|
-
"@tsed/testing-mongoose": "6.
|
|
32
|
+
"@tsed/schema": "6.100.0",
|
|
33
|
+
"@tsed/testing-mongoose": "6.100.0",
|
|
34
34
|
"ts-mockito": "^2.6.1"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@mikro-orm/core": ">=4.5.9",
|
|
38
|
-
"@tsed/common": "^6.
|
|
39
|
-
"@tsed/core": "^6.
|
|
40
|
-
"@tsed/di": "^6.
|
|
38
|
+
"@tsed/common": "^6.100.0",
|
|
39
|
+
"@tsed/core": "^6.100.0",
|
|
40
|
+
"@tsed/di": "^6.100.0",
|
|
41
41
|
"@tsed/logger": "^6.0.0"
|
|
42
42
|
},
|
|
43
43
|
"repository": "https://github.com/tsedio/tsed",
|
package/readme.md
CHANGED
|
@@ -33,7 +33,7 @@ A package of Ts.ED framework. See website: https://tsed.io/tutorials/mikro-orm
|
|
|
33
33
|
|
|
34
34
|
Currently, `@tsed/mikro-orm` allows you:
|
|
35
35
|
|
|
36
|
-
- Configure one or more MikroOrm
|
|
36
|
+
- Configure one or more MikroOrm instances via the `@Configuration` decorator. All databases will be initialized
|
|
37
37
|
when the server starts during the server's `OnInit` phase.
|
|
38
38
|
- Use the Entity MikroOrm as Model for Controllers, AJV Validation and Swagger.
|
|
39
39
|
|
|
@@ -96,32 +96,32 @@ export class Server {}
|
|
|
96
96
|
|
|
97
97
|
The `mikroOrm` options accepts the same configuration object as `init()` from the MikroORM package. Check [this page](https://mikro-orm.io/docs/configuration) for the complete configuration documentation.
|
|
98
98
|
|
|
99
|
-
## Obtain
|
|
99
|
+
## Obtain ORM instance
|
|
100
100
|
|
|
101
|
-
`@
|
|
101
|
+
`@Orm` decorator lets you retrieve an instance of MikroOrm.
|
|
102
102
|
|
|
103
103
|
```typescript
|
|
104
104
|
import {Injectable, AfterRoutesInit} from "@tsed/common";
|
|
105
|
-
import {
|
|
105
|
+
import {Orm} from "@tsed/mikro-orm";
|
|
106
106
|
import {MikroORM} from "@mikro-orm/core";
|
|
107
107
|
|
|
108
108
|
@Injectable()
|
|
109
109
|
export class UsersService {
|
|
110
|
-
@
|
|
111
|
-
private readonly
|
|
110
|
+
@Orm()
|
|
111
|
+
private readonly orm!: MikroORM;
|
|
112
112
|
|
|
113
113
|
async create(user: User): Promise<User> {
|
|
114
114
|
// do something
|
|
115
115
|
// ...
|
|
116
116
|
// Then save
|
|
117
|
-
await this.
|
|
117
|
+
await this.orm.em.persistAndFlush(user);
|
|
118
118
|
console.log("Saved a new user with id: " + user.id);
|
|
119
119
|
|
|
120
120
|
return user;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
async find(): Promise<User[]> {
|
|
124
|
-
const users = await this.
|
|
124
|
+
const users = await this.orm.em.find(User, {});
|
|
125
125
|
console.log("Loaded users: ", users);
|
|
126
126
|
|
|
127
127
|
return users;
|
|
@@ -129,15 +129,15 @@ export class UsersService {
|
|
|
129
129
|
}
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
-
It's also possible to inject
|
|
132
|
+
It's also possible to inject an ORM by its context name:
|
|
133
133
|
|
|
134
134
|
```ts
|
|
135
135
|
import {Injectable} from "@tsed/di";
|
|
136
136
|
|
|
137
137
|
@Injectable()
|
|
138
138
|
export class MyService {
|
|
139
|
-
@
|
|
140
|
-
private readonly
|
|
139
|
+
@Orm("mongo")
|
|
140
|
+
private readonly orm!: MikroORM;
|
|
141
141
|
}
|
|
142
142
|
```
|
|
143
143
|
|
|
@@ -164,7 +164,7 @@ export class UsersService {
|
|
|
164
164
|
}
|
|
165
165
|
```
|
|
166
166
|
|
|
167
|
-
It's also possible to inject Entity manager by his
|
|
167
|
+
It's also possible to inject Entity manager by his context name:
|
|
168
168
|
|
|
169
169
|
```typescript
|
|
170
170
|
import {Injectable, AfterRoutesInit} from "@tsed/common";
|
|
@@ -173,7 +173,7 @@ import {EntityManager} from '@mikro-orm/mysql'; // Import EntityManager from you
|
|
|
173
173
|
|
|
174
174
|
@Injectable()
|
|
175
175
|
export class UsersService {
|
|
176
|
-
@Em("
|
|
176
|
+
@Em("contextName")
|
|
177
177
|
private readonly em!: EntityManager;
|
|
178
178
|
|
|
179
179
|
async create(user: User): Promise<User> {
|
|
@@ -195,25 +195,24 @@ import {Entity, Property, PrimaryKey, Property as Column} from "@mikro-orm/core"
|
|
|
195
195
|
|
|
196
196
|
@Entity()
|
|
197
197
|
export class User {
|
|
198
|
-
|
|
199
198
|
@PrimaryKey()
|
|
200
199
|
@Property()
|
|
201
|
-
id
|
|
200
|
+
id!: number;
|
|
202
201
|
|
|
203
202
|
@Column()
|
|
204
203
|
@MaxLength(100)
|
|
205
204
|
@Required()
|
|
206
|
-
firstName
|
|
205
|
+
firstName!: string;
|
|
207
206
|
|
|
208
207
|
@Column()
|
|
209
208
|
@MaxLength(100)
|
|
210
209
|
@Required()
|
|
211
|
-
lastName
|
|
210
|
+
lastName!: string;
|
|
212
211
|
|
|
213
212
|
@Column()
|
|
214
213
|
@Mininum(0)
|
|
215
214
|
@Maximum(100)
|
|
216
|
-
age
|
|
215
|
+
age!: number;
|
|
217
216
|
}
|
|
218
217
|
```
|
|
219
218
|
|
|
@@ -229,7 +228,7 @@ import {Controller, Post, BodyParams, Inject, Post, Get} from "@tsed/common";
|
|
|
229
228
|
@Controller("/users")
|
|
230
229
|
export class UsersCtrl {
|
|
231
230
|
@Inject()
|
|
232
|
-
private usersService
|
|
231
|
+
private readonly usersService!: UsersService;
|
|
233
232
|
|
|
234
233
|
@Post("/")
|
|
235
234
|
create(@BodyParams() user: User): Promise<User> {
|
|
@@ -258,7 +257,7 @@ import {Transactional} from "@tsed/mikro-orm";
|
|
|
258
257
|
@Controller("/users")
|
|
259
258
|
export class UsersCtrl {
|
|
260
259
|
@Inject()
|
|
261
|
-
private usersService
|
|
260
|
+
private readonly usersService!: UsersService;
|
|
262
261
|
|
|
263
262
|
@Post("/")
|
|
264
263
|
@Transactional()
|
|
@@ -345,7 +344,7 @@ import {Transactional} from "@tsed/mikro-orm";
|
|
|
345
344
|
@Controller("/users")
|
|
346
345
|
export class UsersCtrl {
|
|
347
346
|
@Inject()
|
|
348
|
-
private usersService
|
|
347
|
+
private readonly usersService!: UsersService;
|
|
349
348
|
|
|
350
349
|
@Post("/")
|
|
351
350
|
@Transactional({retry: true})
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const Connection: (connectionName?: string | undefined) => PropertyDecorator;
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const Connection: (connectionName?: string | undefined) => PropertyDecorator;
|