@mikro-orm/core 7.0.4 → 7.0.5-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 +1895 -1922
- 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 +179 -212
- package/entity/Collection.js +721 -727
- package/entity/EntityAssigner.d.ts +77 -88
- package/entity/EntityAssigner.js +230 -231
- package/entity/EntityFactory.d.ts +54 -66
- package/entity/EntityFactory.js +383 -425
- package/entity/EntityHelper.d.ts +22 -34
- package/entity/EntityHelper.js +267 -280
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +72 -98
- package/entity/EntityLoader.js +723 -753
- 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 +82 -126
- package/entity/Reference.js +274 -278
- package/entity/WrappedEntity.d.ts +72 -115
- package/entity/WrappedEntity.js +166 -168
- package/entity/defineEntity.d.ts +636 -1315
- 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 +416 -481
- 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 +127 -211
- package/metadata/EntitySchema.js +398 -397
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1870 -1951
- package/metadata/MetadataProvider.d.ts +21 -24
- package/metadata/MetadataProvider.js +84 -82
- 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 +301 -310
- package/platforms/Platform.js +640 -663
- 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 +737 -1250
- package/typings.js +231 -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 +170 -178
- package/unit-of-work/ChangeSetPersister.d.ts +44 -63
- package/unit-of-work/ChangeSetPersister.js +421 -442
- 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 +1183 -1200
- 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 +81 -98
- package/utils/EntityComparator.js +732 -828
- 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 +812 -812
- package/utils/clone.js +113 -104
- 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
|
@@ -8,494 +8,429 @@ import { Raw } from '../utils/RawQueryFragment.js';
|
|
|
8
8
|
import { ValidationError } from '../errors.js';
|
|
9
9
|
/** @internal JIT-compiled hydrator that converts raw database rows into entity instances with optimized generated code. */
|
|
10
10
|
export class ObjectHydrator extends Hydrator {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
#tmpIndex = 0;
|
|
18
|
-
/**
|
|
19
|
-
* @inheritDoc
|
|
20
|
-
*/
|
|
21
|
-
hydrate(
|
|
22
|
-
entity,
|
|
23
|
-
meta,
|
|
24
|
-
data,
|
|
25
|
-
factory,
|
|
26
|
-
type,
|
|
27
|
-
newEntity = false,
|
|
28
|
-
convertCustomTypes = false,
|
|
29
|
-
schema,
|
|
30
|
-
parentSchema,
|
|
31
|
-
normalizeAccessors,
|
|
32
|
-
) {
|
|
33
|
-
const hydrate = this.getEntityHydrator(meta, type, normalizeAccessors);
|
|
34
|
-
const running = this.running;
|
|
35
|
-
// the running state is used to consider propagation as hydration, saving the values directly to the entity data,
|
|
36
|
-
// but we don't want that for new entities, their propagation should result in entity updates when flushing
|
|
37
|
-
this.running = !newEntity;
|
|
38
|
-
Utils.callCompiledFunction(
|
|
39
|
-
hydrate,
|
|
40
|
-
entity,
|
|
41
|
-
data,
|
|
42
|
-
factory,
|
|
43
|
-
newEntity,
|
|
44
|
-
convertCustomTypes,
|
|
45
|
-
schema,
|
|
46
|
-
parentSchema,
|
|
47
|
-
normalizeAccessors,
|
|
48
|
-
);
|
|
49
|
-
this.running = running;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* @inheritDoc
|
|
53
|
-
*/
|
|
54
|
-
hydrateReference(entity, meta, data, factory, convertCustomTypes = false, schema, parentSchema, normalizeAccessors) {
|
|
55
|
-
const hydrate = this.getEntityHydrator(meta, 'reference', normalizeAccessors);
|
|
56
|
-
const running = this.running;
|
|
57
|
-
this.running = true;
|
|
58
|
-
Utils.callCompiledFunction(
|
|
59
|
-
hydrate,
|
|
60
|
-
entity,
|
|
61
|
-
data,
|
|
62
|
-
factory,
|
|
63
|
-
false,
|
|
64
|
-
convertCustomTypes,
|
|
65
|
-
schema,
|
|
66
|
-
parentSchema,
|
|
67
|
-
normalizeAccessors,
|
|
68
|
-
);
|
|
69
|
-
this.running = running;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* @internal Highly performance-sensitive method.
|
|
73
|
-
*/
|
|
74
|
-
getEntityHydrator(meta, type, normalizeAccessors = false) {
|
|
75
|
-
const key = `${type}~${normalizeAccessors}`;
|
|
76
|
-
const exists = this.#hydrators[key].get(meta.class);
|
|
77
|
-
if (exists) {
|
|
78
|
-
return exists;
|
|
79
|
-
}
|
|
80
|
-
const lines = [];
|
|
81
|
-
const context = new Map();
|
|
82
|
-
const props = this.getProperties(meta, type);
|
|
83
|
-
context.set('isPrimaryKey', Utils.isPrimaryKey);
|
|
84
|
-
context.set('Collection', Collection);
|
|
85
|
-
context.set('Reference', Reference);
|
|
86
|
-
context.set('PolymorphicRef', PolymorphicRef);
|
|
87
|
-
context.set('ValidationError', ValidationError);
|
|
88
|
-
const registerCustomType = (prop, convertorKey, method, context) => {
|
|
89
|
-
context.set(`${method}_${convertorKey}`, val => {
|
|
90
|
-
/* v8 ignore next */
|
|
91
|
-
if (Raw.isKnownFragment(val)) {
|
|
92
|
-
return val;
|
|
93
|
-
}
|
|
94
|
-
return prop.customType[method](val, this.platform, { mode: 'serialization' });
|
|
95
|
-
});
|
|
96
|
-
return convertorKey;
|
|
97
|
-
};
|
|
98
|
-
const hydrateScalar = (prop, path, dataKey) => {
|
|
99
|
-
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
100
|
-
const tz = this.platform.getTimezone();
|
|
101
|
-
const convertorKey = path
|
|
102
|
-
.filter(k => !/\[idx_\d+]/.exec(k))
|
|
103
|
-
.map(k => this.safeKey(k))
|
|
104
|
-
.join('_');
|
|
105
|
-
const ret = [];
|
|
106
|
-
const idx = this.#tmpIndex++;
|
|
107
|
-
const nullVal = this.config.get('forceUndefined') ? 'undefined' : 'null';
|
|
108
|
-
if (prop.getter && !prop.setter && prop.persist === false) {
|
|
109
|
-
return [];
|
|
110
|
-
}
|
|
111
|
-
if (prop.ref) {
|
|
112
|
-
context.set('ScalarReference', ScalarReference);
|
|
113
|
-
ret.push(` const oldValue_${idx} = entity${entityKey};`);
|
|
114
|
-
}
|
|
115
|
-
ret.push(` if (data${dataKey} === null) {`);
|
|
116
|
-
if (prop.ref) {
|
|
117
|
-
ret.push(` entity${entityKey} = new ScalarReference();`);
|
|
118
|
-
ret.push(` entity${entityKey}.bind(entity, '${prop.name}');`);
|
|
119
|
-
ret.push(` entity${entityKey}.set(${nullVal});`);
|
|
120
|
-
} else {
|
|
121
|
-
ret.push(` entity${entityKey} = ${nullVal};`);
|
|
122
|
-
}
|
|
123
|
-
ret.push(` } else if (typeof data${dataKey} !== 'undefined') {`);
|
|
124
|
-
if (prop.customType) {
|
|
125
|
-
registerCustomType(prop, convertorKey, 'convertToJSValue', context);
|
|
126
|
-
registerCustomType(prop, convertorKey, 'convertToDatabaseValue', context);
|
|
127
|
-
ret.push(
|
|
128
|
-
` if (convertCustomTypes) {`,
|
|
129
|
-
` const value = convertToJSValue_${convertorKey}(data${dataKey});`,
|
|
130
|
-
);
|
|
131
|
-
if (prop.customType.ensureComparable(meta, prop)) {
|
|
132
|
-
ret.push(` data${dataKey} = convertToDatabaseValue_${convertorKey}(value);`);
|
|
133
|
-
}
|
|
134
|
-
ret.push(
|
|
135
|
-
` entity${entityKey} = value;`,
|
|
136
|
-
` } else {`,
|
|
137
|
-
` entity${entityKey} = data${dataKey};`,
|
|
138
|
-
` }`,
|
|
139
|
-
);
|
|
140
|
-
} else if (prop.runtimeType === 'boolean') {
|
|
141
|
-
ret.push(` entity${entityKey} = !!data${dataKey};`);
|
|
142
|
-
} else if (prop.runtimeType === 'Date' && !this.platform.isNumericProperty(prop)) {
|
|
143
|
-
ret.push(` if (data${dataKey} instanceof Date) {`);
|
|
144
|
-
ret.push(` entity${entityKey} = data${dataKey};`);
|
|
145
|
-
if (!tz || tz === 'local') {
|
|
146
|
-
ret.push(` } else {`);
|
|
147
|
-
ret.push(` entity${entityKey} = new Date(data${dataKey});`);
|
|
148
|
-
} else {
|
|
149
|
-
ret.push(
|
|
150
|
-
` } else if (typeof data${dataKey} === 'number' || data${dataKey}.includes('+') || data${dataKey}.lastIndexOf('-') > 10 || data${dataKey}.endsWith('Z')) {`,
|
|
151
|
-
);
|
|
152
|
-
ret.push(` entity${entityKey} = new Date(data${dataKey});`);
|
|
153
|
-
ret.push(` } else {`);
|
|
154
|
-
ret.push(` entity${entityKey} = new Date(data${dataKey} + '${tz}');`);
|
|
155
|
-
}
|
|
156
|
-
ret.push(` }`);
|
|
157
|
-
} else {
|
|
158
|
-
ret.push(` entity${entityKey} = data${dataKey};`);
|
|
159
|
-
}
|
|
160
|
-
if (prop.ref) {
|
|
161
|
-
ret.push(
|
|
162
|
-
` const value = entity${entityKey}?.__scalarReference ? entity${entityKey}.unwrap() : entity${entityKey};`,
|
|
163
|
-
);
|
|
164
|
-
ret.push(` entity${entityKey} = oldValue_${idx} ?? new ScalarReference(value);`);
|
|
165
|
-
ret.push(` entity${entityKey}.bind(entity, '${prop.name}');`);
|
|
166
|
-
ret.push(` entity${entityKey}.set(value);`);
|
|
167
|
-
}
|
|
168
|
-
ret.push(` }`);
|
|
169
|
-
if (prop.ref) {
|
|
170
|
-
ret.push(` if (!entity${entityKey}) {`);
|
|
171
|
-
ret.push(` entity${entityKey} = new ScalarReference();`);
|
|
172
|
-
ret.push(` entity${entityKey}.bind(entity, '${prop.name}');`);
|
|
173
|
-
ret.push(` }`);
|
|
174
|
-
}
|
|
175
|
-
return ret;
|
|
11
|
+
#hydrators = {
|
|
12
|
+
'full~true': new Map(),
|
|
13
|
+
'full~false': new Map(),
|
|
14
|
+
'reference~true': new Map(),
|
|
15
|
+
'reference~false': new Map(),
|
|
176
16
|
};
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
// For regular: fixed target class, PK is the data itself
|
|
209
|
-
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
210
|
-
context.set(targetKey, prop.targetMeta.class);
|
|
211
|
-
if (prop.ref) {
|
|
212
|
-
ret.push(
|
|
213
|
-
` entity${entityKey} = Reference.create(factory.createReference(${targetKey}, data${dataKey}, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} }));`,
|
|
214
|
-
);
|
|
215
|
-
} else {
|
|
216
|
-
ret.push(
|
|
217
|
-
` entity${entityKey} = factory.createReference(${targetKey}, data${dataKey}, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} });`,
|
|
218
|
-
);
|
|
17
|
+
#tmpIndex = 0;
|
|
18
|
+
/**
|
|
19
|
+
* @inheritDoc
|
|
20
|
+
*/
|
|
21
|
+
hydrate(entity, meta, data, factory, type, newEntity = false, convertCustomTypes = false, schema, parentSchema, normalizeAccessors) {
|
|
22
|
+
const hydrate = this.getEntityHydrator(meta, type, normalizeAccessors);
|
|
23
|
+
const running = this.running;
|
|
24
|
+
// the running state is used to consider propagation as hydration, saving the values directly to the entity data,
|
|
25
|
+
// but we don't want that for new entities, their propagation should result in entity updates when flushing
|
|
26
|
+
this.running = !newEntity;
|
|
27
|
+
Utils.callCompiledFunction(hydrate, entity, data, factory, newEntity, convertCustomTypes, schema, parentSchema, normalizeAccessors);
|
|
28
|
+
this.running = running;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @inheritDoc
|
|
32
|
+
*/
|
|
33
|
+
hydrateReference(entity, meta, data, factory, convertCustomTypes = false, schema, parentSchema, normalizeAccessors) {
|
|
34
|
+
const hydrate = this.getEntityHydrator(meta, 'reference', normalizeAccessors);
|
|
35
|
+
const running = this.running;
|
|
36
|
+
this.running = true;
|
|
37
|
+
Utils.callCompiledFunction(hydrate, entity, data, factory, false, convertCustomTypes, schema, parentSchema, normalizeAccessors);
|
|
38
|
+
this.running = running;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @internal Highly performance-sensitive method.
|
|
42
|
+
*/
|
|
43
|
+
getEntityHydrator(meta, type, normalizeAccessors = false) {
|
|
44
|
+
const key = `${type}~${normalizeAccessors}`;
|
|
45
|
+
const exists = this.#hydrators[key].get(meta.class);
|
|
46
|
+
if (exists) {
|
|
47
|
+
return exists;
|
|
219
48
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
)
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
49
|
+
const lines = [];
|
|
50
|
+
const context = new Map();
|
|
51
|
+
const props = this.getProperties(meta, type);
|
|
52
|
+
context.set('isPrimaryKey', Utils.isPrimaryKey);
|
|
53
|
+
context.set('Collection', Collection);
|
|
54
|
+
context.set('Reference', Reference);
|
|
55
|
+
context.set('PolymorphicRef', PolymorphicRef);
|
|
56
|
+
context.set('ValidationError', ValidationError);
|
|
57
|
+
const registerCustomType = (prop, convertorKey, method, context) => {
|
|
58
|
+
context.set(`${method}_${convertorKey}`, (val) => {
|
|
59
|
+
/* v8 ignore next */
|
|
60
|
+
if (Raw.isKnownFragment(val)) {
|
|
61
|
+
return val;
|
|
62
|
+
}
|
|
63
|
+
return prop.customType[method](val, this.platform, { mode: 'serialization' });
|
|
64
|
+
});
|
|
65
|
+
return convertorKey;
|
|
66
|
+
};
|
|
67
|
+
const hydrateScalar = (prop, path, dataKey) => {
|
|
68
|
+
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
69
|
+
const tz = this.platform.getTimezone();
|
|
70
|
+
const convertorKey = path
|
|
71
|
+
.filter(k => !/\[idx_\d+]/.exec(k))
|
|
72
|
+
.map(k => this.safeKey(k))
|
|
73
|
+
.join('_');
|
|
74
|
+
const ret = [];
|
|
75
|
+
const idx = this.#tmpIndex++;
|
|
76
|
+
const nullVal = this.config.get('forceUndefined') ? 'undefined' : 'null';
|
|
77
|
+
if (prop.getter && !prop.setter && prop.persist === false) {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
if (prop.ref) {
|
|
81
|
+
context.set('ScalarReference', ScalarReference);
|
|
82
|
+
ret.push(` const oldValue_${idx} = entity${entityKey};`);
|
|
83
|
+
}
|
|
84
|
+
ret.push(` if (data${dataKey} === null) {`);
|
|
85
|
+
if (prop.ref) {
|
|
86
|
+
ret.push(` entity${entityKey} = new ScalarReference();`);
|
|
87
|
+
ret.push(` entity${entityKey}.bind(entity, '${prop.name}');`);
|
|
88
|
+
ret.push(` entity${entityKey}.set(${nullVal});`);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
ret.push(` entity${entityKey} = ${nullVal};`);
|
|
92
|
+
}
|
|
93
|
+
ret.push(` } else if (typeof data${dataKey} !== 'undefined') {`);
|
|
94
|
+
if (prop.customType) {
|
|
95
|
+
registerCustomType(prop, convertorKey, 'convertToJSValue', context);
|
|
96
|
+
registerCustomType(prop, convertorKey, 'convertToDatabaseValue', context);
|
|
97
|
+
ret.push(` if (convertCustomTypes) {`, ` const value = convertToJSValue_${convertorKey}(data${dataKey});`);
|
|
98
|
+
if (prop.customType.ensureComparable(meta, prop)) {
|
|
99
|
+
ret.push(` data${dataKey} = convertToDatabaseValue_${convertorKey}(value);`);
|
|
100
|
+
}
|
|
101
|
+
ret.push(` entity${entityKey} = value;`, ` } else {`, ` entity${entityKey} = data${dataKey};`, ` }`);
|
|
102
|
+
}
|
|
103
|
+
else if (prop.runtimeType === 'boolean') {
|
|
104
|
+
ret.push(` entity${entityKey} = !!data${dataKey};`);
|
|
105
|
+
}
|
|
106
|
+
else if (prop.runtimeType === 'Date' && !this.platform.isNumericProperty(prop)) {
|
|
107
|
+
ret.push(` if (data${dataKey} instanceof Date) {`);
|
|
108
|
+
ret.push(` entity${entityKey} = data${dataKey};`);
|
|
109
|
+
if (!tz || tz === 'local') {
|
|
110
|
+
ret.push(` } else {`);
|
|
111
|
+
ret.push(` entity${entityKey} = new Date(data${dataKey});`);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
ret.push(` } else if (typeof data${dataKey} === 'number' || data${dataKey}.includes('+') || data${dataKey}.lastIndexOf('-') > 10 || data${dataKey}.endsWith('Z')) {`);
|
|
115
|
+
ret.push(` entity${entityKey} = new Date(data${dataKey});`);
|
|
116
|
+
ret.push(` } else {`);
|
|
117
|
+
ret.push(` entity${entityKey} = new Date(data${dataKey} + '${tz}');`);
|
|
118
|
+
}
|
|
119
|
+
ret.push(` }`);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
ret.push(` entity${entityKey} = data${dataKey};`);
|
|
123
|
+
}
|
|
124
|
+
if (prop.ref) {
|
|
125
|
+
ret.push(` const value = entity${entityKey}?.__scalarReference ? entity${entityKey}.unwrap() : entity${entityKey};`);
|
|
126
|
+
ret.push(` entity${entityKey} = oldValue_${idx} ?? new ScalarReference(value);`);
|
|
127
|
+
ret.push(` entity${entityKey}.bind(entity, '${prop.name}');`);
|
|
128
|
+
ret.push(` entity${entityKey}.set(value);`);
|
|
129
|
+
}
|
|
130
|
+
ret.push(` }`);
|
|
131
|
+
if (prop.ref) {
|
|
132
|
+
ret.push(` if (!entity${entityKey}) {`);
|
|
133
|
+
ret.push(` entity${entityKey} = new ScalarReference();`);
|
|
134
|
+
ret.push(` entity${entityKey}.bind(entity, '${prop.name}');`);
|
|
135
|
+
ret.push(` }`);
|
|
136
|
+
}
|
|
137
|
+
return ret;
|
|
138
|
+
};
|
|
139
|
+
const hydrateToOne = (prop, dataKey, entityKey) => {
|
|
140
|
+
const ret = [];
|
|
141
|
+
const method = type === 'reference' ? 'createReference' : 'create';
|
|
142
|
+
const nullVal = this.config.get('forceUndefined') ? 'undefined' : 'null';
|
|
143
|
+
ret.push(` if (data${dataKey} === null) {\n entity${entityKey} = ${nullVal};`);
|
|
144
|
+
ret.push(` } else if (typeof data${dataKey} !== 'undefined') {`);
|
|
145
|
+
// For polymorphic: instanceof check; for regular: isPrimaryKey() check
|
|
146
|
+
const pkCheck = prop.polymorphic
|
|
147
|
+
? `data${dataKey} instanceof PolymorphicRef`
|
|
148
|
+
: `isPrimaryKey(data${dataKey}, true)`;
|
|
149
|
+
ret.push(` if (${pkCheck}) {`);
|
|
150
|
+
// When targetKey is set, pass the key option to createReference so it uses the alternate key
|
|
151
|
+
const keyOption = prop.targetKey ? `, key: '${prop.targetKey}'` : '';
|
|
152
|
+
if (prop.polymorphic) {
|
|
153
|
+
// For polymorphic: target class from discriminator map, PK from data.id
|
|
154
|
+
const discriminatorMapKey = this.safeKey(`discriminatorMap_${prop.name}_${this.#tmpIndex++}`);
|
|
155
|
+
context.set(discriminatorMapKey, prop.discriminatorMap);
|
|
156
|
+
ret.push(` const targetClass = ${discriminatorMapKey}[data${dataKey}.discriminator];`);
|
|
157
|
+
ret.push(` if (!targetClass) throw new ValidationError(\`Unknown discriminator value '\${data${dataKey}.discriminator}' for polymorphic relation '${prop.name}'. Valid values: \${Object.keys(${discriminatorMapKey}).join(', ')}\`);`);
|
|
158
|
+
if (prop.ref) {
|
|
159
|
+
ret.push(` entity${entityKey} = Reference.create(factory.createReference(targetClass, data${dataKey}.id, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} }));`);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
ret.push(` entity${entityKey} = factory.createReference(targetClass, data${dataKey}.id, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} });`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
// For regular: fixed target class, PK is the data itself
|
|
167
|
+
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
168
|
+
context.set(targetKey, prop.targetMeta.class);
|
|
169
|
+
if (prop.ref) {
|
|
170
|
+
ret.push(` entity${entityKey} = Reference.create(factory.createReference(${targetKey}, data${dataKey}, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} }));`);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
ret.push(` entity${entityKey} = factory.createReference(${targetKey}, data${dataKey}, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} });`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
ret.push(` } else if (data${dataKey} && typeof data${dataKey} === 'object') {`);
|
|
177
|
+
// For full entity hydration, polymorphic needs to determine target class from entity itself
|
|
178
|
+
let hydrateTargetExpr;
|
|
179
|
+
if (prop.polymorphic) {
|
|
180
|
+
hydrateTargetExpr = `data${dataKey}.constructor`;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
184
|
+
context.set(targetKey, prop.targetMeta.class);
|
|
185
|
+
hydrateTargetExpr = targetKey;
|
|
186
|
+
}
|
|
187
|
+
if (prop.ref) {
|
|
188
|
+
ret.push(` entity${entityKey} = Reference.create(factory.${method}(${hydrateTargetExpr}, data${dataKey}, { initialized: true, merge: true, newEntity, convertCustomTypes, normalizeAccessors, schema }));`);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
ret.push(` entity${entityKey} = factory.${method}(${hydrateTargetExpr}, data${dataKey}, { initialized: true, merge: true, newEntity, convertCustomTypes, normalizeAccessors, schema });`);
|
|
192
|
+
}
|
|
193
|
+
ret.push(` }`);
|
|
194
|
+
ret.push(` }`);
|
|
195
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
196
|
+
const meta2 = this.metadata.get(prop.targetMeta.class);
|
|
197
|
+
const prop2 = meta2.properties[prop.inversedBy || prop.mappedBy];
|
|
198
|
+
if (prop2 && !prop2.mapToPk) {
|
|
199
|
+
ret.push(` if (data${dataKey} && entity${entityKey} && !entity${entityKey}.${this.safeKey(prop2.name)}) {`);
|
|
200
|
+
ret.push(` entity${entityKey}.${prop.ref ? 'unwrap().' : ''}${this.safeKey(prop2.name)} = ${prop2.ref ? 'Reference.create(entity)' : 'entity'};`);
|
|
201
|
+
ret.push(` }`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (prop.customType?.ensureComparable(meta, prop)) {
|
|
205
|
+
registerCustomType(prop, this.safeKey(prop.name), 'convertToDatabaseValue', context);
|
|
206
|
+
ret.push(` if (data${dataKey} != null && typeof data${dataKey} !== 'object' && convertCustomTypes) {`);
|
|
207
|
+
ret.push(` data${dataKey} = convertToDatabaseValue_${this.safeKey(prop.name)}(entity${entityKey}.__helper.getPrimaryKey());`);
|
|
208
|
+
ret.push(` }`);
|
|
209
|
+
}
|
|
210
|
+
return ret;
|
|
211
|
+
};
|
|
212
|
+
const hydrateToMany = (prop, dataKey, entityKey) => {
|
|
213
|
+
const ret = [];
|
|
214
|
+
ret.push(...this.createCollectionItemMapper(prop, context));
|
|
215
|
+
ret.push(` if (data${dataKey} && !Array.isArray(data${dataKey}) && typeof data${dataKey} === 'object') {`);
|
|
216
|
+
ret.push(` data${dataKey} = [data${dataKey}];`);
|
|
217
|
+
ret.push(` }`);
|
|
218
|
+
ret.push(` if (Array.isArray(data${dataKey})) {`);
|
|
219
|
+
ret.push(` const items = data${dataKey}.map(value => createCollectionItem_${this.safeKey(prop.name)}(value, entity));`);
|
|
220
|
+
ret.push(` const coll = Collection.create(entity, '${prop.name}', items, newEntity);`);
|
|
221
|
+
ret.push(` if (newEntity) {`);
|
|
222
|
+
ret.push(` coll.setDirty();`);
|
|
223
|
+
ret.push(` } else {`);
|
|
224
|
+
ret.push(` coll.takeSnapshot(true);`);
|
|
225
|
+
ret.push(` }`);
|
|
226
|
+
ret.push(` } else if (!entity${entityKey} && data${dataKey} instanceof Collection) {`);
|
|
227
|
+
ret.push(` entity${entityKey} = data${dataKey};`);
|
|
228
|
+
if (!this.platform.usesPivotTable() && prop.owner && prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
229
|
+
ret.push(` } else if (!entity${entityKey} && Array.isArray(data${dataKey})) {`);
|
|
230
|
+
const items = this.platform.usesPivotTable() || !prop.owner ? 'undefined' : '[]';
|
|
231
|
+
ret.push(` const coll = Collection.create(entity, '${prop.name}', ${items}, !!data${dataKey} || newEntity);`);
|
|
232
|
+
ret.push(` coll.setDirty(false);`);
|
|
233
|
+
}
|
|
234
|
+
ret.push(` } else if (!entity${entityKey}) {`);
|
|
235
|
+
ret.push(` const coll = Collection.create(entity, '${prop.name}', undefined, newEntity);`);
|
|
236
|
+
ret.push(` coll.setDirty(false);`);
|
|
237
|
+
ret.push(` }`);
|
|
238
|
+
return ret;
|
|
239
|
+
};
|
|
240
|
+
const registerEmbeddedPrototype = (prop, path) => {
|
|
241
|
+
const convertorKey = path
|
|
242
|
+
.filter(k => !/\[idx_\d+]/.exec(k))
|
|
243
|
+
.map(k => this.safeKey(k))
|
|
244
|
+
.join('_');
|
|
245
|
+
if (prop.targetMeta?.polymorphs) {
|
|
246
|
+
prop.targetMeta.polymorphs.forEach(meta => {
|
|
247
|
+
context.set(`prototype_${convertorKey}_${meta.className}`, meta.prototype);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
context.set(`prototype_${convertorKey}`, prop.embeddable.prototype);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const parseObjectEmbeddable = (prop, dataKey, ret) => {
|
|
255
|
+
if (!this.platform.convertsJsonAutomatically() && (prop.object || prop.array)) {
|
|
256
|
+
context.set('parseJsonSafe', parseJsonSafe);
|
|
257
|
+
ret.push(` if (typeof data${dataKey} === 'string') {`, ` data${dataKey} = parseJsonSafe(data${dataKey});`, ` }`);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const createCond = (prop, dataKey, cond) => {
|
|
261
|
+
const conds = [];
|
|
262
|
+
if (prop.object) {
|
|
263
|
+
conds.push(`data${dataKey} ${cond ?? '!= null'}`);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
const notNull = cond ?? (prop.nullable ? '!= null' : '!== undefined');
|
|
267
|
+
meta.props
|
|
268
|
+
.filter(p => p.embedded?.[0] === prop.name)
|
|
269
|
+
.forEach(p => {
|
|
270
|
+
if (p.kind === ReferenceKind.EMBEDDED && !p.object && !p.array) {
|
|
271
|
+
conds.push(...createCond(p, dataKey + this.wrap(p.embedded[1]), cond));
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
conds.push(`data${this.wrap(p.name)} ${notNull}`);
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return conds;
|
|
278
|
+
};
|
|
279
|
+
const hydrateEmbedded = (prop, path, dataKey) => {
|
|
280
|
+
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
281
|
+
const ret = [];
|
|
282
|
+
registerEmbeddedPrototype(prop, path);
|
|
283
|
+
parseObjectEmbeddable(prop, dataKey, ret);
|
|
284
|
+
ret.push(` if (${createCond(prop, dataKey).join(' || ')}) {`);
|
|
285
|
+
if (prop.object) {
|
|
286
|
+
ret.push(` const embeddedData = data${dataKey};`);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
ret.push(` const embeddedData = {`);
|
|
290
|
+
for (const childProp of Object.values(prop.embeddedProps)) {
|
|
291
|
+
const key = /^\w+$/.exec(childProp.embedded[1]) ? childProp.embedded[1] : `'${childProp.embedded[1]}'`;
|
|
292
|
+
ret.push(` ${key}: data${this.wrap(childProp.name)},`);
|
|
293
|
+
}
|
|
294
|
+
ret.push(` };`);
|
|
295
|
+
}
|
|
296
|
+
if (prop.targetMeta?.polymorphs) {
|
|
297
|
+
prop.targetMeta.polymorphs.forEach(childMeta => {
|
|
298
|
+
const childProp = prop.embeddedProps[prop.targetMeta.discriminatorColumn];
|
|
299
|
+
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
300
|
+
context.set(childMeta.className, childMeta.class);
|
|
301
|
+
// weak comparison as we can have numbers that might have been converted to strings due to being object keys
|
|
302
|
+
ret.push(` if (data${childDataKey} == '${childMeta.discriminatorValue}') {`);
|
|
303
|
+
ret.push(` if (entity${entityKey} == null) {`);
|
|
304
|
+
ret.push(` entity${entityKey} = factory.createEmbeddable(${childMeta.className}, embeddedData, { newEntity, convertCustomTypes, normalizeAccessors });`);
|
|
305
|
+
ret.push(` }`);
|
|
306
|
+
meta.props
|
|
307
|
+
.filter(p => p.embedded?.[0] === prop.name)
|
|
308
|
+
.forEach(childProp => {
|
|
309
|
+
const childDataKey = prop.object
|
|
310
|
+
? dataKey + this.wrap(childProp.embedded[1])
|
|
311
|
+
: this.wrap(childProp.name);
|
|
312
|
+
const prop2 = childMeta.properties[childProp.embedded[1]];
|
|
313
|
+
const prop3 = {
|
|
314
|
+
...prop2,
|
|
315
|
+
name: childProp.name,
|
|
316
|
+
embedded: childProp.embedded,
|
|
317
|
+
embeddedProps: childProp.embeddedProps,
|
|
318
|
+
};
|
|
319
|
+
ret.push(
|
|
320
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define, no-use-before-define
|
|
321
|
+
...hydrateProperty(prop3, childProp.object, [...path, childProp.embedded[1]], childDataKey).map(l => ' ' + l));
|
|
322
|
+
});
|
|
323
|
+
ret.push(` }`);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
328
|
+
context.set(targetKey, prop.targetMeta.class);
|
|
329
|
+
ret.push(` if (entity${entityKey} == null) {`);
|
|
330
|
+
ret.push(` entity${entityKey} = factory.createEmbeddable(${targetKey}, embeddedData, { newEntity, convertCustomTypes, normalizeAccessors });`);
|
|
331
|
+
ret.push(` }`);
|
|
332
|
+
meta.props
|
|
333
|
+
.filter(p => p.embedded?.[0] === prop.name)
|
|
334
|
+
.forEach(childProp => {
|
|
335
|
+
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
336
|
+
ret.push(
|
|
337
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define, no-use-before-define
|
|
338
|
+
...hydrateProperty(childProp, prop.object, [...path, childProp.embedded[1]], childDataKey).map(l => ' ' + l));
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
/* v8 ignore next */
|
|
342
|
+
const nullVal = this.config.get('forceUndefined') ? 'undefined' : 'null';
|
|
343
|
+
if (prop.object) {
|
|
344
|
+
ret.push(` } else if (data${dataKey} === null) {`);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
ret.push(` } else if (${createCond(prop, dataKey, '=== null').join(' && ')}) {`);
|
|
348
|
+
}
|
|
349
|
+
ret.push(` entity${entityKey} = ${nullVal};`);
|
|
350
|
+
ret.push(` }`);
|
|
351
|
+
return ret;
|
|
352
|
+
};
|
|
353
|
+
const hydrateEmbeddedArray = (prop, path, dataKey) => {
|
|
354
|
+
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
355
|
+
const ret = [];
|
|
356
|
+
const idx = this.#tmpIndex++;
|
|
357
|
+
registerEmbeddedPrototype(prop, path);
|
|
358
|
+
parseObjectEmbeddable(prop, dataKey, ret);
|
|
359
|
+
ret.push(` if (Array.isArray(data${dataKey})) {`);
|
|
360
|
+
ret.push(` entity${entityKey} = [];`);
|
|
361
|
+
ret.push(` data${dataKey}.forEach((_, idx_${idx}) => {`);
|
|
362
|
+
ret.push(...hydrateEmbedded(prop, [...path, `[idx_${idx}]`], `${dataKey}[idx_${idx}]`).map(l => ' ' + l));
|
|
363
|
+
ret.push(` });`);
|
|
364
|
+
ret.push(` }`);
|
|
365
|
+
return ret;
|
|
366
|
+
};
|
|
367
|
+
const hydrateProperty = (prop, object = prop.object, path = [prop.name], dataKey) => {
|
|
368
|
+
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
369
|
+
dataKey =
|
|
370
|
+
dataKey ?? (object ? entityKey : this.wrap(normalizeAccessors ? (prop.accessor ?? prop.name) : prop.name));
|
|
371
|
+
const ret = [];
|
|
372
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && !prop.mapToPk) {
|
|
373
|
+
ret.push(...hydrateToOne(prop, dataKey, entityKey));
|
|
374
|
+
}
|
|
375
|
+
else if (prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
376
|
+
ret.push(...hydrateToMany(prop, dataKey, entityKey));
|
|
377
|
+
}
|
|
378
|
+
else if (prop.kind === ReferenceKind.EMBEDDED) {
|
|
379
|
+
if (prop.array) {
|
|
380
|
+
ret.push(...hydrateEmbeddedArray(prop, path, dataKey));
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
ret.push(...hydrateEmbedded(prop, path, dataKey));
|
|
384
|
+
if (!prop.object) {
|
|
385
|
+
ret.push(...hydrateEmbedded({ ...prop, object: true }, path, dataKey));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
// ReferenceKind.SCALAR
|
|
391
|
+
ret.push(...hydrateScalar(prop, path, dataKey));
|
|
392
|
+
}
|
|
393
|
+
if (this.config.get('forceUndefined')) {
|
|
394
|
+
ret.push(` if (data${dataKey} === null) entity${entityKey} = undefined;`);
|
|
395
|
+
}
|
|
396
|
+
return ret;
|
|
397
|
+
};
|
|
398
|
+
for (const prop of props) {
|
|
399
|
+
lines.push(...hydrateProperty(prop));
|
|
251
400
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
ret.push(` if (Array.isArray(data${dataKey})) {`);
|
|
270
|
-
ret.push(
|
|
271
|
-
` const items = data${dataKey}.map(value => createCollectionItem_${this.safeKey(prop.name)}(value, entity));`,
|
|
272
|
-
);
|
|
273
|
-
ret.push(` const coll = Collection.create(entity, '${prop.name}', items, newEntity);`);
|
|
274
|
-
ret.push(` if (newEntity) {`);
|
|
275
|
-
ret.push(` coll.setDirty();`);
|
|
276
|
-
ret.push(` } else {`);
|
|
277
|
-
ret.push(` coll.takeSnapshot(true);`);
|
|
278
|
-
ret.push(` }`);
|
|
279
|
-
ret.push(` } else if (!entity${entityKey} && data${dataKey} instanceof Collection) {`);
|
|
280
|
-
ret.push(` entity${entityKey} = data${dataKey};`);
|
|
281
|
-
if (!this.platform.usesPivotTable() && prop.owner && prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
282
|
-
ret.push(` } else if (!entity${entityKey} && Array.isArray(data${dataKey})) {`);
|
|
283
|
-
const items = this.platform.usesPivotTable() || !prop.owner ? 'undefined' : '[]';
|
|
284
|
-
ret.push(
|
|
285
|
-
` const coll = Collection.create(entity, '${prop.name}', ${items}, !!data${dataKey} || newEntity);`,
|
|
286
|
-
);
|
|
287
|
-
ret.push(` coll.setDirty(false);`);
|
|
288
|
-
}
|
|
289
|
-
ret.push(` } else if (!entity${entityKey}) {`);
|
|
290
|
-
ret.push(` const coll = Collection.create(entity, '${prop.name}', undefined, newEntity);`);
|
|
291
|
-
ret.push(` coll.setDirty(false);`);
|
|
292
|
-
ret.push(` }`);
|
|
293
|
-
return ret;
|
|
294
|
-
};
|
|
295
|
-
const registerEmbeddedPrototype = (prop, path) => {
|
|
296
|
-
const convertorKey = path
|
|
297
|
-
.filter(k => !/\[idx_\d+]/.exec(k))
|
|
298
|
-
.map(k => this.safeKey(k))
|
|
299
|
-
.join('_');
|
|
300
|
-
if (prop.targetMeta?.polymorphs) {
|
|
301
|
-
prop.targetMeta.polymorphs.forEach(meta => {
|
|
302
|
-
context.set(`prototype_${convertorKey}_${meta.className}`, meta.prototype);
|
|
303
|
-
});
|
|
304
|
-
} else {
|
|
305
|
-
context.set(`prototype_${convertorKey}`, prop.embeddable.prototype);
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
const parseObjectEmbeddable = (prop, dataKey, ret) => {
|
|
309
|
-
if (!this.platform.convertsJsonAutomatically() && (prop.object || prop.array)) {
|
|
310
|
-
context.set('parseJsonSafe', parseJsonSafe);
|
|
311
|
-
ret.push(
|
|
312
|
-
` if (typeof data${dataKey} === 'string') {`,
|
|
313
|
-
` data${dataKey} = parseJsonSafe(data${dataKey});`,
|
|
314
|
-
` }`,
|
|
315
|
-
);
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
const createCond = (prop, dataKey, cond) => {
|
|
319
|
-
const conds = [];
|
|
320
|
-
if (prop.object) {
|
|
321
|
-
conds.push(`data${dataKey} ${cond ?? '!= null'}`);
|
|
322
|
-
} else {
|
|
323
|
-
const notNull = cond ?? (prop.nullable ? '!= null' : '!== undefined');
|
|
324
|
-
meta.props
|
|
325
|
-
.filter(p => p.embedded?.[0] === prop.name)
|
|
326
|
-
.forEach(p => {
|
|
327
|
-
if (p.kind === ReferenceKind.EMBEDDED && !p.object && !p.array) {
|
|
328
|
-
conds.push(...createCond(p, dataKey + this.wrap(p.embedded[1]), cond));
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
conds.push(`data${this.wrap(p.name)} ${notNull}`);
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
return conds;
|
|
335
|
-
};
|
|
336
|
-
const hydrateEmbedded = (prop, path, dataKey) => {
|
|
337
|
-
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
338
|
-
const ret = [];
|
|
339
|
-
registerEmbeddedPrototype(prop, path);
|
|
340
|
-
parseObjectEmbeddable(prop, dataKey, ret);
|
|
341
|
-
ret.push(` if (${createCond(prop, dataKey).join(' || ')}) {`);
|
|
342
|
-
if (prop.object) {
|
|
343
|
-
ret.push(` const embeddedData = data${dataKey};`);
|
|
344
|
-
} else {
|
|
345
|
-
ret.push(` const embeddedData = {`);
|
|
346
|
-
for (const childProp of Object.values(prop.embeddedProps)) {
|
|
347
|
-
const key = /^\w+$/.exec(childProp.embedded[1]) ? childProp.embedded[1] : `'${childProp.embedded[1]}'`;
|
|
348
|
-
ret.push(` ${key}: data${this.wrap(childProp.name)},`);
|
|
401
|
+
const code = `// compiled hydrator for entity ${meta.className} (${type + normalizeAccessors ? ' normalized' : ''})\n` +
|
|
402
|
+
`return function(entity, data, factory, newEntity, convertCustomTypes, schema, parentSchema, normalizeAccessors) {\n` +
|
|
403
|
+
`${lines.join('\n')}\n}`;
|
|
404
|
+
const fnKey = `hydrator-${meta.uniqueName}-${type}-${normalizeAccessors}`;
|
|
405
|
+
const hydrator = Utils.createFunction(context, code, this.config.get('compiledFunctions'), fnKey);
|
|
406
|
+
this.#hydrators[key].set(meta.class, hydrator);
|
|
407
|
+
return hydrator;
|
|
408
|
+
}
|
|
409
|
+
createCollectionItemMapper(prop, context) {
|
|
410
|
+
const meta = this.metadata.get(prop.targetMeta.class);
|
|
411
|
+
const lines = [];
|
|
412
|
+
lines.push(` const createCollectionItem_${this.safeKey(prop.name)} = (value, entity) => {`);
|
|
413
|
+
const prop2 = prop.targetMeta.properties[prop.mappedBy];
|
|
414
|
+
if (prop.kind === ReferenceKind.ONE_TO_MANY && prop2.primary) {
|
|
415
|
+
lines.push(` if (typeof value === 'object' && value?.['${prop2.name}'] == null) {`);
|
|
416
|
+
lines.push(` value = { ...value, ['${prop2.name}']: Reference.wrapReference(entity, { ref: ${prop2.ref} }) };`);
|
|
417
|
+
lines.push(` }`);
|
|
349
418
|
}
|
|
350
|
-
ret.push(` };`);
|
|
351
|
-
}
|
|
352
|
-
if (prop.targetMeta?.polymorphs) {
|
|
353
|
-
prop.targetMeta.polymorphs.forEach(childMeta => {
|
|
354
|
-
const childProp = prop.embeddedProps[prop.targetMeta.discriminatorColumn];
|
|
355
|
-
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
356
|
-
context.set(childMeta.className, childMeta.class);
|
|
357
|
-
// weak comparison as we can have numbers that might have been converted to strings due to being object keys
|
|
358
|
-
ret.push(` if (data${childDataKey} == '${childMeta.discriminatorValue}') {`);
|
|
359
|
-
ret.push(` if (entity${entityKey} == null) {`);
|
|
360
|
-
ret.push(
|
|
361
|
-
` entity${entityKey} = factory.createEmbeddable(${childMeta.className}, embeddedData, { newEntity, convertCustomTypes, normalizeAccessors });`,
|
|
362
|
-
);
|
|
363
|
-
ret.push(` }`);
|
|
364
|
-
meta.props
|
|
365
|
-
.filter(p => p.embedded?.[0] === prop.name)
|
|
366
|
-
.forEach(childProp => {
|
|
367
|
-
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
368
|
-
const prop2 = childMeta.properties[childProp.embedded[1]];
|
|
369
|
-
const prop3 = {
|
|
370
|
-
...prop2,
|
|
371
|
-
name: childProp.name,
|
|
372
|
-
embedded: childProp.embedded,
|
|
373
|
-
embeddedProps: childProp.embeddedProps,
|
|
374
|
-
};
|
|
375
|
-
ret.push(
|
|
376
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define, no-use-before-define
|
|
377
|
-
...hydrateProperty(prop3, childProp.object, [...path, childProp.embedded[1]], childDataKey).map(
|
|
378
|
-
l => ' ' + l,
|
|
379
|
-
),
|
|
380
|
-
);
|
|
381
|
-
});
|
|
382
|
-
ret.push(` }`);
|
|
383
|
-
});
|
|
384
|
-
} else {
|
|
385
419
|
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
386
420
|
context.set(targetKey, prop.targetMeta.class);
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
);
|
|
391
|
-
|
|
392
|
-
meta.props
|
|
393
|
-
.filter(p => p.embedded?.[0] === prop.name)
|
|
394
|
-
.forEach(childProp => {
|
|
395
|
-
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
396
|
-
ret.push(
|
|
397
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define, no-use-before-define
|
|
398
|
-
...hydrateProperty(childProp, prop.object, [...path, childProp.embedded[1]], childDataKey).map(
|
|
399
|
-
l => ' ' + l,
|
|
400
|
-
),
|
|
401
|
-
);
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
/* v8 ignore next */
|
|
405
|
-
const nullVal = this.config.get('forceUndefined') ? 'undefined' : 'null';
|
|
406
|
-
if (prop.object) {
|
|
407
|
-
ret.push(` } else if (data${dataKey} === null) {`);
|
|
408
|
-
} else {
|
|
409
|
-
ret.push(` } else if (${createCond(prop, dataKey, '=== null').join(' && ')}) {`);
|
|
410
|
-
}
|
|
411
|
-
ret.push(` entity${entityKey} = ${nullVal};`);
|
|
412
|
-
ret.push(` }`);
|
|
413
|
-
return ret;
|
|
414
|
-
};
|
|
415
|
-
const hydrateEmbeddedArray = (prop, path, dataKey) => {
|
|
416
|
-
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
417
|
-
const ret = [];
|
|
418
|
-
const idx = this.#tmpIndex++;
|
|
419
|
-
registerEmbeddedPrototype(prop, path);
|
|
420
|
-
parseObjectEmbeddable(prop, dataKey, ret);
|
|
421
|
-
ret.push(` if (Array.isArray(data${dataKey})) {`);
|
|
422
|
-
ret.push(` entity${entityKey} = [];`);
|
|
423
|
-
ret.push(` data${dataKey}.forEach((_, idx_${idx}) => {`);
|
|
424
|
-
ret.push(...hydrateEmbedded(prop, [...path, `[idx_${idx}]`], `${dataKey}[idx_${idx}]`).map(l => ' ' + l));
|
|
425
|
-
ret.push(` });`);
|
|
426
|
-
ret.push(` }`);
|
|
427
|
-
return ret;
|
|
428
|
-
};
|
|
429
|
-
const hydrateProperty = (prop, object = prop.object, path = [prop.name], dataKey) => {
|
|
430
|
-
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
431
|
-
dataKey =
|
|
432
|
-
dataKey ?? (object ? entityKey : this.wrap(normalizeAccessors ? (prop.accessor ?? prop.name) : prop.name));
|
|
433
|
-
const ret = [];
|
|
434
|
-
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && !prop.mapToPk) {
|
|
435
|
-
ret.push(...hydrateToOne(prop, dataKey, entityKey));
|
|
436
|
-
} else if (prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
437
|
-
ret.push(...hydrateToMany(prop, dataKey, entityKey));
|
|
438
|
-
} else if (prop.kind === ReferenceKind.EMBEDDED) {
|
|
439
|
-
if (prop.array) {
|
|
440
|
-
ret.push(...hydrateEmbeddedArray(prop, path, dataKey));
|
|
441
|
-
} else {
|
|
442
|
-
ret.push(...hydrateEmbedded(prop, path, dataKey));
|
|
443
|
-
if (!prop.object) {
|
|
444
|
-
ret.push(...hydrateEmbedded({ ...prop, object: true }, path, dataKey));
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
} else {
|
|
448
|
-
// ReferenceKind.SCALAR
|
|
449
|
-
ret.push(...hydrateScalar(prop, path, dataKey));
|
|
450
|
-
}
|
|
451
|
-
if (this.config.get('forceUndefined')) {
|
|
452
|
-
ret.push(` if (data${dataKey} === null) entity${entityKey} = undefined;`);
|
|
453
|
-
}
|
|
454
|
-
return ret;
|
|
455
|
-
};
|
|
456
|
-
for (const prop of props) {
|
|
457
|
-
lines.push(...hydrateProperty(prop));
|
|
421
|
+
lines.push(` if (isPrimaryKey(value, ${meta.compositePK})) return factory.createReference(${targetKey}, value, { convertCustomTypes, schema, normalizeAccessors, merge: true });`);
|
|
422
|
+
lines.push(` if (value && value.__entity) return value;`);
|
|
423
|
+
lines.push(` return factory.create(${targetKey}, value, { newEntity, convertCustomTypes, schema, normalizeAccessors, merge: true });`);
|
|
424
|
+
lines.push(` }`);
|
|
425
|
+
return lines;
|
|
458
426
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
const hydrator = Utils.createFunction(context, code, this.config.get('compiledFunctions'), fnKey);
|
|
465
|
-
this.#hydrators[key].set(meta.class, hydrator);
|
|
466
|
-
return hydrator;
|
|
467
|
-
}
|
|
468
|
-
createCollectionItemMapper(prop, context) {
|
|
469
|
-
const meta = this.metadata.get(prop.targetMeta.class);
|
|
470
|
-
const lines = [];
|
|
471
|
-
lines.push(` const createCollectionItem_${this.safeKey(prop.name)} = (value, entity) => {`);
|
|
472
|
-
const prop2 = prop.targetMeta.properties[prop.mappedBy];
|
|
473
|
-
if (prop.kind === ReferenceKind.ONE_TO_MANY && prop2.primary) {
|
|
474
|
-
lines.push(` if (typeof value === 'object' && value?.['${prop2.name}'] == null) {`);
|
|
475
|
-
lines.push(
|
|
476
|
-
` value = { ...value, ['${prop2.name}']: Reference.wrapReference(entity, { ref: ${prop2.ref} }) };`,
|
|
477
|
-
);
|
|
478
|
-
lines.push(` }`);
|
|
427
|
+
wrap(key) {
|
|
428
|
+
if (/^\[.*]$/.exec(key)) {
|
|
429
|
+
return key;
|
|
430
|
+
}
|
|
431
|
+
return /^\w+$/.exec(key) ? `.${key}` : `['${key}']`;
|
|
479
432
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
lines.push(
|
|
483
|
-
` if (isPrimaryKey(value, ${meta.compositePK})) return factory.createReference(${targetKey}, value, { convertCustomTypes, schema, normalizeAccessors, merge: true });`,
|
|
484
|
-
);
|
|
485
|
-
lines.push(` if (value && value.__entity) return value;`);
|
|
486
|
-
lines.push(
|
|
487
|
-
` return factory.create(${targetKey}, value, { newEntity, convertCustomTypes, schema, normalizeAccessors, merge: true });`,
|
|
488
|
-
);
|
|
489
|
-
lines.push(` }`);
|
|
490
|
-
return lines;
|
|
491
|
-
}
|
|
492
|
-
wrap(key) {
|
|
493
|
-
if (/^\[.*]$/.exec(key)) {
|
|
494
|
-
return key;
|
|
433
|
+
safeKey(key) {
|
|
434
|
+
return key.replace(/\W/g, '_');
|
|
495
435
|
}
|
|
496
|
-
return /^\w+$/.exec(key) ? `.${key}` : `['${key}']`;
|
|
497
|
-
}
|
|
498
|
-
safeKey(key) {
|
|
499
|
-
return key.replace(/\W/g, '_');
|
|
500
|
-
}
|
|
501
436
|
}
|