@mikro-orm/core 6.3.14-dev.30 → 6.3.14-dev.32
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/decorators/CreateRequestContext.d.ts +3 -9
- package/decorators/CreateRequestContext.js +16 -43
- package/decorators/Transactional.d.ts +13 -0
- package/decorators/Transactional.js +31 -0
- package/decorators/index.d.ts +1 -0
- package/decorators/index.js +1 -0
- package/index.mjs +1 -0
- package/package.json +2 -2
- package/typings.d.ts +7 -0
- package/utils/resolveContextProvider.d.ts +10 -0
- package/utils/resolveContextProvider.js +31 -0
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export declare function CreateRequestContext<T>(getContext?: MikroORM | Promise<MikroORM> | ((type: T) => MikroORM | Promise<MikroORM> | EntityManager | EntityRepository<any> | {
|
|
5
|
-
getEntityManager(): EntityManager;
|
|
6
|
-
}), respectExistingContext?: boolean): MethodDecorator;
|
|
7
|
-
export declare function EnsureRequestContext<T>(getContext?: MikroORM | Promise<MikroORM> | ((type: T) => MikroORM | Promise<MikroORM> | EntityManager | EntityRepository<any> | {
|
|
8
|
-
getEntityManager(): EntityManager;
|
|
9
|
-
})): MethodDecorator;
|
|
1
|
+
import type { ContextProvider } from '../typings';
|
|
2
|
+
export declare function CreateRequestContext<T extends object>(context?: ContextProvider<T>, respectExistingContext?: boolean): MethodDecorator;
|
|
3
|
+
export declare function EnsureRequestContext<T extends object>(context?: ContextProvider<T>): MethodDecorator;
|
|
@@ -2,59 +2,32 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CreateRequestContext = CreateRequestContext;
|
|
4
4
|
exports.EnsureRequestContext = EnsureRequestContext;
|
|
5
|
-
const MikroORM_1 = require("../MikroORM");
|
|
6
5
|
const RequestContext_1 = require("../utils/RequestContext");
|
|
7
|
-
const
|
|
8
|
-
const EntityRepository_1 = require("../entity/EntityRepository");
|
|
6
|
+
const resolveContextProvider_1 = require("../utils/resolveContextProvider");
|
|
9
7
|
const TransactionContext_1 = require("../utils/TransactionContext");
|
|
10
|
-
function CreateRequestContext(
|
|
8
|
+
function CreateRequestContext(context, respectExistingContext = false) {
|
|
11
9
|
return function (target, propertyKey, descriptor) {
|
|
12
10
|
const originalMethod = descriptor.value;
|
|
13
11
|
descriptor.value = async function (...args) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const provider = txContext ? TransactionContext_1.TransactionContext : RequestContext_1.RequestContext;
|
|
17
|
-
let orm;
|
|
18
|
-
let em;
|
|
19
|
-
if (typeof getContext === 'function') {
|
|
20
|
-
const context = await getContext(this);
|
|
21
|
-
orm = context ?? await this.orm;
|
|
22
|
-
em = context ?? await this.em;
|
|
23
|
-
}
|
|
24
|
-
else if (getContext) {
|
|
25
|
-
orm = em = await getContext;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
orm = await this.orm;
|
|
29
|
-
em = await this.em;
|
|
30
|
-
}
|
|
31
|
-
const create = async (em) => {
|
|
32
|
-
// reuse existing context if available for given respect `contextName`
|
|
33
|
-
if (respectExistingContext && RequestContext_1.RequestContext.getEntityManager(em.name)) {
|
|
34
|
-
return originalMethod.apply(this, args);
|
|
35
|
-
}
|
|
36
|
-
if (txContext) {
|
|
37
|
-
em = em.fork({ useContext: true });
|
|
38
|
-
}
|
|
39
|
-
return await provider.create(em, () => {
|
|
40
|
-
return originalMethod.apply(this, args);
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
if (em instanceof EntityManager_1.EntityManager) {
|
|
44
|
-
return await create(em);
|
|
45
|
-
}
|
|
46
|
-
if (orm instanceof EntityRepository_1.EntityRepository) {
|
|
47
|
-
return await create(orm.getEntityManager());
|
|
48
|
-
}
|
|
49
|
-
if (!(orm instanceof MikroORM_1.MikroORM)) {
|
|
12
|
+
const em = await (0, resolveContextProvider_1.resolveContextProvider)(this, context);
|
|
13
|
+
if (!em) {
|
|
50
14
|
const name = respectExistingContext ? 'EnsureRequestContext' : 'CreateRequestContext';
|
|
51
15
|
throw new Error(`@${name}() decorator can only be applied to methods of classes with \`orm: MikroORM\` property, \`em: EntityManager\` property, or with a callback parameter like \`@${name}(() => orm)\` that returns one of those types. The parameter will contain a reference to current \`this\`. Returning an EntityRepository from it is also supported.`);
|
|
52
16
|
}
|
|
53
|
-
|
|
17
|
+
// reuse existing context if available for given respect `contextName`
|
|
18
|
+
if (respectExistingContext && RequestContext_1.RequestContext.getEntityManager(em.name)) {
|
|
19
|
+
return originalMethod.apply(this, args);
|
|
20
|
+
}
|
|
21
|
+
// Otherwise, the outer tx context would be preferred.
|
|
22
|
+
const txContext = TransactionContext_1.TransactionContext.currentTransactionContext();
|
|
23
|
+
const provider = txContext ? TransactionContext_1.TransactionContext : RequestContext_1.RequestContext;
|
|
24
|
+
return txContext
|
|
25
|
+
? provider.create(em.fork({ useContext: true }), () => originalMethod.apply(this, args))
|
|
26
|
+
: provider.create(em, () => originalMethod.apply(this, args));
|
|
54
27
|
};
|
|
55
28
|
return descriptor;
|
|
56
29
|
};
|
|
57
30
|
}
|
|
58
|
-
function EnsureRequestContext(
|
|
59
|
-
return CreateRequestContext(
|
|
31
|
+
function EnsureRequestContext(context) {
|
|
32
|
+
return CreateRequestContext(context, true);
|
|
60
33
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { TransactionOptions } from '../enums';
|
|
2
|
+
import type { ContextProvider } from '../typings';
|
|
3
|
+
type TransactionalOptions<T> = TransactionOptions & {
|
|
4
|
+
context?: ContextProvider<T>;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
|
|
8
|
+
* The difference is that you can specify the context in which the transaction begins by providing `context` option,
|
|
9
|
+
* and if omitted, the transaction will begin in the current context implicitly.
|
|
10
|
+
* It works on async functions and can be nested with `em.transactional()`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function Transactional<T extends object>(options?: TransactionalOptions<T>): MethodDecorator;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Transactional = Transactional;
|
|
4
|
+
const RequestContext_1 = require("../utils/RequestContext");
|
|
5
|
+
const resolveContextProvider_1 = require("../utils/resolveContextProvider");
|
|
6
|
+
const TransactionContext_1 = require("../utils/TransactionContext");
|
|
7
|
+
/**
|
|
8
|
+
* This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
|
|
9
|
+
* The difference is that you can specify the context in which the transaction begins by providing `context` option,
|
|
10
|
+
* and if omitted, the transaction will begin in the current context implicitly.
|
|
11
|
+
* It works on async functions and can be nested with `em.transactional()`.
|
|
12
|
+
*/
|
|
13
|
+
function Transactional(options = {}) {
|
|
14
|
+
return function (target, propertyKey, descriptor) {
|
|
15
|
+
const originalMethod = descriptor.value;
|
|
16
|
+
if (originalMethod.constructor.name !== 'AsyncFunction') {
|
|
17
|
+
throw new Error('@Transactional() should be use with async functions');
|
|
18
|
+
}
|
|
19
|
+
descriptor.value = async function (...args) {
|
|
20
|
+
const { context, ...txOptions } = options;
|
|
21
|
+
const em = await (0, resolveContextProvider_1.resolveContextProvider)(this, context)
|
|
22
|
+
|| TransactionContext_1.TransactionContext.getEntityManager()
|
|
23
|
+
|| RequestContext_1.RequestContext.getEntityManager();
|
|
24
|
+
if (!em) {
|
|
25
|
+
throw new Error(`@Transactional() decorator can only be applied to methods of classes with \`orm: MikroORM\` property, \`em: EntityManager\` property, or with a callback parameter like \`@Transactional(() => orm)\` that returns one of those types. The parameter will contain a reference to current \`this\`. Returning an EntityRepository from it is also supported.`);
|
|
26
|
+
}
|
|
27
|
+
return em.transactional(() => originalMethod.apply(this, args), txOptions);
|
|
28
|
+
};
|
|
29
|
+
return descriptor;
|
|
30
|
+
};
|
|
31
|
+
}
|
package/decorators/index.d.ts
CHANGED
package/decorators/index.js
CHANGED
package/index.mjs
CHANGED
|
@@ -162,6 +162,7 @@ export const TimeType = mod.TimeType;
|
|
|
162
162
|
export const TinyIntType = mod.TinyIntType;
|
|
163
163
|
export const TransactionContext = mod.TransactionContext;
|
|
164
164
|
export const TransactionEventBroadcaster = mod.TransactionEventBroadcaster;
|
|
165
|
+
export const Transactional = mod.Transactional;
|
|
165
166
|
export const Type = mod.Type;
|
|
166
167
|
export const Uint8ArrayType = mod.Uint8ArrayType;
|
|
167
168
|
export const UnderscoreNamingStrategy = mod.UnderscoreNamingStrategy;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "6.3.14-dev.
|
|
3
|
+
"version": "6.3.14-dev.32",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"esprima": "4.0.1",
|
|
65
65
|
"fs-extra": "11.2.0",
|
|
66
66
|
"globby": "11.1.0",
|
|
67
|
-
"mikro-orm": "6.3.14-dev.
|
|
67
|
+
"mikro-orm": "6.3.14-dev.32",
|
|
68
68
|
"reflect-metadata": "0.2.2"
|
|
69
69
|
}
|
|
70
70
|
}
|
package/typings.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Transaction } from './connections';
|
|
2
2
|
import { type Cascade, type DeferMode, type EventType, type LoadStrategy, type PopulatePath, type QueryOrderMap, ReferenceKind } from './enums';
|
|
3
3
|
import { type AssignOptions, type Collection, type EntityFactory, type EntityIdentifier, type EntityLoaderOptions, type EntityRepository, Reference, type ScalarReference } from './entity';
|
|
4
|
+
import type { MikroORM } from './MikroORM';
|
|
4
5
|
import type { SerializationContext, SerializeOptions } from './serialization';
|
|
5
6
|
import type { EntitySchema, MetadataStorage } from './metadata';
|
|
6
7
|
import type { Type, types } from './types';
|
|
@@ -786,4 +787,10 @@ export interface Seeder<T extends Dictionary = Dictionary> {
|
|
|
786
787
|
}
|
|
787
788
|
export type ConnectionType = 'read' | 'write';
|
|
788
789
|
export type MetadataProcessor = (metadata: EntityMetadata[], platform: Platform) => MaybePromise<void>;
|
|
790
|
+
/**
|
|
791
|
+
* The type of context that the user intends to inject.
|
|
792
|
+
*/
|
|
793
|
+
export type ContextProvider<T> = MaybePromise<MikroORM> | ((type: T) => MaybePromise<MikroORM | EntityManager | EntityRepository<any> | {
|
|
794
|
+
getEntityManager(): EntityManager;
|
|
795
|
+
}>);
|
|
789
796
|
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EntityManager } from '../EntityManager';
|
|
2
|
+
import { MikroORM } from '../MikroORM';
|
|
3
|
+
import type { ContextProvider, MaybePromise } from '../typings';
|
|
4
|
+
/**
|
|
5
|
+
* Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
|
|
6
|
+
*/
|
|
7
|
+
export declare function resolveContextProvider<T>(caller: T & {
|
|
8
|
+
orm?: MaybePromise<MikroORM>;
|
|
9
|
+
em?: MaybePromise<EntityManager>;
|
|
10
|
+
}, provider?: ContextProvider<T>): Promise<EntityManager | undefined>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveContextProvider = resolveContextProvider;
|
|
4
|
+
const EntityRepository_1 = require("../entity/EntityRepository");
|
|
5
|
+
const EntityManager_1 = require("../EntityManager");
|
|
6
|
+
const MikroORM_1 = require("../MikroORM");
|
|
7
|
+
function getEntityManager(caller, context) {
|
|
8
|
+
if (context instanceof EntityManager_1.EntityManager) {
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
if (context instanceof EntityRepository_1.EntityRepository) {
|
|
12
|
+
return context.getEntityManager();
|
|
13
|
+
}
|
|
14
|
+
if (context instanceof MikroORM_1.MikroORM) {
|
|
15
|
+
return context.em;
|
|
16
|
+
}
|
|
17
|
+
if (caller.em instanceof EntityManager_1.EntityManager) {
|
|
18
|
+
return caller.em;
|
|
19
|
+
}
|
|
20
|
+
if (caller.orm instanceof MikroORM_1.MikroORM) {
|
|
21
|
+
return caller.orm.em;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
|
|
27
|
+
*/
|
|
28
|
+
async function resolveContextProvider(caller, provider) {
|
|
29
|
+
const context = typeof provider === 'function' ? await provider(caller) : await provider;
|
|
30
|
+
return getEntityManager({ orm: await caller.orm, em: await caller.em }, context);
|
|
31
|
+
}
|