@mikro-orm/core 7.0.4-dev.9 → 7.0.4
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 +1922 -1895
- 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 +212 -179
- package/entity/Collection.js +727 -721
- package/entity/EntityAssigner.d.ts +88 -77
- package/entity/EntityAssigner.js +231 -230
- package/entity/EntityFactory.d.ts +66 -54
- package/entity/EntityFactory.js +425 -383
- package/entity/EntityHelper.d.ts +34 -22
- package/entity/EntityHelper.js +280 -267
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +98 -72
- package/entity/EntityLoader.js +753 -723
- 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 +126 -82
- package/entity/Reference.js +278 -274
- package/entity/WrappedEntity.d.ts +115 -72
- package/entity/WrappedEntity.js +168 -166
- package/entity/defineEntity.d.ts +1315 -636
- 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 +481 -416
- 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 +211 -127
- package/metadata/EntitySchema.js +397 -398
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1951 -1863
- package/metadata/MetadataProvider.d.ts +24 -21
- package/metadata/MetadataProvider.js +82 -84
- 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 -27
- 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 +310 -299
- package/platforms/Platform.js +663 -636
- 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 +1250 -737
- package/typings.js +244 -231
- 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 +178 -170
- package/unit-of-work/ChangeSetPersister.d.ts +63 -44
- package/unit-of-work/ChangeSetPersister.js +442 -421
- 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 +1200 -1183
- 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 +98 -81
- package/utils/EntityComparator.js +828 -728
- 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 +812 -810
- package/utils/clone.js +104 -113
- 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
|
@@ -1,747 +1,847 @@
|
|
|
1
1
|
import { clone } from './clone.js';
|
|
2
2
|
import { ReferenceKind } from '../enums.js';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
compareArrays,
|
|
5
|
+
compareBooleans,
|
|
6
|
+
compareBuffers,
|
|
7
|
+
compareObjects,
|
|
8
|
+
equals,
|
|
9
|
+
parseJsonSafe,
|
|
10
|
+
Utils,
|
|
11
|
+
} from './Utils.js';
|
|
4
12
|
import { JsonType } from '../types/JsonType.js';
|
|
5
13
|
import { Raw } from './RawQueryFragment.js';
|
|
6
14
|
import { EntityIdentifier } from '../entity/EntityIdentifier.js';
|
|
7
15
|
import { PolymorphicRef } from '../entity/PolymorphicRef.js';
|
|
8
16
|
/** @internal Generates and caches JIT-compiled functions for comparing, snapshotting, and mapping entity data. */
|
|
9
17
|
export class EntityComparator {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
else {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
18
|
+
#comparators = new Map();
|
|
19
|
+
#mappers = new Map();
|
|
20
|
+
#snapshotGenerators = new Map();
|
|
21
|
+
#pkGetters = new Map();
|
|
22
|
+
#pkGettersConverted = new Map();
|
|
23
|
+
#pkSerializers = new Map();
|
|
24
|
+
#tmpIndex = 0;
|
|
25
|
+
#metadata;
|
|
26
|
+
#platform;
|
|
27
|
+
#config;
|
|
28
|
+
constructor(metadata, platform, config) {
|
|
29
|
+
this.#metadata = metadata;
|
|
30
|
+
this.#platform = platform;
|
|
31
|
+
this.#config = config;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Computes difference between two entities.
|
|
35
|
+
*/
|
|
36
|
+
diffEntities(entityName, a, b, options) {
|
|
37
|
+
const comparator = this.getEntityComparator(entityName);
|
|
38
|
+
return Utils.callCompiledFunction(comparator, a, b, options);
|
|
39
|
+
}
|
|
40
|
+
/** Returns true if two entity snapshots are identical (no differences). */
|
|
41
|
+
matching(entityName, a, b) {
|
|
42
|
+
const diff = this.diffEntities(entityName, a, b);
|
|
43
|
+
return Utils.getObjectKeysSize(diff) === 0;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Removes ORM specific code from entities and prepares it for serializing. Used before change set computation.
|
|
47
|
+
* References will be mapped to primary keys, collections to arrays of primary keys.
|
|
48
|
+
*/
|
|
49
|
+
prepareEntity(entity) {
|
|
50
|
+
const generator = this.getSnapshotGenerator(entity.constructor);
|
|
51
|
+
return Utils.callCompiledFunction(generator, entity);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Maps database columns to properties.
|
|
55
|
+
*/
|
|
56
|
+
mapResult(meta, result) {
|
|
57
|
+
const mapper = this.getResultMapper(meta);
|
|
58
|
+
return Utils.callCompiledFunction(mapper, result);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @internal Highly performance-sensitive method.
|
|
62
|
+
*/
|
|
63
|
+
getPkGetter(meta) {
|
|
64
|
+
const exists = this.#pkGetters.get(meta);
|
|
65
|
+
/* v8 ignore next */
|
|
66
|
+
if (exists) {
|
|
67
|
+
return exists;
|
|
68
|
+
}
|
|
69
|
+
const lines = [];
|
|
70
|
+
const context = new Map();
|
|
71
|
+
if (meta.primaryKeys.length > 1) {
|
|
72
|
+
lines.push(` const cond = {`);
|
|
73
|
+
meta.primaryKeys.forEach(pk => {
|
|
74
|
+
if (meta.properties[pk].kind !== ReferenceKind.SCALAR) {
|
|
75
|
+
lines.push(
|
|
76
|
+
` ${pk}: (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) ? entity${this.wrap(pk)}.__helper.getPrimaryKey() : entity${this.wrap(pk)},`,
|
|
77
|
+
);
|
|
78
|
+
} else {
|
|
79
|
+
lines.push(` ${pk}: entity${this.wrap(pk)},`);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
lines.push(` };`);
|
|
83
|
+
lines.push(` if (${meta.primaryKeys.map(pk => `cond.${pk} == null`).join(' || ')}) return null;`);
|
|
84
|
+
lines.push(` return cond;`);
|
|
85
|
+
} else {
|
|
86
|
+
const pk = meta.primaryKeys[0];
|
|
87
|
+
if (meta.properties[pk].kind !== ReferenceKind.SCALAR) {
|
|
88
|
+
lines.push(
|
|
89
|
+
` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) {`,
|
|
90
|
+
);
|
|
91
|
+
lines.push(` const pk = entity${this.wrap(pk)}.__helper.getPrimaryKey();`);
|
|
92
|
+
if (meta.properties[pk].targetMeta.compositePK) {
|
|
93
|
+
lines.push(` if (typeof pk === 'object' && pk != null) {`);
|
|
94
|
+
lines.push(` return [`);
|
|
95
|
+
for (const childPK of meta.properties[pk].targetMeta.primaryKeys) {
|
|
96
|
+
lines.push(` pk${this.wrap(childPK)},`);
|
|
97
|
+
}
|
|
98
|
+
lines.push(` ];`);
|
|
99
|
+
lines.push(` }`);
|
|
100
|
+
}
|
|
101
|
+
lines.push(` return pk;`);
|
|
102
|
+
lines.push(` }`);
|
|
103
|
+
}
|
|
104
|
+
lines.push(` return entity${this.wrap(pk)};`);
|
|
105
|
+
}
|
|
106
|
+
const code =
|
|
107
|
+
`// compiled pk getter for entity ${meta.className}\n` + `return function(entity) {\n${lines.join('\n')}\n}`;
|
|
108
|
+
const fnKey = `pkGetter-${meta.uniqueName}`;
|
|
109
|
+
const pkSerializer = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
110
|
+
this.#pkGetters.set(meta, pkSerializer);
|
|
111
|
+
return pkSerializer;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* @internal Highly performance-sensitive method.
|
|
115
|
+
*/
|
|
116
|
+
getPkGetterConverted(meta) {
|
|
117
|
+
const exists = this.#pkGettersConverted.get(meta);
|
|
118
|
+
/* v8 ignore next */
|
|
119
|
+
if (exists) {
|
|
120
|
+
return exists;
|
|
121
|
+
}
|
|
122
|
+
const lines = [];
|
|
123
|
+
const context = new Map();
|
|
124
|
+
if (meta.primaryKeys.length > 1) {
|
|
125
|
+
lines.push(` const cond = {`);
|
|
126
|
+
meta.primaryKeys.forEach(pk => {
|
|
127
|
+
if (meta.properties[pk].kind !== ReferenceKind.SCALAR) {
|
|
128
|
+
lines.push(
|
|
129
|
+
` ${pk}: (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) ? entity${this.wrap(pk)}.__helper.getPrimaryKey(true) : entity${this.wrap(pk)},`,
|
|
130
|
+
);
|
|
131
|
+
} else {
|
|
132
|
+
if (meta.properties[pk].customType) {
|
|
133
|
+
const convertorKey = this.registerCustomType(meta.properties[pk], context);
|
|
134
|
+
lines.push(` ${pk}: convertToDatabaseValue_${convertorKey}(entity${this.wrap(pk)}),`);
|
|
135
|
+
} else {
|
|
136
|
+
lines.push(` ${pk}: entity${this.wrap(pk)},`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
lines.push(` };`);
|
|
141
|
+
lines.push(` if (${meta.primaryKeys.map(pk => `cond.${pk} == null`).join(' || ')}) return null;`);
|
|
142
|
+
lines.push(` return cond;`);
|
|
143
|
+
} else {
|
|
144
|
+
const pk = meta.primaryKeys[0];
|
|
145
|
+
if (meta.properties[pk].kind !== ReferenceKind.SCALAR) {
|
|
146
|
+
lines.push(
|
|
147
|
+
` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getPrimaryKey(true);`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
if (meta.properties[pk].customType) {
|
|
151
|
+
const convertorKey = this.registerCustomType(meta.properties[pk], context);
|
|
152
|
+
lines.push(` return convertToDatabaseValue_${convertorKey}(entity${this.wrap(pk)});`);
|
|
153
|
+
} else {
|
|
154
|
+
lines.push(` return entity${this.wrap(pk)};`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const code =
|
|
158
|
+
`// compiled pk getter (with converted custom types) for entity ${meta.className}\n` +
|
|
159
|
+
`return function(entity) {\n${lines.join('\n')}\n}`;
|
|
160
|
+
const fnKey = `pkGetterConverted-${meta.uniqueName}`;
|
|
161
|
+
const pkSerializer = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
162
|
+
this.#pkGettersConverted.set(meta, pkSerializer);
|
|
163
|
+
return pkSerializer;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @internal Highly performance-sensitive method.
|
|
167
|
+
*/
|
|
168
|
+
getPkSerializer(meta) {
|
|
169
|
+
const exists = this.#pkSerializers.get(meta);
|
|
170
|
+
/* v8 ignore next */
|
|
171
|
+
if (exists) {
|
|
172
|
+
return exists;
|
|
173
|
+
}
|
|
174
|
+
const lines = [];
|
|
175
|
+
const context = new Map();
|
|
176
|
+
context.set('getCompositeKeyValue', val =>
|
|
177
|
+
Utils.flatten(Utils.getCompositeKeyValue(val, meta, 'convertToDatabaseValue', this.#platform)),
|
|
178
|
+
);
|
|
179
|
+
context.set('getPrimaryKeyHash', val => Utils.getPrimaryKeyHash(Utils.asArray(val)));
|
|
180
|
+
if (meta.primaryKeys.length > 1) {
|
|
181
|
+
lines.push(` const pks = entity.__helper.__pk ? getCompositeKeyValue(entity.__helper.__pk) : [`);
|
|
182
|
+
meta.primaryKeys.forEach(pk => {
|
|
183
|
+
if (meta.properties[pk].kind !== ReferenceKind.SCALAR) {
|
|
184
|
+
lines.push(
|
|
185
|
+
` (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) ? entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey() : entity${this.wrap(pk)},`,
|
|
186
|
+
);
|
|
187
|
+
} else {
|
|
188
|
+
lines.push(` entity${this.wrap(pk)},`);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
lines.push(` ];`);
|
|
192
|
+
lines.push(` return pks.join('${Utils.PK_SEPARATOR}');`);
|
|
193
|
+
} else {
|
|
194
|
+
const pk = meta.primaryKeys[0];
|
|
195
|
+
const prop = meta.properties[pk];
|
|
196
|
+
if (prop.kind !== ReferenceKind.SCALAR) {
|
|
197
|
+
lines.push(
|
|
198
|
+
` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey();`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
202
|
+
if (serializedPrimaryKey) {
|
|
203
|
+
lines.push(` return '' + entity.${serializedPrimaryKey.name};`);
|
|
204
|
+
} else if (prop.customType) {
|
|
205
|
+
const convertorKey = this.registerCustomType(meta.properties[pk], context);
|
|
206
|
+
const idx = this.#tmpIndex++;
|
|
207
|
+
lines.push(` const val_${idx} = convertToDatabaseValue_${convertorKey}(entity${this.wrap(pk)});`);
|
|
208
|
+
lines.push(` return getPrimaryKeyHash(val_${idx});`);
|
|
209
|
+
} else {
|
|
210
|
+
lines.push(` return '' + entity${this.wrap(pk)};`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const code =
|
|
214
|
+
`// compiled pk serializer for entity ${meta.className}\n` + `return function(entity) {\n${lines.join('\n')}\n}`;
|
|
215
|
+
const fnKey = `pkSerializer-${meta.uniqueName}`;
|
|
216
|
+
const pkSerializer = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
217
|
+
this.#pkSerializers.set(meta, pkSerializer);
|
|
218
|
+
return pkSerializer;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* @internal Highly performance-sensitive method.
|
|
222
|
+
*/
|
|
223
|
+
getSnapshotGenerator(entityName) {
|
|
224
|
+
const meta = this.#metadata.find(entityName);
|
|
225
|
+
const exists = this.#snapshotGenerators.get(meta);
|
|
226
|
+
if (exists) {
|
|
227
|
+
return exists;
|
|
228
|
+
}
|
|
229
|
+
const lines = [];
|
|
230
|
+
const context = new Map();
|
|
231
|
+
context.set('clone', clone);
|
|
232
|
+
context.set('cloneEmbeddable', o => this.#platform.cloneEmbeddable(o)); // do not clone prototypes
|
|
233
|
+
if (meta.root.inheritanceType === 'sti' && meta.discriminatorValue) {
|
|
234
|
+
lines.push(` ret${this.wrap(meta.root.discriminatorColumn)} = '${meta.discriminatorValue}'`);
|
|
235
|
+
}
|
|
236
|
+
const getRootProperty = prop => (prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop);
|
|
237
|
+
// copy all comparable props, ignore collections and references, process custom types
|
|
238
|
+
meta.comparableProps
|
|
239
|
+
.filter(prop => {
|
|
240
|
+
const root = getRootProperty(prop);
|
|
241
|
+
return prop === root || root.kind !== ReferenceKind.EMBEDDED;
|
|
242
|
+
})
|
|
243
|
+
.forEach(prop =>
|
|
244
|
+
lines.push(
|
|
245
|
+
this.getPropertySnapshot(meta, prop, context, this.wrap(prop.name), this.wrap(prop.name), [prop.name]),
|
|
246
|
+
),
|
|
247
|
+
);
|
|
248
|
+
const code = `return function(entity) {\n const ret = {};\n${lines.join('\n')}\n return ret;\n}`;
|
|
249
|
+
const fnKey = `snapshotGenerator-${meta.uniqueName}`;
|
|
250
|
+
const snapshotGenerator = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
251
|
+
this.#snapshotGenerators.set(meta, snapshotGenerator);
|
|
252
|
+
return snapshotGenerator;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* @internal
|
|
256
|
+
*/
|
|
257
|
+
propName(name, parent = 'result') {
|
|
258
|
+
return parent + this.wrap(name);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* @internal respects nested composite keys, e.g. `[1, [2, 3]]`
|
|
262
|
+
*/
|
|
263
|
+
createCompositeKeyArray(prop, parents = []) {
|
|
264
|
+
if (!prop.targetMeta) {
|
|
265
|
+
let fieldName = prop.fieldNames[0];
|
|
266
|
+
// traverse all parents, mapping my field name to each parent's field name until we reach the root
|
|
267
|
+
for (let i = parents.length - 1; i >= 0; i--) {
|
|
268
|
+
const parent = parents[i];
|
|
269
|
+
// skip m:n since it does not represent any column directly
|
|
270
|
+
if (parent.pivotEntity) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const idx = parent.referencedColumnNames.indexOf(fieldName);
|
|
274
|
+
fieldName = parent.fieldNames[idx];
|
|
275
|
+
}
|
|
276
|
+
return this.propName(fieldName);
|
|
277
|
+
}
|
|
278
|
+
const parts = [];
|
|
279
|
+
prop.targetMeta.getPrimaryProps().forEach(pk => {
|
|
280
|
+
const part = this.createCompositeKeyArray(pk, [...parents, prop]);
|
|
281
|
+
parts.push(part);
|
|
282
|
+
});
|
|
283
|
+
return this.formatCompositeKeyPart(parts);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* @internal
|
|
287
|
+
*/
|
|
288
|
+
formatCompositeKeyPart(part) {
|
|
289
|
+
if (!Array.isArray(part)) {
|
|
290
|
+
return part;
|
|
291
|
+
}
|
|
292
|
+
if (part.length === 1) {
|
|
293
|
+
return this.formatCompositeKeyPart(part[0]);
|
|
294
|
+
}
|
|
295
|
+
const formatted = part.map(p => this.formatCompositeKeyPart(p)).join(', ');
|
|
296
|
+
return `[${formatted}]`;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* @internal Highly performance-sensitive method.
|
|
300
|
+
*/
|
|
301
|
+
getResultMapper(meta) {
|
|
302
|
+
const exists = this.#mappers.get(meta);
|
|
303
|
+
if (exists) {
|
|
304
|
+
return exists;
|
|
305
|
+
}
|
|
306
|
+
const lines = [];
|
|
307
|
+
const context = new Map();
|
|
308
|
+
context.set('PolymorphicRef', PolymorphicRef);
|
|
309
|
+
const tz = this.#platform.getTimezone();
|
|
310
|
+
const parseDate = (key, value, padding = '') => {
|
|
311
|
+
lines.push(`${padding} if (${value} == null || ${value} instanceof Date) {`);
|
|
312
|
+
lines.push(`${padding} ${key} = ${value};`);
|
|
313
|
+
if (!tz || tz === 'local') {
|
|
314
|
+
lines.push(`${padding} } else if (typeof ${value} === 'bigint') {`);
|
|
315
|
+
lines.push(`${padding} ${key} = parseDate(Number(${value}));`);
|
|
316
|
+
lines.push(`${padding} } else {`);
|
|
317
|
+
lines.push(`${padding} ${key} = parseDate(${value});`);
|
|
318
|
+
} else {
|
|
319
|
+
lines.push(`${padding} } else if (typeof ${value} === 'bigint') {`);
|
|
320
|
+
lines.push(`${padding} ${key} = parseDate(Number(${value}));`);
|
|
321
|
+
lines.push(
|
|
322
|
+
`${padding} } else if (typeof ${value} === 'number' || ${value}.includes('+') || ${value}.lastIndexOf('-') > 10 || ${value}.endsWith('Z')) {`,
|
|
323
|
+
);
|
|
324
|
+
lines.push(`${padding} ${key} = parseDate(${value});`);
|
|
325
|
+
lines.push(`${padding} } else {`);
|
|
326
|
+
lines.push(`${padding} ${key} = parseDate(${value} + '${tz}');`);
|
|
327
|
+
}
|
|
328
|
+
lines.push(`${padding} }`);
|
|
329
|
+
};
|
|
330
|
+
lines.push(` const mapped = {};`);
|
|
331
|
+
const mapEntityProperties = (meta, padding = '') => {
|
|
332
|
+
for (const prop of meta.props) {
|
|
333
|
+
if (!prop.fieldNames) {
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (prop.polymorphic && prop.fieldNames.length >= 2) {
|
|
337
|
+
const discriminatorField = prop.fieldNames[0];
|
|
338
|
+
const idFields = prop.fieldNames.slice(1);
|
|
339
|
+
lines.push(
|
|
340
|
+
`${padding} if (${prop.fieldNames.map(field => `typeof ${this.propName(field)} === 'undefined'`).join(' && ')}) {`,
|
|
341
|
+
);
|
|
342
|
+
lines.push(
|
|
343
|
+
`${padding} } else if (${prop.fieldNames.map(field => `${this.propName(field)} != null`).join(' && ')}) {`,
|
|
344
|
+
);
|
|
345
|
+
if (idFields.length === 1) {
|
|
346
|
+
lines.push(
|
|
347
|
+
`${padding} ret${this.wrap(prop.name)} = new PolymorphicRef(${this.propName(discriminatorField)}, ${this.propName(idFields[0])});`,
|
|
348
|
+
);
|
|
349
|
+
} else {
|
|
350
|
+
lines.push(
|
|
351
|
+
`${padding} ret${this.wrap(prop.name)} = new PolymorphicRef(${this.propName(discriminatorField)}, [${idFields.map(f => this.propName(f)).join(', ')}]);`,
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
lines.push(...prop.fieldNames.map(field => `${padding} ${this.propName(field, 'mapped')} = true;`));
|
|
355
|
+
lines.push(
|
|
356
|
+
`${padding} } else if (${prop.fieldNames.map(field => `${this.propName(field)} == null`).join(' && ')}) {\n${padding} ret${this.wrap(prop.name)} = null;`,
|
|
357
|
+
);
|
|
358
|
+
lines.push(...prop.fieldNames.map(field => `${padding} ${this.propName(field, 'mapped')} = true;`), ' }');
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (prop.targetMeta && prop.fieldNames.length > 1) {
|
|
362
|
+
lines.push(
|
|
363
|
+
`${padding} if (${prop.fieldNames.map(field => `typeof ${this.propName(field)} === 'undefined'`).join(' && ')}) {`,
|
|
364
|
+
);
|
|
365
|
+
lines.push(
|
|
366
|
+
`${padding} } else if (${prop.fieldNames.map(field => `${this.propName(field)} != null`).join(' && ')}) {`,
|
|
367
|
+
);
|
|
368
|
+
lines.push(`${padding} ret${this.wrap(prop.name)} = ${this.createCompositeKeyArray(prop)};`);
|
|
369
|
+
lines.push(...prop.fieldNames.map(field => `${padding} ${this.propName(field, 'mapped')} = true;`));
|
|
370
|
+
lines.push(
|
|
371
|
+
`${padding} } else if (${prop.fieldNames.map(field => `${this.propName(field)} == null`).join(' && ')}) {\n${padding} ret${this.wrap(prop.name)} = null;`,
|
|
372
|
+
);
|
|
373
|
+
lines.push(...prop.fieldNames.map(field => `${padding} ${this.propName(field, 'mapped')} = true;`), ' }');
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (prop.embedded && (meta.embeddable || meta.properties[prop.embedded[0]].object)) {
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (prop.runtimeType === 'boolean') {
|
|
380
|
+
lines.push(`${padding} if (typeof ${this.propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
381
|
+
lines.push(
|
|
382
|
+
`${padding} ret${this.wrap(prop.name)} = ${this.propName(prop.fieldNames[0])} == null ? ${this.propName(prop.fieldNames[0])} : !!${this.propName(prop.fieldNames[0])};`,
|
|
383
|
+
);
|
|
384
|
+
lines.push(`${padding} ${this.propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
385
|
+
lines.push(`${padding} }`);
|
|
386
|
+
} else if (prop.runtimeType === 'Date' && !this.#platform.isNumericProperty(prop)) {
|
|
387
|
+
lines.push(`${padding} if (typeof ${this.propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
388
|
+
context.set('parseDate', value => this.#platform.parseDate(value));
|
|
389
|
+
parseDate('ret' + this.wrap(prop.name), this.propName(prop.fieldNames[0]), padding);
|
|
390
|
+
lines.push(`${padding} ${this.propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
391
|
+
lines.push(`${padding} }`);
|
|
392
|
+
} else if (prop.kind === ReferenceKind.EMBEDDED && (prop.object || meta.embeddable)) {
|
|
393
|
+
const idx = this.#tmpIndex++;
|
|
394
|
+
context.set(`mapEmbeddedResult_${idx}`, data => {
|
|
395
|
+
const item = parseJsonSafe(data);
|
|
396
|
+
if (Array.isArray(item)) {
|
|
397
|
+
return item.map(row => (row == null ? row : this.getResultMapper(prop.targetMeta)(row)));
|
|
194
398
|
}
|
|
195
|
-
|
|
196
|
-
|
|
399
|
+
return item == null ? item : this.getResultMapper(prop.targetMeta)(item);
|
|
400
|
+
});
|
|
401
|
+
lines.push(`${padding} if (typeof ${this.propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
402
|
+
lines.push(
|
|
403
|
+
`${padding} ret${this.wrap(prop.name)} = ${this.propName(prop.fieldNames[0])} == null ? ${this.propName(prop.fieldNames[0])} : mapEmbeddedResult_${idx}(${this.propName(prop.fieldNames[0])});`,
|
|
404
|
+
);
|
|
405
|
+
lines.push(`${padding} ${this.propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
406
|
+
lines.push(`${padding} }`);
|
|
407
|
+
} else if (prop.kind !== ReferenceKind.EMBEDDED) {
|
|
408
|
+
lines.push(`${padding} if (typeof ${this.propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
409
|
+
lines.push(`${padding} ret${this.wrap(prop.name)} = ${this.propName(prop.fieldNames[0])};`);
|
|
410
|
+
lines.push(`${padding} ${this.propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
411
|
+
lines.push(`${padding} }`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
if (meta.polymorphs && meta.discriminatorColumn) {
|
|
416
|
+
for (const polymorph of meta.polymorphs) {
|
|
417
|
+
const first = polymorph === meta.polymorphs[0];
|
|
418
|
+
lines.push(
|
|
419
|
+
` ${first ? '' : 'else '}if (${this.propName(meta.discriminatorColumn)} == '${polymorph.discriminatorValue}') {`,
|
|
420
|
+
);
|
|
421
|
+
mapEntityProperties(polymorph, ' ');
|
|
422
|
+
lines.push(` }`);
|
|
423
|
+
}
|
|
424
|
+
lines.push(` else {`);
|
|
425
|
+
mapEntityProperties(meta, ' ');
|
|
426
|
+
lines.push(` }`);
|
|
427
|
+
} else {
|
|
428
|
+
mapEntityProperties(meta);
|
|
429
|
+
}
|
|
430
|
+
lines.push(
|
|
431
|
+
` for (let k in result) { if (Object.hasOwn(result, k) && !mapped[k] && ret[k] === undefined) ret[k] = result[k]; }`,
|
|
432
|
+
);
|
|
433
|
+
const code =
|
|
434
|
+
`// compiled mapper for entity ${meta.className}\n` +
|
|
435
|
+
`return function(result) {\n const ret = {};\n${lines.join('\n')}\n return ret;\n}`;
|
|
436
|
+
const fnKey = `resultMapper-${meta.uniqueName}`;
|
|
437
|
+
const resultMapper = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
438
|
+
this.#mappers.set(meta, resultMapper);
|
|
439
|
+
return resultMapper;
|
|
440
|
+
}
|
|
441
|
+
getPropertyCondition(path) {
|
|
442
|
+
const parts = path.slice(); // copy first
|
|
443
|
+
if (parts.length > 1) {
|
|
444
|
+
parts.pop();
|
|
445
|
+
}
|
|
446
|
+
let tail = '';
|
|
447
|
+
return parts
|
|
448
|
+
.map(k => {
|
|
449
|
+
if (/^\[idx_\d+]$/.exec(k)) {
|
|
450
|
+
tail += k;
|
|
451
|
+
return '';
|
|
452
|
+
}
|
|
453
|
+
const mapped = `typeof entity${tail ? '.' + tail : ''}${this.wrap(k)} !== 'undefined'`;
|
|
454
|
+
tail += tail ? '.' + k : k;
|
|
455
|
+
return mapped;
|
|
456
|
+
})
|
|
457
|
+
.filter(k => k)
|
|
458
|
+
.join(' && ');
|
|
459
|
+
}
|
|
460
|
+
getEmbeddedArrayPropertySnapshot(meta, prop, context, level, path, dataKey) {
|
|
461
|
+
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
462
|
+
const ret = [];
|
|
463
|
+
const padding = ' '.repeat(level * 2);
|
|
464
|
+
const idx = this.#tmpIndex++;
|
|
465
|
+
ret.push(`${padding}if (Array.isArray(entity${entityKey})) {`);
|
|
466
|
+
ret.push(`${padding} ret${dataKey} = [];`);
|
|
467
|
+
ret.push(`${padding} entity${entityKey}.forEach((_, idx_${idx}) => {`);
|
|
468
|
+
ret.push(
|
|
469
|
+
this.getEmbeddedPropertySnapshot(
|
|
470
|
+
meta,
|
|
471
|
+
prop,
|
|
472
|
+
context,
|
|
473
|
+
level + 2,
|
|
474
|
+
[...path, `[idx_${idx}]`],
|
|
475
|
+
`${dataKey}[idx_${idx}]`,
|
|
476
|
+
true,
|
|
477
|
+
),
|
|
478
|
+
);
|
|
479
|
+
ret.push(`${padding} });`);
|
|
480
|
+
if (this.shouldSerialize(prop, dataKey)) {
|
|
481
|
+
ret.push(`${padding} ret${dataKey} = cloneEmbeddable(ret${dataKey});`);
|
|
482
|
+
}
|
|
483
|
+
ret.push(`${padding}}`);
|
|
484
|
+
return ret.join('\n');
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* we need to serialize only object embeddables, and only the top level ones, so root object embeddable
|
|
488
|
+
* properties and first child nested object embeddables with inlined parent
|
|
489
|
+
*/
|
|
490
|
+
shouldSerialize(prop, dataKey) {
|
|
491
|
+
dataKey = dataKey.replace(/^\./, '');
|
|
492
|
+
const contains = (str, re) => (str.match(re) || []).length > 0;
|
|
493
|
+
const a = contains(dataKey, /\./g);
|
|
494
|
+
const b = contains(dataKey, /\[idx_/g);
|
|
495
|
+
return !!prop.object && !a && !b;
|
|
496
|
+
}
|
|
497
|
+
getEmbeddedPropertySnapshot(meta, prop, context, level, path, dataKey, object = prop.object) {
|
|
498
|
+
const padding = ' '.repeat(level * 2);
|
|
499
|
+
const nullCond = `entity${path.map(k => this.wrap(k)).join('')} === null`;
|
|
500
|
+
let ret = level === 1 ? '' : '\n';
|
|
501
|
+
if (object) {
|
|
502
|
+
ret += `${padding}if (${nullCond}) ret${dataKey} = null;\n`;
|
|
503
|
+
} else {
|
|
504
|
+
ret += `${padding}if (${nullCond}) {\n`;
|
|
505
|
+
ret +=
|
|
506
|
+
meta.props
|
|
507
|
+
.filter(
|
|
508
|
+
p =>
|
|
509
|
+
p.embedded?.[0] === prop.name &&
|
|
510
|
+
// object for JSON embeddable
|
|
511
|
+
(p.object || p.persist !== false),
|
|
512
|
+
)
|
|
513
|
+
.map(childProp => {
|
|
514
|
+
const childDataKey =
|
|
515
|
+
meta.embeddable || prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
516
|
+
return `${padding} ret${childDataKey} = null;`;
|
|
517
|
+
})
|
|
518
|
+
.join('\n') + `\n`;
|
|
519
|
+
ret += `${padding}}\n`;
|
|
520
|
+
}
|
|
521
|
+
const cond = `entity${path.map(k => this.wrap(k)).join('')} != null`;
|
|
522
|
+
ret += `${padding}if (${cond}) {\n`;
|
|
523
|
+
if (object) {
|
|
524
|
+
ret += `${padding} ret${dataKey} = {};\n`;
|
|
525
|
+
}
|
|
526
|
+
function shouldProcessCustomType(childProp) {
|
|
527
|
+
if (!childProp.customType) {
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
if (childProp.customType instanceof JsonType) {
|
|
531
|
+
return !prop.object;
|
|
532
|
+
}
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
ret +=
|
|
536
|
+
meta.props
|
|
537
|
+
.filter(
|
|
538
|
+
p =>
|
|
539
|
+
p.embedded?.[0] === prop.name &&
|
|
540
|
+
// object for JSON embeddable
|
|
541
|
+
(p.object || p.persist !== false),
|
|
542
|
+
)
|
|
543
|
+
.map(childProp => {
|
|
544
|
+
const childDataKey =
|
|
545
|
+
meta.embeddable || prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
546
|
+
const childEntityKey = [...path, childProp.embedded[1]].map(k => this.wrap(k)).join('');
|
|
547
|
+
const childCond = `typeof entity${childEntityKey} !== 'undefined'`;
|
|
548
|
+
if (childProp.kind === ReferenceKind.EMBEDDED) {
|
|
549
|
+
return this.getPropertySnapshot(
|
|
550
|
+
meta,
|
|
551
|
+
childProp,
|
|
552
|
+
context,
|
|
553
|
+
childDataKey,
|
|
554
|
+
childEntityKey,
|
|
555
|
+
[...path, childProp.embedded[1]],
|
|
556
|
+
level + 1,
|
|
557
|
+
prop.object,
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
if (childProp.kind !== ReferenceKind.SCALAR) {
|
|
561
|
+
return this.getPropertySnapshot(
|
|
562
|
+
meta,
|
|
563
|
+
childProp,
|
|
564
|
+
context,
|
|
565
|
+
childDataKey,
|
|
566
|
+
childEntityKey,
|
|
567
|
+
[...path, childProp.embedded[1]],
|
|
568
|
+
level,
|
|
569
|
+
prop.object,
|
|
570
|
+
)
|
|
571
|
+
.split('\n')
|
|
572
|
+
.map(l => padding + l)
|
|
573
|
+
.join('\n');
|
|
574
|
+
}
|
|
575
|
+
if (shouldProcessCustomType(childProp)) {
|
|
576
|
+
const convertorKey = this.registerCustomType(childProp, context);
|
|
577
|
+
if (
|
|
578
|
+
['number', 'string', 'boolean', 'bigint'].includes(childProp.customType.compareAsType().toLowerCase())
|
|
579
|
+
) {
|
|
580
|
+
return `${padding} if (${childCond}) ret${childDataKey} = convertToDatabaseValue_${convertorKey}(entity${childEntityKey});`;
|
|
197
581
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const pkSerializer = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
202
|
-
this.#pkSerializers.set(meta, pkSerializer);
|
|
203
|
-
return pkSerializer;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* @internal Highly performance-sensitive method.
|
|
207
|
-
*/
|
|
208
|
-
getSnapshotGenerator(entityName) {
|
|
209
|
-
const meta = this.#metadata.find(entityName);
|
|
210
|
-
const exists = this.#snapshotGenerators.get(meta);
|
|
211
|
-
if (exists) {
|
|
212
|
-
return exists;
|
|
213
|
-
}
|
|
214
|
-
const lines = [];
|
|
215
|
-
const context = new Map();
|
|
216
|
-
context.set('clone', clone);
|
|
217
|
-
context.set('cloneEmbeddable', (o) => this.#platform.cloneEmbeddable(o)); // do not clone prototypes
|
|
218
|
-
if (meta.root.inheritanceType === 'sti' && meta.discriminatorValue) {
|
|
219
|
-
lines.push(` ret${this.wrap(meta.root.discriminatorColumn)} = '${meta.discriminatorValue}'`);
|
|
220
|
-
}
|
|
221
|
-
const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
|
|
222
|
-
// copy all comparable props, ignore collections and references, process custom types
|
|
223
|
-
meta.comparableProps
|
|
224
|
-
.filter(prop => {
|
|
225
|
-
const root = getRootProperty(prop);
|
|
226
|
-
return prop === root || root.kind !== ReferenceKind.EMBEDDED;
|
|
582
|
+
return `${padding} if (${childCond}) ret${childDataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${childEntityKey}));`;
|
|
583
|
+
}
|
|
584
|
+
return `${padding} if (${childCond}) ret${childDataKey} = clone(entity${childEntityKey});`;
|
|
227
585
|
})
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
return
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* @internal Highly performance-sensitive method.
|
|
281
|
-
*/
|
|
282
|
-
getResultMapper(meta) {
|
|
283
|
-
const exists = this.#mappers.get(meta);
|
|
284
|
-
if (exists) {
|
|
285
|
-
return exists;
|
|
286
|
-
}
|
|
287
|
-
const lines = [];
|
|
288
|
-
const context = new Map();
|
|
586
|
+
.join('\n') + `\n`;
|
|
587
|
+
if (this.shouldSerialize(prop, dataKey)) {
|
|
588
|
+
return `${ret + padding} ret${dataKey} = cloneEmbeddable(ret${dataKey});\n${padding}}`;
|
|
589
|
+
}
|
|
590
|
+
return `${ret}${padding}}`;
|
|
591
|
+
}
|
|
592
|
+
registerCustomType(prop, context) {
|
|
593
|
+
const convertorKey = this.safeKey(prop.name);
|
|
594
|
+
context.set(`convertToDatabaseValue_${convertorKey}`, val => {
|
|
595
|
+
/* v8 ignore next */
|
|
596
|
+
if (Raw.isKnownFragment(val)) {
|
|
597
|
+
return val;
|
|
598
|
+
}
|
|
599
|
+
return prop.customType.convertToDatabaseValue(val, this.#platform, { mode: 'serialization' });
|
|
600
|
+
});
|
|
601
|
+
return convertorKey;
|
|
602
|
+
}
|
|
603
|
+
getPropertySnapshot(meta, prop, context, dataKey, entityKey, path, level = 1, object) {
|
|
604
|
+
const unwrap = prop.ref ? '?.unwrap()' : '';
|
|
605
|
+
let ret = ` if (${this.getPropertyCondition(path)}`;
|
|
606
|
+
if (prop.lazy && prop.ref) {
|
|
607
|
+
ret += ` && entity${path.map(k => this.wrap(k)).join('')}?.isInitialized()`;
|
|
608
|
+
}
|
|
609
|
+
ret += `) {\n`;
|
|
610
|
+
if (['number', 'string', 'boolean'].includes(prop.type.toLowerCase())) {
|
|
611
|
+
return ret + ` ret${dataKey} = entity${entityKey}${unwrap};\n }\n`;
|
|
612
|
+
}
|
|
613
|
+
if (prop.kind === ReferenceKind.EMBEDDED) {
|
|
614
|
+
if (prop.array) {
|
|
615
|
+
return this.getEmbeddedArrayPropertySnapshot(meta, prop, context, level, path, dataKey) + '\n';
|
|
616
|
+
}
|
|
617
|
+
return this.getEmbeddedPropertySnapshot(meta, prop, context, level, path, dataKey, object) + '\n';
|
|
618
|
+
}
|
|
619
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE || prop.kind === ReferenceKind.MANY_TO_ONE) {
|
|
620
|
+
if (prop.mapToPk) {
|
|
621
|
+
if (prop.customType) {
|
|
622
|
+
const convertorKey = this.registerCustomType(prop, context);
|
|
623
|
+
ret += ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(entity${entityKey});\n`;
|
|
624
|
+
} else {
|
|
625
|
+
ret += ` ret${dataKey} = entity${entityKey};\n`;
|
|
626
|
+
}
|
|
627
|
+
} else if (prop.polymorphic) {
|
|
628
|
+
const discriminatorMapKey = `discriminatorMapReverse_${prop.name}`;
|
|
629
|
+
const reverseMap = new Map();
|
|
630
|
+
for (const [key, value] of Object.entries(prop.discriminatorMap)) {
|
|
631
|
+
reverseMap.set(value, key);
|
|
632
|
+
}
|
|
633
|
+
context.set(discriminatorMapKey, reverseMap);
|
|
634
|
+
this.setToArrayHelper(context);
|
|
635
|
+
context.set('EntityIdentifier', EntityIdentifier);
|
|
289
636
|
context.set('PolymorphicRef', PolymorphicRef);
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
const
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
if (parts.length > 1) {
|
|
404
|
-
parts.pop();
|
|
405
|
-
}
|
|
406
|
-
let tail = '';
|
|
407
|
-
return parts
|
|
408
|
-
.map(k => {
|
|
409
|
-
if (/^\[idx_\d+]$/.exec(k)) {
|
|
410
|
-
tail += k;
|
|
411
|
-
return '';
|
|
412
|
-
}
|
|
413
|
-
const mapped = `typeof entity${tail ? '.' + tail : ''}${this.wrap(k)} !== 'undefined'`;
|
|
414
|
-
tail += tail ? '.' + k : k;
|
|
415
|
-
return mapped;
|
|
416
|
-
})
|
|
417
|
-
.filter(k => k)
|
|
418
|
-
.join(' && ');
|
|
637
|
+
ret += ` if (entity${entityKey} === null) {\n`;
|
|
638
|
+
ret += ` ret${dataKey} = null;\n`;
|
|
639
|
+
ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
|
|
640
|
+
ret += ` const val${level} = entity${entityKey}${unwrap};\n`;
|
|
641
|
+
ret += ` const discriminator = ${discriminatorMapKey}.get(val${level}?.constructor);\n`;
|
|
642
|
+
ret += ` const pk = val${level}?.__helper?.__identifier && !val${level}?.__helper?.hasPrimaryKey()\n`;
|
|
643
|
+
ret += ` ? val${level}.__helper.__identifier\n`;
|
|
644
|
+
ret += ` : toArray(val${level}?.__helper?.getPrimaryKey(true));\n`;
|
|
645
|
+
ret += ` ret${dataKey} = new PolymorphicRef(discriminator, pk);\n`;
|
|
646
|
+
ret += ` }\n`;
|
|
647
|
+
} else if (prop.targetKey) {
|
|
648
|
+
// When targetKey is set, extract that property value instead of the PK
|
|
649
|
+
const targetProp = prop.targetMeta?.properties[prop.targetKey];
|
|
650
|
+
ret += ` if (entity${entityKey} === null) {\n`;
|
|
651
|
+
ret += ` ret${dataKey} = null;\n`;
|
|
652
|
+
ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
|
|
653
|
+
ret += ` const val${level} = entity${entityKey}${unwrap};\n`;
|
|
654
|
+
if (targetProp?.customType) {
|
|
655
|
+
// If targetKey property has a custom type, convert to database value
|
|
656
|
+
const convertorKey = this.registerCustomType(targetProp, context);
|
|
657
|
+
ret += ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(val${level}?.${prop.targetKey});\n`;
|
|
658
|
+
} else {
|
|
659
|
+
ret += ` ret${dataKey} = val${level}?.${prop.targetKey};\n`;
|
|
660
|
+
}
|
|
661
|
+
ret += ` }\n`;
|
|
662
|
+
} else {
|
|
663
|
+
this.setToArrayHelper(context);
|
|
664
|
+
context.set('EntityIdentifier', EntityIdentifier);
|
|
665
|
+
ret += ` if (entity${entityKey} === null) {\n`;
|
|
666
|
+
ret += ` ret${dataKey} = null;\n`;
|
|
667
|
+
ret += ` } else if (entity${entityKey}?.__helper.__identifier && !entity${entityKey}.__helper.hasPrimaryKey()) {\n`;
|
|
668
|
+
ret += ` ret${dataKey} = entity${entityKey}?.__helper.__identifier;\n`;
|
|
669
|
+
ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
|
|
670
|
+
ret += ` ret${dataKey} = toArray(entity${entityKey}.__helper.getPrimaryKey(true));\n`;
|
|
671
|
+
ret += ` }\n`;
|
|
672
|
+
}
|
|
673
|
+
return ret + ' }\n';
|
|
674
|
+
}
|
|
675
|
+
if (prop.customType) {
|
|
676
|
+
const convertorKey = this.registerCustomType(prop, context);
|
|
677
|
+
if (['number', 'string', 'boolean', 'bigint'].includes(prop.customType.compareAsType().toLowerCase())) {
|
|
678
|
+
return ret + ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(entity${entityKey}${unwrap});\n }\n`;
|
|
679
|
+
}
|
|
680
|
+
return (
|
|
681
|
+
ret + ` ret${dataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${entityKey}${unwrap}));\n }\n`
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
if (prop.runtimeType === 'Date') {
|
|
685
|
+
context.set('processDateProperty', this.#platform.processDateProperty.bind(this.#platform));
|
|
686
|
+
return ret + ` ret${dataKey} = clone(processDateProperty(entity${entityKey}${unwrap}));\n }\n`;
|
|
687
|
+
}
|
|
688
|
+
return ret + ` ret${dataKey} = clone(entity${entityKey}${unwrap});\n }\n`;
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* @internal Highly performance-sensitive method.
|
|
692
|
+
*/
|
|
693
|
+
getEntityComparator(entityName) {
|
|
694
|
+
const meta = this.#metadata.find(entityName);
|
|
695
|
+
const exists = this.#comparators.get(meta);
|
|
696
|
+
if (exists) {
|
|
697
|
+
return exists;
|
|
698
|
+
}
|
|
699
|
+
const lines = [];
|
|
700
|
+
const context = new Map();
|
|
701
|
+
context.set('compareArrays', compareArrays);
|
|
702
|
+
context.set('compareBooleans', compareBooleans);
|
|
703
|
+
context.set('compareBuffers', compareBuffers);
|
|
704
|
+
context.set('compareObjects', compareObjects);
|
|
705
|
+
context.set('equals', equals);
|
|
706
|
+
for (const prop of meta.comparableProps) {
|
|
707
|
+
lines.push(this.getPropertyComparator(prop, context));
|
|
708
|
+
}
|
|
709
|
+
// also compare 1:1 inverse sides, important for `factory.mergeData`
|
|
710
|
+
lines.push(`if (options?.includeInverseSides) {`);
|
|
711
|
+
for (const prop of meta.bidirectionalRelations) {
|
|
712
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner && prop.hydrate !== false) {
|
|
713
|
+
lines.push(this.getPropertyComparator(prop, context));
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
lines.push(`}`);
|
|
717
|
+
const code =
|
|
718
|
+
`// compiled comparator for entity ${meta.className}\n` +
|
|
719
|
+
`return function(last, current, options) {\n const diff = {};\n${lines.join('\n')}\n return diff;\n}`;
|
|
720
|
+
const fnKey = `comparator-${meta.uniqueName}`;
|
|
721
|
+
const comparator = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
722
|
+
this.#comparators.set(meta, comparator);
|
|
723
|
+
return comparator;
|
|
724
|
+
}
|
|
725
|
+
getGenericComparator(prop, cond) {
|
|
726
|
+
return (
|
|
727
|
+
` if (current${prop} === null && last${prop} === undefined) {\n` +
|
|
728
|
+
` diff${prop} = current${prop};\n` +
|
|
729
|
+
` } else if (current${prop} == null && last${prop} == null) {\n\n` +
|
|
730
|
+
` } else if ((current${prop} != null && last${prop} == null) || (current${prop} == null && last${prop} != null)) {\n` +
|
|
731
|
+
` diff${prop} = current${prop};\n` +
|
|
732
|
+
` } else if (${cond}) {\n` +
|
|
733
|
+
` diff${prop} = current${prop};\n` +
|
|
734
|
+
` }\n`
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
getPropertyComparator(prop, context) {
|
|
738
|
+
let type = prop.type.toLowerCase();
|
|
739
|
+
if (prop.kind !== ReferenceKind.SCALAR && prop.kind !== ReferenceKind.EMBEDDED) {
|
|
740
|
+
if (prop.polymorphic) {
|
|
741
|
+
type = 'object';
|
|
742
|
+
} else {
|
|
743
|
+
const meta2 = prop.targetMeta;
|
|
744
|
+
if (meta2.primaryKeys.length > 1) {
|
|
745
|
+
type = 'array';
|
|
746
|
+
} else {
|
|
747
|
+
type = meta2.getPrimaryProp().type.toLowerCase();
|
|
748
|
+
}
|
|
749
|
+
}
|
|
419
750
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
const ret = [];
|
|
423
|
-
const padding = ' '.repeat(level * 2);
|
|
751
|
+
if (prop.customType) {
|
|
752
|
+
if (prop.customType.compareValues) {
|
|
424
753
|
const idx = this.#tmpIndex++;
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
if (this.shouldSerialize(prop, dataKey)) {
|
|
431
|
-
ret.push(`${padding} ret${dataKey} = cloneEmbeddable(ret${dataKey});`);
|
|
432
|
-
}
|
|
433
|
-
ret.push(`${padding}}`);
|
|
434
|
-
return ret.join('\n');
|
|
435
|
-
}
|
|
436
|
-
/**
|
|
437
|
-
* we need to serialize only object embeddables, and only the top level ones, so root object embeddable
|
|
438
|
-
* properties and first child nested object embeddables with inlined parent
|
|
439
|
-
*/
|
|
440
|
-
shouldSerialize(prop, dataKey) {
|
|
441
|
-
dataKey = dataKey.replace(/^\./, '');
|
|
442
|
-
const contains = (str, re) => (str.match(re) || []).length > 0;
|
|
443
|
-
const a = contains(dataKey, /\./g);
|
|
444
|
-
const b = contains(dataKey, /\[idx_/g);
|
|
445
|
-
return !!prop.object && !a && !b;
|
|
446
|
-
}
|
|
447
|
-
getEmbeddedPropertySnapshot(meta, prop, context, level, path, dataKey, object = prop.object) {
|
|
448
|
-
const padding = ' '.repeat(level * 2);
|
|
449
|
-
const nullCond = `entity${path.map(k => this.wrap(k)).join('')} === null`;
|
|
450
|
-
let ret = level === 1 ? '' : '\n';
|
|
451
|
-
if (object) {
|
|
452
|
-
ret += `${padding}if (${nullCond}) ret${dataKey} = null;\n`;
|
|
453
|
-
}
|
|
454
|
-
else {
|
|
455
|
-
ret += `${padding}if (${nullCond}) {\n`;
|
|
456
|
-
ret +=
|
|
457
|
-
meta.props
|
|
458
|
-
.filter(p => p.embedded?.[0] === prop.name &&
|
|
459
|
-
// object for JSON embeddable
|
|
460
|
-
(p.object || p.persist !== false))
|
|
461
|
-
.map(childProp => {
|
|
462
|
-
const childDataKey = meta.embeddable || prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
463
|
-
return `${padding} ret${childDataKey} = null;`;
|
|
464
|
-
})
|
|
465
|
-
.join('\n') + `\n`;
|
|
466
|
-
ret += `${padding}}\n`;
|
|
467
|
-
}
|
|
468
|
-
const cond = `entity${path.map(k => this.wrap(k)).join('')} != null`;
|
|
469
|
-
ret += `${padding}if (${cond}) {\n`;
|
|
470
|
-
if (object) {
|
|
471
|
-
ret += `${padding} ret${dataKey} = {};\n`;
|
|
472
|
-
}
|
|
473
|
-
function shouldProcessCustomType(childProp) {
|
|
474
|
-
if (!childProp.customType) {
|
|
475
|
-
return false;
|
|
476
|
-
}
|
|
477
|
-
if (childProp.customType instanceof JsonType) {
|
|
478
|
-
return !prop.object;
|
|
479
|
-
}
|
|
480
|
-
return true;
|
|
481
|
-
}
|
|
482
|
-
ret +=
|
|
483
|
-
meta.props
|
|
484
|
-
.filter(p => p.embedded?.[0] === prop.name &&
|
|
485
|
-
// object for JSON embeddable
|
|
486
|
-
(p.object || p.persist !== false))
|
|
487
|
-
.map(childProp => {
|
|
488
|
-
const childDataKey = meta.embeddable || prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
489
|
-
const childEntityKey = [...path, childProp.embedded[1]].map(k => this.wrap(k)).join('');
|
|
490
|
-
const childCond = `typeof entity${childEntityKey} !== 'undefined'`;
|
|
491
|
-
if (childProp.kind === ReferenceKind.EMBEDDED) {
|
|
492
|
-
return this.getPropertySnapshot(meta, childProp, context, childDataKey, childEntityKey, [...path, childProp.embedded[1]], level + 1, prop.object);
|
|
493
|
-
}
|
|
494
|
-
if (childProp.kind !== ReferenceKind.SCALAR) {
|
|
495
|
-
return this.getPropertySnapshot(meta, childProp, context, childDataKey, childEntityKey, [...path, childProp.embedded[1]], level, prop.object)
|
|
496
|
-
.split('\n')
|
|
497
|
-
.map(l => padding + l)
|
|
498
|
-
.join('\n');
|
|
499
|
-
}
|
|
500
|
-
if (shouldProcessCustomType(childProp)) {
|
|
501
|
-
const convertorKey = this.registerCustomType(childProp, context);
|
|
502
|
-
if (['number', 'string', 'boolean', 'bigint'].includes(childProp.customType.compareAsType().toLowerCase())) {
|
|
503
|
-
return `${padding} if (${childCond}) ret${childDataKey} = convertToDatabaseValue_${convertorKey}(entity${childEntityKey});`;
|
|
504
|
-
}
|
|
505
|
-
return `${padding} if (${childCond}) ret${childDataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${childEntityKey}));`;
|
|
506
|
-
}
|
|
507
|
-
return `${padding} if (${childCond}) ret${childDataKey} = clone(entity${childEntityKey});`;
|
|
508
|
-
})
|
|
509
|
-
.join('\n') + `\n`;
|
|
510
|
-
if (this.shouldSerialize(prop, dataKey)) {
|
|
511
|
-
return `${ret + padding} ret${dataKey} = cloneEmbeddable(ret${dataKey});\n${padding}}`;
|
|
512
|
-
}
|
|
513
|
-
return `${ret}${padding}}`;
|
|
514
|
-
}
|
|
515
|
-
registerCustomType(prop, context) {
|
|
516
|
-
const convertorKey = this.safeKey(prop.name);
|
|
517
|
-
context.set(`convertToDatabaseValue_${convertorKey}`, (val) => {
|
|
518
|
-
/* v8 ignore next */
|
|
519
|
-
if (Raw.isKnownFragment(val)) {
|
|
520
|
-
return val;
|
|
521
|
-
}
|
|
522
|
-
return prop.customType.convertToDatabaseValue(val, this.#platform, { mode: 'serialization' });
|
|
754
|
+
context.set(`compareValues_${idx}`, (a, b) => {
|
|
755
|
+
if (Raw.isKnownFragment(a) || Raw.isKnownFragment(b)) {
|
|
756
|
+
return Raw.getKnownFragment(a) === Raw.getKnownFragment(b);
|
|
757
|
+
}
|
|
758
|
+
return prop.customType.compareValues(a, b);
|
|
523
759
|
});
|
|
524
|
-
return
|
|
760
|
+
return this.getGenericComparator(
|
|
761
|
+
this.wrap(prop.name),
|
|
762
|
+
`!compareValues_${idx}(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`,
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
type = prop.customType.compareAsType().toLowerCase();
|
|
525
766
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
let ret = ` if (${this.getPropertyCondition(path)}) {\n`;
|
|
529
|
-
if (['number', 'string', 'boolean'].includes(prop.type.toLowerCase())) {
|
|
530
|
-
return ret + ` ret${dataKey} = entity${entityKey}${unwrap};\n }\n`;
|
|
531
|
-
}
|
|
532
|
-
if (prop.kind === ReferenceKind.EMBEDDED) {
|
|
533
|
-
if (prop.array) {
|
|
534
|
-
return this.getEmbeddedArrayPropertySnapshot(meta, prop, context, level, path, dataKey) + '\n';
|
|
535
|
-
}
|
|
536
|
-
return this.getEmbeddedPropertySnapshot(meta, prop, context, level, path, dataKey, object) + '\n';
|
|
537
|
-
}
|
|
538
|
-
if (prop.kind === ReferenceKind.ONE_TO_ONE || prop.kind === ReferenceKind.MANY_TO_ONE) {
|
|
539
|
-
if (prop.mapToPk) {
|
|
540
|
-
if (prop.customType) {
|
|
541
|
-
const convertorKey = this.registerCustomType(prop, context);
|
|
542
|
-
ret += ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(entity${entityKey});\n`;
|
|
543
|
-
}
|
|
544
|
-
else {
|
|
545
|
-
ret += ` ret${dataKey} = entity${entityKey};\n`;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
else if (prop.polymorphic) {
|
|
549
|
-
const discriminatorMapKey = `discriminatorMapReverse_${prop.name}`;
|
|
550
|
-
const reverseMap = new Map();
|
|
551
|
-
for (const [key, value] of Object.entries(prop.discriminatorMap)) {
|
|
552
|
-
reverseMap.set(value, key);
|
|
553
|
-
}
|
|
554
|
-
context.set(discriminatorMapKey, reverseMap);
|
|
555
|
-
this.setToArrayHelper(context);
|
|
556
|
-
context.set('EntityIdentifier', EntityIdentifier);
|
|
557
|
-
context.set('PolymorphicRef', PolymorphicRef);
|
|
558
|
-
ret += ` if (entity${entityKey} === null) {\n`;
|
|
559
|
-
ret += ` ret${dataKey} = null;\n`;
|
|
560
|
-
ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
|
|
561
|
-
ret += ` const val${level} = entity${entityKey}${unwrap};\n`;
|
|
562
|
-
ret += ` const discriminator = ${discriminatorMapKey}.get(val${level}?.constructor);\n`;
|
|
563
|
-
ret += ` const pk = val${level}?.__helper?.__identifier && !val${level}?.__helper?.hasPrimaryKey()\n`;
|
|
564
|
-
ret += ` ? val${level}.__helper.__identifier\n`;
|
|
565
|
-
ret += ` : toArray(val${level}?.__helper?.getPrimaryKey(true));\n`;
|
|
566
|
-
ret += ` ret${dataKey} = new PolymorphicRef(discriminator, pk);\n`;
|
|
567
|
-
ret += ` }\n`;
|
|
568
|
-
}
|
|
569
|
-
else if (prop.targetKey) {
|
|
570
|
-
// When targetKey is set, extract that property value instead of the PK
|
|
571
|
-
const targetProp = prop.targetMeta?.properties[prop.targetKey];
|
|
572
|
-
ret += ` if (entity${entityKey} === null) {\n`;
|
|
573
|
-
ret += ` ret${dataKey} = null;\n`;
|
|
574
|
-
ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
|
|
575
|
-
ret += ` const val${level} = entity${entityKey}${unwrap};\n`;
|
|
576
|
-
if (targetProp?.customType) {
|
|
577
|
-
// If targetKey property has a custom type, convert to database value
|
|
578
|
-
const convertorKey = this.registerCustomType(targetProp, context);
|
|
579
|
-
ret += ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(val${level}?.${prop.targetKey});\n`;
|
|
580
|
-
}
|
|
581
|
-
else {
|
|
582
|
-
ret += ` ret${dataKey} = val${level}?.${prop.targetKey};\n`;
|
|
583
|
-
}
|
|
584
|
-
ret += ` }\n`;
|
|
585
|
-
}
|
|
586
|
-
else {
|
|
587
|
-
this.setToArrayHelper(context);
|
|
588
|
-
context.set('EntityIdentifier', EntityIdentifier);
|
|
589
|
-
ret += ` if (entity${entityKey} === null) {\n`;
|
|
590
|
-
ret += ` ret${dataKey} = null;\n`;
|
|
591
|
-
ret += ` } else if (entity${entityKey}?.__helper.__identifier && !entity${entityKey}.__helper.hasPrimaryKey()) {\n`;
|
|
592
|
-
ret += ` ret${dataKey} = entity${entityKey}?.__helper.__identifier;\n`;
|
|
593
|
-
ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
|
|
594
|
-
ret += ` ret${dataKey} = toArray(entity${entityKey}.__helper.getPrimaryKey(true));\n`;
|
|
595
|
-
ret += ` }\n`;
|
|
596
|
-
}
|
|
597
|
-
return ret + ' }\n';
|
|
598
|
-
}
|
|
599
|
-
if (prop.customType) {
|
|
600
|
-
const convertorKey = this.registerCustomType(prop, context);
|
|
601
|
-
if (['number', 'string', 'boolean', 'bigint'].includes(prop.customType.compareAsType().toLowerCase())) {
|
|
602
|
-
return ret + ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(entity${entityKey}${unwrap});\n }\n`;
|
|
603
|
-
}
|
|
604
|
-
return (ret + ` ret${dataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${entityKey}${unwrap}));\n }\n`);
|
|
605
|
-
}
|
|
606
|
-
if (prop.runtimeType === 'Date') {
|
|
607
|
-
context.set('processDateProperty', this.#platform.processDateProperty.bind(this.#platform));
|
|
608
|
-
return ret + ` ret${dataKey} = clone(processDateProperty(entity${entityKey}${unwrap}));\n }\n`;
|
|
609
|
-
}
|
|
610
|
-
return ret + ` ret${dataKey} = clone(entity${entityKey}${unwrap});\n }\n`;
|
|
611
|
-
}
|
|
612
|
-
/**
|
|
613
|
-
* @internal Highly performance-sensitive method.
|
|
614
|
-
*/
|
|
615
|
-
getEntityComparator(entityName) {
|
|
616
|
-
const meta = this.#metadata.find(entityName);
|
|
617
|
-
const exists = this.#comparators.get(meta);
|
|
618
|
-
if (exists) {
|
|
619
|
-
return exists;
|
|
620
|
-
}
|
|
621
|
-
const lines = [];
|
|
622
|
-
const context = new Map();
|
|
623
|
-
context.set('compareArrays', compareArrays);
|
|
624
|
-
context.set('compareBooleans', compareBooleans);
|
|
625
|
-
context.set('compareBuffers', compareBuffers);
|
|
626
|
-
context.set('compareObjects', compareObjects);
|
|
627
|
-
context.set('equals', equals);
|
|
628
|
-
for (const prop of meta.comparableProps) {
|
|
629
|
-
lines.push(this.getPropertyComparator(prop, context));
|
|
630
|
-
}
|
|
631
|
-
// also compare 1:1 inverse sides, important for `factory.mergeData`
|
|
632
|
-
lines.push(`if (options?.includeInverseSides) {`);
|
|
633
|
-
for (const prop of meta.bidirectionalRelations) {
|
|
634
|
-
if (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner && prop.hydrate !== false) {
|
|
635
|
-
lines.push(this.getPropertyComparator(prop, context));
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
lines.push(`}`);
|
|
639
|
-
const code = `// compiled comparator for entity ${meta.className}\n` +
|
|
640
|
-
`return function(last, current, options) {\n const diff = {};\n${lines.join('\n')}\n return diff;\n}`;
|
|
641
|
-
const fnKey = `comparator-${meta.uniqueName}`;
|
|
642
|
-
const comparator = Utils.createFunction(context, code, this.#config?.get('compiledFunctions'), fnKey);
|
|
643
|
-
this.#comparators.set(meta, comparator);
|
|
644
|
-
return comparator;
|
|
645
|
-
}
|
|
646
|
-
getGenericComparator(prop, cond) {
|
|
647
|
-
return (` if (current${prop} === null && last${prop} === undefined) {\n` +
|
|
648
|
-
` diff${prop} = current${prop};\n` +
|
|
649
|
-
` } else if (current${prop} == null && last${prop} == null) {\n\n` +
|
|
650
|
-
` } else if ((current${prop} != null && last${prop} == null) || (current${prop} == null && last${prop} != null)) {\n` +
|
|
651
|
-
` diff${prop} = current${prop};\n` +
|
|
652
|
-
` } else if (${cond}) {\n` +
|
|
653
|
-
` diff${prop} = current${prop};\n` +
|
|
654
|
-
` }\n`);
|
|
655
|
-
}
|
|
656
|
-
getPropertyComparator(prop, context) {
|
|
657
|
-
let type = prop.type.toLowerCase();
|
|
658
|
-
if (prop.kind !== ReferenceKind.SCALAR && prop.kind !== ReferenceKind.EMBEDDED) {
|
|
659
|
-
if (prop.polymorphic) {
|
|
660
|
-
type = 'object';
|
|
661
|
-
}
|
|
662
|
-
else {
|
|
663
|
-
const meta2 = prop.targetMeta;
|
|
664
|
-
if (meta2.primaryKeys.length > 1) {
|
|
665
|
-
type = 'array';
|
|
666
|
-
}
|
|
667
|
-
else {
|
|
668
|
-
type = meta2.getPrimaryProp().type.toLowerCase();
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
if (prop.customType) {
|
|
673
|
-
if (prop.customType.compareValues) {
|
|
674
|
-
const idx = this.#tmpIndex++;
|
|
675
|
-
context.set(`compareValues_${idx}`, (a, b) => {
|
|
676
|
-
if (Raw.isKnownFragment(a) || Raw.isKnownFragment(b)) {
|
|
677
|
-
return Raw.getKnownFragment(a) === Raw.getKnownFragment(b);
|
|
678
|
-
}
|
|
679
|
-
return prop.customType.compareValues(a, b);
|
|
680
|
-
});
|
|
681
|
-
return this.getGenericComparator(this.wrap(prop.name), `!compareValues_${idx}(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
682
|
-
}
|
|
683
|
-
type = prop.customType.compareAsType().toLowerCase();
|
|
684
|
-
}
|
|
685
|
-
if (type.endsWith('[]')) {
|
|
686
|
-
type = 'array';
|
|
687
|
-
}
|
|
688
|
-
if (['string', 'number', 'bigint'].includes(type)) {
|
|
689
|
-
return this.getGenericComparator(this.wrap(prop.name), `last${this.wrap(prop.name)} !== current${this.wrap(prop.name)}`);
|
|
690
|
-
}
|
|
691
|
-
if (type === 'boolean') {
|
|
692
|
-
return this.getGenericComparator(this.wrap(prop.name), `!compareBooleans(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
693
|
-
}
|
|
694
|
-
if (['array'].includes(type) || type.endsWith('[]')) {
|
|
695
|
-
return this.getGenericComparator(this.wrap(prop.name), `!compareArrays(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
696
|
-
}
|
|
697
|
-
if (['buffer', 'uint8array'].includes(type)) {
|
|
698
|
-
return this.getGenericComparator(this.wrap(prop.name), `!compareBuffers(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
699
|
-
}
|
|
700
|
-
if (type === 'date') {
|
|
701
|
-
return this.getGenericComparator(this.wrap(prop.name), `last${this.wrap(prop.name)}.valueOf() !== current${this.wrap(prop.name)}.valueOf()`);
|
|
702
|
-
}
|
|
703
|
-
if (type === 'objectid') {
|
|
704
|
-
// We might be comparing PK to object, in case we compare with cached data of populated entity
|
|
705
|
-
// in such case we just ignore the comparison and fallback to `equals()` (which will still mark
|
|
706
|
-
// it as not equal as we compare PK to plain object).
|
|
707
|
-
const cond = `last${this.wrap(prop.name)}.toHexString?.() !== current${this.wrap(prop.name)}.toHexString?.()`;
|
|
708
|
-
return this.getGenericComparator(this.wrap(prop.name), cond);
|
|
709
|
-
}
|
|
710
|
-
return this.getGenericComparator(this.wrap(prop.name), `!equals(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
767
|
+
if (type.endsWith('[]')) {
|
|
768
|
+
type = 'array';
|
|
711
769
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
770
|
+
if (['string', 'number', 'bigint'].includes(type)) {
|
|
771
|
+
return this.getGenericComparator(
|
|
772
|
+
this.wrap(prop.name),
|
|
773
|
+
`last${this.wrap(prop.name)} !== current${this.wrap(prop.name)}`,
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
if (type === 'boolean') {
|
|
777
|
+
return this.getGenericComparator(
|
|
778
|
+
this.wrap(prop.name),
|
|
779
|
+
`!compareBooleans(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`,
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
if (['array'].includes(type) || type.endsWith('[]')) {
|
|
783
|
+
return this.getGenericComparator(
|
|
784
|
+
this.wrap(prop.name),
|
|
785
|
+
`!compareArrays(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`,
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
if (['buffer', 'uint8array'].includes(type)) {
|
|
789
|
+
return this.getGenericComparator(
|
|
790
|
+
this.wrap(prop.name),
|
|
791
|
+
`!compareBuffers(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`,
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
if (type === 'date') {
|
|
795
|
+
return this.getGenericComparator(
|
|
796
|
+
this.wrap(prop.name),
|
|
797
|
+
`last${this.wrap(prop.name)}.valueOf() !== current${this.wrap(prop.name)}.valueOf()`,
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
if (type === 'objectid') {
|
|
801
|
+
// We might be comparing PK to object, in case we compare with cached data of populated entity
|
|
802
|
+
// in such case we just ignore the comparison and fallback to `equals()` (which will still mark
|
|
803
|
+
// it as not equal as we compare PK to plain object).
|
|
804
|
+
const cond = `last${this.wrap(prop.name)}.toHexString?.() !== current${this.wrap(prop.name)}.toHexString?.()`;
|
|
805
|
+
return this.getGenericComparator(this.wrap(prop.name), cond);
|
|
806
|
+
}
|
|
807
|
+
return this.getGenericComparator(
|
|
808
|
+
this.wrap(prop.name),
|
|
809
|
+
`!equals(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`,
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
wrap(key) {
|
|
813
|
+
if (/^\[.*]$/.exec(key)) {
|
|
814
|
+
return key;
|
|
815
|
+
}
|
|
816
|
+
return /^\w+$/.exec(key) ? `.${key}` : `['${key}']`;
|
|
817
|
+
}
|
|
818
|
+
safeKey(key) {
|
|
819
|
+
return key.replace(/\W/g, '_');
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Sets the toArray helper in the context if not already set.
|
|
823
|
+
* Used for converting composite PKs to arrays.
|
|
824
|
+
*/
|
|
825
|
+
setToArrayHelper(context) {
|
|
826
|
+
if (context.has('toArray')) {
|
|
827
|
+
return;
|
|
746
828
|
}
|
|
829
|
+
const toArray = val => {
|
|
830
|
+
if (Utils.isPlainObject(val)) {
|
|
831
|
+
return Object.values(val).map(v => toArray(v));
|
|
832
|
+
}
|
|
833
|
+
return val;
|
|
834
|
+
};
|
|
835
|
+
context.set('toArray', toArray);
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* perf: used to generate list of comparable properties during discovery, so we speed up the runtime comparison
|
|
839
|
+
*/
|
|
840
|
+
static isComparable(prop, root) {
|
|
841
|
+
const virtual = prop.persist === false || (prop.generated && !prop.primary);
|
|
842
|
+
const inverse = prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner;
|
|
843
|
+
const discriminator = prop.name === root.discriminatorColumn;
|
|
844
|
+
const collection = prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY;
|
|
845
|
+
return !virtual && !collection && !inverse && !discriminator && !prop.version;
|
|
846
|
+
}
|
|
747
847
|
}
|