@mikro-orm/decorators 7.0.4 → 7.0.5-dev.1
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 +1 -3
- package/es/Check.js +6 -6
- package/es/CreateRequestContext.d.ts +2 -13
- package/es/CreateRequestContext.js +22 -24
- package/es/Embeddable.d.ts +1 -3
- package/es/Embeddable.js +9 -9
- package/es/Embedded.d.ts +1 -4
- package/es/Embedded.js +10 -10
- package/es/Entity.d.ts +1 -3
- package/es/Entity.js +9 -9
- package/es/Enum.d.ts +1 -3
- package/es/Enum.js +10 -10
- package/es/Filter.js +4 -4
- package/es/Formula.d.ts +1 -4
- package/es/Formula.js +9 -9
- package/es/Indexed.d.ts +2 -6
- package/es/Indexed.js +11 -11
- package/es/ManyToMany.d.ts +1 -5
- package/es/ManyToMany.js +8 -8
- package/es/ManyToOne.d.ts +1 -4
- package/es/ManyToOne.js +7 -7
- package/es/OneToMany.d.ts +2 -8
- package/es/OneToMany.js +8 -8
- package/es/OneToOne.d.ts +1 -5
- package/es/OneToOne.js +9 -9
- package/es/PrimaryKey.d.ts +2 -6
- package/es/PrimaryKey.js +12 -12
- package/es/Property.d.ts +1 -11
- package/es/Property.js +41 -37
- package/es/Transactional.d.ts +3 -8
- package/es/Transactional.js +15 -18
- package/es/hooks.d.ts +8 -32
- package/es/hooks.js +16 -16
- package/legacy/Check.d.ts +1 -3
- package/legacy/Check.js +9 -9
- package/legacy/CreateRequestContext.d.ts +1 -4
- package/legacy/CreateRequestContext.js +24 -26
- package/legacy/Embeddable.js +8 -8
- package/legacy/Embedded.d.ts +1 -4
- package/legacy/Embedded.js +11 -11
- package/legacy/Entity.js +8 -8
- package/legacy/Enum.d.ts +1 -3
- package/legacy/Enum.js +10 -10
- package/legacy/Filter.js +4 -4
- package/legacy/Formula.d.ts +1 -4
- package/legacy/Formula.js +9 -9
- package/legacy/Indexed.d.ts +2 -6
- package/legacy/Indexed.js +12 -12
- package/legacy/ManyToMany.d.ts +2 -8
- package/legacy/ManyToMany.js +8 -8
- package/legacy/ManyToOne.d.ts +2 -7
- package/legacy/ManyToOne.js +8 -8
- package/legacy/OneToMany.d.ts +2 -8
- package/legacy/OneToMany.js +8 -8
- package/legacy/OneToOne.d.ts +2 -8
- package/legacy/OneToOne.js +10 -10
- package/legacy/PrimaryKey.d.ts +2 -6
- package/legacy/PrimaryKey.js +13 -13
- package/legacy/Property.d.ts +1 -3
- package/legacy/Property.js +25 -25
- package/legacy/ReflectMetadataProvider.d.ts +2 -2
- package/legacy/ReflectMetadataProvider.js +44 -48
- package/legacy/Transactional.d.ts +2 -2
- package/legacy/Transactional.js +17 -20
- package/legacy/hooks.js +15 -15
- package/package.json +2 -2
- package/utils.d.ts +10 -40
- package/utils.js +86 -89
package/utils.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { EntityManager, EntityRepository, MetadataError, MetadataStorage, MikroORM, Utils } from '@mikro-orm/core';
|
|
1
|
+
import { EntityManager, EntityRepository, MetadataError, MetadataStorage, MikroORM, Utils, } from '@mikro-orm/core';
|
|
2
2
|
function getEntityManager(caller, context) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
if (context instanceof EntityManager) {
|
|
4
|
+
return context;
|
|
5
|
+
}
|
|
6
|
+
if (context instanceof EntityRepository) {
|
|
7
|
+
return context.getEntityManager();
|
|
8
|
+
}
|
|
9
|
+
if (context instanceof MikroORM) {
|
|
10
|
+
return context.em;
|
|
11
|
+
}
|
|
12
|
+
if (caller.em instanceof EntityManager) {
|
|
13
|
+
return caller.em;
|
|
14
|
+
}
|
|
15
|
+
if (caller.orm instanceof MikroORM) {
|
|
16
|
+
return caller.orm.em;
|
|
17
|
+
}
|
|
18
|
+
return undefined;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
|
|
22
22
|
*/
|
|
23
23
|
export async function resolveContextProvider(caller, provider) {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const context = typeof provider === 'function' ? await provider(caller) : await provider;
|
|
25
|
+
return getEntityManager({ orm: await caller.orm, em: await caller.em }, context);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Relation decorators allow using two signatures
|
|
@@ -34,23 +34,21 @@ export async function resolveContextProvider(caller, provider) {
|
|
|
34
34
|
* @internal
|
|
35
35
|
*/
|
|
36
36
|
export function processDecoratorParameters(params) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
return values[0];
|
|
37
|
+
const keys = Object.keys(params);
|
|
38
|
+
const values = Object.values(params);
|
|
39
|
+
if (!Utils.isPlainObject(values[0])) {
|
|
40
|
+
const lastKey = keys[keys.length - 1];
|
|
41
|
+
const last = params[lastKey];
|
|
42
|
+
delete params[lastKey];
|
|
43
|
+
return { ...last, ...params };
|
|
44
|
+
}
|
|
45
|
+
// validate only first parameter is used if its an option object
|
|
46
|
+
const empty = (v) => v == null || (Utils.isPlainObject(v) && !Utils.hasObjectKeys(v));
|
|
47
|
+
if (values.slice(1).some(v => !empty(v))) {
|
|
48
|
+
throw new Error('Mixing first decorator parameter as options object with other parameters is forbidden. ' +
|
|
49
|
+
'If you want to use the options parameter at first position, provide all options inside it.');
|
|
50
|
+
}
|
|
51
|
+
return values[0];
|
|
54
52
|
}
|
|
55
53
|
/**
|
|
56
54
|
* Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
|
|
@@ -58,9 +56,9 @@ export function processDecoratorParameters(params) {
|
|
|
58
56
|
* We allow the existence of the property in metadata if the reference kind is the same, this should allow things like HMR to work.
|
|
59
57
|
*/
|
|
60
58
|
export function validateSingleDecorator(meta, propertyName, kind) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
if (meta.properties[propertyName] && meta.properties[propertyName].kind !== kind) {
|
|
60
|
+
throw MetadataError.multipleDecorators(meta.className, propertyName);
|
|
61
|
+
}
|
|
64
62
|
}
|
|
65
63
|
/**
|
|
66
64
|
* Prepares and returns a metadata context for an entity, ensuring default structure and validating proper usage of a single decorator.
|
|
@@ -68,63 +66,62 @@ export function validateSingleDecorator(meta, propertyName, kind) {
|
|
|
68
66
|
* exist for some base entity.
|
|
69
67
|
*/
|
|
70
68
|
export function prepareMetadataContext(context, kind) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
const meta = context.metadata;
|
|
70
|
+
if (!Object.hasOwn(meta, 'properties')) {
|
|
71
|
+
meta.properties = { ...meta.properties };
|
|
72
|
+
}
|
|
73
|
+
if (kind) {
|
|
74
|
+
validateSingleDecorator(meta, context.name, kind);
|
|
75
|
+
}
|
|
76
|
+
return meta;
|
|
79
77
|
}
|
|
80
78
|
/**
|
|
81
79
|
* Uses some dark magic to get source path to caller where decorator is used.
|
|
82
80
|
* Analyzes stack trace of error created inside the function call.
|
|
83
81
|
*/
|
|
84
82
|
export function lookupPathFromDecorator(name, stack) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
line
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
83
|
+
// use some dark magic to get source path to caller
|
|
84
|
+
stack = stack || new Error().stack.split('\n');
|
|
85
|
+
// In some situations (e.g. swc 1.3.4+), the presence of a source map can obscure the call to
|
|
86
|
+
// __decorate(), replacing it with the constructor name. To support these cases we look for
|
|
87
|
+
// Reflect.decorate() as well. Also when babel is used, we need to check
|
|
88
|
+
// the `_applyDecoratedDescriptor` method instead.
|
|
89
|
+
let line = stack.findIndex(line => /__decorate|Reflect\.decorate|_applyDecoratedDescriptor/.exec(line));
|
|
90
|
+
// bun does not have those lines at all, only the DecorateProperty/DecorateConstructor,
|
|
91
|
+
// but those are also present in node, so we need to check this only if they weren't found.
|
|
92
|
+
if (line === -1) {
|
|
93
|
+
// here we handle bun which stack is different from nodejs so we search for reflect-metadata
|
|
94
|
+
// Different bun versions might have different stack traces. The "last index" works for both 1.2.6 and 1.2.7.
|
|
95
|
+
const reflectLine = stack.findLastIndex(line => line.replace(/\\/g, '/').includes('node_modules/reflect-metadata/Reflect.js'));
|
|
96
|
+
if (reflectLine === -1 || reflectLine + 2 >= stack.length || !stack[reflectLine + 1].includes('bun:wrap')) {
|
|
97
|
+
return name;
|
|
98
|
+
}
|
|
99
|
+
line = reflectLine + 2;
|
|
100
|
+
}
|
|
101
|
+
if (stack[line].includes('Reflect.decorate')) {
|
|
102
|
+
line++;
|
|
103
|
+
}
|
|
104
|
+
// Skip decorator runtime helpers (tslib, @oxc-project/runtime, etc.)
|
|
105
|
+
// The @oxc-project/runtime check covers both node_modules installs and Rolldown-bundled
|
|
106
|
+
// virtual modules (e.g. \0@oxc-project+runtime@0.120.0/helpers/decorate.js).
|
|
107
|
+
while (line < stack.length && /node_modules\/tslib\/|@oxc-project[/+]runtime/.test(stack[line].replace(/\\/g, '/'))) {
|
|
108
|
+
line++;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const re = /\(.+\)/i.exec(stack[line]) ? /\((.*):\d+:\d+\)/ : /at\s*(.*):\d+:\d+$/;
|
|
112
|
+
return stack[line].match(re)[1];
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return name;
|
|
116
|
+
}
|
|
120
117
|
}
|
|
121
118
|
/** Retrieves or creates the metadata object for a decorated entity class. */
|
|
122
119
|
export function getMetadataFromDecorator(target) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
120
|
+
if (!Object.hasOwn(target, MetadataStorage.PATH_SYMBOL)) {
|
|
121
|
+
Object.defineProperty(target, MetadataStorage.PATH_SYMBOL, {
|
|
122
|
+
value: lookupPathFromDecorator(target.name),
|
|
123
|
+
writable: true,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return MetadataStorage.getMetadata(target.name, target[MetadataStorage.PATH_SYMBOL]);
|
|
130
127
|
}
|