@mikro-orm/core 7.0.8-dev.8 → 7.0.8
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 +884 -583
- package/EntityManager.js +1926 -1899
- package/MikroORM.d.ts +103 -74
- package/MikroORM.js +178 -179
- package/README.md +1 -1
- package/cache/CacheAdapter.d.ts +36 -36
- package/cache/FileCacheAdapter.d.ts +30 -24
- package/cache/FileCacheAdapter.js +80 -78
- package/cache/GeneratedCacheAdapter.d.ts +18 -20
- package/cache/GeneratedCacheAdapter.js +30 -30
- package/cache/MemoryCacheAdapter.d.ts +18 -20
- package/cache/MemoryCacheAdapter.js +35 -36
- package/cache/NullCacheAdapter.d.ts +16 -16
- package/cache/NullCacheAdapter.js +24 -24
- package/connections/Connection.d.ts +95 -84
- package/connections/Connection.js +165 -168
- package/drivers/DatabaseDriver.d.ts +186 -80
- package/drivers/DatabaseDriver.js +450 -443
- package/drivers/IDatabaseDriver.d.ts +440 -301
- package/entity/BaseEntity.d.ts +120 -83
- package/entity/BaseEntity.js +43 -43
- package/entity/Collection.d.ts +213 -180
- package/entity/Collection.js +730 -724
- package/entity/EntityAssigner.d.ts +88 -77
- package/entity/EntityAssigner.js +231 -230
- package/entity/EntityFactory.d.ts +67 -55
- package/entity/EntityFactory.js +455 -414
- package/entity/EntityHelper.d.ts +35 -23
- package/entity/EntityHelper.js +291 -279
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +98 -72
- package/entity/EntityLoader.js +792 -761
- package/entity/EntityRepository.d.ts +316 -201
- package/entity/EntityRepository.js +213 -213
- package/entity/PolymorphicRef.d.ts +5 -5
- package/entity/PolymorphicRef.js +10 -10
- package/entity/Reference.d.ts +127 -83
- package/entity/Reference.js +281 -277
- package/entity/WrappedEntity.d.ts +115 -72
- package/entity/WrappedEntity.js +168 -166
- package/entity/defineEntity.d.ts +1359 -654
- package/entity/defineEntity.js +527 -518
- package/entity/utils.d.ts +13 -3
- package/entity/utils.js +71 -73
- package/entity/validators.js +43 -43
- package/entity/wrap.js +8 -8
- package/enums.d.ts +258 -253
- package/enums.js +251 -252
- package/errors.d.ts +114 -72
- package/errors.js +350 -253
- package/events/EventManager.d.ts +26 -14
- package/events/EventManager.js +79 -77
- package/events/EventSubscriber.d.ts +29 -29
- package/events/TransactionEventBroadcaster.d.ts +15 -8
- package/events/TransactionEventBroadcaster.js +14 -14
- package/exceptions.d.ts +23 -40
- package/exceptions.js +35 -52
- package/hydration/Hydrator.d.ts +42 -17
- package/hydration/Hydrator.js +43 -43
- package/hydration/ObjectHydrator.d.ts +50 -17
- package/hydration/ObjectHydrator.js +483 -418
- package/index.d.ts +116 -2
- package/index.js +10 -1
- package/logging/DefaultLogger.d.ts +34 -32
- package/logging/DefaultLogger.js +86 -86
- package/logging/Logger.d.ts +41 -41
- package/logging/SimpleLogger.d.ts +13 -11
- package/logging/SimpleLogger.js +22 -22
- package/logging/colors.d.ts +6 -6
- package/logging/colors.js +11 -10
- package/logging/inspect.js +7 -7
- package/metadata/EntitySchema.d.ts +214 -130
- package/metadata/EntitySchema.js +411 -412
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1944 -1868
- package/metadata/MetadataProvider.d.ts +29 -26
- package/metadata/MetadataProvider.js +95 -97
- package/metadata/MetadataStorage.d.ts +38 -32
- package/metadata/MetadataStorage.js +118 -118
- package/metadata/MetadataValidator.d.ts +39 -39
- package/metadata/MetadataValidator.js +381 -338
- package/metadata/discover-entities.d.ts +5 -2
- package/metadata/discover-entities.js +35 -37
- package/metadata/types.d.ts +615 -531
- package/naming-strategy/AbstractNamingStrategy.d.ts +54 -39
- package/naming-strategy/AbstractNamingStrategy.js +90 -85
- 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 +109 -99
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
- package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
- package/not-supported.js +7 -4
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -1
- package/platforms/ExceptionConverter.js +4 -4
- package/platforms/Platform.d.ts +312 -303
- package/platforms/Platform.js +667 -644
- package/serialization/EntitySerializer.d.ts +49 -26
- package/serialization/EntitySerializer.js +224 -218
- package/serialization/EntityTransformer.d.ts +10 -6
- package/serialization/EntityTransformer.js +219 -217
- package/serialization/SerializationContext.d.ts +27 -23
- package/serialization/SerializationContext.js +105 -105
- package/types/ArrayType.d.ts +8 -8
- package/types/ArrayType.js +33 -33
- package/types/BigIntType.d.ts +17 -10
- 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 +83 -79
- 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 +75 -49
- package/types/index.js +52 -26
- package/typings.d.ts +1254 -741
- package/typings.js +244 -233
- 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 +187 -179
- package/unit-of-work/ChangeSetPersister.d.ts +63 -44
- package/unit-of-work/ChangeSetPersister.js +446 -423
- package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
- package/unit-of-work/CommitOrderCalculator.js +89 -88
- package/unit-of-work/IdentityMap.d.ts +31 -31
- package/unit-of-work/IdentityMap.js +105 -105
- package/unit-of-work/UnitOfWork.d.ts +181 -141
- package/unit-of-work/UnitOfWork.js +1236 -1222
- package/utils/AbstractMigrator.d.ts +111 -91
- package/utils/AbstractMigrator.js +275 -275
- package/utils/AbstractSchemaGenerator.d.ts +43 -34
- package/utils/AbstractSchemaGenerator.js +121 -122
- package/utils/AsyncContext.d.ts +3 -3
- package/utils/AsyncContext.js +34 -35
- package/utils/Configuration.d.ts +852 -808
- package/utils/Configuration.js +359 -344
- package/utils/Cursor.d.ts +40 -22
- package/utils/Cursor.js +135 -127
- package/utils/DataloaderUtils.d.ts +58 -43
- package/utils/DataloaderUtils.js +203 -198
- package/utils/EntityComparator.d.ts +99 -82
- package/utils/EntityComparator.js +829 -737
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +3 -3
- package/utils/QueryHelper.d.ts +79 -51
- package/utils/QueryHelper.js +372 -361
- package/utils/RawQueryFragment.d.ts +50 -34
- package/utils/RawQueryFragment.js +107 -105
- package/utils/RequestContext.d.ts +32 -32
- package/utils/RequestContext.js +52 -53
- package/utils/TransactionContext.d.ts +16 -16
- package/utils/TransactionContext.js +27 -27
- package/utils/TransactionManager.d.ts +58 -58
- package/utils/TransactionManager.js +199 -197
- package/utils/Utils.d.ts +204 -145
- package/utils/Utils.js +815 -815
- package/utils/clone.js +105 -114
- package/utils/env-vars.js +90 -88
- package/utils/fs-utils.d.ts +15 -15
- package/utils/fs-utils.js +180 -181
- package/utils/upsert-utils.d.ts +20 -5
- package/utils/upsert-utils.js +114 -116
package/entity/EntityHelper.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
EagerProps,
|
|
3
|
+
EntityName,
|
|
4
|
+
EntityRepositoryType,
|
|
5
|
+
HiddenProps,
|
|
6
|
+
OptionalProps,
|
|
7
|
+
PrimaryKeyProp,
|
|
8
|
+
} from '../typings.js';
|
|
2
9
|
import { EntityTransformer } from '../serialization/EntityTransformer.js';
|
|
3
10
|
import { Reference } from './Reference.js';
|
|
4
11
|
import { Utils } from '../utils/Utils.js';
|
|
@@ -12,303 +19,308 @@ const entitySymbol = Symbol('Entity');
|
|
|
12
19
|
* @internal
|
|
13
20
|
*/
|
|
14
21
|
export class EntityHelper {
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
static isEntity(data) {
|
|
23
|
+
return data != null && typeof data === 'object' && !!data[entitySymbol];
|
|
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
|
+
});
|
|
17
38
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
}
|
|
39
|
+
EntityHelper.defineBaseProperties(meta, meta.prototype, fork);
|
|
40
|
+
EntityHelper.defineCustomInspect(meta);
|
|
41
|
+
if (em.config.get('propagationOnPrototype') && !meta.embeddable && !meta.virtual) {
|
|
42
|
+
EntityHelper.defineProperties(meta, fork);
|
|
54
43
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
});
|
|
44
|
+
const prototype = meta.prototype;
|
|
45
|
+
if (!prototype.toJSON) {
|
|
46
|
+
// toJSON can be overridden
|
|
47
|
+
Object.defineProperty(prototype, 'toJSON', {
|
|
48
|
+
value: function (...args) {
|
|
49
|
+
// Guard against being called on the prototype itself (e.g. by serializers
|
|
50
|
+
// walking the object graph and calling toJSON on prototype objects)
|
|
51
|
+
if (this === prototype) {
|
|
52
|
+
return {};
|
|
53
|
+
}
|
|
54
|
+
return EntityTransformer.toObject(this, ...args);
|
|
55
|
+
},
|
|
56
|
+
writable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
enumerable: false,
|
|
59
|
+
});
|
|
82
60
|
}
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* As a performance optimization, we create entity state methods lazily. We first add
|
|
64
|
+
* the `null` value to the prototype to reserve space in memory. Then we define a setter on the
|
|
65
|
+
* prototype that will be executed exactly once per entity instance. There we redefine the given
|
|
66
|
+
* property on the entity instance, so shadowing the prototype setter.
|
|
67
|
+
*/
|
|
68
|
+
static defineBaseProperties(meta, prototype, em) {
|
|
69
|
+
// oxfmt-ignore
|
|
70
|
+
const helperParams = meta.embeddable || meta.virtual ? [] : [em.getComparator().getPkGetter(meta), em.getComparator().getPkSerializer(meta), em.getComparator().getPkGetterConverted(meta)];
|
|
71
|
+
Object.defineProperties(prototype, {
|
|
72
|
+
[entitySymbol]: { value: !meta.embeddable, enumerable: false, configurable: true },
|
|
73
|
+
__meta: { value: meta, configurable: true },
|
|
74
|
+
__config: { value: em.config, configurable: true },
|
|
75
|
+
__platform: { value: em.getPlatform(), configurable: true },
|
|
76
|
+
__factory: { value: em.getEntityFactory(), configurable: true },
|
|
77
|
+
__helper: {
|
|
78
|
+
get() {
|
|
79
|
+
Object.defineProperty(this, '__helper', {
|
|
80
|
+
value: new WrappedEntity(this, em.getHydrator(), ...helperParams),
|
|
81
|
+
enumerable: false,
|
|
82
|
+
configurable: true,
|
|
83
|
+
});
|
|
84
|
+
return this.__helper;
|
|
85
|
+
},
|
|
86
|
+
configurable: true, // otherwise jest fails when trying to compare entities ¯\_(ツ)_/¯
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Defines getter and setter for every owning side of m:1 and 1:1 relation. This is then used for propagation of
|
|
92
|
+
* changes to the inverse side of bi-directional relations. Rest of the properties are also defined this way to
|
|
93
|
+
* achieve dirtiness, which is then used for fast checks whether we need to auto-flush because of managed entities.
|
|
94
|
+
*
|
|
95
|
+
* First defines a setter on the prototype, once called, actual get/set handlers are registered on the instance rather
|
|
96
|
+
* than on its prototype. Thanks to this we still have those properties enumerable (e.g. part of `Object.keys(entity)`).
|
|
97
|
+
*/
|
|
98
|
+
static defineProperties(meta, em) {
|
|
99
|
+
Object.values(meta.properties).forEach(prop => {
|
|
100
|
+
const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
|
|
101
|
+
// oxfmt-ignore
|
|
102
|
+
const isReference = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
|
|
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,
|
|
125
110
|
});
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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, {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (prop.inherited || prop.primary || prop.accessor || prop.persist === false || prop.embedded || isCollection) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
Object.defineProperty(meta.prototype, prop.name, {
|
|
117
|
+
set(val) {
|
|
118
|
+
Object.defineProperty(this, prop.name, {
|
|
166
119
|
get() {
|
|
167
|
-
|
|
120
|
+
return this.__helper?.__data[prop.name];
|
|
168
121
|
},
|
|
169
122
|
set(val) {
|
|
170
|
-
|
|
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);
|
|
123
|
+
this.__helper.__data[prop.name] = val;
|
|
182
124
|
},
|
|
183
125
|
enumerable: true,
|
|
184
126
|
configurable: true,
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
127
|
+
});
|
|
128
|
+
this.__helper.__data[prop.name] = val;
|
|
129
|
+
},
|
|
130
|
+
configurable: true,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
static defineCustomInspect(meta) {
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
meta.prototype[Symbol.for('nodejs.util.inspect.custom')] ??= function (depth = 2) {
|
|
137
|
+
const object = {};
|
|
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];
|
|
196
142
|
}
|
|
197
|
-
|
|
198
|
-
|
|
143
|
+
}
|
|
144
|
+
for (const key of keys) {
|
|
145
|
+
if (!meta.properties[key]) {
|
|
146
|
+
object[key] = this[key];
|
|
199
147
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
if (old && old !== value && prop.orphanRemoval) {
|
|
238
|
-
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
239
|
-
helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
|
|
148
|
+
}
|
|
149
|
+
// ensure we dont have internal symbols in the POJO
|
|
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]`;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// distinguish not initialized entities
|
|
165
|
+
if (!helper(this).__initialized) {
|
|
166
|
+
name = `(${name})`;
|
|
167
|
+
}
|
|
168
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
static defineReferenceProperty(meta, prop, ref, hydrator) {
|
|
172
|
+
const wrapped = helper(ref);
|
|
173
|
+
Object.defineProperty(ref, prop.name, {
|
|
174
|
+
get() {
|
|
175
|
+
return helper(ref).__data[prop.name];
|
|
176
|
+
},
|
|
177
|
+
set(val) {
|
|
178
|
+
const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
|
|
179
|
+
const old = Reference.unwrapReference(wrapped.__data[prop.name]);
|
|
180
|
+
// oxfmt-ignore
|
|
181
|
+
if (old && old !== entity && prop.kind === ReferenceKind.MANY_TO_ONE && prop.inversedBy && old[prop.inversedBy]) {
|
|
182
|
+
old[prop.inversedBy].removeWithoutPropagation(this);
|
|
240
183
|
}
|
|
241
|
-
|
|
184
|
+
wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
|
|
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
|
+
);
|
|
242
192
|
}
|
|
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;
|
|
243
210
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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);
|
|
211
|
+
for (const prop2 of bidirectionalRelations) {
|
|
212
|
+
if ((prop2.inversedBy || prop2.mappedBy) !== prop.name) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
// oxfmt-ignore
|
|
216
|
+
if (prop2.targetMeta.abstract ? prop2.targetMeta.root.class !== meta.root.class : prop2.targetMeta.class !== meta.class) {
|
|
217
|
+
continue;
|
|
255
218
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
219
|
+
const inverse = value?.[prop2.name];
|
|
220
|
+
if (prop.ref && owner[prop.name]) {
|
|
221
|
+
// eslint-disable-next-line dot-notation
|
|
222
|
+
owner[prop.name]['property'] = prop;
|
|
223
|
+
}
|
|
224
|
+
if (Utils.isCollection(inverse) && inverse.isPartial()) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE && Utils.isCollection(inverse) && inverse.isInitialized()) {
|
|
228
|
+
inverse.addWithoutPropagation(owner);
|
|
229
|
+
helper(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
|
|
230
|
+
}
|
|
231
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
232
|
+
if (
|
|
233
|
+
(value != null && Reference.unwrapReference(inverse) !== owner) ||
|
|
234
|
+
(value == null && entity?.[prop2.name] != null)
|
|
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)) {
|
|
273
245
|
delete helper(old).__data[prop2.name];
|
|
274
246
|
old[prop2.name] = null;
|
|
247
|
+
}
|
|
275
248
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
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];
|
|
249
|
+
if (old && old !== value && prop.orphanRemoval) {
|
|
250
|
+
helper(old).__pk ??= helper(old).getPrimaryKey();
|
|
251
|
+
helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
|
|
312
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
|
+
}
|
|
315
|
+
for (const prop of meta.trackingProps) {
|
|
316
|
+
if (entity[prop.name] !== undefined) {
|
|
317
|
+
values.push(prop.name, entity[prop.name]);
|
|
318
|
+
}
|
|
319
|
+
delete entity[prop.name];
|
|
320
|
+
}
|
|
321
|
+
Object.defineProperties(entity, meta.definedProperties);
|
|
322
|
+
for (let i = 0; i < values.length; i += 2) {
|
|
323
|
+
entity[values[i]] = values[i + 1];
|
|
313
324
|
}
|
|
325
|
+
}
|
|
314
326
|
}
|
|
@@ -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
|
}
|