@mikro-orm/core 7.0.0-dev.1 → 7.0.0-dev.3
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 +18 -13
- package/EntityManager.js +203 -203
- package/MikroORM.d.ts +7 -6
- package/MikroORM.js +33 -45
- package/cache/CacheAdapter.js +1 -2
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +18 -26
- package/cache/GeneratedCacheAdapter.d.ts +2 -2
- package/cache/GeneratedCacheAdapter.js +1 -5
- package/cache/MemoryCacheAdapter.d.ts +1 -1
- package/cache/MemoryCacheAdapter.js +1 -5
- package/cache/NullCacheAdapter.d.ts +1 -1
- package/cache/NullCacheAdapter.js +1 -5
- package/cache/index.d.ts +5 -5
- package/cache/index.js +5 -21
- package/connections/Connection.d.ts +7 -7
- package/connections/Connection.js +8 -12
- package/connections/index.d.ts +1 -1
- package/connections/index.js +1 -17
- package/decorators/Check.d.ts +1 -1
- package/decorators/Check.js +5 -8
- package/decorators/CreateRequestContext.d.ts +1 -1
- package/decorators/CreateRequestContext.js +13 -14
- package/decorators/Embeddable.d.ts +1 -1
- package/decorators/Embeddable.js +3 -6
- package/decorators/Embedded.d.ts +1 -1
- package/decorators/Embedded.js +10 -12
- package/decorators/Entity.d.ts +2 -2
- package/decorators/Entity.js +5 -8
- package/decorators/Enum.d.ts +2 -2
- package/decorators/Enum.js +7 -10
- package/decorators/Filter.d.ts +1 -1
- package/decorators/Filter.js +3 -6
- package/decorators/Formula.d.ts +2 -2
- package/decorators/Formula.js +7 -10
- package/decorators/Indexed.d.ts +2 -2
- package/decorators/Indexed.js +6 -10
- package/decorators/ManyToMany.d.ts +3 -3
- package/decorators/ManyToMany.js +10 -12
- package/decorators/ManyToOne.d.ts +3 -3
- package/decorators/ManyToOne.js +10 -12
- package/decorators/OneToMany.d.ts +3 -3
- package/decorators/OneToMany.js +11 -14
- package/decorators/OneToOne.d.ts +4 -4
- package/decorators/OneToOne.js +4 -7
- package/decorators/PrimaryKey.d.ts +2 -2
- package/decorators/PrimaryKey.js +10 -13
- package/decorators/Property.d.ts +5 -5
- package/decorators/Property.js +10 -12
- package/decorators/Transactional.d.ts +2 -2
- package/decorators/Transactional.js +7 -10
- package/decorators/hooks.js +23 -35
- package/decorators/index.d.ts +17 -17
- package/decorators/index.js +17 -36
- package/drivers/DatabaseDriver.d.ts +13 -12
- package/drivers/DatabaseDriver.js +60 -64
- package/drivers/IDatabaseDriver.d.ts +12 -12
- package/drivers/IDatabaseDriver.js +1 -4
- package/drivers/index.d.ts +2 -2
- package/drivers/index.js +2 -18
- package/entity/ArrayCollection.d.ts +2 -2
- package/entity/ArrayCollection.js +29 -33
- package/entity/BaseEntity.d.ts +6 -6
- package/entity/BaseEntity.js +17 -21
- package/entity/Collection.d.ts +6 -6
- package/entity/Collection.js +40 -44
- package/entity/EntityAssigner.d.ts +2 -2
- package/entity/EntityAssigner.js +58 -63
- package/entity/EntityFactory.d.ts +3 -3
- package/entity/EntityFactory.js +58 -62
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +41 -45
- package/entity/EntityIdentifier.d.ts +1 -1
- package/entity/EntityIdentifier.js +1 -5
- package/entity/EntityLoader.d.ts +5 -5
- package/entity/EntityLoader.js +92 -96
- package/entity/EntityRepository.d.ts +7 -7
- package/entity/EntityRepository.js +7 -11
- package/entity/EntityValidator.d.ts +1 -1
- package/entity/EntityValidator.js +25 -29
- package/entity/Reference.d.ts +2 -2
- package/entity/Reference.js +35 -42
- package/entity/WrappedEntity.d.ts +12 -12
- package/entity/WrappedEntity.js +23 -27
- package/entity/index.d.ts +13 -13
- package/entity/index.js +13 -29
- package/entity/utils.d.ts +1 -1
- package/entity/utils.js +9 -12
- package/entity/wrap.d.ts +1 -1
- package/entity/wrap.js +2 -6
- package/enums.d.ts +3 -3
- package/enums.js +37 -41
- package/errors.d.ts +1 -1
- package/errors.js +15 -24
- package/events/EventManager.d.ts +3 -3
- package/events/EventManager.js +8 -12
- package/events/EventSubscriber.d.ts +5 -4
- package/events/EventSubscriber.js +1 -2
- package/events/TransactionEventBroadcaster.d.ts +3 -3
- package/events/TransactionEventBroadcaster.js +1 -5
- package/events/index.d.ts +3 -3
- package/events/index.js +3 -19
- package/exceptions.js +18 -39
- package/hydration/Hydrator.d.ts +5 -5
- package/hydration/Hydrator.js +3 -6
- package/hydration/ObjectHydrator.d.ts +3 -3
- package/hydration/ObjectHydrator.js +25 -28
- package/hydration/index.d.ts +2 -2
- package/hydration/index.js +2 -18
- package/index.d.ts +21 -21
- package/index.js +21 -46
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +9 -13
- package/logging/Logger.d.ts +1 -1
- package/logging/Logger.js +1 -2
- package/logging/SimpleLogger.d.ts +2 -2
- package/logging/SimpleLogger.js +2 -6
- package/logging/colors.js +1 -5
- package/logging/index.d.ts +4 -4
- package/logging/index.js +4 -20
- package/metadata/EntitySchema.d.ts +12 -4
- package/metadata/EntitySchema.js +41 -45
- package/metadata/MetadataDiscovery.d.ts +7 -7
- package/metadata/MetadataDiscovery.js +174 -177
- package/metadata/MetadataProvider.d.ts +2 -2
- package/metadata/MetadataProvider.js +4 -7
- package/metadata/MetadataStorage.d.ts +2 -2
- package/metadata/MetadataStorage.js +15 -19
- package/metadata/MetadataValidator.d.ts +4 -4
- package/metadata/MetadataValidator.js +49 -53
- package/metadata/ReflectMetadataProvider.d.ts +2 -2
- package/metadata/ReflectMetadataProvider.js +8 -12
- package/metadata/index.d.ts +6 -6
- package/metadata/index.js +6 -22
- package/naming-strategy/AbstractNamingStrategy.d.ts +2 -2
- package/naming-strategy/AbstractNamingStrategy.js +4 -8
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +1 -1
- package/naming-strategy/EntityCaseNamingStrategy.js +2 -6
- package/naming-strategy/MongoNamingStrategy.d.ts +1 -1
- package/naming-strategy/MongoNamingStrategy.js +2 -6
- package/naming-strategy/NamingStrategy.d.ts +1 -1
- package/naming-strategy/NamingStrategy.js +1 -2
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +1 -1
- package/naming-strategy/UnderscoreNamingStrategy.js +2 -6
- package/naming-strategy/index.d.ts +5 -5
- package/naming-strategy/index.js +5 -21
- package/package.json +5 -14
- package/platforms/ExceptionConverter.d.ts +2 -2
- package/platforms/ExceptionConverter.js +4 -8
- package/platforms/Platform.d.ts +10 -10
- package/platforms/Platform.js +57 -61
- package/platforms/index.d.ts +2 -2
- package/platforms/index.js +2 -18
- package/serialization/EntitySerializer.d.ts +2 -2
- package/serialization/EntitySerializer.js +36 -41
- package/serialization/EntityTransformer.d.ts +1 -1
- package/serialization/EntityTransformer.js +27 -31
- package/serialization/SerializationContext.d.ts +2 -2
- package/serialization/SerializationContext.js +10 -14
- package/serialization/index.d.ts +3 -3
- package/serialization/index.js +3 -19
- package/types/ArrayType.d.ts +3 -3
- package/types/ArrayType.js +7 -11
- package/types/BigIntType.d.ts +3 -3
- package/types/BigIntType.js +3 -6
- package/types/BlobType.d.ts +3 -3
- package/types/BlobType.js +2 -8
- package/types/BooleanType.d.ts +3 -3
- package/types/BooleanType.js +2 -6
- package/types/CharacterType.d.ts +3 -3
- package/types/CharacterType.js +2 -6
- package/types/DateTimeType.d.ts +3 -3
- package/types/DateTimeType.js +2 -6
- package/types/DateType.d.ts +3 -3
- package/types/DateType.js +2 -6
- package/types/DecimalType.d.ts +3 -3
- package/types/DecimalType.js +4 -7
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +3 -6
- package/types/EnumArrayType.d.ts +4 -4
- package/types/EnumArrayType.js +5 -10
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +2 -6
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +2 -6
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +2 -6
- package/types/IntervalType.d.ts +3 -3
- package/types/IntervalType.js +2 -6
- package/types/JsonType.d.ts +3 -3
- package/types/JsonType.js +2 -6
- package/types/MediumIntType.d.ts +3 -3
- package/types/MediumIntType.js +2 -6
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +2 -6
- package/types/StringType.d.ts +3 -3
- package/types/StringType.js +2 -6
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +2 -6
- package/types/TimeType.d.ts +3 -3
- package/types/TimeType.js +4 -8
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +3 -6
- package/types/Type.d.ts +2 -2
- package/types/Type.js +5 -9
- package/types/Uint8ArrayType.d.ts +3 -3
- package/types/Uint8ArrayType.js +3 -9
- package/types/UnknownType.d.ts +3 -3
- package/types/UnknownType.js +2 -6
- package/types/UuidType.d.ts +3 -3
- package/types/UuidType.js +2 -6
- package/types/index.d.ts +25 -25
- package/types/index.js +52 -79
- package/typings.d.ts +22 -20
- package/typings.js +34 -38
- package/unit-of-work/ChangeSet.d.ts +1 -1
- package/unit-of-work/ChangeSet.js +13 -17
- package/unit-of-work/ChangeSetComputer.d.ts +8 -7
- package/unit-of-work/ChangeSetComputer.js +26 -30
- package/unit-of-work/ChangeSetPersister.d.ts +7 -6
- package/unit-of-work/ChangeSetPersister.js +44 -47
- package/unit-of-work/CommitOrderCalculator.d.ts +1 -1
- package/unit-of-work/CommitOrderCalculator.js +6 -10
- package/unit-of-work/IdentityMap.d.ts +1 -1
- package/unit-of-work/IdentityMap.js +1 -5
- package/unit-of-work/UnitOfWork.d.ts +8 -7
- package/unit-of-work/UnitOfWork.js +171 -172
- package/unit-of-work/index.d.ts +6 -6
- package/unit-of-work/index.js +6 -22
- package/utils/AbstractSchemaGenerator.d.ts +6 -6
- package/utils/AbstractSchemaGenerator.js +7 -11
- package/utils/Configuration.d.ts +26 -27
- package/utils/Configuration.js +50 -55
- package/utils/ConfigurationLoader.d.ts +9 -8
- package/utils/ConfigurationLoader.js +71 -86
- package/utils/Cursor.d.ts +3 -3
- package/utils/Cursor.js +22 -25
- package/utils/DataloaderUtils.d.ts +4 -4
- package/utils/DataloaderUtils.js +12 -16
- package/utils/EntityComparator.d.ts +2 -2
- package/utils/EntityComparator.js +48 -52
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +1 -5
- package/utils/QueryHelper.d.ts +3 -3
- package/utils/QueryHelper.js +47 -51
- package/utils/RawQueryFragment.d.ts +1 -1
- package/utils/RawQueryFragment.js +16 -24
- package/utils/RequestContext.d.ts +2 -2
- package/utils/RequestContext.js +3 -7
- package/utils/TransactionContext.d.ts +1 -1
- package/utils/TransactionContext.js +4 -8
- package/utils/Utils.d.ts +16 -12
- package/utils/Utils.js +89 -93
- package/utils/clone.js +8 -11
- package/utils/index.d.ts +13 -13
- package/utils/index.js +13 -29
- package/utils/resolveContextProvider.d.ts +3 -3
- package/utils/resolveContextProvider.js +9 -12
- package/utils/upsert-utils.d.ts +3 -3
- package/utils/upsert-utils.js +5 -9
- package/index.mjs +0 -199
package/entity/EntityFactory.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const wrap_1 = require("./wrap");
|
|
9
|
-
const EntityHelper_1 = require("./EntityHelper");
|
|
10
|
-
class EntityFactory {
|
|
1
|
+
import { Utils } from '../utils/Utils.js';
|
|
2
|
+
import { QueryHelper } from '../utils/QueryHelper.js';
|
|
3
|
+
import { EventType, ReferenceKind } from '../enums.js';
|
|
4
|
+
import { Reference } from './Reference.js';
|
|
5
|
+
import { helper } from './wrap.js';
|
|
6
|
+
import { EntityHelper } from './EntityHelper.js';
|
|
7
|
+
export class EntityFactory {
|
|
11
8
|
em;
|
|
12
9
|
driver;
|
|
13
10
|
platform;
|
|
@@ -27,12 +24,12 @@ class EntityFactory {
|
|
|
27
24
|
this.comparator = this.em.getComparator();
|
|
28
25
|
}
|
|
29
26
|
create(entityName, data, options = {}) {
|
|
30
|
-
data =
|
|
27
|
+
data = Reference.unwrapReference(data);
|
|
31
28
|
options.initialized ??= true;
|
|
32
29
|
if (data.__entity) {
|
|
33
30
|
return data;
|
|
34
31
|
}
|
|
35
|
-
entityName =
|
|
32
|
+
entityName = Utils.className(entityName);
|
|
36
33
|
const meta = this.metadata.get(entityName);
|
|
37
34
|
if (meta.virtual) {
|
|
38
35
|
data = { ...data };
|
|
@@ -45,7 +42,7 @@ class EntityFactory {
|
|
|
45
42
|
}
|
|
46
43
|
const meta2 = this.processDiscriminatorColumn(meta, data);
|
|
47
44
|
const exists = this.findEntity(data, meta2, options);
|
|
48
|
-
let wrapped = exists &&
|
|
45
|
+
let wrapped = exists && helper(exists);
|
|
49
46
|
if (wrapped && !options.refresh) {
|
|
50
47
|
wrapped.__processing = true;
|
|
51
48
|
this.mergeData(meta2, exists, data, options);
|
|
@@ -56,7 +53,7 @@ class EntityFactory {
|
|
|
56
53
|
}
|
|
57
54
|
data = { ...data };
|
|
58
55
|
const entity = exists ?? this.createEntity(data, meta2, options);
|
|
59
|
-
wrapped =
|
|
56
|
+
wrapped = helper(entity);
|
|
60
57
|
wrapped.__processing = true;
|
|
61
58
|
wrapped.__initialized = options.initialized;
|
|
62
59
|
if (options.newEntity || meta.forceConstructor || meta.virtual) {
|
|
@@ -69,11 +66,11 @@ class EntityFactory {
|
|
|
69
66
|
if (options.convertCustomTypes) {
|
|
70
67
|
for (const prop of meta.props) {
|
|
71
68
|
if (prop.customType?.ensureComparable(meta, prop) && data[prop.name]) {
|
|
72
|
-
if ([
|
|
69
|
+
if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind)) {
|
|
73
70
|
continue;
|
|
74
71
|
}
|
|
75
|
-
if ([
|
|
76
|
-
data[prop.name] =
|
|
72
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isPlainObject(data[prop.name])) {
|
|
73
|
+
data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
|
|
77
74
|
}
|
|
78
75
|
data[prop.name] = prop.customType.convertToDatabaseValue(data[prop.name], this.platform, { key: prop.name, mode: 'hydration' });
|
|
79
76
|
}
|
|
@@ -97,17 +94,17 @@ class EntityFactory {
|
|
|
97
94
|
wrapped.__originalEntityData = this.comparator.prepareEntity(entity);
|
|
98
95
|
}
|
|
99
96
|
}
|
|
100
|
-
if (this.eventManager.hasListeners(
|
|
101
|
-
this.eventManager.dispatchEvent(
|
|
97
|
+
if (this.eventManager.hasListeners(EventType.onInit, meta2)) {
|
|
98
|
+
this.eventManager.dispatchEvent(EventType.onInit, { entity, meta: meta2, em: this.em });
|
|
102
99
|
}
|
|
103
100
|
wrapped.__processing = false;
|
|
104
101
|
return entity;
|
|
105
102
|
}
|
|
106
103
|
mergeData(meta, entity, data, options = {}) {
|
|
107
104
|
// merge unchanged properties automatically
|
|
108
|
-
data =
|
|
105
|
+
data = QueryHelper.processParams(data);
|
|
109
106
|
const existsData = this.comparator.prepareEntity(entity);
|
|
110
|
-
const originalEntityData =
|
|
107
|
+
const originalEntityData = helper(entity).__originalEntityData ?? {};
|
|
111
108
|
const diff = this.comparator.diffEntities(meta.className, originalEntityData, existsData);
|
|
112
109
|
// version properties are not part of entity snapshots
|
|
113
110
|
if (meta.versionProperty && data[meta.versionProperty] && data[meta.versionProperty] !== originalEntityData[meta.versionProperty]) {
|
|
@@ -115,8 +112,8 @@ class EntityFactory {
|
|
|
115
112
|
}
|
|
116
113
|
const diff2 = this.comparator.diffEntities(meta.className, existsData, data);
|
|
117
114
|
// do not override values changed by user
|
|
118
|
-
|
|
119
|
-
|
|
115
|
+
Utils.keys(diff).forEach(key => delete diff2[key]);
|
|
116
|
+
Utils.keys(diff2).filter(key => {
|
|
120
117
|
// ignore null values if there is already present non-null value
|
|
121
118
|
if (existsData[key] != null) {
|
|
122
119
|
return diff2[key] == null;
|
|
@@ -124,40 +121,40 @@ class EntityFactory {
|
|
|
124
121
|
return diff2[key] === undefined;
|
|
125
122
|
}).forEach(key => delete diff2[key]);
|
|
126
123
|
// but always add collection properties and formulas if they are part of the `data`
|
|
127
|
-
|
|
128
|
-
.filter(key => meta.properties[key]?.formula || [
|
|
124
|
+
Utils.keys(data)
|
|
125
|
+
.filter(key => meta.properties[key]?.formula || [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(meta.properties[key]?.kind))
|
|
129
126
|
.forEach(key => diff2[key] = data[key]);
|
|
130
127
|
// rehydrated with the new values, skip those changed by user
|
|
131
128
|
this.hydrate(entity, meta, diff2, options);
|
|
132
129
|
// we need to update the entity data only with keys that were not present before
|
|
133
130
|
const nullVal = this.config.get('forceUndefined') ? undefined : null;
|
|
134
|
-
|
|
131
|
+
Utils.keys(diff2).forEach(key => {
|
|
135
132
|
const prop = meta.properties[key];
|
|
136
|
-
if ([
|
|
137
|
-
diff2[key] = entity[prop.name] ?
|
|
133
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isPlainObject(data[prop.name])) {
|
|
134
|
+
diff2[key] = entity[prop.name] ? helper(entity[prop.name]).getPrimaryKey(options.convertCustomTypes) : null;
|
|
138
135
|
}
|
|
139
136
|
originalEntityData[key] = diff2[key] === null ? nullVal : diff2[key];
|
|
140
|
-
|
|
137
|
+
helper(entity).__loadedProperties.add(key);
|
|
141
138
|
});
|
|
142
139
|
// in case of joined loading strategy, we need to cascade the merging to possibly loaded relations manually
|
|
143
140
|
meta.relations.forEach(prop => {
|
|
144
|
-
if ([
|
|
141
|
+
if ([ReferenceKind.MANY_TO_MANY, ReferenceKind.ONE_TO_MANY].includes(prop.kind) && Array.isArray(data[prop.name])) {
|
|
145
142
|
// instead of trying to match the collection items (which could easily fail if the collection was loaded with different ordering),
|
|
146
143
|
// we just create the entity from scratch, which will automatically pick the right one from the identity map and call `mergeData` on it
|
|
147
144
|
data[prop.name]
|
|
148
|
-
.filter(child =>
|
|
145
|
+
.filter(child => Utils.isPlainObject(child)) // objects with prototype can be PKs (e.g. `ObjectId`)
|
|
149
146
|
.forEach(child => this.create(prop.type, child, options)); // we can ignore the value, we just care about the `mergeData` call
|
|
150
147
|
return;
|
|
151
148
|
}
|
|
152
|
-
if ([
|
|
149
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isPlainObject(data[prop.name]) && entity[prop.name] && helper(entity[prop.name]).__initialized) {
|
|
153
150
|
this.create(prop.type, data[prop.name], options); // we can ignore the value, we just care about the `mergeData` call
|
|
154
151
|
}
|
|
155
152
|
});
|
|
156
|
-
|
|
153
|
+
helper(entity).__touched = false;
|
|
157
154
|
}
|
|
158
155
|
createReference(entityName, id, options = {}) {
|
|
159
156
|
options.convertCustomTypes ??= true;
|
|
160
|
-
entityName =
|
|
157
|
+
entityName = Utils.className(entityName);
|
|
161
158
|
const meta = this.metadata.get(entityName);
|
|
162
159
|
const schema = this.driver.getSchemaName(meta, options);
|
|
163
160
|
if (meta.simplePK) {
|
|
@@ -165,24 +162,24 @@ class EntityFactory {
|
|
|
165
162
|
if (exists) {
|
|
166
163
|
return exists;
|
|
167
164
|
}
|
|
168
|
-
const data =
|
|
165
|
+
const data = Utils.isPlainObject(id) ? id : { [meta.primaryKeys[0]]: Array.isArray(id) ? id[0] : id };
|
|
169
166
|
return this.create(entityName, data, { ...options, initialized: false });
|
|
170
167
|
}
|
|
171
168
|
if (Array.isArray(id)) {
|
|
172
|
-
id =
|
|
169
|
+
id = Utils.getPrimaryKeyCondFromArray(id, meta);
|
|
173
170
|
}
|
|
174
|
-
const pks =
|
|
171
|
+
const pks = Utils.getOrderedPrimaryKeys(id, meta, this.platform, options.convertCustomTypes);
|
|
175
172
|
const exists = this.unitOfWork.getById(entityName, pks, schema);
|
|
176
173
|
if (exists) {
|
|
177
174
|
return exists;
|
|
178
175
|
}
|
|
179
|
-
if (
|
|
176
|
+
if (Utils.isPrimaryKey(id)) {
|
|
180
177
|
id = { [meta.primaryKeys[0]]: id };
|
|
181
178
|
}
|
|
182
179
|
return this.create(entityName, id, { ...options, initialized: false });
|
|
183
180
|
}
|
|
184
181
|
createEmbeddable(entityName, data, options = {}) {
|
|
185
|
-
entityName =
|
|
182
|
+
entityName = Utils.className(entityName);
|
|
186
183
|
data = { ...data };
|
|
187
184
|
const meta = this.metadata.get(entityName);
|
|
188
185
|
const meta2 = this.processDiscriminatorColumn(meta, data);
|
|
@@ -211,23 +208,23 @@ class EntityFactory {
|
|
|
211
208
|
if (meta.virtual) {
|
|
212
209
|
return entity;
|
|
213
210
|
}
|
|
214
|
-
|
|
211
|
+
helper(entity).__schema = schema;
|
|
215
212
|
if (options.initialized) {
|
|
216
|
-
|
|
213
|
+
EntityHelper.ensurePropagation(entity);
|
|
217
214
|
}
|
|
218
215
|
return entity;
|
|
219
216
|
}
|
|
220
217
|
// creates new entity instance, bypassing constructor call as its already persisted entity
|
|
221
218
|
const entity = Object.create(meta.class.prototype);
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
219
|
+
helper(entity).__managed = true;
|
|
220
|
+
helper(entity).__processing = !meta.embeddable && !meta.virtual;
|
|
221
|
+
helper(entity).__schema = schema;
|
|
225
222
|
if (options.merge && !options.newEntity) {
|
|
226
223
|
this.hydrator.hydrateReference(entity, meta, data, this, options.convertCustomTypes, options.schema, options.parentSchema);
|
|
227
224
|
this.unitOfWork.register(entity);
|
|
228
225
|
}
|
|
229
226
|
if (options.initialized) {
|
|
230
|
-
|
|
227
|
+
EntityHelper.ensurePropagation(entity);
|
|
231
228
|
}
|
|
232
229
|
return entity;
|
|
233
230
|
}
|
|
@@ -238,9 +235,9 @@ class EntityFactory {
|
|
|
238
235
|
else {
|
|
239
236
|
this.hydrator.hydrateReference(entity, meta, data, this, options.convertCustomTypes, options.schema, this.driver.getSchemaName(meta, options));
|
|
240
237
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
238
|
+
Utils.keys(data).forEach(key => {
|
|
239
|
+
helper(entity)?.__loadedProperties.add(key);
|
|
240
|
+
helper(entity)?.__serializationContext.fields?.add(key);
|
|
244
241
|
});
|
|
245
242
|
}
|
|
246
243
|
findEntity(data, meta, options) {
|
|
@@ -251,7 +248,7 @@ class EntityFactory {
|
|
|
251
248
|
if (!Array.isArray(data) && meta.primaryKeys.some(pk => data[pk] == null)) {
|
|
252
249
|
return undefined;
|
|
253
250
|
}
|
|
254
|
-
const pks =
|
|
251
|
+
const pks = Utils.getOrderedPrimaryKeys(data, meta, this.platform);
|
|
255
252
|
return this.unitOfWork.getById(meta.className, pks, schema);
|
|
256
253
|
}
|
|
257
254
|
processDiscriminatorColumn(meta, data) {
|
|
@@ -283,26 +280,26 @@ class EntityFactory {
|
|
|
283
280
|
*/
|
|
284
281
|
extractConstructorParams(meta, data, options) {
|
|
285
282
|
return meta.constructorParams.map(k => {
|
|
286
|
-
if (meta.properties[k] && [
|
|
287
|
-
const pk =
|
|
283
|
+
if (meta.properties[k] && [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(meta.properties[k].kind) && data[k]) {
|
|
284
|
+
const pk = Reference.unwrapReference(data[k]);
|
|
288
285
|
const entity = this.unitOfWork.getById(meta.properties[k].type, pk, options.schema);
|
|
289
286
|
if (entity) {
|
|
290
287
|
return entity;
|
|
291
288
|
}
|
|
292
|
-
if (
|
|
289
|
+
if (Utils.isEntity(data[k])) {
|
|
293
290
|
return data[k];
|
|
294
291
|
}
|
|
295
|
-
const nakedPk =
|
|
296
|
-
if (
|
|
292
|
+
const nakedPk = Utils.extractPK(data[k], meta.properties[k].targetMeta, true);
|
|
293
|
+
if (Utils.isObject(data[k]) && !nakedPk) {
|
|
297
294
|
return this.create(meta.properties[k].type, data[k], options);
|
|
298
295
|
}
|
|
299
296
|
const { newEntity, initialized, ...rest } = options;
|
|
300
297
|
const target = this.createReference(meta.properties[k].type, nakedPk, rest);
|
|
301
|
-
return
|
|
298
|
+
return Reference.wrapReference(target, meta.properties[k]);
|
|
302
299
|
}
|
|
303
|
-
if (meta.properties[k]?.kind ===
|
|
304
|
-
/*
|
|
305
|
-
if (
|
|
300
|
+
if (meta.properties[k]?.kind === ReferenceKind.EMBEDDED && data[k]) {
|
|
301
|
+
/* v8 ignore next 3 */
|
|
302
|
+
if (Utils.isEntity(data[k])) {
|
|
306
303
|
return data[k];
|
|
307
304
|
}
|
|
308
305
|
return this.createEmbeddable(meta.properties[k].type, data[k], options);
|
|
@@ -313,10 +310,10 @@ class EntityFactory {
|
|
|
313
310
|
if (!options.convertCustomTypes || !prop.customType || tmp[prop.name] == null) {
|
|
314
311
|
continue;
|
|
315
312
|
}
|
|
316
|
-
if ([
|
|
317
|
-
tmp[prop.name] =
|
|
313
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isPlainObject(tmp[prop.name]) && !Utils.extractPK(tmp[prop.name], meta.properties[prop.name].targetMeta, true)) {
|
|
314
|
+
tmp[prop.name] = Reference.wrapReference(this.create(meta.properties[prop.name].type, tmp[prop.name], options), prop);
|
|
318
315
|
}
|
|
319
|
-
else if (prop.kind ===
|
|
316
|
+
else if (prop.kind === ReferenceKind.SCALAR) {
|
|
320
317
|
tmp[prop.name] = prop.customType.convertToJSValue(tmp[prop.name], this.platform);
|
|
321
318
|
}
|
|
322
319
|
}
|
|
@@ -332,4 +329,3 @@ class EntityFactory {
|
|
|
332
329
|
return this.em.getUnitOfWork(false);
|
|
333
330
|
}
|
|
334
331
|
}
|
|
335
|
-
exports.EntityFactory = EntityFactory;
|
package/entity/EntityHelper.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { EntityManager } from '../EntityManager';
|
|
2
|
-
import { type EntityMetadata, type EntityProperty, type IHydrator } from '../typings';
|
|
1
|
+
import type { EntityManager } from '../EntityManager.js';
|
|
2
|
+
import { type EntityMetadata, type EntityProperty, type IHydrator } from '../typings.js';
|
|
3
3
|
/**
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
package/entity/EntityHelper.js
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const WrappedEntity_1 = require("./WrappedEntity");
|
|
10
|
-
const enums_1 = require("../enums");
|
|
11
|
-
const wrap_1 = require("./wrap");
|
|
1
|
+
import { inspect } from 'node:util';
|
|
2
|
+
import { EagerProps, EntityRepositoryType, HiddenProps, OptionalProps, PrimaryKeyProp, } from '../typings.js';
|
|
3
|
+
import { EntityTransformer } from '../serialization/EntityTransformer.js';
|
|
4
|
+
import { Reference } from './Reference.js';
|
|
5
|
+
import { Utils } from '../utils/Utils.js';
|
|
6
|
+
import { WrappedEntity } from './WrappedEntity.js';
|
|
7
|
+
import { ReferenceKind } from '../enums.js';
|
|
8
|
+
import { helper } from './wrap.js';
|
|
12
9
|
/**
|
|
13
10
|
* @internal
|
|
14
11
|
*/
|
|
15
|
-
class EntityHelper {
|
|
12
|
+
export class EntityHelper {
|
|
16
13
|
static decorate(meta, em) {
|
|
17
14
|
const fork = em.fork(); // use fork so we can access `EntityFactory`
|
|
18
15
|
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
@@ -35,7 +32,7 @@ class EntityHelper {
|
|
|
35
32
|
const prototype = meta.prototype;
|
|
36
33
|
if (!prototype.toJSON) { // toJSON can be overridden
|
|
37
34
|
prototype.toJSON = function (...args) {
|
|
38
|
-
return
|
|
35
|
+
return EntityTransformer.toObject(this, ...args.slice(meta.toJsonParams.length));
|
|
39
36
|
};
|
|
40
37
|
}
|
|
41
38
|
}
|
|
@@ -56,7 +53,7 @@ class EntityHelper {
|
|
|
56
53
|
__helper: {
|
|
57
54
|
get() {
|
|
58
55
|
Object.defineProperty(this, '__helper', {
|
|
59
|
-
value: new
|
|
56
|
+
value: new WrappedEntity(this, em.getHydrator(), ...helperParams),
|
|
60
57
|
enumerable: false,
|
|
61
58
|
configurable: true,
|
|
62
59
|
});
|
|
@@ -78,8 +75,8 @@ class EntityHelper {
|
|
|
78
75
|
Object
|
|
79
76
|
.values(meta.properties)
|
|
80
77
|
.forEach(prop => {
|
|
81
|
-
const isCollection = [
|
|
82
|
-
const isReference = [
|
|
78
|
+
const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
|
|
79
|
+
const isReference = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
|
|
83
80
|
if (isReference) {
|
|
84
81
|
Object.defineProperty(meta.prototype, prop.name, {
|
|
85
82
|
set(val) {
|
|
@@ -115,49 +112,49 @@ class EntityHelper {
|
|
|
115
112
|
}
|
|
116
113
|
static defineCustomInspect(meta) {
|
|
117
114
|
// @ts-ignore
|
|
118
|
-
meta.prototype[
|
|
115
|
+
meta.prototype[inspect.custom] ??= function (depth = 2) {
|
|
119
116
|
const object = { ...this };
|
|
120
117
|
// ensure we dont have internal symbols in the POJO
|
|
121
|
-
[
|
|
118
|
+
[OptionalProps, EntityRepositoryType, PrimaryKeyProp, EagerProps, HiddenProps].forEach(sym => delete object[sym]);
|
|
122
119
|
meta.props
|
|
123
120
|
.filter(prop => object[prop.name] === undefined)
|
|
124
121
|
.forEach(prop => delete object[prop.name]);
|
|
125
|
-
const ret =
|
|
122
|
+
const ret = inspect(object, { depth });
|
|
126
123
|
let name = (this).constructor.name;
|
|
127
124
|
const showEM = ['true', 't', '1'].includes(process.env.MIKRO_ORM_LOG_EM_ID?.toString().toLowerCase() ?? '');
|
|
128
125
|
if (showEM) {
|
|
129
|
-
if (
|
|
130
|
-
name += ` [managed by ${
|
|
126
|
+
if (helper(this).__em) {
|
|
127
|
+
name += ` [managed by ${helper(this).__em.id}]`;
|
|
131
128
|
}
|
|
132
129
|
else {
|
|
133
130
|
name += ` [not managed]`;
|
|
134
131
|
}
|
|
135
132
|
}
|
|
136
133
|
// distinguish not initialized entities
|
|
137
|
-
if (!
|
|
134
|
+
if (!helper(this).__initialized) {
|
|
138
135
|
name = `(${name})`;
|
|
139
136
|
}
|
|
140
137
|
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
141
138
|
};
|
|
142
139
|
}
|
|
143
140
|
static defineReferenceProperty(meta, prop, ref, hydrator) {
|
|
144
|
-
const wrapped =
|
|
141
|
+
const wrapped = helper(ref);
|
|
145
142
|
Object.defineProperty(ref, prop.name, {
|
|
146
143
|
get() {
|
|
147
|
-
return
|
|
144
|
+
return helper(ref).__data[prop.name];
|
|
148
145
|
},
|
|
149
146
|
set(val) {
|
|
150
|
-
const entity =
|
|
151
|
-
const old =
|
|
152
|
-
wrapped.__data[prop.name] =
|
|
147
|
+
const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
|
|
148
|
+
const old = Reference.unwrapReference(wrapped.__data[prop.name]);
|
|
149
|
+
wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
|
|
153
150
|
// when propagation from inside hydration, we set the FK to the entity data immediately
|
|
154
151
|
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
|
|
155
|
-
wrapped.__originalEntityData[prop.name] =
|
|
152
|
+
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta.primaryKeys, true);
|
|
156
153
|
}
|
|
157
154
|
else {
|
|
158
155
|
wrapped.__touched = !hydrator.isRunning();
|
|
159
156
|
}
|
|
160
|
-
EntityHelper.propagate(meta, entity, this, prop,
|
|
157
|
+
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
|
|
161
158
|
},
|
|
162
159
|
enumerable: true,
|
|
163
160
|
configurable: true,
|
|
@@ -172,51 +169,51 @@ class EntityHelper {
|
|
|
172
169
|
continue;
|
|
173
170
|
}
|
|
174
171
|
const inverse = value?.[prop2.name];
|
|
175
|
-
if (prop.kind ===
|
|
172
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE && Utils.isCollection(inverse) && inverse.isInitialized()) {
|
|
176
173
|
inverse.addWithoutPropagation(owner);
|
|
177
|
-
|
|
174
|
+
helper(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
|
|
178
175
|
}
|
|
179
|
-
if (prop.kind ===
|
|
180
|
-
if ((value != null &&
|
|
176
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
177
|
+
if ((value != null && Reference.unwrapReference(inverse) !== owner) ||
|
|
181
178
|
(value == null && entity?.[prop2.name] != null)) {
|
|
182
|
-
if (entity && (!prop.owner ||
|
|
179
|
+
if (entity && (!prop.owner || helper(entity).__initialized)) {
|
|
183
180
|
EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
|
|
184
181
|
}
|
|
185
182
|
if (old && prop.orphanRemoval) {
|
|
186
|
-
|
|
183
|
+
helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
|
|
187
184
|
}
|
|
188
185
|
}
|
|
189
186
|
}
|
|
190
187
|
}
|
|
191
188
|
}
|
|
192
189
|
static propagateOneToOne(entity, owner, prop, prop2, value, old) {
|
|
193
|
-
|
|
190
|
+
helper(entity).__pk = helper(entity).getPrimaryKey();
|
|
194
191
|
// the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
|
|
195
|
-
if (!prop.primary && !prop2.mapToPk && value?.[prop2.name] != null &&
|
|
196
|
-
const other =
|
|
197
|
-
delete
|
|
192
|
+
if (!prop.primary && !prop2.mapToPk && value?.[prop2.name] != null && Reference.unwrapReference(value[prop2.name]) !== entity) {
|
|
193
|
+
const other = Reference.unwrapReference(value[prop2.name]);
|
|
194
|
+
delete helper(other).__data[prop.name];
|
|
198
195
|
if (prop2.orphanRemoval) {
|
|
199
|
-
|
|
196
|
+
helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
|
|
200
197
|
}
|
|
201
198
|
}
|
|
202
199
|
if (value == null) {
|
|
203
200
|
entity[prop2.name] = value;
|
|
204
201
|
}
|
|
205
202
|
else if (prop2.mapToPk) {
|
|
206
|
-
entity[prop2.name] =
|
|
203
|
+
entity[prop2.name] = helper(owner).getPrimaryKey();
|
|
207
204
|
}
|
|
208
205
|
else {
|
|
209
|
-
entity[prop2.name] =
|
|
206
|
+
entity[prop2.name] = Reference.wrapReference(owner, prop);
|
|
210
207
|
}
|
|
211
208
|
if (old?.[prop2.name] != null) {
|
|
212
|
-
delete
|
|
209
|
+
delete helper(old).__data[prop2.name];
|
|
213
210
|
}
|
|
214
211
|
}
|
|
215
212
|
static ensurePropagation(entity) {
|
|
216
213
|
if (entity.__gettersDefined) {
|
|
217
214
|
return;
|
|
218
215
|
}
|
|
219
|
-
const wrapped =
|
|
216
|
+
const wrapped = helper(entity);
|
|
220
217
|
const meta = wrapped.__meta;
|
|
221
218
|
const platform = wrapped.__platform;
|
|
222
219
|
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
@@ -250,4 +247,3 @@ class EntityHelper {
|
|
|
250
247
|
}
|
|
251
248
|
}
|
|
252
249
|
}
|
|
253
|
-
exports.EntityHelper = EntityHelper;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EntityIdentifier = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* @internal
|
|
6
3
|
*/
|
|
7
|
-
class EntityIdentifier {
|
|
4
|
+
export class EntityIdentifier {
|
|
8
5
|
value;
|
|
9
6
|
constructor(value) {
|
|
10
7
|
this.value = value;
|
|
@@ -16,4 +13,3 @@ class EntityIdentifier {
|
|
|
16
13
|
return this.value;
|
|
17
14
|
}
|
|
18
15
|
}
|
|
19
|
-
exports.EntityIdentifier = EntityIdentifier;
|
package/entity/EntityLoader.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { ConnectionType, Dictionary, FilterQuery, PopulateOptions } from '../typings';
|
|
2
|
-
import type { EntityManager } from '../EntityManager';
|
|
3
|
-
import { LoadStrategy, type LockMode, type PopulateHint, PopulatePath, type QueryOrderMap } from '../enums';
|
|
4
|
-
import type { EntityField } from '../drivers/IDatabaseDriver';
|
|
5
|
-
import type { LoggingOptions } from '../logging/Logger';
|
|
1
|
+
import type { ConnectionType, Dictionary, FilterQuery, PopulateOptions } from '../typings.js';
|
|
2
|
+
import type { EntityManager } from '../EntityManager.js';
|
|
3
|
+
import { LoadStrategy, type LockMode, type PopulateHint, PopulatePath, type QueryOrderMap } from '../enums.js';
|
|
4
|
+
import type { EntityField } from '../drivers/IDatabaseDriver.js';
|
|
5
|
+
import type { LoggingOptions } from '../logging/Logger.js';
|
|
6
6
|
export type EntityLoaderOptions<Entity, Fields extends string = PopulatePath.ALL, Excludes extends string = never> = {
|
|
7
7
|
where?: FilterQuery<Entity>;
|
|
8
8
|
populateWhere?: PopulateHint | `${PopulateHint}`;
|