@mikro-orm/decorators 7.0.4-dev.8 → 7.0.4
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/README.md +1 -1
- package/es/Check.d.ts +3 -1
- package/es/Check.js +6 -6
- package/es/CreateRequestContext.d.ts +13 -2
- package/es/CreateRequestContext.js +24 -22
- package/es/Embeddable.d.ts +3 -1
- package/es/Embeddable.js +9 -9
- package/es/Embedded.d.ts +4 -1
- package/es/Embedded.js +10 -10
- package/es/Entity.d.ts +3 -1
- package/es/Entity.js +9 -9
- package/es/Enum.d.ts +3 -1
- package/es/Enum.js +10 -10
- package/es/Filter.js +4 -4
- package/es/Formula.d.ts +4 -1
- package/es/Formula.js +9 -9
- package/es/Indexed.d.ts +6 -2
- package/es/Indexed.js +11 -11
- package/es/ManyToMany.d.ts +5 -1
- package/es/ManyToMany.js +8 -8
- package/es/ManyToOne.d.ts +4 -1
- package/es/ManyToOne.js +7 -7
- package/es/OneToMany.d.ts +8 -2
- package/es/OneToMany.js +8 -8
- package/es/OneToOne.d.ts +5 -1
- package/es/OneToOne.js +9 -9
- package/es/PrimaryKey.d.ts +6 -2
- package/es/PrimaryKey.js +12 -12
- package/es/Property.d.ts +11 -1
- package/es/Property.js +37 -41
- package/es/Transactional.d.ts +8 -3
- package/es/Transactional.js +18 -15
- package/es/hooks.d.ts +32 -8
- package/es/hooks.js +16 -16
- package/legacy/Check.d.ts +3 -1
- package/legacy/Check.js +9 -9
- package/legacy/CreateRequestContext.d.ts +4 -1
- package/legacy/CreateRequestContext.js +26 -24
- package/legacy/Embeddable.js +8 -8
- package/legacy/Embedded.d.ts +4 -1
- package/legacy/Embedded.js +11 -11
- package/legacy/Entity.js +8 -8
- package/legacy/Enum.d.ts +3 -1
- package/legacy/Enum.js +10 -10
- package/legacy/Filter.js +4 -4
- package/legacy/Formula.d.ts +4 -1
- package/legacy/Formula.js +9 -9
- package/legacy/Indexed.d.ts +6 -2
- package/legacy/Indexed.js +12 -12
- package/legacy/ManyToMany.d.ts +8 -2
- package/legacy/ManyToMany.js +8 -8
- package/legacy/ManyToOne.d.ts +7 -2
- package/legacy/ManyToOne.js +8 -8
- package/legacy/OneToMany.d.ts +8 -2
- package/legacy/OneToMany.js +8 -8
- package/legacy/OneToOne.d.ts +8 -2
- package/legacy/OneToOne.js +10 -10
- package/legacy/PrimaryKey.d.ts +6 -2
- package/legacy/PrimaryKey.js +13 -13
- package/legacy/Property.d.ts +3 -1
- package/legacy/Property.js +25 -25
- package/legacy/ReflectMetadataProvider.d.ts +2 -2
- package/legacy/ReflectMetadataProvider.js +48 -44
- package/legacy/Transactional.d.ts +2 -2
- package/legacy/Transactional.js +20 -17
- package/legacy/hooks.js +15 -15
- package/package.json +3 -3
- package/utils.d.ts +40 -10
- package/utils.js +89 -85
package/legacy/ManyToOne.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { type ManyToOneOptions, type EntityName } from '@mikro-orm/core';
|
|
2
2
|
/** Defines a many-to-one relationship (legacy TypeScript decorator). */
|
|
3
|
-
export declare function ManyToOne<Target extends object, Owner extends object>(
|
|
3
|
+
export declare function ManyToOne<Target extends object, Owner extends object>(
|
|
4
|
+
entity: (e?: any) => EntityName<Target> | EntityName[],
|
|
5
|
+
options?: Partial<ManyToOneOptions<Owner, Target>>,
|
|
6
|
+
): (target: Owner, propertyName: string) => void;
|
|
4
7
|
export declare function ManyToOne<Target extends object, Owner extends object>(entity: string, options?: any): never;
|
|
5
|
-
export declare function ManyToOne<Target extends object, Owner extends object>(
|
|
8
|
+
export declare function ManyToOne<Target extends object, Owner extends object>(
|
|
9
|
+
options?: ManyToOneOptions<Owner, Target>,
|
|
10
|
+
): (target: Owner, propertyName: string) => void;
|
package/legacy/ManyToOne.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ReferenceKind
|
|
1
|
+
import { ReferenceKind } from '@mikro-orm/core';
|
|
2
2
|
import { processDecoratorParameters, validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
|
|
3
3
|
export function ManyToOne(entity = {}, options = {}) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
return function (target, propertyName) {
|
|
5
|
+
options = processDecoratorParameters({ entity, options });
|
|
6
|
+
const meta = getMetadataFromDecorator(target.constructor);
|
|
7
|
+
validateSingleDecorator(meta, propertyName, ReferenceKind.MANY_TO_ONE);
|
|
8
|
+
const property = { name: propertyName, kind: ReferenceKind.MANY_TO_ONE };
|
|
9
|
+
meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
|
|
10
|
+
};
|
|
11
11
|
}
|
package/legacy/OneToMany.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { type EntityName, type OneToManyOptions } from '@mikro-orm/core';
|
|
2
2
|
/** Defines a one-to-many relationship (legacy TypeScript decorator). */
|
|
3
|
-
export declare function OneToMany<Target extends object, Owner extends object>(
|
|
4
|
-
|
|
3
|
+
export declare function OneToMany<Target extends object, Owner extends object>(
|
|
4
|
+
entity: (e?: any) => EntityName<Target>,
|
|
5
|
+
mappedBy: (string & keyof Target) | ((e: Target) => any),
|
|
6
|
+
options?: Partial<OneToManyOptions<Owner, Target>>,
|
|
7
|
+
): (target: Owner, propertyName: string) => void;
|
|
8
|
+
export declare function OneToMany<Target extends object, Owner extends object>(
|
|
9
|
+
options: OneToManyOptions<Owner, Target>,
|
|
10
|
+
): (target: Owner, propertyName: string) => void;
|
package/legacy/OneToMany.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ReferenceKind
|
|
1
|
+
import { ReferenceKind } from '@mikro-orm/core';
|
|
2
2
|
import { processDecoratorParameters, validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
|
|
3
3
|
export function OneToMany(entity, mappedBy, options = {}) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
return function (target, propertyName) {
|
|
5
|
+
options = processDecoratorParameters({ entity, mappedBy, options });
|
|
6
|
+
const meta = getMetadataFromDecorator(target.constructor);
|
|
7
|
+
validateSingleDecorator(meta, propertyName, ReferenceKind.ONE_TO_MANY);
|
|
8
|
+
const property = { name: propertyName, kind: ReferenceKind.ONE_TO_MANY };
|
|
9
|
+
meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
|
|
10
|
+
};
|
|
11
11
|
}
|
package/legacy/OneToOne.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { type EntityName, type OneToOneOptions } from '@mikro-orm/core';
|
|
2
2
|
/** Defines a one-to-one relationship (legacy TypeScript decorator). */
|
|
3
|
-
export declare function OneToOne<Target, Owner>(
|
|
4
|
-
|
|
3
|
+
export declare function OneToOne<Target, Owner>(
|
|
4
|
+
entity: (e: Owner) => EntityName<Target> | EntityName[],
|
|
5
|
+
mappedByOrOptions?: (string & keyof Target) | ((e: Target) => any) | Partial<OneToOneOptions<Owner, Target>>,
|
|
6
|
+
options?: Partial<OneToOneOptions<Owner, Target>>,
|
|
7
|
+
): (target: Owner, propertyName: string) => void;
|
|
8
|
+
export declare function OneToOne<Target, Owner>(
|
|
9
|
+
entity?: OneToOneOptions<Owner, Target>,
|
|
10
|
+
): (target: Owner, propertyName: string) => void;
|
package/legacy/OneToOne.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { ReferenceKind
|
|
1
|
+
import { ReferenceKind } from '@mikro-orm/core';
|
|
2
2
|
import { processDecoratorParameters, validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
|
|
3
3
|
export function OneToOne(entity, mappedByOrOptions, options = {}) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
const mappedBy = typeof mappedByOrOptions === 'object' ? mappedByOrOptions.mappedBy : mappedByOrOptions;
|
|
5
|
+
options = typeof mappedByOrOptions === 'object' ? { ...mappedByOrOptions, ...options } : options;
|
|
6
|
+
return function (target, propertyName) {
|
|
7
|
+
options = processDecoratorParameters({ entity, mappedBy, options });
|
|
8
|
+
const meta = getMetadataFromDecorator(target.constructor);
|
|
9
|
+
validateSingleDecorator(meta, propertyName, ReferenceKind.ONE_TO_ONE);
|
|
10
|
+
const property = { name: propertyName, kind: ReferenceKind.ONE_TO_ONE };
|
|
11
|
+
meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
|
|
12
|
+
};
|
|
13
13
|
}
|
package/legacy/PrimaryKey.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { type PrimaryKeyOptions, type SerializedPrimaryKeyOptions } from '@mikro-orm/core';
|
|
2
2
|
/** Marks a property as the primary key of an entity (legacy TypeScript decorator). */
|
|
3
|
-
export declare function PrimaryKey<T extends object>(
|
|
3
|
+
export declare function PrimaryKey<T extends object>(
|
|
4
|
+
options?: PrimaryKeyOptions<T>,
|
|
5
|
+
): (target: T, propertyName: string) => void;
|
|
4
6
|
/** Marks a property as the serialized form of the primary key, e.g. for MongoDB ObjectId (legacy TypeScript decorator). */
|
|
5
|
-
export declare function SerializedPrimaryKey<T extends object>(
|
|
7
|
+
export declare function SerializedPrimaryKey<T extends object>(
|
|
8
|
+
options?: SerializedPrimaryKeyOptions<T>,
|
|
9
|
+
): (target: T, propertyName: string) => void;
|
package/legacy/PrimaryKey.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { ReferenceKind
|
|
1
|
+
import { ReferenceKind } from '@mikro-orm/core';
|
|
2
2
|
import { validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
|
|
3
3
|
function createDecorator(options, serialized) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
4
|
+
return function (target, propertyName) {
|
|
5
|
+
const meta = getMetadataFromDecorator(target.constructor);
|
|
6
|
+
validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
|
|
7
|
+
const k = serialized ? 'serializedPrimaryKey' : 'primary';
|
|
8
|
+
options[k] = true;
|
|
9
|
+
meta.properties[propertyName] = {
|
|
10
|
+
name: propertyName,
|
|
11
|
+
kind: ReferenceKind.SCALAR,
|
|
12
|
+
...options,
|
|
14
13
|
};
|
|
14
|
+
};
|
|
15
15
|
}
|
|
16
16
|
/** Marks a property as the primary key of an entity (legacy TypeScript decorator). */
|
|
17
17
|
export function PrimaryKey(options = {}) {
|
|
18
|
-
|
|
18
|
+
return createDecorator(options, false);
|
|
19
19
|
}
|
|
20
20
|
/** Marks a property as the serialized form of the primary key, e.g. for MongoDB ObjectId (legacy TypeScript decorator). */
|
|
21
21
|
export function SerializedPrimaryKey(options = {}) {
|
|
22
|
-
|
|
22
|
+
return createDecorator(options, true);
|
|
23
23
|
}
|
package/legacy/Property.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import { type PropertyOptions } from '@mikro-orm/core';
|
|
2
2
|
/** Defines a scalar property on an entity (legacy TypeScript decorator). */
|
|
3
|
-
export declare function Property<T extends object>(
|
|
3
|
+
export declare function Property<T extends object>(
|
|
4
|
+
options?: PropertyOptions<T>,
|
|
5
|
+
): (target: T, propertyName: string) => void;
|
package/legacy/Property.js
CHANGED
|
@@ -2,29 +2,29 @@ import { Utils, ReferenceKind } from '@mikro-orm/core';
|
|
|
2
2
|
import { validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
|
|
3
3
|
/** Defines a scalar property on an entity (legacy TypeScript decorator). */
|
|
4
4
|
export function Property(options = {}) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
5
|
+
return function (target, propertyName) {
|
|
6
|
+
const meta = getMetadataFromDecorator(target.constructor);
|
|
7
|
+
const desc = Object.getOwnPropertyDescriptor(target, propertyName) || {};
|
|
8
|
+
validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
|
|
9
|
+
const name = options.name || propertyName;
|
|
10
|
+
if (propertyName !== name && !(desc.value instanceof Function)) {
|
|
11
|
+
Utils.renameKey(options, 'name', 'fieldName');
|
|
12
|
+
}
|
|
13
|
+
options.name = propertyName;
|
|
14
|
+
const { check, ...opts } = options;
|
|
15
|
+
const prop = { kind: ReferenceKind.SCALAR, ...opts };
|
|
16
|
+
prop.getter = !!desc.get;
|
|
17
|
+
prop.setter = !!desc.set;
|
|
18
|
+
if (desc.value instanceof Function) {
|
|
19
|
+
prop.getter = true;
|
|
20
|
+
prop.persist = false;
|
|
21
|
+
prop.type = 'method';
|
|
22
|
+
prop.getterName = propertyName;
|
|
23
|
+
prop.name = name;
|
|
24
|
+
}
|
|
25
|
+
if (check) {
|
|
26
|
+
meta.checks.push({ property: prop.name, expression: check });
|
|
27
|
+
}
|
|
28
|
+
meta.properties[prop.name] = prop;
|
|
29
|
+
};
|
|
30
30
|
}
|
|
@@ -2,6 +2,6 @@ import 'reflect-metadata';
|
|
|
2
2
|
import { type EntityMetadata, type EntityProperty, MetadataProvider } from '@mikro-orm/core';
|
|
3
3
|
/** Metadata provider that uses `reflect-metadata` to infer property types from TypeScript's emitted design:type metadata. */
|
|
4
4
|
export declare class ReflectMetadataProvider extends MetadataProvider {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
loadEntityMetadata(meta: EntityMetadata): void;
|
|
6
|
+
protected initPropertyType(meta: EntityMetadata, prop: EntityProperty): void;
|
|
7
7
|
}
|
|
@@ -1,50 +1,54 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import { EntitySchema, MetadataProvider, ReferenceKind, Utils
|
|
2
|
+
import { EntitySchema, MetadataProvider, ReferenceKind, Utils } from '@mikro-orm/core';
|
|
3
3
|
/** Metadata provider that uses `reflect-metadata` to infer property types from TypeScript's emitted design:type metadata. */
|
|
4
4
|
export class ReflectMetadataProvider extends MetadataProvider {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
5
|
+
loadEntityMetadata(meta) {
|
|
6
|
+
// load types and column names
|
|
7
|
+
for (const prop of meta.props) {
|
|
8
|
+
/* v8 ignore next */
|
|
9
|
+
if (typeof prop.entity === 'string') {
|
|
10
|
+
throw new Error(
|
|
11
|
+
`Relation target needs to be an entity class or EntitySchema instance, '${prop.entity}' given instead for ${meta.className}.${prop.name}.`,
|
|
12
|
+
);
|
|
13
|
+
} else if (prop.entity) {
|
|
14
|
+
const tmp = prop.entity();
|
|
15
|
+
prop.type = Array.isArray(tmp)
|
|
16
|
+
? tmp
|
|
17
|
+
.map(t => Utils.className(t))
|
|
18
|
+
.sort()
|
|
19
|
+
.join(' | ')
|
|
20
|
+
: Utils.className(tmp);
|
|
21
|
+
prop.target = EntitySchema.is(tmp) ? tmp.meta.class : tmp;
|
|
22
|
+
} else {
|
|
23
|
+
this.initPropertyType(meta, prop);
|
|
24
|
+
}
|
|
26
25
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (prop.kind === ReferenceKind.SCALAR && type === Object && !prop.columnTypes) {
|
|
39
|
-
prop.type ??= 'any';
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
let typeName = type?.name;
|
|
43
|
-
if (typeName && ['string', 'number', 'boolean', 'array', 'object'].includes(typeName.toLowerCase())) {
|
|
44
|
-
typeName = typeName.toLowerCase();
|
|
45
|
-
}
|
|
46
|
-
prop.type ??= typeName;
|
|
47
|
-
prop.runtimeType ??= typeName;
|
|
48
|
-
prop.target = type;
|
|
26
|
+
}
|
|
27
|
+
initPropertyType(meta, prop) {
|
|
28
|
+
const type = Reflect.getMetadata('design:type', meta.prototype, prop.name);
|
|
29
|
+
if (
|
|
30
|
+
!prop.type &&
|
|
31
|
+
(!type || (type === Object && prop.kind !== ReferenceKind.SCALAR)) &&
|
|
32
|
+
!(prop.enum && (prop.items?.length ?? 0) > 0)
|
|
33
|
+
) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Please provide either 'type' or 'entity' attribute in ${meta.className}.${prop.name}. Make sure you have 'emitDecoratorMetadata' enabled in your tsconfig.json.`,
|
|
36
|
+
);
|
|
49
37
|
}
|
|
38
|
+
// Force mapping to UnknownType which is a string when we see just `Object`, as that often means failed inference.
|
|
39
|
+
// This is to prevent defaulting to JSON column type, which can often be hard to revert and cause hard to understand issues with PKs.
|
|
40
|
+
// If there are explicitly provided `columnTypes`, we use those instead for the inference, this way
|
|
41
|
+
// we can have things like `columnType: 'timestamp'` be respected as `type: 'Date'`.
|
|
42
|
+
if (prop.kind === ReferenceKind.SCALAR && type === Object && !prop.columnTypes) {
|
|
43
|
+
prop.type ??= 'any';
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
let typeName = type?.name;
|
|
47
|
+
if (typeName && ['string', 'number', 'boolean', 'array', 'object'].includes(typeName.toLowerCase())) {
|
|
48
|
+
typeName = typeName.toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
prop.type ??= typeName;
|
|
51
|
+
prop.runtimeType ??= typeName;
|
|
52
|
+
prop.target = type;
|
|
53
|
+
}
|
|
50
54
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type TransactionOptions } from '@mikro-orm/core';
|
|
2
2
|
import { type ContextProvider } from '../utils.js';
|
|
3
3
|
type TransactionalOptions<T> = TransactionOptions & {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
context?: ContextProvider<T>;
|
|
5
|
+
contextName?: string;
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
8
|
* This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
|
package/legacy/Transactional.js
CHANGED
|
@@ -8,22 +8,25 @@ import { resolveContextProvider } from '../utils.js';
|
|
|
8
8
|
* Unlike `em.transactional()`, this decorator uses `REQUIRED` propagation by default, which means it will join existing transactions.
|
|
9
9
|
*/
|
|
10
10
|
export function Transactional(options = {}) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
return function (target, propertyKey, descriptor) {
|
|
12
|
+
const originalMethod = descriptor.value;
|
|
13
|
+
if (originalMethod.constructor.name !== 'AsyncFunction') {
|
|
14
|
+
throw new Error('@Transactional() should be use with async functions');
|
|
15
|
+
}
|
|
16
|
+
descriptor.value = async function (...args) {
|
|
17
|
+
const { context, contextName, ...txOptions } = options;
|
|
18
|
+
txOptions.propagation ??= TransactionPropagation.REQUIRED;
|
|
19
|
+
const em =
|
|
20
|
+
(await resolveContextProvider(this, context)) ||
|
|
21
|
+
TransactionContext.getEntityManager(contextName) ||
|
|
22
|
+
RequestContext.getEntityManager(contextName);
|
|
23
|
+
if (!em) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`@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
|
+
}
|
|
28
|
+
return em.transactional(() => originalMethod.apply(this, args), txOptions);
|
|
28
29
|
};
|
|
30
|
+
return descriptor;
|
|
31
|
+
};
|
|
29
32
|
}
|
package/legacy/hooks.js
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
1
|
import { EventType } from '@mikro-orm/core';
|
|
2
2
|
import { getMetadataFromDecorator } from '../utils.js';
|
|
3
3
|
function hook(type) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
return function (target, method) {
|
|
5
|
+
const meta = getMetadataFromDecorator(target.constructor);
|
|
6
|
+
meta.hooks[type] ??= [];
|
|
7
|
+
meta.hooks[type].push(method);
|
|
8
|
+
};
|
|
9
9
|
}
|
|
10
10
|
/** Called before a new entity is persisted to the database (legacy TypeScript decorator). */
|
|
11
11
|
export function BeforeCreate() {
|
|
12
|
-
|
|
12
|
+
return hook(EventType.beforeCreate);
|
|
13
13
|
}
|
|
14
14
|
/** Called after a new entity has been persisted to the database (legacy TypeScript decorator). */
|
|
15
15
|
export function AfterCreate() {
|
|
16
|
-
|
|
16
|
+
return hook(EventType.afterCreate);
|
|
17
17
|
}
|
|
18
18
|
/** Called before an existing entity is updated in the database (legacy TypeScript decorator). */
|
|
19
19
|
export function BeforeUpdate() {
|
|
20
|
-
|
|
20
|
+
return hook(EventType.beforeUpdate);
|
|
21
21
|
}
|
|
22
22
|
/** Called after an existing entity has been updated in the database (legacy TypeScript decorator). */
|
|
23
23
|
export function AfterUpdate() {
|
|
24
|
-
|
|
24
|
+
return hook(EventType.afterUpdate);
|
|
25
25
|
}
|
|
26
26
|
/** Called before an entity is upserted (legacy TypeScript decorator). */
|
|
27
27
|
export function BeforeUpsert() {
|
|
28
|
-
|
|
28
|
+
return hook(EventType.beforeUpsert);
|
|
29
29
|
}
|
|
30
30
|
/** Called after an entity has been upserted (legacy TypeScript decorator). */
|
|
31
31
|
export function AfterUpsert() {
|
|
32
|
-
|
|
32
|
+
return hook(EventType.afterUpsert);
|
|
33
33
|
}
|
|
34
34
|
/** Called when an entity is instantiated by the EntityManager (legacy TypeScript decorator). */
|
|
35
35
|
export function OnInit() {
|
|
36
|
-
|
|
36
|
+
return hook(EventType.onInit);
|
|
37
37
|
}
|
|
38
38
|
/** Called after an entity is loaded from the database (legacy TypeScript decorator). */
|
|
39
39
|
export function OnLoad() {
|
|
40
|
-
|
|
40
|
+
return hook(EventType.onLoad);
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Called before deleting entity, but only when providing initialized entity to EM#remove()
|
|
44
44
|
*/
|
|
45
45
|
export function BeforeDelete() {
|
|
46
|
-
|
|
46
|
+
return hook(EventType.beforeDelete);
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Called after deleting entity, but only when providing initialized entity to EM#remove()
|
|
50
50
|
*/
|
|
51
51
|
export function AfterDelete() {
|
|
52
|
-
|
|
52
|
+
return hook(EventType.afterDelete);
|
|
53
53
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/decorators",
|
|
3
|
-
"version": "7.0.4
|
|
3
|
+
"version": "7.0.4",
|
|
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
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -49,10 +49,10 @@
|
|
|
49
49
|
"copy": "node ../../scripts/copy.mjs"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@mikro-orm/core": "^7.0.
|
|
52
|
+
"@mikro-orm/core": "^7.0.4"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@mikro-orm/core": "7.0.4
|
|
55
|
+
"@mikro-orm/core": "7.0.4",
|
|
56
56
|
"reflect-metadata": "^0.1.0 || ^0.2.0"
|
|
57
57
|
},
|
|
58
58
|
"peerDependenciesMeta": {
|
package/utils.d.ts
CHANGED
|
@@ -1,17 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type Dictionary,
|
|
3
|
+
EntityManager,
|
|
4
|
+
type EntityMetadata,
|
|
5
|
+
EntityRepository,
|
|
6
|
+
type MaybePromise,
|
|
7
|
+
MetadataStorage,
|
|
8
|
+
MikroORM,
|
|
9
|
+
type ReferenceKind,
|
|
10
|
+
} from '@mikro-orm/core';
|
|
2
11
|
/**
|
|
3
12
|
* The type of context that the user intends to inject.
|
|
4
13
|
*/
|
|
5
|
-
export type ContextProvider<T> =
|
|
6
|
-
|
|
7
|
-
|
|
14
|
+
export type ContextProvider<T> =
|
|
15
|
+
| MaybePromise<MikroORM>
|
|
16
|
+
| ((type: T) => MaybePromise<
|
|
17
|
+
| MikroORM
|
|
18
|
+
| EntityManager
|
|
19
|
+
| EntityRepository<any>
|
|
20
|
+
| {
|
|
21
|
+
getEntityManager(): EntityManager;
|
|
22
|
+
}
|
|
23
|
+
>);
|
|
8
24
|
/**
|
|
9
25
|
* Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
|
|
10
26
|
*/
|
|
11
|
-
export declare function resolveContextProvider<T>(
|
|
27
|
+
export declare function resolveContextProvider<T>(
|
|
28
|
+
caller: T & {
|
|
12
29
|
orm?: MaybePromise<MikroORM>;
|
|
13
30
|
em?: MaybePromise<EntityManager>;
|
|
14
|
-
},
|
|
31
|
+
},
|
|
32
|
+
provider?: ContextProvider<T>,
|
|
33
|
+
): Promise<EntityManager | undefined>;
|
|
15
34
|
/**
|
|
16
35
|
* Relation decorators allow using two signatures
|
|
17
36
|
* - using first parameter as options object
|
|
@@ -33,13 +52,24 @@ export declare function validateSingleDecorator(meta: EntityMetadata, propertyNa
|
|
|
33
52
|
* We need to use the `Object.hasOwn` here, since the metadata object respects inheritance, and the `properties` object might already
|
|
34
53
|
* exist for some base entity.
|
|
35
54
|
*/
|
|
36
|
-
export declare function prepareMetadataContext<T>(
|
|
55
|
+
export declare function prepareMetadataContext<T>(
|
|
56
|
+
context:
|
|
57
|
+
| ClassFieldDecoratorContext<T>
|
|
58
|
+
| ClassGetterDecoratorContext<T>
|
|
59
|
+
| ClassSetterDecoratorContext<T>
|
|
60
|
+
| ClassAccessorDecoratorContext<T>
|
|
61
|
+
| ClassMethodDecoratorContext<T>,
|
|
62
|
+
kind?: ReferenceKind,
|
|
63
|
+
): EntityMetadata<T>;
|
|
37
64
|
/**
|
|
38
65
|
* Uses some dark magic to get source path to caller where decorator is used.
|
|
39
66
|
* Analyzes stack trace of error created inside the function call.
|
|
40
67
|
*/
|
|
41
68
|
export declare function lookupPathFromDecorator(name: string, stack?: string[]): string;
|
|
42
69
|
/** Retrieves or creates the metadata object for a decorated entity class. */
|
|
43
|
-
export declare function getMetadataFromDecorator<T = any>(
|
|
44
|
-
|
|
45
|
-
|
|
70
|
+
export declare function getMetadataFromDecorator<T = any>(
|
|
71
|
+
target: T &
|
|
72
|
+
Dictionary & {
|
|
73
|
+
[MetadataStorage.PATH_SYMBOL]?: string;
|
|
74
|
+
},
|
|
75
|
+
): EntityMetadata<T>;
|