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