@mikro-orm/core 7.0.8 → 7.0.9-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 +583 -884
- package/EntityManager.js +1899 -1926
- package/MikroORM.d.ts +74 -103
- package/MikroORM.js +179 -178
- 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 -213
- 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 +414 -455
- package/entity/EntityHelper.d.ts +23 -35
- package/entity/EntityHelper.js +279 -291
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +72 -98
- package/entity/EntityLoader.js +761 -792
- 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 +1868 -1944
- 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 +644 -667
- 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 +743 -1254
- package/typings.js +235 -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 +44 -63
- package/unit-of-work/ChangeSetPersister.js +423 -446
- 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 +1222 -1236
- 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 +361 -372
- package/utils/RawQueryFragment.d.ts +34 -50
- package/utils/RawQueryFragment.js +105 -107
- 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 +197 -199
- package/utils/Utils.d.ts +145 -204
- package/utils/Utils.js +815 -815
- 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 +181 -180
- 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';
|
|
@@ -19,308 +12,303 @@ const entitySymbol = Symbol('Entity');
|
|
|
19
12
|
* @internal
|
|
20
13
|
*/
|
|
21
14
|
export class EntityHelper {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
static decorate(meta, em) {
|
|
26
|
-
const fork = em.fork(); // use fork so we can access `EntityFactory`
|
|
27
|
-
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
28
|
-
if (serializedPrimaryKey) {
|
|
29
|
-
Object.defineProperty(meta.prototype, serializedPrimaryKey.name, {
|
|
30
|
-
get() {
|
|
31
|
-
return this._id ? em.getPlatform().normalizePrimaryKey(this._id) : null;
|
|
32
|
-
},
|
|
33
|
-
set(id) {
|
|
34
|
-
this._id = id ? em.getPlatform().denormalizePrimaryKey(id) : null;
|
|
35
|
-
},
|
|
36
|
-
configurable: true,
|
|
37
|
-
});
|
|
15
|
+
static isEntity(data) {
|
|
16
|
+
return data != null && typeof data === 'object' && !!data[entitySymbol];
|
|
38
17
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
18
|
+
static decorate(meta, em) {
|
|
19
|
+
const fork = em.fork(); // use fork so we can access `EntityFactory`
|
|
20
|
+
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
21
|
+
if (serializedPrimaryKey) {
|
|
22
|
+
Object.defineProperty(meta.prototype, serializedPrimaryKey.name, {
|
|
23
|
+
get() {
|
|
24
|
+
return this._id ? em.getPlatform().normalizePrimaryKey(this._id) : null;
|
|
25
|
+
},
|
|
26
|
+
set(id) {
|
|
27
|
+
this._id = id ? em.getPlatform().denormalizePrimaryKey(id) : null;
|
|
28
|
+
},
|
|
29
|
+
configurable: true,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
EntityHelper.defineBaseProperties(meta, meta.prototype, fork);
|
|
33
|
+
EntityHelper.defineCustomInspect(meta);
|
|
34
|
+
if (em.config.get('propagationOnPrototype') && !meta.embeddable && !meta.virtual) {
|
|
35
|
+
EntityHelper.defineProperties(meta, fork);
|
|
36
|
+
}
|
|
37
|
+
const prototype = meta.prototype;
|
|
38
|
+
if (!prototype.toJSON) {
|
|
39
|
+
// toJSON can be overridden
|
|
40
|
+
Object.defineProperty(prototype, 'toJSON', {
|
|
41
|
+
value: function (...args) {
|
|
42
|
+
// Guard against being called on the prototype itself (e.g. by serializers
|
|
43
|
+
// walking the object graph and calling toJSON on prototype objects)
|
|
44
|
+
if (this === prototype) {
|
|
45
|
+
return {};
|
|
46
|
+
}
|
|
47
|
+
return EntityTransformer.toObject(this, ...args);
|
|
48
|
+
},
|
|
49
|
+
writable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
enumerable: false,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
43
54
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
/**
|
|
56
|
+
* As a performance optimization, we create entity state methods lazily. We first add
|
|
57
|
+
* the `null` value to the prototype to reserve space in memory. Then we define a setter on the
|
|
58
|
+
* prototype that will be executed exactly once per entity instance. There we redefine the given
|
|
59
|
+
* property on the entity instance, so shadowing the prototype setter.
|
|
60
|
+
*/
|
|
61
|
+
static defineBaseProperties(meta, prototype, em) {
|
|
62
|
+
// oxfmt-ignore
|
|
63
|
+
const helperParams = meta.embeddable || meta.virtual ? [] : [em.getComparator().getPkGetter(meta), em.getComparator().getPkSerializer(meta), em.getComparator().getPkGetterConverted(meta)];
|
|
64
|
+
Object.defineProperties(prototype, {
|
|
65
|
+
[entitySymbol]: { value: !meta.embeddable, enumerable: false, configurable: true },
|
|
66
|
+
__meta: { value: meta, configurable: true },
|
|
67
|
+
__config: { value: em.config, configurable: true },
|
|
68
|
+
__platform: { value: em.getPlatform(), configurable: true },
|
|
69
|
+
__factory: { value: em.getEntityFactory(), configurable: true },
|
|
70
|
+
__helper: {
|
|
71
|
+
get() {
|
|
72
|
+
Object.defineProperty(this, '__helper', {
|
|
73
|
+
value: new WrappedEntity(this, em.getHydrator(), ...helperParams),
|
|
74
|
+
enumerable: false,
|
|
75
|
+
configurable: true,
|
|
76
|
+
});
|
|
77
|
+
return this.__helper;
|
|
78
|
+
},
|
|
79
|
+
configurable: true, // otherwise jest fails when trying to compare entities ¯\_(ツ)_/¯
|
|
80
|
+
},
|
|
81
|
+
});
|
|
60
82
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
if (isReference) {
|
|
104
|
-
Object.defineProperty(meta.prototype, prop.name, {
|
|
105
|
-
set(val) {
|
|
106
|
-
EntityHelper.defineReferenceProperty(meta, prop, this, em.getHydrator());
|
|
107
|
-
this[prop.name] = val;
|
|
108
|
-
},
|
|
109
|
-
configurable: true,
|
|
83
|
+
/**
|
|
84
|
+
* Defines getter and setter for every owning side of m:1 and 1:1 relation. This is then used for propagation of
|
|
85
|
+
* changes to the inverse side of bi-directional relations. Rest of the properties are also defined this way to
|
|
86
|
+
* achieve dirtiness, which is then used for fast checks whether we need to auto-flush because of managed entities.
|
|
87
|
+
*
|
|
88
|
+
* First defines a setter on the prototype, once called, actual get/set handlers are registered on the instance rather
|
|
89
|
+
* than on its prototype. Thanks to this we still have those properties enumerable (e.g. part of `Object.keys(entity)`).
|
|
90
|
+
*/
|
|
91
|
+
static defineProperties(meta, em) {
|
|
92
|
+
Object.values(meta.properties).forEach(prop => {
|
|
93
|
+
const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
|
|
94
|
+
// oxfmt-ignore
|
|
95
|
+
const isReference = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
|
|
96
|
+
if (isReference) {
|
|
97
|
+
Object.defineProperty(meta.prototype, prop.name, {
|
|
98
|
+
set(val) {
|
|
99
|
+
EntityHelper.defineReferenceProperty(meta, prop, this, em.getHydrator());
|
|
100
|
+
this[prop.name] = val;
|
|
101
|
+
},
|
|
102
|
+
configurable: true,
|
|
103
|
+
});
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (prop.inherited || prop.primary || prop.accessor || prop.persist === false || prop.embedded || isCollection) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
Object.defineProperty(meta.prototype, prop.name, {
|
|
110
|
+
set(val) {
|
|
111
|
+
Object.defineProperty(this, prop.name, {
|
|
112
|
+
get() {
|
|
113
|
+
return this.__helper?.__data[prop.name];
|
|
114
|
+
},
|
|
115
|
+
set(val) {
|
|
116
|
+
this.__helper.__data[prop.name] = val;
|
|
117
|
+
},
|
|
118
|
+
enumerable: true,
|
|
119
|
+
configurable: true,
|
|
120
|
+
});
|
|
121
|
+
this.__helper.__data[prop.name] = val;
|
|
122
|
+
},
|
|
123
|
+
configurable: true,
|
|
124
|
+
});
|
|
110
125
|
});
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
126
|
+
}
|
|
127
|
+
static defineCustomInspect(meta) {
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
meta.prototype[Symbol.for('nodejs.util.inspect.custom')] ??= function (depth = 2) {
|
|
130
|
+
const object = {};
|
|
131
|
+
const keys = new Set(Utils.keys(this));
|
|
132
|
+
for (const prop of meta.props) {
|
|
133
|
+
if (keys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
|
|
134
|
+
object[prop.name] = this[prop.name];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const key of keys) {
|
|
138
|
+
if (!meta.properties[key]) {
|
|
139
|
+
object[key] = this[key];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// ensure we dont have internal symbols in the POJO
|
|
143
|
+
[OptionalProps, EntityRepositoryType, PrimaryKeyProp, EagerProps, HiddenProps, EntityName].forEach(sym => delete object[sym]);
|
|
144
|
+
meta.props.filter(prop => object[prop.name] === undefined).forEach(prop => delete object[prop.name]);
|
|
145
|
+
const ret = inspect(object, { depth });
|
|
146
|
+
let name = this.constructor.name;
|
|
147
|
+
const showEM = ['true', 't', '1'].includes(getEnv('MIKRO_ORM_LOG_EM_ID')?.toLowerCase() ?? '');
|
|
148
|
+
if (showEM) {
|
|
149
|
+
if (helper(this).__em) {
|
|
150
|
+
name += ` [managed by ${helper(this).__em.id}]`;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
name += ` [not managed]`;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// distinguish not initialized entities
|
|
157
|
+
if (!helper(this).__initialized) {
|
|
158
|
+
name = `(${name})`;
|
|
159
|
+
}
|
|
160
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
static defineReferenceProperty(meta, prop, ref, hydrator) {
|
|
164
|
+
const wrapped = helper(ref);
|
|
165
|
+
Object.defineProperty(ref, prop.name, {
|
|
119
166
|
get() {
|
|
120
|
-
|
|
167
|
+
return helper(ref).__data[prop.name];
|
|
121
168
|
},
|
|
122
169
|
set(val) {
|
|
123
|
-
|
|
170
|
+
const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
|
|
171
|
+
const old = Reference.unwrapReference(wrapped.__data[prop.name]);
|
|
172
|
+
// oxfmt-ignore
|
|
173
|
+
if (old && old !== entity && prop.kind === ReferenceKind.MANY_TO_ONE && prop.inversedBy && old[prop.inversedBy]) {
|
|
174
|
+
old[prop.inversedBy].removeWithoutPropagation(this);
|
|
175
|
+
}
|
|
176
|
+
wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
|
|
177
|
+
// when propagation from inside hydration, we set the FK to the entity data immediately
|
|
178
|
+
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
|
|
179
|
+
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta, true);
|
|
180
|
+
}
|
|
181
|
+
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
|
|
124
182
|
},
|
|
125
183
|
enumerable: true,
|
|
126
184
|
configurable: true,
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const keys = new Set(Utils.keys(this));
|
|
139
|
-
for (const prop of meta.props) {
|
|
140
|
-
if (keys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
|
|
141
|
-
object[prop.name] = this[prop.name];
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
for (const key of keys) {
|
|
145
|
-
if (!meta.properties[key]) {
|
|
146
|
-
object[key] = this[key];
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
static propagate(meta, entity, owner, prop, value, old) {
|
|
188
|
+
// For polymorphic relations, get bidirectional relations from the actual entity's metadata
|
|
189
|
+
let bidirectionalRelations;
|
|
190
|
+
if (prop.polymorphic && prop.polymorphTargets?.length) {
|
|
191
|
+
// For polymorphic relations, we need to get the bidirectional relations from the actual value's metadata
|
|
192
|
+
if (!value) {
|
|
193
|
+
return; // No value means no propagation needed
|
|
194
|
+
}
|
|
195
|
+
bidirectionalRelations = helper(value).__meta.bidirectionalRelations;
|
|
147
196
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
[OptionalProps, EntityRepositoryType, PrimaryKeyProp, EagerProps, HiddenProps, EntityName].forEach(
|
|
151
|
-
sym => delete object[sym],
|
|
152
|
-
);
|
|
153
|
-
meta.props.filter(prop => object[prop.name] === undefined).forEach(prop => delete object[prop.name]);
|
|
154
|
-
const ret = inspect(object, { depth });
|
|
155
|
-
let name = this.constructor.name;
|
|
156
|
-
const showEM = ['true', 't', '1'].includes(getEnv('MIKRO_ORM_LOG_EM_ID')?.toLowerCase() ?? '');
|
|
157
|
-
if (showEM) {
|
|
158
|
-
if (helper(this).__em) {
|
|
159
|
-
name += ` [managed by ${helper(this).__em.id}]`;
|
|
160
|
-
} else {
|
|
161
|
-
name += ` [not managed]`;
|
|
197
|
+
else {
|
|
198
|
+
bidirectionalRelations = prop.targetMeta.bidirectionalRelations;
|
|
162
199
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
200
|
+
for (const prop2 of bidirectionalRelations) {
|
|
201
|
+
if ((prop2.inversedBy || prop2.mappedBy) !== prop.name) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
// oxfmt-ignore
|
|
205
|
+
if (prop2.targetMeta.abstract ? prop2.targetMeta.root.class !== meta.root.class : prop2.targetMeta.class !== meta.class) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const inverse = value?.[prop2.name];
|
|
209
|
+
if (prop.ref && owner[prop.name]) {
|
|
210
|
+
// eslint-disable-next-line dot-notation
|
|
211
|
+
owner[prop.name]['property'] = prop;
|
|
212
|
+
}
|
|
213
|
+
if (Utils.isCollection(inverse) && inverse.isPartial()) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE && Utils.isCollection(inverse) && inverse.isInitialized()) {
|
|
217
|
+
inverse.addWithoutPropagation(owner);
|
|
218
|
+
helper(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
|
|
219
|
+
}
|
|
220
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
221
|
+
if ((value != null && Reference.unwrapReference(inverse) !== owner) ||
|
|
222
|
+
(value == null && entity?.[prop2.name] != null)) {
|
|
223
|
+
if (entity && (!prop.owner || helper(entity).__initialized)) {
|
|
224
|
+
EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
else if (old && old !== value) {
|
|
228
|
+
// Inverse already points to owner — propagation is not needed,
|
|
229
|
+
// but we still need to clean up old's inverse side.
|
|
230
|
+
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
231
|
+
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
232
|
+
if (old[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
233
|
+
delete helper(old).__data[prop2.name];
|
|
234
|
+
old[prop2.name] = null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (old && old !== value && prop.orphanRemoval) {
|
|
238
|
+
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
239
|
+
helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
|
|
183
240
|
}
|
|
184
|
-
|
|
185
|
-
// when propagation from inside hydration, we set the FK to the entity data immediately
|
|
186
|
-
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
|
|
187
|
-
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(
|
|
188
|
-
wrapped.__data[prop.name],
|
|
189
|
-
prop.targetMeta,
|
|
190
|
-
true,
|
|
191
|
-
);
|
|
241
|
+
}
|
|
192
242
|
}
|
|
193
|
-
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
|
|
194
|
-
},
|
|
195
|
-
enumerable: true,
|
|
196
|
-
configurable: true,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
static propagate(meta, entity, owner, prop, value, old) {
|
|
200
|
-
// For polymorphic relations, get bidirectional relations from the actual entity's metadata
|
|
201
|
-
let bidirectionalRelations;
|
|
202
|
-
if (prop.polymorphic && prop.polymorphTargets?.length) {
|
|
203
|
-
// For polymorphic relations, we need to get the bidirectional relations from the actual value's metadata
|
|
204
|
-
if (!value) {
|
|
205
|
-
return; // No value means no propagation needed
|
|
206
|
-
}
|
|
207
|
-
bidirectionalRelations = helper(value).__meta.bidirectionalRelations;
|
|
208
|
-
} else {
|
|
209
|
-
bidirectionalRelations = prop.targetMeta.bidirectionalRelations;
|
|
210
243
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
244
|
+
static propagateOneToOne(entity, owner, prop, prop2, value, old) {
|
|
245
|
+
helper(entity).__pk = helper(entity).getPrimaryKey();
|
|
246
|
+
// the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
|
|
247
|
+
if (!prop.primary &&
|
|
248
|
+
!prop2.mapToPk &&
|
|
249
|
+
value?.[prop2.name] != null &&
|
|
250
|
+
Reference.unwrapReference(value[prop2.name]) !== entity) {
|
|
251
|
+
const other = Reference.unwrapReference(value[prop2.name]);
|
|
252
|
+
delete helper(other).__data[prop.name];
|
|
253
|
+
if (prop2.orphanRemoval) {
|
|
254
|
+
helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
|
|
218
255
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
//
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
) {
|
|
236
|
-
if (entity && (!prop.owner || helper(entity).__initialized)) {
|
|
237
|
-
EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
|
|
238
|
-
}
|
|
239
|
-
} else if (old && old !== value) {
|
|
240
|
-
// Inverse already points to owner — propagation is not needed,
|
|
241
|
-
// but we still need to clean up old's inverse side.
|
|
242
|
-
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
243
|
-
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
244
|
-
if (old[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
256
|
+
}
|
|
257
|
+
// Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
|
|
258
|
+
// Setting a primary key to null would corrupt the entity and cause validation errors
|
|
259
|
+
if (value == null && prop.orphanRemoval && prop2.primary) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (value == null) {
|
|
263
|
+
entity[prop2.name] = value;
|
|
264
|
+
}
|
|
265
|
+
else if (prop2.mapToPk) {
|
|
266
|
+
entity[prop2.name] = helper(owner).getPrimaryKey();
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
entity[prop2.name] = Reference.wrapReference(owner, prop);
|
|
270
|
+
}
|
|
271
|
+
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
272
|
+
if (old?.[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
245
273
|
delete helper(old).__data[prop2.name];
|
|
246
274
|
old[prop2.name] = null;
|
|
247
|
-
}
|
|
248
275
|
}
|
|
249
|
-
if (old && old !== value && prop.orphanRemoval) {
|
|
250
|
-
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
251
|
-
helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
static propagateOneToOne(entity, owner, prop, prop2, value, old) {
|
|
257
|
-
helper(entity).__pk = helper(entity).getPrimaryKey();
|
|
258
|
-
// the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
|
|
259
|
-
if (
|
|
260
|
-
!prop.primary &&
|
|
261
|
-
!prop2.mapToPk &&
|
|
262
|
-
value?.[prop2.name] != null &&
|
|
263
|
-
Reference.unwrapReference(value[prop2.name]) !== entity
|
|
264
|
-
) {
|
|
265
|
-
const other = Reference.unwrapReference(value[prop2.name]);
|
|
266
|
-
delete helper(other).__data[prop.name];
|
|
267
|
-
if (prop2.orphanRemoval) {
|
|
268
|
-
helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
// Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
|
|
272
|
-
// Setting a primary key to null would corrupt the entity and cause validation errors
|
|
273
|
-
if (value == null && prop.orphanRemoval && prop2.primary) {
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
if (value == null) {
|
|
277
|
-
entity[prop2.name] = value;
|
|
278
|
-
} else if (prop2.mapToPk) {
|
|
279
|
-
entity[prop2.name] = helper(owner).getPrimaryKey();
|
|
280
|
-
} else {
|
|
281
|
-
entity[prop2.name] = Reference.wrapReference(owner, prop);
|
|
282
|
-
}
|
|
283
|
-
// Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
|
|
284
|
-
if (old?.[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
|
|
285
|
-
delete helper(old).__data[prop2.name];
|
|
286
|
-
old[prop2.name] = null;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
static ensurePropagation(entity) {
|
|
290
|
-
if (entity.__gettersDefined) {
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
const wrapped = helper(entity);
|
|
294
|
-
const meta = wrapped.__meta;
|
|
295
|
-
const platform = wrapped.__platform;
|
|
296
|
-
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
297
|
-
const values = [];
|
|
298
|
-
if (serializedPrimaryKey) {
|
|
299
|
-
const pk = meta.getPrimaryProps()[0];
|
|
300
|
-
const val = entity[serializedPrimaryKey.name];
|
|
301
|
-
delete entity[serializedPrimaryKey.name];
|
|
302
|
-
Object.defineProperty(entity, serializedPrimaryKey.name, {
|
|
303
|
-
get() {
|
|
304
|
-
return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
|
|
305
|
-
},
|
|
306
|
-
set(id) {
|
|
307
|
-
this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
|
|
308
|
-
},
|
|
309
|
-
configurable: true,
|
|
310
|
-
});
|
|
311
|
-
if (entity[pk.name] == null && val != null) {
|
|
312
|
-
values.push(serializedPrimaryKey.name, val);
|
|
313
|
-
}
|
|
314
276
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
277
|
+
static ensurePropagation(entity) {
|
|
278
|
+
if (entity.__gettersDefined) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const wrapped = helper(entity);
|
|
282
|
+
const meta = wrapped.__meta;
|
|
283
|
+
const platform = wrapped.__platform;
|
|
284
|
+
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
285
|
+
const values = [];
|
|
286
|
+
if (serializedPrimaryKey) {
|
|
287
|
+
const pk = meta.getPrimaryProps()[0];
|
|
288
|
+
const val = entity[serializedPrimaryKey.name];
|
|
289
|
+
delete entity[serializedPrimaryKey.name];
|
|
290
|
+
Object.defineProperty(entity, serializedPrimaryKey.name, {
|
|
291
|
+
get() {
|
|
292
|
+
return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
|
|
293
|
+
},
|
|
294
|
+
set(id) {
|
|
295
|
+
this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
|
|
296
|
+
},
|
|
297
|
+
configurable: true,
|
|
298
|
+
});
|
|
299
|
+
if (entity[pk.name] == null && val != null) {
|
|
300
|
+
values.push(serializedPrimaryKey.name, val);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
for (const prop of meta.trackingProps) {
|
|
304
|
+
if (entity[prop.name] !== undefined) {
|
|
305
|
+
values.push(prop.name, entity[prop.name]);
|
|
306
|
+
}
|
|
307
|
+
delete entity[prop.name];
|
|
308
|
+
}
|
|
309
|
+
Object.defineProperties(entity, meta.definedProperties);
|
|
310
|
+
for (let i = 0; i < values.length; i += 2) {
|
|
311
|
+
entity[values[i]] = values[i + 1];
|
|
312
|
+
}
|
|
324
313
|
}
|
|
325
|
-
}
|
|
326
314
|
}
|
|
@@ -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
|
}
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
* @internal
|
|
3
3
|
*/
|
|
4
4
|
export class EntityIdentifier {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
value;
|
|
6
|
+
constructor(value) {
|
|
7
|
+
this.value = value;
|
|
8
|
+
}
|
|
9
|
+
setValue(value) {
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
getValue() {
|
|
13
|
+
return this.value;
|
|
14
|
+
}
|
|
15
15
|
}
|