@mikro-orm/core 7.0.10 → 7.0.11-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/EntityManager.d.ts +584 -885
- package/EntityManager.js +1900 -1927
- package/MikroORM.d.ts +74 -103
- package/MikroORM.js +178 -177
- package/README.md +1 -1
- package/cache/CacheAdapter.d.ts +36 -36
- package/cache/FileCacheAdapter.d.ts +24 -30
- package/cache/FileCacheAdapter.js +78 -80
- package/cache/GeneratedCacheAdapter.d.ts +20 -18
- package/cache/GeneratedCacheAdapter.js +30 -30
- package/cache/MemoryCacheAdapter.d.ts +20 -18
- package/cache/MemoryCacheAdapter.js +36 -35
- package/cache/NullCacheAdapter.d.ts +16 -16
- package/cache/NullCacheAdapter.js +24 -24
- package/connections/Connection.d.ts +84 -95
- package/connections/Connection.js +168 -165
- package/drivers/DatabaseDriver.d.ts +80 -186
- package/drivers/DatabaseDriver.js +443 -450
- package/drivers/IDatabaseDriver.d.ts +301 -440
- package/entity/BaseEntity.d.ts +83 -120
- package/entity/BaseEntity.js +43 -43
- package/entity/Collection.d.ts +181 -215
- package/entity/Collection.js +724 -730
- package/entity/EntityAssigner.d.ts +77 -88
- package/entity/EntityAssigner.js +230 -231
- package/entity/EntityFactory.d.ts +55 -67
- package/entity/EntityFactory.js +416 -457
- package/entity/EntityHelper.d.ts +23 -35
- package/entity/EntityHelper.js +290 -302
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +72 -98
- package/entity/EntityLoader.js +776 -805
- package/entity/EntityRepository.d.ts +201 -316
- package/entity/EntityRepository.js +213 -213
- package/entity/PolymorphicRef.d.ts +5 -5
- package/entity/PolymorphicRef.js +10 -10
- package/entity/Reference.d.ts +83 -127
- package/entity/Reference.js +277 -281
- package/entity/WrappedEntity.d.ts +72 -115
- package/entity/WrappedEntity.js +166 -168
- package/entity/defineEntity.d.ts +654 -1359
- package/entity/defineEntity.js +518 -527
- package/entity/utils.d.ts +3 -13
- package/entity/utils.js +73 -71
- package/entity/validators.js +43 -43
- package/entity/wrap.js +8 -8
- package/enums.d.ts +253 -258
- package/enums.js +252 -251
- package/errors.d.ts +72 -114
- package/errors.js +253 -350
- package/events/EventManager.d.ts +14 -26
- package/events/EventManager.js +77 -79
- package/events/EventSubscriber.d.ts +29 -29
- package/events/TransactionEventBroadcaster.d.ts +8 -15
- package/events/TransactionEventBroadcaster.js +14 -14
- package/exceptions.d.ts +40 -23
- package/exceptions.js +52 -35
- package/hydration/Hydrator.d.ts +17 -42
- package/hydration/Hydrator.js +43 -43
- package/hydration/ObjectHydrator.d.ts +17 -50
- package/hydration/ObjectHydrator.js +418 -483
- package/index.d.ts +2 -116
- package/index.js +1 -10
- package/logging/DefaultLogger.d.ts +32 -34
- package/logging/DefaultLogger.js +86 -86
- package/logging/Logger.d.ts +41 -41
- package/logging/SimpleLogger.d.ts +11 -13
- package/logging/SimpleLogger.js +22 -22
- package/logging/colors.d.ts +6 -6
- package/logging/colors.js +10 -11
- package/logging/inspect.js +7 -7
- package/metadata/EntitySchema.d.ts +130 -214
- package/metadata/EntitySchema.js +412 -411
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1884 -1962
- package/metadata/MetadataProvider.d.ts +26 -29
- package/metadata/MetadataProvider.js +97 -95
- package/metadata/MetadataStorage.d.ts +32 -38
- package/metadata/MetadataStorage.js +118 -118
- package/metadata/MetadataValidator.d.ts +39 -39
- package/metadata/MetadataValidator.js +338 -381
- package/metadata/discover-entities.d.ts +2 -5
- package/metadata/discover-entities.js +37 -35
- package/metadata/types.d.ts +531 -615
- package/naming-strategy/AbstractNamingStrategy.d.ts +39 -54
- package/naming-strategy/AbstractNamingStrategy.js +85 -90
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
- package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
- package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
- package/naming-strategy/MongoNamingStrategy.js +18 -18
- package/naming-strategy/NamingStrategy.d.ts +99 -109
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
- package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
- package/not-supported.js +4 -7
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -1
- package/platforms/ExceptionConverter.js +4 -4
- package/platforms/Platform.d.ts +303 -312
- package/platforms/Platform.js +675 -695
- package/serialization/EntitySerializer.d.ts +26 -49
- package/serialization/EntitySerializer.js +218 -224
- package/serialization/EntityTransformer.d.ts +6 -10
- package/serialization/EntityTransformer.js +217 -219
- package/serialization/SerializationContext.d.ts +23 -27
- package/serialization/SerializationContext.js +105 -105
- package/types/ArrayType.d.ts +8 -8
- package/types/ArrayType.js +33 -33
- package/types/BigIntType.d.ts +10 -17
- package/types/BigIntType.js +37 -37
- package/types/BlobType.d.ts +3 -3
- package/types/BlobType.js +13 -13
- package/types/BooleanType.d.ts +4 -4
- package/types/BooleanType.js +12 -12
- package/types/CharacterType.d.ts +2 -2
- package/types/CharacterType.js +6 -6
- package/types/DateTimeType.d.ts +5 -5
- package/types/DateTimeType.js +15 -15
- package/types/DateType.d.ts +5 -5
- package/types/DateType.js +15 -15
- package/types/DecimalType.d.ts +7 -7
- package/types/DecimalType.js +26 -26
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +12 -12
- package/types/EnumArrayType.d.ts +5 -5
- package/types/EnumArrayType.js +24 -24
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +11 -11
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +9 -9
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +9 -9
- package/types/IntervalType.d.ts +4 -4
- package/types/IntervalType.js +12 -12
- package/types/JsonType.d.ts +8 -8
- package/types/JsonType.js +32 -32
- package/types/MediumIntType.d.ts +1 -1
- package/types/MediumIntType.js +3 -3
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +9 -9
- package/types/StringType.d.ts +4 -4
- package/types/StringType.js +12 -12
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +9 -9
- package/types/TimeType.d.ts +5 -5
- package/types/TimeType.js +17 -17
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +10 -10
- package/types/Type.d.ts +79 -83
- package/types/Type.js +82 -82
- package/types/Uint8ArrayType.d.ts +4 -4
- package/types/Uint8ArrayType.js +21 -21
- package/types/UnknownType.d.ts +4 -4
- package/types/UnknownType.js +12 -12
- package/types/UuidType.d.ts +5 -5
- package/types/UuidType.js +19 -19
- package/types/index.d.ts +49 -75
- package/types/index.js +26 -52
- package/typings.d.ts +741 -1254
- package/typings.js +233 -244
- package/unit-of-work/ChangeSet.d.ts +26 -26
- package/unit-of-work/ChangeSet.js +56 -56
- package/unit-of-work/ChangeSetComputer.d.ts +12 -12
- package/unit-of-work/ChangeSetComputer.js +179 -187
- package/unit-of-work/ChangeSetPersister.d.ts +50 -69
- package/unit-of-work/ChangeSetPersister.js +442 -465
- package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
- package/unit-of-work/CommitOrderCalculator.js +88 -89
- package/unit-of-work/IdentityMap.d.ts +31 -31
- package/unit-of-work/IdentityMap.js +105 -105
- package/unit-of-work/UnitOfWork.d.ts +141 -181
- package/unit-of-work/UnitOfWork.js +1223 -1237
- package/utils/AbstractMigrator.d.ts +91 -111
- package/utils/AbstractMigrator.js +275 -275
- package/utils/AbstractSchemaGenerator.d.ts +34 -43
- package/utils/AbstractSchemaGenerator.js +122 -121
- package/utils/AsyncContext.d.ts +3 -3
- package/utils/AsyncContext.js +35 -34
- package/utils/Configuration.d.ts +808 -852
- package/utils/Configuration.js +344 -359
- package/utils/Cursor.d.ts +22 -40
- package/utils/Cursor.js +127 -135
- package/utils/DataloaderUtils.d.ts +43 -58
- package/utils/DataloaderUtils.js +198 -203
- package/utils/EntityComparator.d.ts +82 -99
- package/utils/EntityComparator.js +737 -829
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +3 -3
- package/utils/QueryHelper.d.ts +51 -79
- package/utils/QueryHelper.js +364 -375
- package/utils/RawQueryFragment.d.ts +34 -50
- package/utils/RawQueryFragment.js +121 -122
- package/utils/RequestContext.d.ts +32 -32
- package/utils/RequestContext.js +53 -52
- package/utils/TransactionContext.d.ts +16 -16
- package/utils/TransactionContext.js +27 -27
- package/utils/TransactionManager.d.ts +58 -58
- package/utils/TransactionManager.js +200 -202
- package/utils/Utils.d.ts +145 -204
- package/utils/Utils.js +826 -826
- package/utils/clone.js +114 -105
- package/utils/env-vars.js +88 -90
- package/utils/fs-utils.d.ts +15 -15
- package/utils/fs-utils.js +182 -181
- package/utils/upsert-utils.d.ts +5 -20
- package/utils/upsert-utils.js +116 -114
package/entity/EntityHelper.js
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EagerProps,
|
|
3
|
-
EntityName,
|
|
4
|
-
EntityRepositoryType,
|
|
5
|
-
HiddenProps,
|
|
6
|
-
OptionalProps,
|
|
7
|
-
PrimaryKeyProp,
|
|
8
|
-
} from '../typings.js';
|
|
1
|
+
import { EagerProps, EntityName, EntityRepositoryType, HiddenProps, OptionalProps, PrimaryKeyProp, } from '../typings.js';
|
|
9
2
|
import { EntityTransformer } from '../serialization/EntityTransformer.js';
|
|
10
3
|
import { Reference } from './Reference.js';
|
|
11
4
|
import { Utils } from '../utils/Utils.js';
|
|
@@ -24,319 +17,314 @@ const entitySymbol = Symbol.for('@mikro-orm/core/EntityHelper.entity');
|
|
|
24
17
|
* @internal
|
|
25
18
|
*/
|
|
26
19
|
export class EntityHelper {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
static decorate(meta, em) {
|
|
31
|
-
const fork = em.fork(); // use fork so we can access `EntityFactory`
|
|
32
|
-
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
33
|
-
if (serializedPrimaryKey) {
|
|
34
|
-
Object.defineProperty(meta.prototype, serializedPrimaryKey.name, {
|
|
35
|
-
get() {
|
|
36
|
-
return this._id ? em.getPlatform().normalizePrimaryKey(this._id) : null;
|
|
37
|
-
},
|
|
38
|
-
set(id) {
|
|
39
|
-
this._id = id ? em.getPlatform().denormalizePrimaryKey(id) : null;
|
|
40
|
-
},
|
|
41
|
-
configurable: true,
|
|
42
|
-
});
|
|
20
|
+
static isEntity(data) {
|
|
21
|
+
return data != null && typeof data === 'object' && !!data[entitySymbol];
|
|
43
22
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
23
|
+
static decorate(meta, em) {
|
|
24
|
+
const fork = em.fork(); // use fork so we can access `EntityFactory`
|
|
25
|
+
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
26
|
+
if (serializedPrimaryKey) {
|
|
27
|
+
Object.defineProperty(meta.prototype, serializedPrimaryKey.name, {
|
|
28
|
+
get() {
|
|
29
|
+
return this._id ? em.getPlatform().normalizePrimaryKey(this._id) : null;
|
|
30
|
+
},
|
|
31
|
+
set(id) {
|
|
32
|
+
this._id = id ? em.getPlatform().denormalizePrimaryKey(id) : null;
|
|
33
|
+
},
|
|
34
|
+
configurable: true,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
EntityHelper.defineBaseProperties(meta, meta.prototype, fork);
|
|
38
|
+
EntityHelper.defineCustomInspect(meta);
|
|
39
|
+
if (em.config.get('propagationOnPrototype') && !meta.embeddable && !meta.virtual) {
|
|
40
|
+
EntityHelper.defineProperties(meta, fork);
|
|
41
|
+
}
|
|
42
|
+
const prototype = meta.prototype;
|
|
43
|
+
if (!prototype.toJSON) {
|
|
44
|
+
// toJSON can be overridden
|
|
45
|
+
Object.defineProperty(prototype, 'toJSON', {
|
|
46
|
+
value: function (...args) {
|
|
47
|
+
return EntityTransformer.toObject(this, ...args);
|
|
48
|
+
},
|
|
49
|
+
writable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
enumerable: false,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// Walkers / serializers reaching the prototype directly invoke its methods and
|
|
55
|
+
// accessors with `this === prototype`. Wrap each so that case is a no-op rather
|
|
56
|
+
// than throwing (when a user `@Property({ persist: false })` getter dereferences
|
|
57
|
+
// unhydrated instance state) or installing state on the prototype itself (#7151).
|
|
58
|
+
for (const name of Object.getOwnPropertyNames(prototype)) {
|
|
59
|
+
const desc = Object.getOwnPropertyDescriptor(prototype, name);
|
|
60
|
+
const fn = desc.get ?? desc.value;
|
|
61
|
+
if (name === 'constructor' || typeof fn !== 'function' || fn.__guarded) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const guarded = function (...args) {
|
|
65
|
+
return this === prototype ? undefined : fn.apply(this, args);
|
|
66
|
+
};
|
|
67
|
+
guarded.__guarded = true;
|
|
68
|
+
Object.defineProperty(prototype, name, desc.get ? { ...desc, get: guarded } : { ...desc, value: guarded });
|
|
69
|
+
}
|
|
60
70
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
71
|
+
/**
|
|
72
|
+
* As a performance optimization, we create entity state methods lazily. We first add
|
|
73
|
+
* the `null` value to the prototype to reserve space in memory. Then we define a setter on the
|
|
74
|
+
* prototype that will be executed exactly once per entity instance. There we redefine the given
|
|
75
|
+
* property on the entity instance, so shadowing the prototype setter.
|
|
76
|
+
*/
|
|
77
|
+
static defineBaseProperties(meta, prototype, em) {
|
|
78
|
+
// oxfmt-ignore
|
|
79
|
+
const helperParams = meta.embeddable || meta.virtual ? [] : [em.getComparator().getPkGetter(meta), em.getComparator().getPkSerializer(meta), em.getComparator().getPkGetterConverted(meta)];
|
|
80
|
+
Object.defineProperties(prototype, {
|
|
81
|
+
[entitySymbol]: { value: !meta.embeddable, enumerable: false, configurable: true },
|
|
82
|
+
__meta: { value: meta, configurable: true },
|
|
83
|
+
__config: { value: em.config, configurable: true },
|
|
84
|
+
__platform: { value: em.getPlatform(), configurable: true },
|
|
85
|
+
__factory: { value: em.getEntityFactory(), configurable: true },
|
|
86
|
+
__helper: {
|
|
87
|
+
get() {
|
|
88
|
+
Object.defineProperty(this, '__helper', {
|
|
89
|
+
value: new WrappedEntity(this, em.getHydrator(), ...helperParams),
|
|
90
|
+
enumerable: false,
|
|
91
|
+
configurable: true,
|
|
92
|
+
});
|
|
93
|
+
return this.__helper;
|
|
94
|
+
},
|
|
95
|
+
configurable: true, // otherwise jest fails when trying to compare entities ¯\_(ツ)_/¯
|
|
96
|
+
},
|
|
97
|
+
});
|
|
76
98
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (isReference) {
|
|
120
|
-
Object.defineProperty(meta.prototype, prop.name, {
|
|
121
|
-
set(val) {
|
|
122
|
-
EntityHelper.defineReferenceProperty(meta, prop, this, em.getHydrator());
|
|
123
|
-
this[prop.name] = val;
|
|
124
|
-
},
|
|
125
|
-
configurable: true,
|
|
99
|
+
/**
|
|
100
|
+
* Defines getter and setter for every owning side of m:1 and 1:1 relation. This is then used for propagation of
|
|
101
|
+
* changes to the inverse side of bi-directional relations. Rest of the properties are also defined this way to
|
|
102
|
+
* achieve dirtiness, which is then used for fast checks whether we need to auto-flush because of managed entities.
|
|
103
|
+
*
|
|
104
|
+
* First defines a setter on the prototype, once called, actual get/set handlers are registered on the instance rather
|
|
105
|
+
* than on its prototype. Thanks to this we still have those properties enumerable (e.g. part of `Object.keys(entity)`).
|
|
106
|
+
*/
|
|
107
|
+
static defineProperties(meta, em) {
|
|
108
|
+
Object.values(meta.properties).forEach(prop => {
|
|
109
|
+
const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
|
|
110
|
+
// oxfmt-ignore
|
|
111
|
+
const isReference = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
|
|
112
|
+
if (isReference) {
|
|
113
|
+
Object.defineProperty(meta.prototype, prop.name, {
|
|
114
|
+
set(val) {
|
|
115
|
+
EntityHelper.defineReferenceProperty(meta, prop, this, em.getHydrator());
|
|
116
|
+
this[prop.name] = val;
|
|
117
|
+
},
|
|
118
|
+
configurable: true,
|
|
119
|
+
});
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (prop.inherited || prop.primary || prop.accessor || prop.persist === false || prop.embedded || isCollection) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
Object.defineProperty(meta.prototype, prop.name, {
|
|
126
|
+
set(val) {
|
|
127
|
+
Object.defineProperty(this, prop.name, {
|
|
128
|
+
get() {
|
|
129
|
+
return this.__helper?.__data[prop.name];
|
|
130
|
+
},
|
|
131
|
+
set(val) {
|
|
132
|
+
this.__helper.__data[prop.name] = val;
|
|
133
|
+
},
|
|
134
|
+
enumerable: true,
|
|
135
|
+
configurable: true,
|
|
136
|
+
});
|
|
137
|
+
this.__helper.__data[prop.name] = val;
|
|
138
|
+
},
|
|
139
|
+
configurable: true,
|
|
140
|
+
});
|
|
126
141
|
});
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
142
|
+
}
|
|
143
|
+
static defineCustomInspect(meta) {
|
|
144
|
+
// @ts-ignore
|
|
145
|
+
meta.prototype[Symbol.for('nodejs.util.inspect.custom')] ??= function (depth = 2) {
|
|
146
|
+
const object = {};
|
|
147
|
+
const keys = new Set(Utils.keys(this));
|
|
148
|
+
for (const prop of meta.props) {
|
|
149
|
+
if (keys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
|
|
150
|
+
object[prop.name] = this[prop.name];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
for (const key of keys) {
|
|
154
|
+
if (!meta.properties[key]) {
|
|
155
|
+
object[key] = this[key];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// ensure we dont have internal symbols in the POJO
|
|
159
|
+
[OptionalProps, EntityRepositoryType, PrimaryKeyProp, EagerProps, HiddenProps, EntityName].forEach(sym => delete object[sym]);
|
|
160
|
+
meta.props.filter(prop => object[prop.name] === undefined).forEach(prop => delete object[prop.name]);
|
|
161
|
+
const ret = inspect(object, { depth });
|
|
162
|
+
let name = this.constructor.name;
|
|
163
|
+
const showEM = ['true', 't', '1'].includes(getEnv('MIKRO_ORM_LOG_EM_ID')?.toLowerCase() ?? '');
|
|
164
|
+
if (showEM) {
|
|
165
|
+
if (helper(this).__em) {
|
|
166
|
+
name += ` [managed by ${helper(this).__em.id}]`;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
name += ` [not managed]`;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// distinguish not initialized entities
|
|
173
|
+
if (!helper(this).__initialized) {
|
|
174
|
+
name = `(${name})`;
|
|
175
|
+
}
|
|
176
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
static defineReferenceProperty(meta, prop, ref, hydrator) {
|
|
180
|
+
const wrapped = helper(ref);
|
|
181
|
+
Object.defineProperty(ref, prop.name, {
|
|
135
182
|
get() {
|
|
136
|
-
|
|
183
|
+
return helper(ref).__data[prop.name];
|
|
137
184
|
},
|
|
138
185
|
set(val) {
|
|
139
|
-
|
|
186
|
+
const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
|
|
187
|
+
const old = Reference.unwrapReference(wrapped.__data[prop.name]);
|
|
188
|
+
// oxfmt-ignore
|
|
189
|
+
if (old && old !== entity && prop.kind === ReferenceKind.MANY_TO_ONE && prop.inversedBy && old[prop.inversedBy]) {
|
|
190
|
+
old[prop.inversedBy].removeWithoutPropagation(this);
|
|
191
|
+
}
|
|
192
|
+
wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
|
|
193
|
+
// when propagation from inside hydration, we set the FK to the entity data immediately
|
|
194
|
+
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
|
|
195
|
+
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta, true);
|
|
196
|
+
}
|
|
197
|
+
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
|
|
140
198
|
},
|
|
141
199
|
enumerable: true,
|
|
142
200
|
configurable: true,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const keys = new Set(Utils.keys(this));
|
|
155
|
-
for (const prop of meta.props) {
|
|
156
|
-
if (keys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
|
|
157
|
-
object[prop.name] = this[prop.name];
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
for (const key of keys) {
|
|
161
|
-
if (!meta.properties[key]) {
|
|
162
|
-
object[key] = this[key];
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
static propagate(meta, entity, owner, prop, value, old) {
|
|
204
|
+
// For polymorphic relations, get bidirectional relations from the actual entity's metadata
|
|
205
|
+
let bidirectionalRelations;
|
|
206
|
+
if (prop.polymorphic && prop.polymorphTargets?.length) {
|
|
207
|
+
// For polymorphic relations, we need to get the bidirectional relations from the actual value's metadata
|
|
208
|
+
if (!value) {
|
|
209
|
+
return; // No value means no propagation needed
|
|
210
|
+
}
|
|
211
|
+
bidirectionalRelations = helper(value).__meta.bidirectionalRelations;
|
|
163
212
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
[OptionalProps, EntityRepositoryType, PrimaryKeyProp, EagerProps, HiddenProps, EntityName].forEach(
|
|
167
|
-
sym => delete object[sym],
|
|
168
|
-
);
|
|
169
|
-
meta.props.filter(prop => object[prop.name] === undefined).forEach(prop => delete object[prop.name]);
|
|
170
|
-
const ret = inspect(object, { depth });
|
|
171
|
-
let name = this.constructor.name;
|
|
172
|
-
const showEM = ['true', 't', '1'].includes(getEnv('MIKRO_ORM_LOG_EM_ID')?.toLowerCase() ?? '');
|
|
173
|
-
if (showEM) {
|
|
174
|
-
if (helper(this).__em) {
|
|
175
|
-
name += ` [managed by ${helper(this).__em.id}]`;
|
|
176
|
-
} else {
|
|
177
|
-
name += ` [not managed]`;
|
|
213
|
+
else {
|
|
214
|
+
bidirectionalRelations = prop.targetMeta.bidirectionalRelations;
|
|
178
215
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
216
|
+
for (const prop2 of bidirectionalRelations) {
|
|
217
|
+
if ((prop2.inversedBy || prop2.mappedBy) !== prop.name) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
// oxfmt-ignore
|
|
221
|
+
if (prop2.targetMeta.abstract ? prop2.targetMeta.root.class !== meta.root.class : prop2.targetMeta.class !== meta.class) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const inverse = value?.[prop2.name];
|
|
225
|
+
if (prop.ref && owner[prop.name]) {
|
|
226
|
+
// eslint-disable-next-line dot-notation
|
|
227
|
+
owner[prop.name]['property'] = prop;
|
|
228
|
+
}
|
|
229
|
+
if (Utils.isCollection(inverse) && inverse.isPartial()) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE && Utils.isCollection(inverse) && inverse.isInitialized()) {
|
|
233
|
+
inverse.addWithoutPropagation(owner);
|
|
234
|
+
helper(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
|
|
235
|
+
}
|
|
236
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
237
|
+
if ((value != null && Reference.unwrapReference(inverse) !== owner) ||
|
|
238
|
+
(value == null && entity?.[prop2.name] != null)) {
|
|
239
|
+
if (entity && (!prop.owner || helper(entity).__initialized)) {
|
|
240
|
+
EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
|
|
241
|
+
}
|
|
199
242
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
243
|
+
else if (old && old !== value) {
|
|
244
|
+
// Inverse already points to owner — propagation is not needed,
|
|
245
|
+
// but we still need to clean up old's inverse side.
|
|
246
|
+
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
247
|
+
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
248
|
+
if (old[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
249
|
+
delete helper(old).__data[prop2.name];
|
|
250
|
+
old[prop2.name] = null;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (old && old !== value && prop.orphanRemoval) {
|
|
254
|
+
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
255
|
+
helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
208
258
|
}
|
|
209
|
-
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
|
|
210
|
-
},
|
|
211
|
-
enumerable: true,
|
|
212
|
-
configurable: true,
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
static propagate(meta, entity, owner, prop, value, old) {
|
|
216
|
-
// For polymorphic relations, get bidirectional relations from the actual entity's metadata
|
|
217
|
-
let bidirectionalRelations;
|
|
218
|
-
if (prop.polymorphic && prop.polymorphTargets?.length) {
|
|
219
|
-
// For polymorphic relations, we need to get the bidirectional relations from the actual value's metadata
|
|
220
|
-
if (!value) {
|
|
221
|
-
return; // No value means no propagation needed
|
|
222
|
-
}
|
|
223
|
-
bidirectionalRelations = helper(value).__meta.bidirectionalRelations;
|
|
224
|
-
} else {
|
|
225
|
-
bidirectionalRelations = prop.targetMeta.bidirectionalRelations;
|
|
226
259
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
260
|
+
static propagateOneToOne(entity, owner, prop, prop2, value, old) {
|
|
261
|
+
helper(entity).__pk = helper(entity).getPrimaryKey();
|
|
262
|
+
// the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
|
|
263
|
+
if (!prop.primary &&
|
|
264
|
+
!prop2.mapToPk &&
|
|
265
|
+
value?.[prop2.name] != null &&
|
|
266
|
+
Reference.unwrapReference(value[prop2.name]) !== entity) {
|
|
267
|
+
const other = Reference.unwrapReference(value[prop2.name]);
|
|
268
|
+
delete helper(other).__data[prop.name];
|
|
269
|
+
if (prop2.orphanRemoval) {
|
|
270
|
+
helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
|
|
234
271
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
//
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
) {
|
|
252
|
-
if (entity && (!prop.owner || helper(entity).__initialized)) {
|
|
253
|
-
EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
|
|
254
|
-
}
|
|
255
|
-
} else if (old && old !== value) {
|
|
256
|
-
// Inverse already points to owner — propagation is not needed,
|
|
257
|
-
// but we still need to clean up old's inverse side.
|
|
258
|
-
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
259
|
-
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
260
|
-
if (old[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
272
|
+
}
|
|
273
|
+
// Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
|
|
274
|
+
// Setting a primary key to null would corrupt the entity and cause validation errors
|
|
275
|
+
if (value == null && prop.orphanRemoval && prop2.primary) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (value == null) {
|
|
279
|
+
entity[prop2.name] = value;
|
|
280
|
+
}
|
|
281
|
+
else if (prop2.mapToPk) {
|
|
282
|
+
entity[prop2.name] = helper(owner).getPrimaryKey();
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
entity[prop2.name] = Reference.wrapReference(owner, prop);
|
|
286
|
+
}
|
|
287
|
+
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
288
|
+
if (old?.[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
261
289
|
delete helper(old).__data[prop2.name];
|
|
262
290
|
old[prop2.name] = null;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
if (old && old !== value && prop.orphanRemoval) {
|
|
266
|
-
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
267
|
-
helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
|
|
268
291
|
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
static propagateOneToOne(entity, owner, prop, prop2, value, old) {
|
|
273
|
-
helper(entity).__pk = helper(entity).getPrimaryKey();
|
|
274
|
-
// the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
|
|
275
|
-
if (
|
|
276
|
-
!prop.primary &&
|
|
277
|
-
!prop2.mapToPk &&
|
|
278
|
-
value?.[prop2.name] != null &&
|
|
279
|
-
Reference.unwrapReference(value[prop2.name]) !== entity
|
|
280
|
-
) {
|
|
281
|
-
const other = Reference.unwrapReference(value[prop2.name]);
|
|
282
|
-
delete helper(other).__data[prop.name];
|
|
283
|
-
if (prop2.orphanRemoval) {
|
|
284
|
-
helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
// Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
|
|
288
|
-
// Setting a primary key to null would corrupt the entity and cause validation errors
|
|
289
|
-
if (value == null && prop.orphanRemoval && prop2.primary) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
if (value == null) {
|
|
293
|
-
entity[prop2.name] = value;
|
|
294
|
-
} else if (prop2.mapToPk) {
|
|
295
|
-
entity[prop2.name] = helper(owner).getPrimaryKey();
|
|
296
|
-
} else {
|
|
297
|
-
entity[prop2.name] = Reference.wrapReference(owner, prop);
|
|
298
|
-
}
|
|
299
|
-
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
300
|
-
if (old?.[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
301
|
-
delete helper(old).__data[prop2.name];
|
|
302
|
-
old[prop2.name] = null;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
static ensurePropagation(entity) {
|
|
306
|
-
if (entity.__gettersDefined) {
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
const wrapped = helper(entity);
|
|
310
|
-
const meta = wrapped.__meta;
|
|
311
|
-
const platform = wrapped.__platform;
|
|
312
|
-
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
313
|
-
const values = [];
|
|
314
|
-
if (serializedPrimaryKey) {
|
|
315
|
-
const pk = meta.getPrimaryProps()[0];
|
|
316
|
-
const val = entity[serializedPrimaryKey.name];
|
|
317
|
-
delete entity[serializedPrimaryKey.name];
|
|
318
|
-
Object.defineProperty(entity, serializedPrimaryKey.name, {
|
|
319
|
-
get() {
|
|
320
|
-
return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
|
|
321
|
-
},
|
|
322
|
-
set(id) {
|
|
323
|
-
this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
|
|
324
|
-
},
|
|
325
|
-
configurable: true,
|
|
326
|
-
});
|
|
327
|
-
if (entity[pk.name] == null && val != null) {
|
|
328
|
-
values.push(serializedPrimaryKey.name, val);
|
|
329
|
-
}
|
|
330
292
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
293
|
+
static ensurePropagation(entity) {
|
|
294
|
+
if (entity.__gettersDefined) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const wrapped = helper(entity);
|
|
298
|
+
const meta = wrapped.__meta;
|
|
299
|
+
const platform = wrapped.__platform;
|
|
300
|
+
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
301
|
+
const values = [];
|
|
302
|
+
if (serializedPrimaryKey) {
|
|
303
|
+
const pk = meta.getPrimaryProps()[0];
|
|
304
|
+
const val = entity[serializedPrimaryKey.name];
|
|
305
|
+
delete entity[serializedPrimaryKey.name];
|
|
306
|
+
Object.defineProperty(entity, serializedPrimaryKey.name, {
|
|
307
|
+
get() {
|
|
308
|
+
return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
|
|
309
|
+
},
|
|
310
|
+
set(id) {
|
|
311
|
+
this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
|
|
312
|
+
},
|
|
313
|
+
configurable: true,
|
|
314
|
+
});
|
|
315
|
+
if (entity[pk.name] == null && val != null) {
|
|
316
|
+
values.push(serializedPrimaryKey.name, val);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
for (const prop of meta.trackingProps) {
|
|
320
|
+
if (entity[prop.name] !== undefined) {
|
|
321
|
+
values.push(prop.name, entity[prop.name]);
|
|
322
|
+
}
|
|
323
|
+
delete entity[prop.name];
|
|
324
|
+
}
|
|
325
|
+
Object.defineProperties(entity, meta.definedProperties);
|
|
326
|
+
for (let i = 0; i < values.length; i += 2) {
|
|
327
|
+
entity[values[i]] = values[i + 1];
|
|
328
|
+
}
|
|
340
329
|
}
|
|
341
|
-
}
|
|
342
330
|
}
|
|
@@ -3,8 +3,8 @@ import type { IPrimaryKey } from '../typings.js';
|
|
|
3
3
|
* @internal
|
|
4
4
|
*/
|
|
5
5
|
export declare class EntityIdentifier {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
private value?;
|
|
7
|
+
constructor(value?: IPrimaryKey | undefined);
|
|
8
|
+
setValue(value: IPrimaryKey): void;
|
|
9
|
+
getValue<T extends IPrimaryKey = IPrimaryKey>(): T;
|
|
10
10
|
}
|