@mikro-orm/core 7.0.4 → 7.0.5-dev.0
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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EntityMetadata } from '../typings.js';
|
|
1
|
+
import { EntityMetadata, } from '../typings.js';
|
|
2
2
|
import { compareArrays, Utils } from '../utils/Utils.js';
|
|
3
3
|
import { QueryHelper } from '../utils/QueryHelper.js';
|
|
4
4
|
import { MetadataValidator } from './MetadataValidator.js';
|
|
@@ -13,1965 +13,1884 @@ import { raw, Raw } from '../utils/RawQueryFragment.js';
|
|
|
13
13
|
import { BaseEntity } from '../entity/BaseEntity.js';
|
|
14
14
|
/** Discovers, validates, and processes entity metadata from configured sources. */
|
|
15
15
|
export class MetadataDiscovery {
|
|
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
|
-
mapDiscoveredEntities() {
|
|
86
|
-
const discovered = new MetadataStorage();
|
|
87
|
-
this.#discovered
|
|
88
|
-
.filter(meta => meta.root.name)
|
|
89
|
-
.sort((a, b) => b.root.name.localeCompare(a.root.name))
|
|
90
|
-
.forEach(meta => {
|
|
91
|
-
this.#platform.validateMetadata(meta);
|
|
92
|
-
discovered.set(meta.class, meta);
|
|
93
|
-
});
|
|
94
|
-
for (const meta of discovered) {
|
|
95
|
-
meta.root = discovered.get(meta.root.class);
|
|
96
|
-
if (meta.inheritanceType === 'tpt') {
|
|
97
|
-
this.computeTPTOwnProps(meta);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return discovered;
|
|
101
|
-
}
|
|
102
|
-
initAccessors(meta) {
|
|
103
|
-
for (const prop of Object.values(meta.properties)) {
|
|
104
|
-
if (!prop.accessor || meta.properties[prop.accessor]) {
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
const desc = Object.getOwnPropertyDescriptor(meta.prototype, prop.name);
|
|
108
|
-
if (desc?.get || desc?.set) {
|
|
109
|
-
this.initRelation(prop);
|
|
110
|
-
this.initFieldName(prop);
|
|
111
|
-
const accessor = prop.name;
|
|
112
|
-
prop.name = typeof prop.accessor === 'string' ? prop.accessor : prop.name;
|
|
113
|
-
if (prop.accessor === true) {
|
|
114
|
-
prop.getter = prop.setter = true;
|
|
115
|
-
} else {
|
|
116
|
-
prop.getter = prop.setter = false;
|
|
117
|
-
}
|
|
118
|
-
prop.accessor = accessor;
|
|
119
|
-
prop.serializedName ??= accessor;
|
|
120
|
-
Utils.renameKey(meta.properties, accessor, prop.name);
|
|
121
|
-
} else {
|
|
122
|
-
const name = prop.name;
|
|
123
|
-
if (prop.kind === ReferenceKind.SCALAR || prop.kind === ReferenceKind.EMBEDDED) {
|
|
124
|
-
prop.name = prop.accessor;
|
|
125
|
-
}
|
|
126
|
-
this.initRelation(prop);
|
|
127
|
-
this.initFieldName(prop);
|
|
128
|
-
prop.serializedName ??= prop.accessor;
|
|
129
|
-
prop.name = name;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/** Processes discovered entities: initializes relations, embeddables, indexes, and inheritance. */
|
|
134
|
-
processDiscoveredEntities(discovered) {
|
|
135
|
-
for (const meta of discovered) {
|
|
136
|
-
let i = 1;
|
|
137
|
-
Object.values(meta.properties).forEach(prop => meta.propertyOrder.set(prop.name, i++));
|
|
138
|
-
Object.values(meta.properties).forEach(prop => this.initPolyEmbeddables(prop, discovered));
|
|
139
|
-
this.initAccessors(meta);
|
|
140
|
-
}
|
|
141
|
-
// ignore base entities (not annotated with @Entity)
|
|
142
|
-
const filtered = discovered.filter(meta => meta.root.name);
|
|
143
|
-
// sort so we discover entities first to get around issues with nested embeddables
|
|
144
|
-
filtered.sort((a, b) => (!a.embeddable === !b.embeddable ? 0 : a.embeddable ? 1 : -1));
|
|
145
|
-
filtered.forEach(meta => this.initSingleTableInheritance(meta, filtered));
|
|
146
|
-
filtered.forEach(meta => this.initTPTRelationships(meta, filtered));
|
|
147
|
-
filtered.forEach(meta => this.defineBaseEntityProperties(meta));
|
|
148
|
-
filtered.forEach(meta => {
|
|
149
|
-
const newMeta = EntitySchema.fromMetadata(meta).init().meta;
|
|
150
|
-
return this.#metadata.set(newMeta.class, newMeta);
|
|
151
|
-
});
|
|
152
|
-
filtered.forEach(meta => this.initAutoincrement(meta));
|
|
153
|
-
const forEachProp = cb => {
|
|
154
|
-
filtered.forEach(meta => Object.values(meta.properties).forEach(prop => cb(meta, prop)));
|
|
155
|
-
};
|
|
156
|
-
forEachProp((m, p) => this.initFactoryField(m, p));
|
|
157
|
-
forEachProp((m, p) => this.initPolymorphicRelation(m, p, filtered));
|
|
158
|
-
forEachProp((_m, p) => this.initRelation(p));
|
|
159
|
-
forEachProp((m, p) => this.initEmbeddables(m, p));
|
|
160
|
-
forEachProp((_m, p) => this.initFieldName(p));
|
|
161
|
-
filtered.forEach(meta => this.finalizeTPTInheritance(meta, filtered));
|
|
162
|
-
filtered.forEach(meta => this.computeTPTOwnProps(meta));
|
|
163
|
-
forEachProp((m, p) => this.initVersionProperty(m, p));
|
|
164
|
-
forEachProp((m, p) => this.initCustomType(m, p));
|
|
165
|
-
forEachProp((m, p) => this.initGeneratedColumn(m, p));
|
|
166
|
-
filtered.forEach(meta => this.initAutoincrement(meta)); // once again after we init custom types
|
|
167
|
-
filtered.forEach(meta => this.initCheckConstraints(meta));
|
|
168
|
-
forEachProp((_m, p) => {
|
|
169
|
-
this.initDefaultValue(p);
|
|
170
|
-
this.inferTypeFromDefault(p);
|
|
171
|
-
this.initRelation(p);
|
|
172
|
-
this.initColumnType(p);
|
|
173
|
-
});
|
|
174
|
-
forEachProp((m, p) => this.initIndexes(m, p));
|
|
175
|
-
filtered.forEach(meta => this.autoWireBidirectionalProperties(meta));
|
|
176
|
-
for (const meta of filtered) {
|
|
177
|
-
discovered.push(...this.processEntity(meta));
|
|
178
|
-
}
|
|
179
|
-
discovered.forEach(meta => meta.sync(true));
|
|
180
|
-
this.#metadataProvider.combineCache();
|
|
181
|
-
return discovered.map(meta => {
|
|
182
|
-
meta = this.#metadata.get(meta.class);
|
|
183
|
-
meta.sync(true);
|
|
184
|
-
this.findReferencingProperties(meta, filtered);
|
|
185
|
-
if (meta.inheritanceType === 'tpt') {
|
|
186
|
-
this.computeTPTOwnProps(meta);
|
|
187
|
-
}
|
|
188
|
-
return meta;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
async findEntities(preferTs) {
|
|
192
|
-
const { entities, entitiesTs, baseDir } = this.#config.getAll();
|
|
193
|
-
const targets = preferTs && entitiesTs.length > 0 ? entitiesTs : entities;
|
|
194
|
-
const processed = [];
|
|
195
|
-
const paths = [];
|
|
196
|
-
for (const entity of targets) {
|
|
197
|
-
if (typeof entity === 'string') {
|
|
198
|
-
paths.push(entity);
|
|
199
|
-
} else {
|
|
200
|
-
processed.push(entity);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
if (paths.length > 0) {
|
|
204
|
-
const { discoverEntities } = await import('@mikro-orm/core/file-discovery');
|
|
205
|
-
processed.push(...(await discoverEntities(paths, { baseDir })));
|
|
206
|
-
}
|
|
207
|
-
return this.discoverReferences(processed);
|
|
208
|
-
}
|
|
209
|
-
discoverMissingTargets() {
|
|
210
|
-
const unwrap = type =>
|
|
211
|
-
type
|
|
212
|
-
.replace(/Array<(.*)>/, '$1') // unwrap array
|
|
213
|
-
.replace(/\[]$/, '') // remove array suffix
|
|
214
|
-
.replace(/\((.*)\)/, '$1'); // unwrap union types
|
|
215
|
-
const missing = [];
|
|
216
|
-
this.#discovered.forEach(meta =>
|
|
217
|
-
Object.values(meta.properties).forEach(prop => {
|
|
218
|
-
if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.pivotEntity) {
|
|
219
|
-
const pivotEntity = prop.pivotEntity;
|
|
220
|
-
const target = typeof pivotEntity === 'function' && !pivotEntity.prototype ? pivotEntity() : pivotEntity;
|
|
221
|
-
if (!this.#discovered.find(m => m.className === Utils.className(target))) {
|
|
222
|
-
missing.push(target);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (prop.kind !== ReferenceKind.SCALAR) {
|
|
226
|
-
const target = typeof prop.entity === 'function' && !prop.entity.prototype ? prop.entity() : prop.type;
|
|
227
|
-
if (
|
|
228
|
-
!unwrap(prop.type)
|
|
229
|
-
.split(/ ?\| ?/)
|
|
230
|
-
.every(type => this.#discovered.find(m => m.className === type))
|
|
231
|
-
) {
|
|
232
|
-
missing.push(...Utils.asArray(target));
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}),
|
|
236
|
-
);
|
|
237
|
-
if (missing.length > 0) {
|
|
238
|
-
this.tryDiscoverTargets(missing);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
tryDiscoverTargets(targets) {
|
|
242
|
-
for (const target of targets) {
|
|
243
|
-
const schema = EntitySchema.is(target) ? target : undefined;
|
|
244
|
-
const isDiscoverable = typeof target === 'function' || schema;
|
|
245
|
-
if (isDiscoverable && target.name) {
|
|
246
|
-
// Get the actual class for EntitySchema, or use target directly for classes
|
|
247
|
-
const targetClass = schema ? schema.meta.class : target;
|
|
248
|
-
if (!this.#metadata.has(targetClass)) {
|
|
249
|
-
this.discoverReferences([target], false);
|
|
250
|
-
this.discoverMissingTargets();
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
discoverReferences(refs, validate = true) {
|
|
256
|
-
const found = [];
|
|
257
|
-
for (const entity of refs) {
|
|
258
|
-
if (typeof entity === 'string') {
|
|
259
|
-
throw new Error('Folder based discovery requires the async `MikroORM.init()` method.');
|
|
260
|
-
}
|
|
261
|
-
const schema = this.getSchema(entity);
|
|
262
|
-
const meta = schema.init().meta;
|
|
263
|
-
this.#metadata.set(meta.class, meta);
|
|
264
|
-
found.push(schema);
|
|
265
|
-
}
|
|
266
|
-
// discover parents (base entities) automatically
|
|
267
|
-
for (const meta of this.#metadata) {
|
|
268
|
-
let parent = meta.extends;
|
|
269
|
-
if (EntitySchema.is(parent) && !this.#metadata.has(parent.init().meta.class)) {
|
|
270
|
-
this.discoverReferences([parent], false);
|
|
271
|
-
}
|
|
272
|
-
if (typeof parent === 'function' && parent.name && !this.#metadata.has(parent)) {
|
|
273
|
-
this.discoverReferences([parent], false);
|
|
274
|
-
}
|
|
275
|
-
/* v8 ignore next */
|
|
276
|
-
if (!meta.class) {
|
|
277
|
-
continue;
|
|
278
|
-
}
|
|
279
|
-
parent = Object.getPrototypeOf(meta.class);
|
|
280
|
-
// Skip if parent is the auto-generated base class for the same entity (from setClass usage)
|
|
281
|
-
if (
|
|
282
|
-
parent.name !== '' &&
|
|
283
|
-
parent.name !== meta.className &&
|
|
284
|
-
!this.#metadata.has(parent) &&
|
|
285
|
-
parent !== BaseEntity
|
|
286
|
-
) {
|
|
287
|
-
this.discoverReferences([parent], false);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
for (const schema of found) {
|
|
291
|
-
this.discoverEntity(schema);
|
|
292
|
-
}
|
|
293
|
-
this.discoverMissingTargets();
|
|
294
|
-
if (validate) {
|
|
295
|
-
this.#validator.validateDiscovered(this.#discovered, this.#config.get('discovery'));
|
|
296
|
-
}
|
|
297
|
-
return this.#discovered.filter(meta => found.find(m => m.name === meta.className));
|
|
298
|
-
}
|
|
299
|
-
reset(entityName) {
|
|
300
|
-
const exists = this.#discovered.findIndex(
|
|
301
|
-
m => m.class === entityName || m.className === Utils.className(entityName),
|
|
302
|
-
);
|
|
303
|
-
if (exists !== -1) {
|
|
304
|
-
this.#metadata.reset(this.#discovered[exists].class);
|
|
305
|
-
this.#discovered.splice(exists, 1);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
getSchema(entity) {
|
|
309
|
-
if (EntitySchema.REGISTRY.has(entity)) {
|
|
310
|
-
entity = EntitySchema.REGISTRY.get(entity);
|
|
311
|
-
}
|
|
312
|
-
if (EntitySchema.is(entity)) {
|
|
313
|
-
const meta = Utils.copy(entity.meta, false);
|
|
314
|
-
return EntitySchema.fromMetadata(meta);
|
|
315
|
-
}
|
|
316
|
-
// After the EntitySchema check, entity must be an EntityClass
|
|
317
|
-
const cls = entity;
|
|
318
|
-
const path = cls[MetadataStorage.PATH_SYMBOL];
|
|
319
|
-
if (path) {
|
|
320
|
-
const meta = Utils.copy(MetadataStorage.getMetadata(cls.name, path), false);
|
|
321
|
-
meta.path = path;
|
|
322
|
-
this.#metadata.set(cls, meta);
|
|
323
|
-
}
|
|
324
|
-
const exists = this.#metadata.has(cls);
|
|
325
|
-
const meta = this.#metadata.get(cls, true);
|
|
326
|
-
meta.abstract ??= !(exists && meta.name);
|
|
327
|
-
const schema = EntitySchema.fromMetadata(meta);
|
|
328
|
-
schema.setClass(cls);
|
|
329
|
-
return schema;
|
|
330
|
-
}
|
|
331
|
-
getRootEntity(meta) {
|
|
332
|
-
const base = meta.extends && this.#metadata.find(meta.extends);
|
|
333
|
-
if (!base || base === meta) {
|
|
334
|
-
// make sure we do not fall into infinite loop
|
|
335
|
-
return meta;
|
|
336
|
-
}
|
|
337
|
-
const root = this.getRootEntity(base);
|
|
338
|
-
// For STI or TPT, use the root entity.
|
|
339
|
-
// Check both `inheritanceType` (set during discovery) and raw `inheritance` option (set before discovery).
|
|
340
|
-
if (root.discriminatorColumn || root.inheritanceType || root.inheritance === 'tpt') {
|
|
341
|
-
return root;
|
|
342
|
-
}
|
|
343
|
-
return meta;
|
|
344
|
-
}
|
|
345
|
-
discoverEntity(schema) {
|
|
346
|
-
const meta = schema.meta;
|
|
347
|
-
const path = meta.path;
|
|
348
|
-
this.#logger.log(
|
|
349
|
-
'discovery',
|
|
350
|
-
`- processing entity ${colors.cyan(meta.className)}${colors.grey(path ? ` (${path})` : '')}`,
|
|
351
|
-
);
|
|
352
|
-
const root = this.getRootEntity(meta);
|
|
353
|
-
schema.meta.path = meta.path;
|
|
354
|
-
const cache = this.#metadataProvider.getCachedMetadata(meta, root);
|
|
355
|
-
if (cache) {
|
|
356
|
-
this.#logger.log('discovery', `- using cached metadata for entity ${colors.cyan(meta.className)}`);
|
|
357
|
-
this.#discovered.push(meta);
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
// infer default value from property initializer early, as the metadata provider might use some defaults, e.g. string for reflect-metadata
|
|
361
|
-
for (const prop of meta.props) {
|
|
362
|
-
this.inferDefaultValue(meta, prop);
|
|
363
|
-
}
|
|
364
|
-
// if the definition is using EntitySchema we still want it to go through the metadata provider to validate no types are missing
|
|
365
|
-
this.#metadataProvider.loadEntityMetadata(meta);
|
|
366
|
-
if (!meta.tableName && meta.name) {
|
|
367
|
-
const entityName = root.discriminatorColumn ? root.name : meta.name;
|
|
368
|
-
meta.tableName = this.#namingStrategy.classToTableName(entityName);
|
|
369
|
-
}
|
|
370
|
-
this.#metadataProvider.saveToCache(meta);
|
|
371
|
-
meta.root = root;
|
|
372
|
-
this.#discovered.push(meta);
|
|
373
|
-
}
|
|
374
|
-
initNullability(prop) {
|
|
375
|
-
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
376
|
-
return Utils.defaultValue(prop, 'nullable', prop.optional || !prop.owner);
|
|
377
|
-
}
|
|
378
|
-
return Utils.defaultValue(prop, 'nullable', prop.optional);
|
|
379
|
-
}
|
|
380
|
-
applyNamingStrategy(meta, prop) {
|
|
381
|
-
if (!prop.fieldNames) {
|
|
382
|
-
this.initFieldName(prop);
|
|
383
|
-
}
|
|
384
|
-
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
385
|
-
this.initManyToManyFields(meta, prop);
|
|
386
|
-
}
|
|
387
|
-
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
388
|
-
this.initManyToOneFields(prop);
|
|
389
|
-
}
|
|
390
|
-
if (prop.kind === ReferenceKind.ONE_TO_MANY) {
|
|
391
|
-
this.initOneToManyFields(prop);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
initOwnColumns(meta) {
|
|
395
|
-
meta.sync();
|
|
396
|
-
for (const prop of meta.props) {
|
|
397
|
-
if (
|
|
398
|
-
!prop.joinColumns ||
|
|
399
|
-
!prop.columnTypes ||
|
|
400
|
-
prop.ownColumns ||
|
|
401
|
-
![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)
|
|
402
|
-
) {
|
|
403
|
-
continue;
|
|
404
|
-
}
|
|
405
|
-
// For polymorphic relations, ownColumns should include all fieldNames
|
|
406
|
-
// (discriminator + ID columns) since they are all owned by this relation
|
|
407
|
-
if (prop.polymorphic) {
|
|
408
|
-
prop.ownColumns = prop.fieldNames;
|
|
409
|
-
continue;
|
|
410
|
-
}
|
|
411
|
-
if (prop.joinColumns.length > 1) {
|
|
412
|
-
prop.ownColumns = prop.joinColumns.filter(col => {
|
|
413
|
-
return !meta.props.find(p => p.name !== prop.name && (!p.fieldNames || p.fieldNames.includes(col)));
|
|
16
|
+
#namingStrategy;
|
|
17
|
+
#metadataProvider;
|
|
18
|
+
#logger;
|
|
19
|
+
#schemaHelper;
|
|
20
|
+
#validator = new MetadataValidator();
|
|
21
|
+
#discovered = [];
|
|
22
|
+
#metadata;
|
|
23
|
+
#platform;
|
|
24
|
+
#config;
|
|
25
|
+
constructor(metadata, platform, config) {
|
|
26
|
+
this.#metadata = metadata;
|
|
27
|
+
this.#platform = platform;
|
|
28
|
+
this.#config = config;
|
|
29
|
+
this.#namingStrategy = this.#config.getNamingStrategy();
|
|
30
|
+
this.#metadataProvider = this.#config.getMetadataProvider();
|
|
31
|
+
this.#logger = this.#config.getLogger();
|
|
32
|
+
this.#schemaHelper = this.#platform.getSchemaHelper();
|
|
33
|
+
}
|
|
34
|
+
/** Discovers all entities asynchronously and returns the populated MetadataStorage. */
|
|
35
|
+
async discover(preferTs = true) {
|
|
36
|
+
this.#discovered.length = 0;
|
|
37
|
+
const startTime = Date.now();
|
|
38
|
+
const suffix = this.#metadataProvider.constructor === MetadataProvider
|
|
39
|
+
? ''
|
|
40
|
+
: `, using ${colors.cyan(this.#metadataProvider.constructor.name)}`;
|
|
41
|
+
this.#logger.log('discovery', `ORM entity discovery started${suffix}`);
|
|
42
|
+
await this.findEntities(preferTs);
|
|
43
|
+
for (const meta of this.#discovered) {
|
|
44
|
+
/* v8 ignore next */
|
|
45
|
+
await this.#config.get('discovery').onMetadata?.(meta, this.#platform);
|
|
46
|
+
}
|
|
47
|
+
this.processDiscoveredEntities(this.#discovered);
|
|
48
|
+
const diff = Date.now() - startTime;
|
|
49
|
+
this.#logger.log('discovery', `- entity discovery finished, found ${colors.green('' + this.#discovered.length)} entities, took ${colors.green(`${diff} ms`)}`);
|
|
50
|
+
const storage = this.mapDiscoveredEntities();
|
|
51
|
+
/* v8 ignore next */
|
|
52
|
+
await this.#config.get('discovery').afterDiscovered?.(storage, this.#platform);
|
|
53
|
+
return storage;
|
|
54
|
+
}
|
|
55
|
+
/** Discovers all entities synchronously and returns the populated MetadataStorage. */
|
|
56
|
+
discoverSync() {
|
|
57
|
+
this.#discovered.length = 0;
|
|
58
|
+
const startTime = Date.now();
|
|
59
|
+
const suffix = this.#metadataProvider.constructor === MetadataProvider
|
|
60
|
+
? ''
|
|
61
|
+
: `, using ${colors.cyan(this.#metadataProvider.constructor.name)}`;
|
|
62
|
+
this.#logger.log('discovery', `ORM entity discovery started${suffix} in sync mode`);
|
|
63
|
+
const refs = this.#config.get('entities');
|
|
64
|
+
this.discoverReferences(refs);
|
|
65
|
+
for (const meta of this.#discovered) {
|
|
66
|
+
/* v8 ignore next */
|
|
67
|
+
void this.#config.get('discovery').onMetadata?.(meta, this.#platform);
|
|
68
|
+
}
|
|
69
|
+
this.processDiscoveredEntities(this.#discovered);
|
|
70
|
+
const diff = Date.now() - startTime;
|
|
71
|
+
this.#logger.log('discovery', `- entity discovery finished, found ${colors.green('' + this.#discovered.length)} entities, took ${colors.green(`${diff} ms`)}`);
|
|
72
|
+
const storage = this.mapDiscoveredEntities();
|
|
73
|
+
/* v8 ignore next */
|
|
74
|
+
void this.#config.get('discovery').afterDiscovered?.(storage, this.#platform);
|
|
75
|
+
return storage;
|
|
76
|
+
}
|
|
77
|
+
mapDiscoveredEntities() {
|
|
78
|
+
const discovered = new MetadataStorage();
|
|
79
|
+
this.#discovered
|
|
80
|
+
.filter(meta => meta.root.name)
|
|
81
|
+
.sort((a, b) => b.root.name.localeCompare(a.root.name))
|
|
82
|
+
.forEach(meta => {
|
|
83
|
+
this.#platform.validateMetadata(meta);
|
|
84
|
+
discovered.set(meta.class, meta);
|
|
414
85
|
});
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
86
|
+
for (const meta of discovered) {
|
|
87
|
+
meta.root = discovered.get(meta.root.class);
|
|
88
|
+
if (meta.inheritanceType === 'tpt') {
|
|
89
|
+
this.computeTPTOwnProps(meta);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return discovered;
|
|
93
|
+
}
|
|
94
|
+
initAccessors(meta) {
|
|
95
|
+
for (const prop of Object.values(meta.properties)) {
|
|
96
|
+
if (!prop.accessor || meta.properties[prop.accessor]) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const desc = Object.getOwnPropertyDescriptor(meta.prototype, prop.name);
|
|
100
|
+
if (desc?.get || desc?.set) {
|
|
101
|
+
this.initRelation(prop);
|
|
102
|
+
this.initFieldName(prop);
|
|
103
|
+
const accessor = prop.name;
|
|
104
|
+
prop.name = typeof prop.accessor === 'string' ? prop.accessor : prop.name;
|
|
105
|
+
if (prop.accessor === true) {
|
|
106
|
+
prop.getter = prop.setter = true;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
prop.getter = prop.setter = false;
|
|
110
|
+
}
|
|
111
|
+
prop.accessor = accessor;
|
|
112
|
+
prop.serializedName ??= accessor;
|
|
113
|
+
Utils.renameKey(meta.properties, accessor, prop.name);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const name = prop.name;
|
|
117
|
+
if (prop.kind === ReferenceKind.SCALAR || prop.kind === ReferenceKind.EMBEDDED) {
|
|
118
|
+
prop.name = prop.accessor;
|
|
119
|
+
}
|
|
120
|
+
this.initRelation(prop);
|
|
121
|
+
this.initFieldName(prop);
|
|
122
|
+
prop.serializedName ??= prop.accessor;
|
|
123
|
+
prop.name = name;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Processes discovered entities: initializes relations, embeddables, indexes, and inheritance. */
|
|
128
|
+
processDiscoveredEntities(discovered) {
|
|
129
|
+
for (const meta of discovered) {
|
|
130
|
+
let i = 1;
|
|
131
|
+
Object.values(meta.properties).forEach(prop => meta.propertyOrder.set(prop.name, i++));
|
|
132
|
+
Object.values(meta.properties).forEach(prop => this.initPolyEmbeddables(prop, discovered));
|
|
133
|
+
this.initAccessors(meta);
|
|
134
|
+
}
|
|
135
|
+
// ignore base entities (not annotated with @Entity)
|
|
136
|
+
const filtered = discovered.filter(meta => meta.root.name);
|
|
137
|
+
// sort so we discover entities first to get around issues with nested embeddables
|
|
138
|
+
filtered.sort((a, b) => (!a.embeddable === !b.embeddable ? 0 : a.embeddable ? 1 : -1));
|
|
139
|
+
filtered.forEach(meta => this.initSingleTableInheritance(meta, filtered));
|
|
140
|
+
filtered.forEach(meta => this.initTPTRelationships(meta, filtered));
|
|
141
|
+
filtered.forEach(meta => this.defineBaseEntityProperties(meta));
|
|
142
|
+
filtered.forEach(meta => {
|
|
143
|
+
const newMeta = EntitySchema.fromMetadata(meta).init().meta;
|
|
144
|
+
return this.#metadata.set(newMeta.class, newMeta);
|
|
145
|
+
});
|
|
146
|
+
filtered.forEach(meta => this.initAutoincrement(meta));
|
|
147
|
+
const forEachProp = (cb) => {
|
|
148
|
+
filtered.forEach(meta => Object.values(meta.properties).forEach(prop => cb(meta, prop)));
|
|
149
|
+
};
|
|
150
|
+
forEachProp((m, p) => this.initFactoryField(m, p));
|
|
151
|
+
forEachProp((m, p) => this.initPolymorphicRelation(m, p, filtered));
|
|
152
|
+
forEachProp((_m, p) => this.initRelation(p));
|
|
153
|
+
forEachProp((m, p) => this.initEmbeddables(m, p));
|
|
154
|
+
forEachProp((_m, p) => this.initFieldName(p));
|
|
155
|
+
filtered.forEach(meta => this.finalizeTPTInheritance(meta, filtered));
|
|
156
|
+
filtered.forEach(meta => this.computeTPTOwnProps(meta));
|
|
157
|
+
forEachProp((m, p) => this.initVersionProperty(m, p));
|
|
158
|
+
forEachProp((m, p) => this.initCustomType(m, p));
|
|
159
|
+
forEachProp((m, p) => this.initGeneratedColumn(m, p));
|
|
160
|
+
filtered.forEach(meta => this.initAutoincrement(meta)); // once again after we init custom types
|
|
161
|
+
filtered.forEach(meta => this.initCheckConstraints(meta));
|
|
162
|
+
forEachProp((_m, p) => {
|
|
163
|
+
this.initDefaultValue(p);
|
|
164
|
+
this.inferTypeFromDefault(p);
|
|
165
|
+
this.initRelation(p);
|
|
166
|
+
this.initColumnType(p);
|
|
427
167
|
});
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
prop.fieldNames = this.initManyToManyFieldName(prop, prop.name);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
initManyToOneFieldName(prop, name, tableName) {
|
|
447
|
-
const meta2 = prop.targetMeta;
|
|
448
|
-
const ret = [];
|
|
449
|
-
for (const primaryKey of meta2.primaryKeys) {
|
|
450
|
-
this.initFieldName(meta2.properties[primaryKey]);
|
|
451
|
-
for (const fieldName of meta2.properties[primaryKey].fieldNames) {
|
|
452
|
-
ret.push(this.#namingStrategy.joinKeyColumnName(name, fieldName, meta2.compositePK, tableName));
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
return ret;
|
|
456
|
-
}
|
|
457
|
-
initManyToManyFieldName(prop, name) {
|
|
458
|
-
const meta2 = prop.targetMeta;
|
|
459
|
-
return meta2.primaryKeys.map(() => this.#namingStrategy.propertyToColumnName(name));
|
|
460
|
-
}
|
|
461
|
-
initManyToManyFields(meta, prop) {
|
|
462
|
-
const meta2 = prop.targetMeta;
|
|
463
|
-
Utils.defaultValue(prop, 'fixedOrder', !!prop.fixedOrderColumn);
|
|
464
|
-
const pivotMeta = this.#metadata.find(prop.pivotEntity);
|
|
465
|
-
const props = Object.values(pivotMeta?.properties ?? {});
|
|
466
|
-
const pks = props.filter(p => p.primary);
|
|
467
|
-
const fks = props.filter(p => p.kind === ReferenceKind.MANY_TO_ONE);
|
|
468
|
-
if (pivotMeta) {
|
|
469
|
-
pivotMeta.pivotTable = true;
|
|
470
|
-
prop.pivotTable = pivotMeta.tableName;
|
|
471
|
-
if (pks.length === 1) {
|
|
472
|
-
prop.fixedOrder = true;
|
|
473
|
-
prop.fixedOrderColumn = pks[0].name;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
if (pivotMeta && (pks.length === 2 || fks.length >= 2)) {
|
|
477
|
-
const owner = prop.mappedBy ? meta2.properties[prop.mappedBy] : prop;
|
|
478
|
-
const [first, second] = this.ensureCorrectFKOrderInPivotEntity(pivotMeta, owner);
|
|
479
|
-
prop.joinColumns ??= first.fieldNames;
|
|
480
|
-
prop.inverseJoinColumns ??= second.fieldNames;
|
|
481
|
-
}
|
|
482
|
-
if (!prop.pivotTable && prop.owner && this.#platform.usesPivotTable()) {
|
|
483
|
-
prop.pivotTable = this.#namingStrategy.joinTableName(meta.className, meta2.tableName, prop.name, meta.tableName);
|
|
484
|
-
}
|
|
485
|
-
if (prop.mappedBy) {
|
|
486
|
-
const prop2 = meta2.properties[prop.mappedBy];
|
|
487
|
-
this.initManyToManyFields(meta2, prop2);
|
|
488
|
-
prop.pivotTable = prop2.pivotTable;
|
|
489
|
-
prop.pivotEntity = prop2.pivotEntity;
|
|
490
|
-
prop.fixedOrder = prop2.fixedOrder;
|
|
491
|
-
prop.fixedOrderColumn = prop2.fixedOrderColumn;
|
|
492
|
-
prop.joinColumns = prop2.inverseJoinColumns;
|
|
493
|
-
prop.inverseJoinColumns = prop2.joinColumns;
|
|
494
|
-
prop.polymorphic = prop2.polymorphic;
|
|
495
|
-
prop.discriminator = prop2.discriminator;
|
|
496
|
-
prop.discriminatorColumn = prop2.discriminatorColumn;
|
|
497
|
-
prop.discriminatorValue = prop2.discriminatorValue;
|
|
498
|
-
}
|
|
499
|
-
prop.referencedColumnNames ??= Utils.flatten(
|
|
500
|
-
meta.primaryKeys.map(primaryKey => meta.properties[primaryKey].fieldNames),
|
|
501
|
-
);
|
|
502
|
-
// For polymorphic M:N, use discriminator base name for FK column (e.g., taggable_id instead of post_id)
|
|
503
|
-
if (prop.polymorphic && prop.discriminator) {
|
|
504
|
-
prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName =>
|
|
505
|
-
this.#namingStrategy.joinKeyColumnName(
|
|
506
|
-
prop.discriminator,
|
|
507
|
-
referencedColumnName,
|
|
508
|
-
prop.referencedColumnNames.length > 1,
|
|
509
|
-
),
|
|
510
|
-
);
|
|
511
|
-
} else {
|
|
512
|
-
const ownerTableName = this.isExplicitTableName(meta.root) ? meta.root.tableName : undefined;
|
|
513
|
-
prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName =>
|
|
514
|
-
this.#namingStrategy.joinKeyColumnName(
|
|
515
|
-
meta.root.className,
|
|
516
|
-
referencedColumnName,
|
|
517
|
-
meta.compositePK,
|
|
518
|
-
ownerTableName,
|
|
519
|
-
),
|
|
520
|
-
);
|
|
521
|
-
}
|
|
522
|
-
const inverseTableName = this.isExplicitTableName(meta2.root) ? meta2.root.tableName : undefined;
|
|
523
|
-
prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className, inverseTableName);
|
|
524
|
-
}
|
|
525
|
-
isExplicitTableName(meta) {
|
|
526
|
-
return meta.tableName !== this.#namingStrategy.classToTableName(meta.className);
|
|
527
|
-
}
|
|
528
|
-
initManyToOneFields(prop) {
|
|
529
|
-
if (prop.polymorphic && prop.polymorphTargets) {
|
|
530
|
-
const fieldNames1 = prop.targetMeta.getPrimaryProps().flatMap(pk => pk.fieldNames);
|
|
531
|
-
const idColumns = fieldNames1.map(fieldName =>
|
|
532
|
-
this.#namingStrategy.joinKeyColumnName(prop.discriminator, fieldName, fieldNames1.length > 1),
|
|
533
|
-
);
|
|
534
|
-
prop.fieldNames ??= [prop.discriminatorColumn, ...idColumns];
|
|
535
|
-
prop.joinColumns ??= idColumns;
|
|
536
|
-
prop.referencedColumnNames ??= fieldNames1;
|
|
537
|
-
prop.referencedTableName ??= prop.targetMeta.tableName;
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
|
-
const meta2 = prop.targetMeta;
|
|
541
|
-
let fieldNames;
|
|
542
|
-
// If targetKey is specified, use that property's field names instead of PKs
|
|
543
|
-
if (prop.targetKey) {
|
|
544
|
-
const targetProp = meta2.properties[prop.targetKey];
|
|
545
|
-
fieldNames = targetProp.fieldNames;
|
|
546
|
-
} else {
|
|
547
|
-
fieldNames = Utils.flatten(meta2.primaryKeys.map(primaryKey => meta2.properties[primaryKey].fieldNames));
|
|
548
|
-
}
|
|
549
|
-
Utils.defaultValue(prop, 'referencedTableName', meta2.tableName);
|
|
550
|
-
if (!prop.joinColumns) {
|
|
551
|
-
prop.joinColumns = fieldNames.map(fieldName =>
|
|
552
|
-
this.#namingStrategy.joinKeyColumnName(prop.name, fieldName, fieldNames.length > 1),
|
|
553
|
-
);
|
|
554
|
-
}
|
|
555
|
-
if (!prop.referencedColumnNames) {
|
|
556
|
-
prop.referencedColumnNames = fieldNames;
|
|
557
|
-
}
|
|
558
|
-
// Relations to composite PK targets need cascade update by default,
|
|
559
|
-
// since composite PKs are more likely to have mutable components
|
|
560
|
-
if (meta2.compositePK) {
|
|
561
|
-
prop.updateRule ??= 'cascade';
|
|
562
|
-
}
|
|
563
|
-
// Nullable relations default to 'set null' on delete - when the referenced
|
|
564
|
-
// entity is deleted, set the FK to null rather than failing
|
|
565
|
-
if (prop.nullable) {
|
|
566
|
-
prop.deleteRule ??= 'set null';
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
initOneToManyFields(prop) {
|
|
570
|
-
const meta2 = prop.targetMeta;
|
|
571
|
-
if (!prop.joinColumns) {
|
|
572
|
-
prop.joinColumns = [this.#namingStrategy.joinColumnName(prop.name)];
|
|
573
|
-
}
|
|
574
|
-
if (!prop.referencedColumnNames) {
|
|
575
|
-
meta2.getPrimaryProps().forEach(pk => this.applyNamingStrategy(meta2, pk));
|
|
576
|
-
prop.referencedColumnNames = Utils.flatten(meta2.getPrimaryProps().map(pk => pk.fieldNames));
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
processEntity(meta) {
|
|
580
|
-
const pks = Object.values(meta.properties).filter(prop => prop.primary);
|
|
581
|
-
meta.primaryKeys = pks.map(prop => prop.name);
|
|
582
|
-
meta.compositePK = pks.length > 1;
|
|
583
|
-
// FK used as PK, we need to cascade - applies to both single FK-as-PK
|
|
584
|
-
// and composite PKs where all PKs are FKs (e.g., pivot-like entities)
|
|
585
|
-
const fkPks = pks.filter(pk => pk.kind !== ReferenceKind.SCALAR);
|
|
586
|
-
if (fkPks.length > 0 && fkPks.length === pks.length) {
|
|
587
|
-
for (const pk of fkPks) {
|
|
588
|
-
pk.deleteRule ??= 'cascade';
|
|
589
|
-
pk.updateRule ??= 'cascade';
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
meta.forceConstructor ??= this.shouldForceConstructorUsage(meta);
|
|
593
|
-
this.#validator.validateEntityDefinition(this.#metadata, meta.class, this.#config.get('discovery'));
|
|
594
|
-
for (const prop of Object.values(meta.properties)) {
|
|
595
|
-
this.initNullability(prop);
|
|
596
|
-
this.applyNamingStrategy(meta, prop);
|
|
597
|
-
this.initDefaultValue(prop);
|
|
598
|
-
this.inferTypeFromDefault(prop);
|
|
599
|
-
this.initVersionProperty(meta, prop);
|
|
600
|
-
this.initCustomType(meta, prop);
|
|
601
|
-
this.initColumnType(prop);
|
|
602
|
-
this.initRelation(prop);
|
|
603
|
-
}
|
|
604
|
-
this.initOwnColumns(meta);
|
|
605
|
-
meta.simplePK =
|
|
606
|
-
pks.length === 1 && pks[0].kind === ReferenceKind.SCALAR && !pks[0].customType && pks[0].runtimeType !== 'Date';
|
|
607
|
-
meta.serializedPrimaryKey ??= meta.props.find(prop => prop.serializedPrimaryKey)?.name;
|
|
608
|
-
if (meta.serializedPrimaryKey && meta.serializedPrimaryKey !== meta.primaryKeys[0]) {
|
|
609
|
-
meta.properties[meta.serializedPrimaryKey].persist ??= false;
|
|
610
|
-
}
|
|
611
|
-
if (this.#platform.usesPivotTable()) {
|
|
612
|
-
return Object.values(meta.properties)
|
|
613
|
-
.filter(prop => prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && prop.pivotTable)
|
|
614
|
-
.map(prop => {
|
|
615
|
-
const pivotMeta = this.definePivotTableEntity(meta, prop);
|
|
616
|
-
prop.pivotEntity = pivotMeta.class;
|
|
617
|
-
if (prop.inversedBy) {
|
|
618
|
-
prop.targetMeta.properties[prop.inversedBy].pivotEntity = pivotMeta.class;
|
|
619
|
-
}
|
|
620
|
-
return pivotMeta;
|
|
168
|
+
forEachProp((m, p) => this.initIndexes(m, p));
|
|
169
|
+
filtered.forEach(meta => this.autoWireBidirectionalProperties(meta));
|
|
170
|
+
for (const meta of filtered) {
|
|
171
|
+
discovered.push(...this.processEntity(meta));
|
|
172
|
+
}
|
|
173
|
+
discovered.forEach(meta => meta.sync(true));
|
|
174
|
+
this.#metadataProvider.combineCache();
|
|
175
|
+
return discovered.map(meta => {
|
|
176
|
+
meta = this.#metadata.get(meta.class);
|
|
177
|
+
meta.sync(true);
|
|
178
|
+
this.findReferencingProperties(meta, filtered);
|
|
179
|
+
if (meta.inheritanceType === 'tpt') {
|
|
180
|
+
this.computeTPTOwnProps(meta);
|
|
181
|
+
}
|
|
182
|
+
return meta;
|
|
621
183
|
});
|
|
622
184
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
185
|
+
async findEntities(preferTs) {
|
|
186
|
+
const { entities, entitiesTs, baseDir } = this.#config.getAll();
|
|
187
|
+
const targets = preferTs && entitiesTs.length > 0 ? entitiesTs : entities;
|
|
188
|
+
const processed = [];
|
|
189
|
+
const paths = [];
|
|
190
|
+
for (const entity of targets) {
|
|
191
|
+
if (typeof entity === 'string') {
|
|
192
|
+
paths.push(entity);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
processed.push(entity);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (paths.length > 0) {
|
|
199
|
+
const { discoverEntities } = await import('@mikro-orm/core/file-discovery');
|
|
200
|
+
processed.push(...(await discoverEntities(paths, { baseDir })));
|
|
201
|
+
}
|
|
202
|
+
return this.discoverReferences(processed);
|
|
203
|
+
}
|
|
204
|
+
discoverMissingTargets() {
|
|
205
|
+
const unwrap = (type) => type
|
|
206
|
+
.replace(/Array<(.*)>/, '$1') // unwrap array
|
|
207
|
+
.replace(/\[]$/, '') // remove array suffix
|
|
208
|
+
.replace(/\((.*)\)/, '$1'); // unwrap union types
|
|
209
|
+
const missing = [];
|
|
210
|
+
this.#discovered.forEach(meta => Object.values(meta.properties).forEach(prop => {
|
|
211
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.pivotEntity) {
|
|
212
|
+
const pivotEntity = prop.pivotEntity;
|
|
213
|
+
const target = typeof pivotEntity === 'function' && !pivotEntity.prototype
|
|
214
|
+
? pivotEntity()
|
|
215
|
+
: pivotEntity;
|
|
216
|
+
if (!this.#discovered.find(m => m.className === Utils.className(target))) {
|
|
217
|
+
missing.push(target);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (prop.kind !== ReferenceKind.SCALAR) {
|
|
221
|
+
const target = typeof prop.entity === 'function' && !prop.entity.prototype ? prop.entity() : prop.type;
|
|
222
|
+
if (!unwrap(prop.type)
|
|
223
|
+
.split(/ ?\| ?/)
|
|
224
|
+
.every(type => this.#discovered.find(m => m.className === type))) {
|
|
225
|
+
missing.push(...Utils.asArray(target));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}));
|
|
229
|
+
if (missing.length > 0) {
|
|
230
|
+
this.tryDiscoverTargets(missing);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
tryDiscoverTargets(targets) {
|
|
234
|
+
for (const target of targets) {
|
|
235
|
+
const schema = EntitySchema.is(target) ? target : undefined;
|
|
236
|
+
const isDiscoverable = typeof target === 'function' || schema;
|
|
237
|
+
if (isDiscoverable && target.name) {
|
|
238
|
+
// Get the actual class for EntitySchema, or use target directly for classes
|
|
239
|
+
const targetClass = schema ? schema.meta.class : target;
|
|
240
|
+
if (!this.#metadata.has(targetClass)) {
|
|
241
|
+
this.discoverReferences([target], false);
|
|
242
|
+
this.discoverMissingTargets();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
discoverReferences(refs, validate = true) {
|
|
248
|
+
const found = [];
|
|
249
|
+
for (const entity of refs) {
|
|
250
|
+
if (typeof entity === 'string') {
|
|
251
|
+
throw new Error('Folder based discovery requires the async `MikroORM.init()` method.');
|
|
252
|
+
}
|
|
253
|
+
const schema = this.getSchema(entity);
|
|
254
|
+
const meta = schema.init().meta;
|
|
255
|
+
this.#metadata.set(meta.class, meta);
|
|
256
|
+
found.push(schema);
|
|
257
|
+
}
|
|
258
|
+
// discover parents (base entities) automatically
|
|
259
|
+
for (const meta of this.#metadata) {
|
|
260
|
+
let parent = meta.extends;
|
|
261
|
+
if (EntitySchema.is(parent) && !this.#metadata.has(parent.init().meta.class)) {
|
|
262
|
+
this.discoverReferences([parent], false);
|
|
263
|
+
}
|
|
264
|
+
if (typeof parent === 'function' && parent.name && !this.#metadata.has(parent)) {
|
|
265
|
+
this.discoverReferences([parent], false);
|
|
266
|
+
}
|
|
267
|
+
/* v8 ignore next */
|
|
268
|
+
if (!meta.class) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
parent = Object.getPrototypeOf(meta.class);
|
|
272
|
+
// Skip if parent is the auto-generated base class for the same entity (from setClass usage)
|
|
273
|
+
if (parent.name !== '' &&
|
|
274
|
+
parent.name !== meta.className &&
|
|
275
|
+
!this.#metadata.has(parent) &&
|
|
276
|
+
parent !== BaseEntity) {
|
|
277
|
+
this.discoverReferences([parent], false);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
for (const schema of found) {
|
|
281
|
+
this.discoverEntity(schema);
|
|
282
|
+
}
|
|
283
|
+
this.discoverMissingTargets();
|
|
284
|
+
if (validate) {
|
|
285
|
+
this.#validator.validateDiscovered(this.#discovered, this.#config.get('discovery'));
|
|
286
|
+
}
|
|
287
|
+
return this.#discovered.filter(meta => found.find(m => m.name === meta.className));
|
|
288
|
+
}
|
|
289
|
+
reset(entityName) {
|
|
290
|
+
const exists = this.#discovered.findIndex(m => m.class === entityName || m.className === Utils.className(entityName));
|
|
291
|
+
if (exists !== -1) {
|
|
292
|
+
this.#metadata.reset(this.#discovered[exists].class);
|
|
293
|
+
this.#discovered.splice(exists, 1);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
getSchema(entity) {
|
|
297
|
+
if (EntitySchema.REGISTRY.has(entity)) {
|
|
298
|
+
entity = EntitySchema.REGISTRY.get(entity);
|
|
299
|
+
}
|
|
300
|
+
if (EntitySchema.is(entity)) {
|
|
301
|
+
const meta = Utils.copy(entity.meta, false);
|
|
302
|
+
return EntitySchema.fromMetadata(meta);
|
|
303
|
+
}
|
|
304
|
+
// After the EntitySchema check, entity must be an EntityClass
|
|
305
|
+
const cls = entity;
|
|
306
|
+
const path = cls[MetadataStorage.PATH_SYMBOL];
|
|
307
|
+
if (path) {
|
|
308
|
+
const meta = Utils.copy(MetadataStorage.getMetadata(cls.name, path), false);
|
|
309
|
+
meta.path = path;
|
|
310
|
+
this.#metadata.set(cls, meta);
|
|
311
|
+
}
|
|
312
|
+
const exists = this.#metadata.has(cls);
|
|
313
|
+
const meta = this.#metadata.get(cls, true);
|
|
314
|
+
meta.abstract ??= !(exists && meta.name);
|
|
315
|
+
const schema = EntitySchema.fromMetadata(meta);
|
|
316
|
+
schema.setClass(cls);
|
|
317
|
+
return schema;
|
|
318
|
+
}
|
|
319
|
+
getRootEntity(meta) {
|
|
320
|
+
const base = meta.extends && this.#metadata.find(meta.extends);
|
|
321
|
+
if (!base || base === meta) {
|
|
322
|
+
// make sure we do not fall into infinite loop
|
|
323
|
+
return meta;
|
|
324
|
+
}
|
|
325
|
+
const root = this.getRootEntity(base);
|
|
326
|
+
// For STI or TPT, use the root entity.
|
|
327
|
+
// Check both `inheritanceType` (set during discovery) and raw `inheritance` option (set before discovery).
|
|
328
|
+
if (root.discriminatorColumn || root.inheritanceType || root.inheritance === 'tpt') {
|
|
329
|
+
return root;
|
|
330
|
+
}
|
|
331
|
+
return meta;
|
|
332
|
+
}
|
|
333
|
+
discoverEntity(schema) {
|
|
334
|
+
const meta = schema.meta;
|
|
335
|
+
const path = meta.path;
|
|
336
|
+
this.#logger.log('discovery', `- processing entity ${colors.cyan(meta.className)}${colors.grey(path ? ` (${path})` : '')}`);
|
|
337
|
+
const root = this.getRootEntity(meta);
|
|
338
|
+
schema.meta.path = meta.path;
|
|
339
|
+
const cache = this.#metadataProvider.getCachedMetadata(meta, root);
|
|
340
|
+
if (cache) {
|
|
341
|
+
this.#logger.log('discovery', `- using cached metadata for entity ${colors.cyan(meta.className)}`);
|
|
342
|
+
this.#discovered.push(meta);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
// infer default value from property initializer early, as the metadata provider might use some defaults, e.g. string for reflect-metadata
|
|
346
|
+
for (const prop of meta.props) {
|
|
347
|
+
this.inferDefaultValue(meta, prop);
|
|
348
|
+
}
|
|
349
|
+
// if the definition is using EntitySchema we still want it to go through the metadata provider to validate no types are missing
|
|
350
|
+
this.#metadataProvider.loadEntityMetadata(meta);
|
|
351
|
+
if (!meta.tableName && meta.name) {
|
|
352
|
+
const entityName = root.discriminatorColumn ? root.name : meta.name;
|
|
353
|
+
meta.tableName = this.#namingStrategy.classToTableName(entityName);
|
|
354
|
+
}
|
|
355
|
+
this.#metadataProvider.saveToCache(meta);
|
|
356
|
+
meta.root = root;
|
|
357
|
+
this.#discovered.push(meta);
|
|
358
|
+
}
|
|
359
|
+
initNullability(prop) {
|
|
360
|
+
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
361
|
+
return Utils.defaultValue(prop, 'nullable', prop.optional || !prop.owner);
|
|
362
|
+
}
|
|
363
|
+
return Utils.defaultValue(prop, 'nullable', prop.optional);
|
|
364
|
+
}
|
|
365
|
+
applyNamingStrategy(meta, prop) {
|
|
366
|
+
if (!prop.fieldNames) {
|
|
367
|
+
this.initFieldName(prop);
|
|
368
|
+
}
|
|
369
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
370
|
+
this.initManyToManyFields(meta, prop);
|
|
371
|
+
}
|
|
372
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
373
|
+
this.initManyToOneFields(prop);
|
|
374
|
+
}
|
|
375
|
+
if (prop.kind === ReferenceKind.ONE_TO_MANY) {
|
|
376
|
+
this.initOneToManyFields(prop);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
initOwnColumns(meta) {
|
|
380
|
+
meta.sync();
|
|
381
|
+
for (const prop of meta.props) {
|
|
382
|
+
if (!prop.joinColumns ||
|
|
383
|
+
!prop.columnTypes ||
|
|
384
|
+
prop.ownColumns ||
|
|
385
|
+
![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
// For polymorphic relations, ownColumns should include all fieldNames
|
|
389
|
+
// (discriminator + ID columns) since they are all owned by this relation
|
|
390
|
+
if (prop.polymorphic) {
|
|
391
|
+
prop.ownColumns = prop.fieldNames;
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (prop.joinColumns.length > 1) {
|
|
395
|
+
prop.ownColumns = prop.joinColumns.filter(col => {
|
|
396
|
+
return !meta.props.find(p => p.name !== prop.name && (!p.fieldNames || p.fieldNames.includes(col)));
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
if (!prop.ownColumns || prop.ownColumns.length === 0) {
|
|
400
|
+
prop.ownColumns = prop.joinColumns;
|
|
401
|
+
}
|
|
402
|
+
if (prop.joinColumns.length !== prop.columnTypes.length) {
|
|
403
|
+
prop.columnTypes = prop.joinColumns.flatMap(field => {
|
|
404
|
+
const matched = meta.props.find(p => p.fieldNames?.includes(field));
|
|
405
|
+
/* v8 ignore next */
|
|
406
|
+
if (!matched) {
|
|
407
|
+
throw MetadataError.fromWrongForeignKey(meta, prop, 'columnTypes');
|
|
408
|
+
}
|
|
409
|
+
return matched.columnTypes;
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
if (prop.joinColumns.length !== prop.referencedColumnNames.length) {
|
|
413
|
+
throw MetadataError.fromWrongForeignKey(meta, prop, 'referencedColumnNames');
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
initFieldName(prop, object = false) {
|
|
418
|
+
if (prop.fieldNames && prop.fieldNames.length > 0) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (prop.kind === ReferenceKind.SCALAR || prop.kind === ReferenceKind.EMBEDDED) {
|
|
422
|
+
prop.fieldNames = [this.#namingStrategy.propertyToColumnName(prop.name, object)];
|
|
423
|
+
}
|
|
424
|
+
else if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && !prop.polymorphic) {
|
|
425
|
+
prop.fieldNames = this.initManyToOneFieldName(prop, prop.name);
|
|
426
|
+
}
|
|
427
|
+
else if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner) {
|
|
428
|
+
prop.fieldNames = this.initManyToManyFieldName(prop, prop.name);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
initManyToOneFieldName(prop, name, tableName) {
|
|
432
|
+
const meta2 = prop.targetMeta;
|
|
433
|
+
const ret = [];
|
|
434
|
+
for (const primaryKey of meta2.primaryKeys) {
|
|
435
|
+
this.initFieldName(meta2.properties[primaryKey]);
|
|
436
|
+
for (const fieldName of meta2.properties[primaryKey].fieldNames) {
|
|
437
|
+
ret.push(this.#namingStrategy.joinKeyColumnName(name, fieldName, meta2.compositePK, tableName));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return ret;
|
|
441
|
+
}
|
|
442
|
+
initManyToManyFieldName(prop, name) {
|
|
443
|
+
const meta2 = prop.targetMeta;
|
|
444
|
+
return meta2.primaryKeys.map(() => this.#namingStrategy.propertyToColumnName(name));
|
|
445
|
+
}
|
|
446
|
+
initManyToManyFields(meta, prop) {
|
|
447
|
+
const meta2 = prop.targetMeta;
|
|
448
|
+
Utils.defaultValue(prop, 'fixedOrder', !!prop.fixedOrderColumn);
|
|
449
|
+
const pivotMeta = this.#metadata.find(prop.pivotEntity);
|
|
450
|
+
const props = Object.values(pivotMeta?.properties ?? {});
|
|
451
|
+
const pks = props.filter(p => p.primary);
|
|
452
|
+
const fks = props.filter(p => p.kind === ReferenceKind.MANY_TO_ONE);
|
|
453
|
+
if (pivotMeta) {
|
|
454
|
+
pivotMeta.pivotTable = true;
|
|
455
|
+
prop.pivotTable = pivotMeta.tableName;
|
|
456
|
+
if (pks.length === 1) {
|
|
457
|
+
prop.fixedOrder = true;
|
|
458
|
+
prop.fixedOrderColumn = pks[0].name;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (pivotMeta && (pks.length === 2 || fks.length >= 2)) {
|
|
462
|
+
const owner = prop.mappedBy ? meta2.properties[prop.mappedBy] : prop;
|
|
463
|
+
const [first, second] = this.ensureCorrectFKOrderInPivotEntity(pivotMeta, owner);
|
|
464
|
+
prop.joinColumns ??= first.fieldNames;
|
|
465
|
+
prop.inverseJoinColumns ??= second.fieldNames;
|
|
466
|
+
}
|
|
467
|
+
if (!prop.pivotTable && prop.owner && this.#platform.usesPivotTable()) {
|
|
468
|
+
prop.pivotTable = this.#namingStrategy.joinTableName(meta.className, meta2.tableName, prop.name, meta.tableName);
|
|
469
|
+
}
|
|
470
|
+
if (prop.mappedBy) {
|
|
471
|
+
const prop2 = meta2.properties[prop.mappedBy];
|
|
472
|
+
this.initManyToManyFields(meta2, prop2);
|
|
473
|
+
prop.pivotTable = prop2.pivotTable;
|
|
474
|
+
prop.pivotEntity = prop2.pivotEntity;
|
|
475
|
+
prop.fixedOrder = prop2.fixedOrder;
|
|
476
|
+
prop.fixedOrderColumn = prop2.fixedOrderColumn;
|
|
477
|
+
prop.joinColumns = prop2.inverseJoinColumns;
|
|
478
|
+
prop.inverseJoinColumns = prop2.joinColumns;
|
|
479
|
+
prop.polymorphic = prop2.polymorphic;
|
|
480
|
+
prop.discriminator = prop2.discriminator;
|
|
481
|
+
prop.discriminatorColumn = prop2.discriminatorColumn;
|
|
482
|
+
prop.discriminatorValue = prop2.discriminatorValue;
|
|
483
|
+
}
|
|
484
|
+
prop.referencedColumnNames ??= Utils.flatten(meta.primaryKeys.map(primaryKey => meta.properties[primaryKey].fieldNames));
|
|
485
|
+
// For polymorphic M:N, use discriminator base name for FK column (e.g., taggable_id instead of post_id)
|
|
486
|
+
if (prop.polymorphic && prop.discriminator) {
|
|
487
|
+
prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.#namingStrategy.joinKeyColumnName(prop.discriminator, referencedColumnName, prop.referencedColumnNames.length > 1));
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
const ownerTableName = this.isExplicitTableName(meta.root) ? meta.root.tableName : undefined;
|
|
491
|
+
prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.#namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK, ownerTableName));
|
|
492
|
+
}
|
|
493
|
+
const inverseTableName = this.isExplicitTableName(meta2.root) ? meta2.root.tableName : undefined;
|
|
494
|
+
prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className, inverseTableName);
|
|
495
|
+
}
|
|
496
|
+
isExplicitTableName(meta) {
|
|
497
|
+
return meta.tableName !== this.#namingStrategy.classToTableName(meta.className);
|
|
498
|
+
}
|
|
499
|
+
initManyToOneFields(prop) {
|
|
500
|
+
if (prop.polymorphic && prop.polymorphTargets) {
|
|
501
|
+
const fieldNames1 = prop.targetMeta.getPrimaryProps().flatMap(pk => pk.fieldNames);
|
|
502
|
+
const idColumns = fieldNames1.map(fieldName => this.#namingStrategy.joinKeyColumnName(prop.discriminator, fieldName, fieldNames1.length > 1));
|
|
503
|
+
prop.fieldNames ??= [prop.discriminatorColumn, ...idColumns];
|
|
504
|
+
prop.joinColumns ??= idColumns;
|
|
505
|
+
prop.referencedColumnNames ??= fieldNames1;
|
|
506
|
+
prop.referencedTableName ??= prop.targetMeta.tableName;
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
const meta2 = prop.targetMeta;
|
|
510
|
+
let fieldNames;
|
|
511
|
+
// If targetKey is specified, use that property's field names instead of PKs
|
|
512
|
+
if (prop.targetKey) {
|
|
513
|
+
const targetProp = meta2.properties[prop.targetKey];
|
|
514
|
+
fieldNames = targetProp.fieldNames;
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
fieldNames = Utils.flatten(meta2.primaryKeys.map(primaryKey => meta2.properties[primaryKey].fieldNames));
|
|
518
|
+
}
|
|
519
|
+
Utils.defaultValue(prop, 'referencedTableName', meta2.tableName);
|
|
520
|
+
if (!prop.joinColumns) {
|
|
521
|
+
prop.joinColumns = fieldNames.map(fieldName => this.#namingStrategy.joinKeyColumnName(prop.name, fieldName, fieldNames.length > 1));
|
|
522
|
+
}
|
|
523
|
+
if (!prop.referencedColumnNames) {
|
|
524
|
+
prop.referencedColumnNames = fieldNames;
|
|
525
|
+
}
|
|
526
|
+
// Relations to composite PK targets need cascade update by default,
|
|
527
|
+
// since composite PKs are more likely to have mutable components
|
|
528
|
+
if (meta2.compositePK) {
|
|
529
|
+
prop.updateRule ??= 'cascade';
|
|
530
|
+
}
|
|
531
|
+
// Nullable relations default to 'set null' on delete - when the referenced
|
|
532
|
+
// entity is deleted, set the FK to null rather than failing
|
|
533
|
+
if (prop.nullable) {
|
|
534
|
+
prop.deleteRule ??= 'set null';
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
initOneToManyFields(prop) {
|
|
538
|
+
const meta2 = prop.targetMeta;
|
|
539
|
+
if (!prop.joinColumns) {
|
|
540
|
+
prop.joinColumns = [this.#namingStrategy.joinColumnName(prop.name)];
|
|
541
|
+
}
|
|
542
|
+
if (!prop.referencedColumnNames) {
|
|
543
|
+
meta2.getPrimaryProps().forEach(pk => this.applyNamingStrategy(meta2, pk));
|
|
544
|
+
prop.referencedColumnNames = Utils.flatten(meta2.getPrimaryProps().map(pk => pk.fieldNames));
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
processEntity(meta) {
|
|
548
|
+
const pks = Object.values(meta.properties).filter(prop => prop.primary);
|
|
549
|
+
meta.primaryKeys = pks.map(prop => prop.name);
|
|
550
|
+
meta.compositePK = pks.length > 1;
|
|
551
|
+
// FK used as PK, we need to cascade - applies to both single FK-as-PK
|
|
552
|
+
// and composite PKs where all PKs are FKs (e.g., pivot-like entities)
|
|
553
|
+
const fkPks = pks.filter(pk => pk.kind !== ReferenceKind.SCALAR);
|
|
554
|
+
if (fkPks.length > 0 && fkPks.length === pks.length) {
|
|
555
|
+
for (const pk of fkPks) {
|
|
556
|
+
pk.deleteRule ??= 'cascade';
|
|
557
|
+
pk.updateRule ??= 'cascade';
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
meta.forceConstructor ??= this.shouldForceConstructorUsage(meta);
|
|
561
|
+
this.#validator.validateEntityDefinition(this.#metadata, meta.class, this.#config.get('discovery'));
|
|
562
|
+
for (const prop of Object.values(meta.properties)) {
|
|
563
|
+
this.initNullability(prop);
|
|
564
|
+
this.applyNamingStrategy(meta, prop);
|
|
565
|
+
this.initDefaultValue(prop);
|
|
566
|
+
this.inferTypeFromDefault(prop);
|
|
567
|
+
this.initVersionProperty(meta, prop);
|
|
568
|
+
this.initCustomType(meta, prop);
|
|
569
|
+
this.initColumnType(prop);
|
|
570
|
+
this.initRelation(prop);
|
|
571
|
+
}
|
|
572
|
+
this.initOwnColumns(meta);
|
|
573
|
+
meta.simplePK =
|
|
574
|
+
pks.length === 1 && pks[0].kind === ReferenceKind.SCALAR && !pks[0].customType && pks[0].runtimeType !== 'Date';
|
|
575
|
+
meta.serializedPrimaryKey ??= meta.props.find(prop => prop.serializedPrimaryKey)?.name;
|
|
576
|
+
if (meta.serializedPrimaryKey && meta.serializedPrimaryKey !== meta.primaryKeys[0]) {
|
|
577
|
+
meta.properties[meta.serializedPrimaryKey].persist ??= false;
|
|
578
|
+
}
|
|
579
|
+
if (this.#platform.usesPivotTable()) {
|
|
580
|
+
return Object.values(meta.properties)
|
|
581
|
+
.filter(prop => prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && prop.pivotTable)
|
|
582
|
+
.map(prop => {
|
|
583
|
+
const pivotMeta = this.definePivotTableEntity(meta, prop);
|
|
584
|
+
prop.pivotEntity = pivotMeta.class;
|
|
585
|
+
if (prop.inversedBy) {
|
|
586
|
+
prop.targetMeta.properties[prop.inversedBy].pivotEntity = pivotMeta.class;
|
|
587
|
+
}
|
|
588
|
+
return pivotMeta;
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
return [];
|
|
592
|
+
}
|
|
593
|
+
findReferencingProperties(meta, metadata) {
|
|
594
|
+
for (const meta2 of metadata) {
|
|
595
|
+
for (const prop2 of meta2.relations) {
|
|
596
|
+
if (prop2.kind !== ReferenceKind.SCALAR && prop2.type === meta.className) {
|
|
597
|
+
meta.referencingProperties.push({ meta: meta2, prop: prop2 });
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
initFactoryField(meta, prop) {
|
|
603
|
+
['mappedBy', 'inversedBy', 'pivotEntity'].forEach(type => {
|
|
604
|
+
const value = prop[type];
|
|
605
|
+
if (value instanceof Function) {
|
|
606
|
+
const meta2 = prop.targetMeta ?? this.#metadata.get(prop.target);
|
|
607
|
+
prop[type] = value(meta2.properties)?.name;
|
|
608
|
+
if (type === 'pivotEntity' && value) {
|
|
609
|
+
prop[type] = value(meta2.properties);
|
|
610
|
+
}
|
|
611
|
+
if (prop[type] == null) {
|
|
612
|
+
throw MetadataError.fromWrongReference(meta, prop, type);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
ensureCorrectFKOrderInPivotEntity(meta, owner) {
|
|
618
|
+
const pks = Object.values(meta.properties).filter(p => p.primary);
|
|
619
|
+
const fks = Object.values(meta.properties).filter(p => p.kind === ReferenceKind.MANY_TO_ONE);
|
|
620
|
+
let first, second;
|
|
621
|
+
if (pks.length === 2) {
|
|
622
|
+
[first, second] = pks;
|
|
623
|
+
}
|
|
624
|
+
else if (fks.length >= 2) {
|
|
625
|
+
[first, second] = fks;
|
|
626
|
+
}
|
|
627
|
+
else {
|
|
628
|
+
/* v8 ignore next */
|
|
629
|
+
return [];
|
|
630
|
+
}
|
|
631
|
+
// wrong FK order, first FK needs to point to the owning side
|
|
632
|
+
// (note that we can detect this only if the FKs target different types)
|
|
633
|
+
if (owner.type === first.type && first.type !== second.type) {
|
|
634
|
+
delete meta.properties[first.name];
|
|
635
|
+
meta.removeProperty(first.name, false);
|
|
636
|
+
meta.addProperty(first);
|
|
637
|
+
[first, second] = [second, first];
|
|
638
|
+
}
|
|
639
|
+
return [first, second];
|
|
640
|
+
}
|
|
641
|
+
definePivotTableEntity(meta, prop) {
|
|
642
|
+
const pivotMeta = prop.pivotEntity
|
|
643
|
+
? this.#metadata.find(prop.pivotEntity)
|
|
644
|
+
: this.#metadata.getByClassName(prop.pivotTable, false);
|
|
645
|
+
// ensure inverse side exists so we can join it when populating via pivot tables
|
|
646
|
+
if (!prop.inversedBy && prop.targetMeta) {
|
|
647
|
+
const inverseName = `${meta.className}_${prop.name}__inverse`;
|
|
648
|
+
prop.inversedBy = inverseName;
|
|
649
|
+
const inverseProp = {
|
|
650
|
+
name: inverseName,
|
|
651
|
+
kind: ReferenceKind.MANY_TO_MANY,
|
|
652
|
+
type: meta.className,
|
|
653
|
+
target: meta.class,
|
|
654
|
+
targetMeta: meta,
|
|
655
|
+
mappedBy: prop.name,
|
|
656
|
+
pivotEntity: prop.pivotEntity,
|
|
657
|
+
pivotTable: prop.pivotTable,
|
|
658
|
+
persist: false,
|
|
659
|
+
hydrate: false,
|
|
660
|
+
};
|
|
661
|
+
this.applyNamingStrategy(prop.targetMeta, inverseProp);
|
|
662
|
+
this.initCustomType(prop.targetMeta, inverseProp);
|
|
663
|
+
prop.targetMeta.properties[inverseName] = inverseProp;
|
|
664
|
+
}
|
|
665
|
+
if (pivotMeta) {
|
|
666
|
+
prop.pivotEntity = pivotMeta.class;
|
|
667
|
+
this.ensureCorrectFKOrderInPivotEntity(pivotMeta, prop);
|
|
668
|
+
if (prop.polymorphic && prop.discriminatorValue) {
|
|
669
|
+
pivotMeta.polymorphicDiscriminatorMap ??= {};
|
|
670
|
+
pivotMeta.polymorphicDiscriminatorMap[prop.discriminatorValue] = meta.class;
|
|
671
|
+
// For composite PK entities sharing a polymorphic pivot table,
|
|
672
|
+
// we need to add columns for each entity type's PKs
|
|
673
|
+
this.addPolymorphicPivotColumns(pivotMeta, meta, prop);
|
|
674
|
+
// Add virtual M:1 relation for this polymorphic owner (for join loading)
|
|
675
|
+
const ownerRelationName = `${prop.discriminator}_${meta.tableName}`;
|
|
676
|
+
if (!pivotMeta.properties[ownerRelationName]) {
|
|
677
|
+
pivotMeta.properties[ownerRelationName] = this.definePolymorphicOwnerRelation(prop, ownerRelationName, meta);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return pivotMeta;
|
|
681
|
+
}
|
|
682
|
+
let tableName = prop.pivotTable;
|
|
683
|
+
let schemaName;
|
|
684
|
+
if (prop.pivotTable.includes('.')) {
|
|
685
|
+
[schemaName, tableName] = prop.pivotTable.split('.');
|
|
686
|
+
}
|
|
687
|
+
schemaName ??= meta.schema;
|
|
688
|
+
const targetMeta = prop.targetMeta;
|
|
689
|
+
const targetType = targetMeta.className;
|
|
690
|
+
const pivotMeta2 = new EntityMetadata({
|
|
691
|
+
name: prop.pivotTable,
|
|
692
|
+
className: prop.pivotTable,
|
|
693
|
+
collection: tableName,
|
|
694
|
+
schema: schemaName,
|
|
695
|
+
pivotTable: true,
|
|
696
|
+
});
|
|
697
|
+
prop.pivotEntity = pivotMeta2.class;
|
|
698
|
+
if (prop.fixedOrder) {
|
|
699
|
+
const primaryProp = this.defineFixedOrderProperty(prop, targetMeta);
|
|
700
|
+
pivotMeta2.properties[primaryProp.name] = primaryProp;
|
|
701
|
+
}
|
|
702
|
+
else {
|
|
703
|
+
pivotMeta2.compositePK = true;
|
|
704
|
+
}
|
|
705
|
+
// handle self-referenced m:n with same default field names
|
|
706
|
+
if (meta.className === targetType &&
|
|
707
|
+
prop.joinColumns.every((joinColumn, idx) => joinColumn === prop.inverseJoinColumns[idx])) {
|
|
708
|
+
// use tableName only when explicitly provided by user, otherwise use className for backwards compatibility
|
|
709
|
+
const baseName = this.isExplicitTableName(meta) ? meta.tableName : meta.className;
|
|
710
|
+
prop.joinColumns = prop.referencedColumnNames.map(name => this.#namingStrategy.joinKeyColumnName(baseName + '_1', name, meta.compositePK));
|
|
711
|
+
prop.inverseJoinColumns = prop.referencedColumnNames.map(name => this.#namingStrategy.joinKeyColumnName(baseName + '_2', name, meta.compositePK));
|
|
712
|
+
if (prop.inversedBy) {
|
|
713
|
+
const prop2 = targetMeta.properties[prop.inversedBy];
|
|
714
|
+
prop2.inverseJoinColumns = prop.joinColumns;
|
|
715
|
+
prop2.joinColumns = prop.inverseJoinColumns;
|
|
716
|
+
}
|
|
717
|
+
// propagate updated joinColumns to all child entities that inherit this property (STI)
|
|
718
|
+
for (const childMeta of this.#discovered.filter(m => m.root === meta && m !== meta)) {
|
|
719
|
+
const childProp = childMeta.properties[prop.name];
|
|
720
|
+
if (childProp) {
|
|
721
|
+
childProp.joinColumns = prop.joinColumns;
|
|
722
|
+
childProp.inverseJoinColumns = prop.inverseJoinColumns;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
// For polymorphic M:N, create discriminator column and polymorphic FK
|
|
727
|
+
if (prop.polymorphic && prop.discriminatorColumn) {
|
|
728
|
+
this.definePolymorphicPivotProperties(pivotMeta2, meta, prop, targetMeta);
|
|
729
|
+
}
|
|
730
|
+
else {
|
|
731
|
+
pivotMeta2.properties[meta.name + '_owner'] = this.definePivotProperty(prop, meta.name + '_owner', meta.class, targetType + '_inverse', true, meta.className === targetType);
|
|
732
|
+
pivotMeta2.properties[targetType + '_inverse'] = this.definePivotProperty(prop, targetType + '_inverse', targetMeta.class, meta.name + '_owner', false, meta.className === targetType);
|
|
733
|
+
}
|
|
734
|
+
return this.#metadata.set(pivotMeta2.class, EntitySchema.fromMetadata(pivotMeta2).init().meta);
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Create a scalar property for a pivot table column.
|
|
738
|
+
*/
|
|
739
|
+
createPivotScalarProperty(name, columnTypes, fieldNames = [name], options = {}) {
|
|
740
|
+
return {
|
|
741
|
+
name,
|
|
742
|
+
fieldNames,
|
|
743
|
+
columnTypes,
|
|
744
|
+
type: options.type ?? 'number',
|
|
745
|
+
kind: ReferenceKind.SCALAR,
|
|
746
|
+
primary: options.primary ?? false,
|
|
747
|
+
nullable: options.nullable ?? true,
|
|
748
|
+
...(options.persist !== undefined && { persist: options.persist }),
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Get column types for an entity's primary keys, initializing them if needed.
|
|
753
|
+
*/
|
|
754
|
+
getPrimaryKeyColumnTypes(meta) {
|
|
755
|
+
const columnTypes = [];
|
|
756
|
+
for (const pk of meta.primaryKeys) {
|
|
757
|
+
const pkProp = meta.properties[pk];
|
|
758
|
+
this.initCustomType(meta, pkProp);
|
|
759
|
+
this.initColumnType(pkProp);
|
|
760
|
+
columnTypes.push(...pkProp.columnTypes);
|
|
761
|
+
}
|
|
762
|
+
return columnTypes;
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Add missing FK columns for a polymorphic entity to an existing pivot table.
|
|
766
|
+
*/
|
|
767
|
+
addPolymorphicPivotColumns(pivotMeta, meta, prop) {
|
|
768
|
+
const existingFieldNames = new Set(Object.values(pivotMeta.properties).flatMap(p => p.fieldNames ?? []));
|
|
769
|
+
const columnTypes = this.getPrimaryKeyColumnTypes(meta);
|
|
770
|
+
for (let i = 0; i < prop.joinColumns.length; i++) {
|
|
771
|
+
const joinColumn = prop.joinColumns[i];
|
|
772
|
+
if (!existingFieldNames.has(joinColumn)) {
|
|
773
|
+
pivotMeta.properties[joinColumn] = this.createPivotScalarProperty(joinColumn, [columnTypes[i]]);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Define properties for a polymorphic pivot table.
|
|
779
|
+
*/
|
|
780
|
+
definePolymorphicPivotProperties(pivotMeta, meta, prop, targetMeta) {
|
|
781
|
+
const discriminatorColumn = prop.discriminatorColumn;
|
|
782
|
+
const isCompositePK = meta.compositePK;
|
|
783
|
+
// For composite PK polymorphic M:N, we need fixedOrder (auto-increment PK)
|
|
784
|
+
if (isCompositePK && !prop.fixedOrder) {
|
|
785
|
+
prop.fixedOrder = true;
|
|
786
|
+
const primaryProp = this.defineFixedOrderProperty(prop, targetMeta);
|
|
787
|
+
pivotMeta.properties[primaryProp.name] = primaryProp;
|
|
788
|
+
pivotMeta.compositePK = false;
|
|
789
|
+
}
|
|
790
|
+
const discriminatorProp = this.createPivotScalarProperty(discriminatorColumn, [this.#platform.getVarcharTypeDeclarationSQL(prop)], [discriminatorColumn], { type: 'string', primary: !isCompositePK, nullable: false });
|
|
791
|
+
this.initFieldName(discriminatorProp);
|
|
792
|
+
pivotMeta.properties[discriminatorColumn] = discriminatorProp;
|
|
793
|
+
const columnTypes = this.getPrimaryKeyColumnTypes(meta);
|
|
794
|
+
if (isCompositePK) {
|
|
795
|
+
// Create separate properties for each PK column (nullable for other entity types)
|
|
796
|
+
for (let i = 0; i < prop.joinColumns.length; i++) {
|
|
797
|
+
pivotMeta.properties[prop.joinColumns[i]] = this.createPivotScalarProperty(prop.joinColumns[i], [
|
|
798
|
+
columnTypes[i],
|
|
799
|
+
]);
|
|
800
|
+
}
|
|
801
|
+
// Virtual property combining all columns (for compatibility)
|
|
802
|
+
pivotMeta.properties[prop.discriminator] = this.createPivotScalarProperty(prop.discriminator, columnTypes, [...prop.joinColumns], { type: meta.className, persist: false });
|
|
803
|
+
}
|
|
804
|
+
else {
|
|
805
|
+
pivotMeta.properties[prop.discriminator] = this.createPivotScalarProperty(prop.discriminator, columnTypes, [...prop.joinColumns], { type: meta.className, primary: true, nullable: false });
|
|
806
|
+
}
|
|
807
|
+
pivotMeta.properties[targetMeta.className + '_inverse'] = this.definePivotProperty(prop, targetMeta.className + '_inverse', targetMeta.class, prop.discriminator, false, false);
|
|
808
|
+
// Create virtual M:1 relation to the polymorphic owner for single-query join loading
|
|
809
|
+
const ownerRelationName = `${prop.discriminator}_${meta.tableName}`;
|
|
810
|
+
pivotMeta.properties[ownerRelationName] = this.definePolymorphicOwnerRelation(prop, ownerRelationName, meta);
|
|
699
811
|
pivotMeta.polymorphicDiscriminatorMap ??= {};
|
|
700
812
|
pivotMeta.polymorphicDiscriminatorMap[prop.discriminatorValue] = meta.class;
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
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
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
meta
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
[this.#platform.getVarcharTypeDeclarationSQL(prop)],
|
|
841
|
-
[discriminatorColumn],
|
|
842
|
-
{ type: 'string', primary: !isCompositePK, nullable: false },
|
|
843
|
-
);
|
|
844
|
-
this.initFieldName(discriminatorProp);
|
|
845
|
-
pivotMeta.properties[discriminatorColumn] = discriminatorProp;
|
|
846
|
-
const columnTypes = this.getPrimaryKeyColumnTypes(meta);
|
|
847
|
-
if (isCompositePK) {
|
|
848
|
-
// Create separate properties for each PK column (nullable for other entity types)
|
|
849
|
-
for (let i = 0; i < prop.joinColumns.length; i++) {
|
|
850
|
-
pivotMeta.properties[prop.joinColumns[i]] = this.createPivotScalarProperty(prop.joinColumns[i], [
|
|
851
|
-
columnTypes[i],
|
|
852
|
-
]);
|
|
853
|
-
}
|
|
854
|
-
// Virtual property combining all columns (for compatibility)
|
|
855
|
-
pivotMeta.properties[prop.discriminator] = this.createPivotScalarProperty(
|
|
856
|
-
prop.discriminator,
|
|
857
|
-
columnTypes,
|
|
858
|
-
[...prop.joinColumns],
|
|
859
|
-
{ type: meta.className, persist: false },
|
|
860
|
-
);
|
|
861
|
-
} else {
|
|
862
|
-
pivotMeta.properties[prop.discriminator] = this.createPivotScalarProperty(
|
|
863
|
-
prop.discriminator,
|
|
864
|
-
columnTypes,
|
|
865
|
-
[...prop.joinColumns],
|
|
866
|
-
{ type: meta.className, primary: true, nullable: false },
|
|
867
|
-
);
|
|
868
|
-
}
|
|
869
|
-
pivotMeta.properties[targetMeta.className + '_inverse'] = this.definePivotProperty(
|
|
870
|
-
prop,
|
|
871
|
-
targetMeta.className + '_inverse',
|
|
872
|
-
targetMeta.class,
|
|
873
|
-
prop.discriminator,
|
|
874
|
-
false,
|
|
875
|
-
false,
|
|
876
|
-
);
|
|
877
|
-
// Create virtual M:1 relation to the polymorphic owner for single-query join loading
|
|
878
|
-
const ownerRelationName = `${prop.discriminator}_${meta.tableName}`;
|
|
879
|
-
pivotMeta.properties[ownerRelationName] = this.definePolymorphicOwnerRelation(prop, ownerRelationName, meta);
|
|
880
|
-
pivotMeta.polymorphicDiscriminatorMap ??= {};
|
|
881
|
-
pivotMeta.polymorphicDiscriminatorMap[prop.discriminatorValue] = meta.class;
|
|
882
|
-
}
|
|
883
|
-
/**
|
|
884
|
-
* Create a virtual M:1 relation from pivot to a polymorphic owner entity.
|
|
885
|
-
* This enables single-query join loading for inverse-side polymorphic M:N.
|
|
886
|
-
*/
|
|
887
|
-
definePolymorphicOwnerRelation(prop, name, targetMeta) {
|
|
888
|
-
const ret = {
|
|
889
|
-
name,
|
|
890
|
-
type: targetMeta.className,
|
|
891
|
-
target: targetMeta.class,
|
|
892
|
-
kind: ReferenceKind.MANY_TO_ONE,
|
|
893
|
-
nullable: true,
|
|
894
|
-
owner: true,
|
|
895
|
-
primary: false,
|
|
896
|
-
createForeignKeyConstraint: false,
|
|
897
|
-
persist: false,
|
|
898
|
-
index: false,
|
|
899
|
-
};
|
|
900
|
-
ret.targetMeta = targetMeta;
|
|
901
|
-
ret.fieldNames = ret.joinColumns = ret.ownColumns = [...prop.joinColumns];
|
|
902
|
-
ret.referencedColumnNames = [];
|
|
903
|
-
ret.inverseJoinColumns = [];
|
|
904
|
-
for (const primaryKey of targetMeta.primaryKeys) {
|
|
905
|
-
const pkProp = targetMeta.properties[primaryKey];
|
|
906
|
-
ret.referencedColumnNames.push(...pkProp.fieldNames);
|
|
907
|
-
ret.inverseJoinColumns.push(...pkProp.fieldNames);
|
|
908
|
-
ret.length = pkProp.length;
|
|
909
|
-
ret.precision = pkProp.precision;
|
|
910
|
-
ret.scale = pkProp.scale;
|
|
911
|
-
}
|
|
912
|
-
const schema = targetMeta.schema ?? this.#config.get('schema') ?? this.#platform.getDefaultSchemaName();
|
|
913
|
-
ret.referencedTableName = schema && schema !== '*' ? schema + '.' + targetMeta.tableName : targetMeta.tableName;
|
|
914
|
-
this.initColumnType(ret);
|
|
915
|
-
this.initRelation(ret);
|
|
916
|
-
return ret;
|
|
917
|
-
}
|
|
918
|
-
defineFixedOrderProperty(prop, targetMeta) {
|
|
919
|
-
const pk = prop.fixedOrderColumn || this.#namingStrategy.referenceColumnName();
|
|
920
|
-
const primaryProp = {
|
|
921
|
-
name: pk,
|
|
922
|
-
type: 'number',
|
|
923
|
-
runtimeType: 'number',
|
|
924
|
-
kind: ReferenceKind.SCALAR,
|
|
925
|
-
primary: true,
|
|
926
|
-
autoincrement: true,
|
|
927
|
-
unsigned: this.#platform.supportsUnsigned(),
|
|
928
|
-
};
|
|
929
|
-
this.initFieldName(primaryProp);
|
|
930
|
-
this.initColumnType(primaryProp);
|
|
931
|
-
prop.fixedOrderColumn = pk;
|
|
932
|
-
if (prop.inversedBy) {
|
|
933
|
-
const prop2 = targetMeta.properties[prop.inversedBy];
|
|
934
|
-
prop2.fixedOrder = true;
|
|
935
|
-
prop2.fixedOrderColumn = pk;
|
|
936
|
-
}
|
|
937
|
-
return primaryProp;
|
|
938
|
-
}
|
|
939
|
-
definePivotProperty(prop, name, type, inverse, owner, selfReferencing) {
|
|
940
|
-
const ret = {
|
|
941
|
-
name,
|
|
942
|
-
type: Utils.className(type),
|
|
943
|
-
target: type,
|
|
944
|
-
kind: ReferenceKind.MANY_TO_ONE,
|
|
945
|
-
cascade: [Cascade.ALL],
|
|
946
|
-
fixedOrder: prop.fixedOrder,
|
|
947
|
-
fixedOrderColumn: prop.fixedOrderColumn,
|
|
948
|
-
index: this.#platform.indexForeignKeys(),
|
|
949
|
-
primary: !prop.fixedOrder,
|
|
950
|
-
autoincrement: false,
|
|
951
|
-
updateRule: prop.updateRule,
|
|
952
|
-
deleteRule: prop.deleteRule,
|
|
953
|
-
createForeignKeyConstraint: prop.createForeignKeyConstraint,
|
|
954
|
-
};
|
|
955
|
-
const defaultRule = selfReferencing && !this.#platform.supportsMultipleCascadePaths() ? 'no action' : 'cascade';
|
|
956
|
-
ret.updateRule ??= defaultRule;
|
|
957
|
-
ret.deleteRule ??= defaultRule;
|
|
958
|
-
const meta = this.#metadata.get(type);
|
|
959
|
-
ret.targetMeta = meta;
|
|
960
|
-
ret.joinColumns = [];
|
|
961
|
-
ret.inverseJoinColumns = [];
|
|
962
|
-
const schema = meta.schema ?? this.#config.get('schema') ?? this.#platform.getDefaultSchemaName();
|
|
963
|
-
ret.referencedTableName = schema && schema !== '*' ? schema + '.' + meta.tableName : meta.tableName;
|
|
964
|
-
if (owner) {
|
|
965
|
-
ret.owner = true;
|
|
966
|
-
ret.inversedBy = inverse;
|
|
967
|
-
ret.referencedColumnNames = prop.referencedColumnNames;
|
|
968
|
-
ret.fieldNames = ret.joinColumns = prop.joinColumns;
|
|
969
|
-
ret.inverseJoinColumns = prop.referencedColumnNames;
|
|
970
|
-
meta.primaryKeys.forEach(primaryKey => {
|
|
971
|
-
const prop2 = meta.properties[primaryKey];
|
|
972
|
-
ret.length = prop2.length;
|
|
973
|
-
ret.precision = prop2.precision;
|
|
974
|
-
ret.scale = prop2.scale;
|
|
975
|
-
});
|
|
976
|
-
} else {
|
|
977
|
-
ret.owner = false;
|
|
978
|
-
ret.mappedBy = inverse;
|
|
979
|
-
ret.fieldNames = ret.joinColumns = prop.inverseJoinColumns;
|
|
980
|
-
ret.referencedColumnNames = [];
|
|
981
|
-
ret.inverseJoinColumns = [];
|
|
982
|
-
meta.primaryKeys.forEach(primaryKey => {
|
|
983
|
-
const prop2 = meta.properties[primaryKey];
|
|
984
|
-
ret.referencedColumnNames.push(...prop2.fieldNames);
|
|
985
|
-
ret.inverseJoinColumns.push(...prop2.fieldNames);
|
|
986
|
-
ret.length = prop2.length;
|
|
987
|
-
ret.precision = prop2.precision;
|
|
988
|
-
ret.scale = prop2.scale;
|
|
989
|
-
});
|
|
990
|
-
}
|
|
991
|
-
this.initColumnType(ret);
|
|
992
|
-
this.initRelation(ret);
|
|
993
|
-
return ret;
|
|
994
|
-
}
|
|
995
|
-
autoWireBidirectionalProperties(meta) {
|
|
996
|
-
Object.values(meta.properties)
|
|
997
|
-
.filter(prop => prop.kind !== ReferenceKind.SCALAR && !prop.owner && prop.mappedBy)
|
|
998
|
-
.forEach(prop => {
|
|
999
|
-
const meta2 = prop.targetMeta;
|
|
1000
|
-
const prop2 = meta2.properties[prop.mappedBy];
|
|
1001
|
-
if (prop2 && !prop2.inversedBy) {
|
|
1002
|
-
prop2.inversedBy = prop.name;
|
|
1003
|
-
}
|
|
1004
|
-
});
|
|
1005
|
-
}
|
|
1006
|
-
defineBaseEntityProperties(meta) {
|
|
1007
|
-
const base = meta.extends && this.#metadata.get(meta.extends);
|
|
1008
|
-
if (!base || base === meta) {
|
|
1009
|
-
// make sure we do not fall into infinite loop
|
|
1010
|
-
return 0;
|
|
1011
|
-
}
|
|
1012
|
-
let order = this.defineBaseEntityProperties(base);
|
|
1013
|
-
const ownProps = Object.values(meta.properties);
|
|
1014
|
-
const old = ownProps.map(x => x.name);
|
|
1015
|
-
meta.properties = {};
|
|
1016
|
-
Object.values(base.properties).forEach(prop => {
|
|
1017
|
-
if (!prop.inherited) {
|
|
1018
|
-
meta.properties[prop.name] = prop;
|
|
1019
|
-
}
|
|
1020
|
-
});
|
|
1021
|
-
ownProps.forEach(prop => (meta.properties[prop.name] = prop));
|
|
1022
|
-
meta.filters = { ...base.filters, ...meta.filters };
|
|
1023
|
-
if (!meta.discriminatorValue) {
|
|
1024
|
-
Object.values(base.properties)
|
|
1025
|
-
.filter(prop => !old.includes(prop.name))
|
|
1026
|
-
.forEach(prop => {
|
|
1027
|
-
meta.properties[prop.name] = { ...prop };
|
|
1028
|
-
meta.propertyOrder.set(prop.name, (order += 0.01));
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Create a virtual M:1 relation from pivot to a polymorphic owner entity.
|
|
816
|
+
* This enables single-query join loading for inverse-side polymorphic M:N.
|
|
817
|
+
*/
|
|
818
|
+
definePolymorphicOwnerRelation(prop, name, targetMeta) {
|
|
819
|
+
const ret = {
|
|
820
|
+
name,
|
|
821
|
+
type: targetMeta.className,
|
|
822
|
+
target: targetMeta.class,
|
|
823
|
+
kind: ReferenceKind.MANY_TO_ONE,
|
|
824
|
+
nullable: true,
|
|
825
|
+
owner: true,
|
|
826
|
+
primary: false,
|
|
827
|
+
createForeignKeyConstraint: false,
|
|
828
|
+
persist: false,
|
|
829
|
+
index: false,
|
|
830
|
+
};
|
|
831
|
+
ret.targetMeta = targetMeta;
|
|
832
|
+
ret.fieldNames = ret.joinColumns = ret.ownColumns = [...prop.joinColumns];
|
|
833
|
+
ret.referencedColumnNames = [];
|
|
834
|
+
ret.inverseJoinColumns = [];
|
|
835
|
+
for (const primaryKey of targetMeta.primaryKeys) {
|
|
836
|
+
const pkProp = targetMeta.properties[primaryKey];
|
|
837
|
+
ret.referencedColumnNames.push(...pkProp.fieldNames);
|
|
838
|
+
ret.inverseJoinColumns.push(...pkProp.fieldNames);
|
|
839
|
+
ret.length = pkProp.length;
|
|
840
|
+
ret.precision = pkProp.precision;
|
|
841
|
+
ret.scale = pkProp.scale;
|
|
842
|
+
}
|
|
843
|
+
const schema = targetMeta.schema ?? this.#config.get('schema') ?? this.#platform.getDefaultSchemaName();
|
|
844
|
+
ret.referencedTableName = schema && schema !== '*' ? schema + '.' + targetMeta.tableName : targetMeta.tableName;
|
|
845
|
+
this.initColumnType(ret);
|
|
846
|
+
this.initRelation(ret);
|
|
847
|
+
return ret;
|
|
848
|
+
}
|
|
849
|
+
defineFixedOrderProperty(prop, targetMeta) {
|
|
850
|
+
const pk = prop.fixedOrderColumn || this.#namingStrategy.referenceColumnName();
|
|
851
|
+
const primaryProp = {
|
|
852
|
+
name: pk,
|
|
853
|
+
type: 'number',
|
|
854
|
+
runtimeType: 'number',
|
|
855
|
+
kind: ReferenceKind.SCALAR,
|
|
856
|
+
primary: true,
|
|
857
|
+
autoincrement: true,
|
|
858
|
+
unsigned: this.#platform.supportsUnsigned(),
|
|
859
|
+
};
|
|
860
|
+
this.initFieldName(primaryProp);
|
|
861
|
+
this.initColumnType(primaryProp);
|
|
862
|
+
prop.fixedOrderColumn = pk;
|
|
863
|
+
if (prop.inversedBy) {
|
|
864
|
+
const prop2 = targetMeta.properties[prop.inversedBy];
|
|
865
|
+
prop2.fixedOrder = true;
|
|
866
|
+
prop2.fixedOrderColumn = pk;
|
|
867
|
+
}
|
|
868
|
+
return primaryProp;
|
|
869
|
+
}
|
|
870
|
+
definePivotProperty(prop, name, type, inverse, owner, selfReferencing) {
|
|
871
|
+
const ret = {
|
|
872
|
+
name,
|
|
873
|
+
type: Utils.className(type),
|
|
874
|
+
target: type,
|
|
875
|
+
kind: ReferenceKind.MANY_TO_ONE,
|
|
876
|
+
cascade: [Cascade.ALL],
|
|
877
|
+
fixedOrder: prop.fixedOrder,
|
|
878
|
+
fixedOrderColumn: prop.fixedOrderColumn,
|
|
879
|
+
index: this.#platform.indexForeignKeys(),
|
|
880
|
+
primary: !prop.fixedOrder,
|
|
881
|
+
autoincrement: false,
|
|
882
|
+
updateRule: prop.updateRule,
|
|
883
|
+
deleteRule: prop.deleteRule,
|
|
884
|
+
createForeignKeyConstraint: prop.createForeignKeyConstraint,
|
|
885
|
+
};
|
|
886
|
+
const defaultRule = selfReferencing && !this.#platform.supportsMultipleCascadePaths() ? 'no action' : 'cascade';
|
|
887
|
+
ret.updateRule ??= defaultRule;
|
|
888
|
+
ret.deleteRule ??= defaultRule;
|
|
889
|
+
const meta = this.#metadata.get(type);
|
|
890
|
+
ret.targetMeta = meta;
|
|
891
|
+
ret.joinColumns = [];
|
|
892
|
+
ret.inverseJoinColumns = [];
|
|
893
|
+
const schema = meta.schema ?? this.#config.get('schema') ?? this.#platform.getDefaultSchemaName();
|
|
894
|
+
ret.referencedTableName = schema && schema !== '*' ? schema + '.' + meta.tableName : meta.tableName;
|
|
895
|
+
if (owner) {
|
|
896
|
+
ret.owner = true;
|
|
897
|
+
ret.inversedBy = inverse;
|
|
898
|
+
ret.referencedColumnNames = prop.referencedColumnNames;
|
|
899
|
+
ret.fieldNames = ret.joinColumns = prop.joinColumns;
|
|
900
|
+
ret.inverseJoinColumns = prop.referencedColumnNames;
|
|
901
|
+
meta.primaryKeys.forEach(primaryKey => {
|
|
902
|
+
const prop2 = meta.properties[primaryKey];
|
|
903
|
+
ret.length = prop2.length;
|
|
904
|
+
ret.precision = prop2.precision;
|
|
905
|
+
ret.scale = prop2.scale;
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
else {
|
|
909
|
+
ret.owner = false;
|
|
910
|
+
ret.mappedBy = inverse;
|
|
911
|
+
ret.fieldNames = ret.joinColumns = prop.inverseJoinColumns;
|
|
912
|
+
ret.referencedColumnNames = [];
|
|
913
|
+
ret.inverseJoinColumns = [];
|
|
914
|
+
meta.primaryKeys.forEach(primaryKey => {
|
|
915
|
+
const prop2 = meta.properties[primaryKey];
|
|
916
|
+
ret.referencedColumnNames.push(...prop2.fieldNames);
|
|
917
|
+
ret.inverseJoinColumns.push(...prop2.fieldNames);
|
|
918
|
+
ret.length = prop2.length;
|
|
919
|
+
ret.precision = prop2.precision;
|
|
920
|
+
ret.scale = prop2.scale;
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
this.initColumnType(ret);
|
|
924
|
+
this.initRelation(ret);
|
|
925
|
+
return ret;
|
|
926
|
+
}
|
|
927
|
+
autoWireBidirectionalProperties(meta) {
|
|
928
|
+
Object.values(meta.properties)
|
|
929
|
+
.filter(prop => prop.kind !== ReferenceKind.SCALAR && !prop.owner && prop.mappedBy)
|
|
930
|
+
.forEach(prop => {
|
|
931
|
+
const meta2 = prop.targetMeta;
|
|
932
|
+
const prop2 = meta2.properties[prop.mappedBy];
|
|
933
|
+
if (prop2 && !prop2.inversedBy) {
|
|
934
|
+
prop2.inversedBy = prop.name;
|
|
935
|
+
}
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
defineBaseEntityProperties(meta) {
|
|
939
|
+
const base = meta.extends && this.#metadata.get(meta.extends);
|
|
940
|
+
if (!base || base === meta) {
|
|
941
|
+
// make sure we do not fall into infinite loop
|
|
942
|
+
return 0;
|
|
943
|
+
}
|
|
944
|
+
let order = this.defineBaseEntityProperties(base);
|
|
945
|
+
const ownProps = Object.values(meta.properties);
|
|
946
|
+
const old = ownProps.map(x => x.name);
|
|
947
|
+
meta.properties = {};
|
|
948
|
+
Object.values(base.properties).forEach(prop => {
|
|
949
|
+
if (!prop.inherited) {
|
|
950
|
+
meta.properties[prop.name] = prop;
|
|
951
|
+
}
|
|
1029
952
|
});
|
|
953
|
+
ownProps.forEach(prop => (meta.properties[prop.name] = prop));
|
|
954
|
+
meta.filters = { ...base.filters, ...meta.filters };
|
|
955
|
+
if (!meta.discriminatorValue) {
|
|
956
|
+
Object.values(base.properties)
|
|
957
|
+
.filter(prop => !old.includes(prop.name))
|
|
958
|
+
.forEach(prop => {
|
|
959
|
+
meta.properties[prop.name] = { ...prop };
|
|
960
|
+
meta.propertyOrder.set(prop.name, (order += 0.01));
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
meta.indexes = Utils.unique([...base.indexes, ...meta.indexes]);
|
|
964
|
+
meta.uniques = Utils.unique([...base.uniques, ...meta.uniques]);
|
|
965
|
+
meta.checks = Utils.unique([...base.checks, ...meta.checks]);
|
|
966
|
+
const pks = Object.values(meta.properties)
|
|
967
|
+
.filter(p => p.primary)
|
|
968
|
+
.map(p => p.name);
|
|
969
|
+
if (pks.length > 0 && meta.primaryKeys.length === 0) {
|
|
970
|
+
meta.primaryKeys = pks;
|
|
971
|
+
}
|
|
972
|
+
Utils.keys(base.hooks).forEach(type => {
|
|
973
|
+
meta.hooks[type] = Utils.unique([...base.hooks[type], ...(meta.hooks[type] || [])]);
|
|
974
|
+
});
|
|
975
|
+
if ((meta.constructorParams?.length ?? 0) === 0 && (base.constructorParams?.length ?? 0) > 0) {
|
|
976
|
+
meta.constructorParams = [...base.constructorParams];
|
|
977
|
+
}
|
|
978
|
+
return order;
|
|
979
|
+
}
|
|
980
|
+
initPolyEmbeddables(embeddedProp, discovered, visited = new Set()) {
|
|
981
|
+
if (embeddedProp.kind !== ReferenceKind.EMBEDDED || visited.has(embeddedProp)) {
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
visited.add(embeddedProp);
|
|
985
|
+
const types = embeddedProp.type.split(/ ?\| ?/);
|
|
986
|
+
let embeddable = this.#discovered.find(m => m.name === embeddedProp.type);
|
|
987
|
+
const polymorphs = this.#discovered.filter(m => types.includes(m.name));
|
|
988
|
+
// create virtual polymorphic entity
|
|
989
|
+
if (!embeddable && polymorphs.length > 0) {
|
|
990
|
+
const properties = {};
|
|
991
|
+
let discriminatorColumn;
|
|
992
|
+
const inlineProperties = (meta) => {
|
|
993
|
+
Object.values(meta.properties).forEach(prop => {
|
|
994
|
+
// defaults on db level would mess up with change tracking
|
|
995
|
+
delete prop.default;
|
|
996
|
+
if (properties[prop.name] && properties[prop.name].type !== prop.type) {
|
|
997
|
+
properties[prop.name].type = `${properties[prop.name].type} | ${prop.type}`;
|
|
998
|
+
properties[prop.name].runtimeType = 'any';
|
|
999
|
+
properties[prop.name].stiMerged = true;
|
|
1000
|
+
return properties[prop.name];
|
|
1001
|
+
}
|
|
1002
|
+
// Deep copy to prevent mutating the original entity's property —
|
|
1003
|
+
// both from the merge path above (GH #6522/#6523) and from
|
|
1004
|
+
// downstream code that mutates nested arrays like fieldNames.
|
|
1005
|
+
return (properties[prop.name] = Utils.copy(prop));
|
|
1006
|
+
});
|
|
1007
|
+
};
|
|
1008
|
+
const processExtensions = (meta) => {
|
|
1009
|
+
const parent = this.#discovered.find(m => {
|
|
1010
|
+
return meta.extends && Utils.className(meta.extends) === m.className;
|
|
1011
|
+
});
|
|
1012
|
+
if (!parent) {
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
discriminatorColumn ??= parent.discriminatorColumn;
|
|
1016
|
+
inlineProperties(parent);
|
|
1017
|
+
processExtensions(parent);
|
|
1018
|
+
};
|
|
1019
|
+
polymorphs.forEach(meta => {
|
|
1020
|
+
inlineProperties(meta);
|
|
1021
|
+
processExtensions(meta);
|
|
1022
|
+
});
|
|
1023
|
+
const name = polymorphs
|
|
1024
|
+
.map(t => t.className)
|
|
1025
|
+
.sort()
|
|
1026
|
+
.join(' | ');
|
|
1027
|
+
embeddable = new EntityMetadata({
|
|
1028
|
+
name,
|
|
1029
|
+
className: name,
|
|
1030
|
+
embeddable: true,
|
|
1031
|
+
abstract: true,
|
|
1032
|
+
properties,
|
|
1033
|
+
polymorphs,
|
|
1034
|
+
discriminatorColumn,
|
|
1035
|
+
});
|
|
1036
|
+
embeddable.sync();
|
|
1037
|
+
discovered.push(embeddable);
|
|
1038
|
+
polymorphs.forEach(meta => (meta.root = embeddable));
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
initPolymorphicRelation(meta, prop, discovered) {
|
|
1042
|
+
if (!prop.discriminator && !prop.discriminatorColumn && !prop.discriminatorMap && !Array.isArray(prop.target)) {
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
prop.polymorphic = true;
|
|
1046
|
+
prop.discriminator ??= prop.name;
|
|
1047
|
+
prop.discriminatorColumn ??= this.#namingStrategy.discriminatorColumnName(prop.discriminator);
|
|
1048
|
+
prop.createForeignKeyConstraint = false;
|
|
1049
|
+
const isToOne = [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind);
|
|
1050
|
+
if (isToOne) {
|
|
1051
|
+
const types = prop.type.split(/ ?\| ?/);
|
|
1052
|
+
prop.polymorphTargets = discovered.filter(m => types.includes(m.className) && !m.embeddable);
|
|
1053
|
+
prop.targetMeta = prop.polymorphTargets[0];
|
|
1054
|
+
prop.referencedPKs = prop.targetMeta?.primaryKeys;
|
|
1055
|
+
}
|
|
1056
|
+
if (prop.discriminatorMap) {
|
|
1057
|
+
const normalizedMap = {};
|
|
1058
|
+
for (const [key, value] of Object.entries(prop.discriminatorMap)) {
|
|
1059
|
+
const targetMeta = this.#metadata.getByClassName(value, false);
|
|
1060
|
+
if (!targetMeta) {
|
|
1061
|
+
throw MetadataError.fromUnknownEntity(value, `${meta.className}.${prop.name} discriminatorMap`);
|
|
1062
|
+
}
|
|
1063
|
+
normalizedMap[key] = targetMeta.class;
|
|
1064
|
+
if (targetMeta.class === meta.class) {
|
|
1065
|
+
prop.discriminatorValue = key;
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
prop.discriminatorMap = normalizedMap;
|
|
1069
|
+
}
|
|
1070
|
+
else if (isToOne) {
|
|
1071
|
+
prop.discriminatorMap = {};
|
|
1072
|
+
const tableNameToTarget = new Map();
|
|
1073
|
+
for (const target of prop.polymorphTargets) {
|
|
1074
|
+
const existing = tableNameToTarget.get(target.tableName);
|
|
1075
|
+
if (existing) {
|
|
1076
|
+
throw MetadataError.incompatiblePolymorphicTargets(meta, prop, existing, target, `both use table '${target.tableName}'. Use separate properties instead of a single polymorphic relation.`);
|
|
1077
|
+
}
|
|
1078
|
+
tableNameToTarget.set(target.tableName, target);
|
|
1079
|
+
prop.discriminatorMap[target.tableName] = target.class;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
else {
|
|
1083
|
+
prop.discriminatorValue ??= meta.tableName;
|
|
1084
|
+
if (!prop.discriminatorMap) {
|
|
1085
|
+
prop.discriminatorMap = { [prop.discriminatorValue]: meta.class };
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
initEmbeddables(meta, embeddedProp, visited = new Set()) {
|
|
1090
|
+
if (embeddedProp.kind !== ReferenceKind.EMBEDDED || visited.has(embeddedProp)) {
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
visited.add(embeddedProp);
|
|
1094
|
+
const embeddable = this.#discovered.find(m => m.name === embeddedProp.type);
|
|
1095
|
+
if (!embeddable) {
|
|
1096
|
+
throw MetadataError.fromUnknownEntity(embeddedProp.type, `${meta.className}.${embeddedProp.name}`);
|
|
1097
|
+
}
|
|
1098
|
+
embeddedProp.embeddable = embeddable.class;
|
|
1099
|
+
embeddedProp.embeddedProps = {};
|
|
1100
|
+
let order = meta.propertyOrder.get(embeddedProp.name);
|
|
1101
|
+
const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
|
|
1102
|
+
const isParentObject = (prop) => {
|
|
1103
|
+
if (prop.object || prop.array) {
|
|
1104
|
+
return true;
|
|
1105
|
+
}
|
|
1106
|
+
return prop.embedded ? isParentObject(meta.properties[prop.embedded[0]]) : false;
|
|
1107
|
+
};
|
|
1108
|
+
const isParentArray = (prop) => {
|
|
1109
|
+
if (prop.array) {
|
|
1110
|
+
return true;
|
|
1111
|
+
}
|
|
1112
|
+
return prop.embedded ? isParentArray(meta.properties[prop.embedded[0]]) : false;
|
|
1113
|
+
};
|
|
1114
|
+
const rootProperty = getRootProperty(embeddedProp);
|
|
1115
|
+
const parentProperty = meta.properties[embeddedProp.embedded?.[0] ?? ''];
|
|
1116
|
+
const object = isParentObject(embeddedProp);
|
|
1117
|
+
const array = isParentArray(embeddedProp);
|
|
1118
|
+
this.initFieldName(embeddedProp, rootProperty !== embeddedProp && object);
|
|
1119
|
+
// the prefix of the parent cannot be a boolean; it already passed here
|
|
1120
|
+
const prefix = this.getPrefix(embeddedProp, parentProperty);
|
|
1121
|
+
const glue = object ? '~' : '_';
|
|
1122
|
+
for (const prop of Object.values(embeddable.properties)) {
|
|
1123
|
+
const name = (embeddedProp.embeddedPath?.join(glue) ?? embeddedProp.fieldNames[0] + glue) + prop.name;
|
|
1124
|
+
meta.properties[name] = Utils.copy(prop);
|
|
1125
|
+
meta.properties[name].name = name;
|
|
1126
|
+
meta.properties[name].embedded = [embeddedProp.name, prop.name];
|
|
1127
|
+
meta.propertyOrder.set(name, (order += 0.01));
|
|
1128
|
+
embeddedProp.embeddedProps[prop.name] = meta.properties[name];
|
|
1129
|
+
meta.properties[name].persist ??= embeddedProp.persist;
|
|
1130
|
+
const refInArray = array && [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && prop.owner;
|
|
1131
|
+
if (embeddedProp.nullable || refInArray) {
|
|
1132
|
+
meta.properties[name].nullable = true;
|
|
1133
|
+
}
|
|
1134
|
+
if (meta.properties[name].fieldNames) {
|
|
1135
|
+
meta.properties[name].fieldNames[0] = prefix + meta.properties[name].fieldNames[0];
|
|
1136
|
+
}
|
|
1137
|
+
else {
|
|
1138
|
+
const name2 = meta.properties[name].name;
|
|
1139
|
+
meta.properties[name].name = prefix + prop.name;
|
|
1140
|
+
this.initFieldName(meta.properties[name]);
|
|
1141
|
+
meta.properties[name].name = name2;
|
|
1142
|
+
}
|
|
1143
|
+
if (object) {
|
|
1144
|
+
embeddedProp.object = true;
|
|
1145
|
+
let path = [];
|
|
1146
|
+
let tmp = embeddedProp;
|
|
1147
|
+
while (tmp.embedded && tmp.object) {
|
|
1148
|
+
path.unshift(tmp.embedded[1]);
|
|
1149
|
+
tmp = meta.properties[tmp.embedded[0]];
|
|
1150
|
+
}
|
|
1151
|
+
if (tmp === rootProperty) {
|
|
1152
|
+
path.unshift(rootProperty.fieldNames[0]);
|
|
1153
|
+
}
|
|
1154
|
+
else if (embeddedProp.embeddedPath) {
|
|
1155
|
+
path = [...embeddedProp.embeddedPath];
|
|
1156
|
+
}
|
|
1157
|
+
else {
|
|
1158
|
+
path = [embeddedProp.fieldNames[0]];
|
|
1159
|
+
}
|
|
1160
|
+
this.initFieldName(prop, true);
|
|
1161
|
+
this.initRelation(prop);
|
|
1162
|
+
path.push(prop.fieldNames[0]);
|
|
1163
|
+
meta.properties[name].fieldNames = prop.fieldNames;
|
|
1164
|
+
meta.properties[name].embeddedPath = path;
|
|
1165
|
+
const targetProp = prop.targetMeta?.getPrimaryProp() ?? prop;
|
|
1166
|
+
const fieldName = raw(this.#platform.getSearchJsonPropertySQL(path.join('->'), targetProp.runtimeType ?? targetProp.type, true));
|
|
1167
|
+
meta.properties[name].fieldNameRaw = fieldName.sql; // for querying in SQL drivers
|
|
1168
|
+
meta.properties[name].persist = false; // only virtual as we store the whole object
|
|
1169
|
+
meta.properties[name].userDefined = false; // mark this as a generated/internal property, so we can distinguish from user-defined non-persist properties
|
|
1170
|
+
meta.properties[name].object = true;
|
|
1171
|
+
this.initCustomType(meta, meta.properties[name], false, true);
|
|
1172
|
+
}
|
|
1173
|
+
this.initEmbeddables(meta, meta.properties[name], visited);
|
|
1174
|
+
}
|
|
1175
|
+
for (const index of embeddable.indexes) {
|
|
1176
|
+
meta.indexes.push({
|
|
1177
|
+
...index,
|
|
1178
|
+
properties: Utils.asArray(index.properties).map(p => {
|
|
1179
|
+
return embeddedProp.embeddedProps[p].name;
|
|
1180
|
+
}),
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
for (const unique of embeddable.uniques) {
|
|
1184
|
+
meta.uniques.push({
|
|
1185
|
+
...unique,
|
|
1186
|
+
properties: Utils.asArray(unique.properties).map(p => {
|
|
1187
|
+
return embeddedProp.embeddedProps[p].name;
|
|
1188
|
+
}),
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1030
1191
|
}
|
|
1031
|
-
meta
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1192
|
+
initSingleTableInheritance(meta, metadata) {
|
|
1193
|
+
if (meta.root !== meta && !meta.__processed) {
|
|
1194
|
+
meta.root = metadata.find(m => m.class === meta.root.class);
|
|
1195
|
+
meta.root.__processed = true;
|
|
1196
|
+
}
|
|
1197
|
+
else {
|
|
1198
|
+
delete meta.root.__processed;
|
|
1199
|
+
}
|
|
1200
|
+
if (!meta.root.discriminatorColumn) {
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
meta.root.inheritanceType = 'sti';
|
|
1204
|
+
if (meta.root.discriminatorMap) {
|
|
1205
|
+
const map = meta.root.discriminatorMap;
|
|
1206
|
+
Object.keys(map)
|
|
1207
|
+
.filter(key => typeof map[key] === 'string')
|
|
1208
|
+
.forEach(key => (map[key] = this.#metadata.getByClassName(map[key]).class));
|
|
1209
|
+
}
|
|
1210
|
+
else {
|
|
1211
|
+
meta.root.discriminatorMap = {};
|
|
1212
|
+
const children = metadata
|
|
1213
|
+
.filter(m => m.root.class === meta.root.class && !m.abstract)
|
|
1214
|
+
.sort((a, b) => a.className.localeCompare(b.className));
|
|
1215
|
+
for (const m of children) {
|
|
1216
|
+
const name = m.discriminatorValue ?? this.#namingStrategy.classToTableName(m.className);
|
|
1217
|
+
meta.root.discriminatorMap[name] = m.class;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
meta.discriminatorValue = QueryHelper.findDiscriminatorValue(meta.root.discriminatorMap, meta.class);
|
|
1221
|
+
if (!meta.root.properties[meta.root.discriminatorColumn]) {
|
|
1222
|
+
this.createDiscriminatorProperty(meta.root);
|
|
1223
|
+
}
|
|
1224
|
+
Utils.defaultValue(meta.root.properties[meta.root.discriminatorColumn], 'items', Object.keys(meta.root.discriminatorMap));
|
|
1225
|
+
Utils.defaultValue(meta.root.properties[meta.root.discriminatorColumn], 'index', true);
|
|
1226
|
+
if (meta.root === meta) {
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1061
1229
|
Object.values(meta.properties).forEach(prop => {
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1230
|
+
const newProp = { ...prop };
|
|
1231
|
+
const rootProp = meta.root.properties[prop.name];
|
|
1232
|
+
// stiMerged is set during inlineProperties when a property was merged
|
|
1233
|
+
// from multiple polymorphic variants with different types. The flag is
|
|
1234
|
+
// cleared implicitly when the first child claims the root property via
|
|
1235
|
+
// addProperty below, so subsequent children correctly trigger renaming.
|
|
1236
|
+
const typesMatch = rootProp?.type === prop.type || rootProp?.stiMerged === true;
|
|
1237
|
+
if (rootProp &&
|
|
1238
|
+
(!typesMatch ||
|
|
1239
|
+
(rootProp.fieldNames && prop.fieldNames && !compareArrays(rootProp.fieldNames, prop.fieldNames)))) {
|
|
1240
|
+
const name = newProp.name;
|
|
1241
|
+
this.initFieldName(newProp, newProp.object);
|
|
1242
|
+
newProp.renamedFrom = name;
|
|
1243
|
+
newProp.name = `${name}_${meta._id}`;
|
|
1244
|
+
meta.root.addProperty(newProp);
|
|
1245
|
+
this.initFieldName(prop, prop.object);
|
|
1246
|
+
// Track all field variants and map discriminator values to field names
|
|
1247
|
+
if (!rootProp.stiFieldNames) {
|
|
1248
|
+
this.initFieldName(rootProp, rootProp.object);
|
|
1249
|
+
rootProp.stiFieldNames = [...rootProp.fieldNames];
|
|
1250
|
+
rootProp.stiFieldNameMap = {};
|
|
1251
|
+
// Find which discriminator owns the original fieldNames
|
|
1252
|
+
for (const [discValue, childClass] of Object.entries(meta.root.discriminatorMap)) {
|
|
1253
|
+
const childMeta = this.#metadata.find(childClass);
|
|
1254
|
+
if (childMeta?.properties[prop.name]?.fieldNames &&
|
|
1255
|
+
compareArrays(childMeta.properties[prop.name].fieldNames, rootProp.fieldNames)) {
|
|
1256
|
+
rootProp.stiFieldNameMap[discValue] = rootProp.fieldNames[0];
|
|
1257
|
+
break;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
rootProp.stiFieldNameMap[meta.discriminatorValue] = prop.fieldNames[0];
|
|
1262
|
+
rootProp.stiFieldNames.push(...prop.fieldNames);
|
|
1263
|
+
newProp.nullable = true;
|
|
1264
|
+
newProp.name = name;
|
|
1265
|
+
newProp.hydrate = false;
|
|
1266
|
+
newProp.inherited = true;
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
if (prop.enum && prop.items && rootProp?.items) {
|
|
1270
|
+
newProp.items = Utils.unique([...rootProp.items, ...prop.items]);
|
|
1271
|
+
}
|
|
1272
|
+
newProp.nullable = true;
|
|
1273
|
+
newProp.inherited = !rootProp;
|
|
1274
|
+
meta.root.addProperty(newProp);
|
|
1074
1275
|
});
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1276
|
+
meta.tableName = meta.root.tableName;
|
|
1277
|
+
meta.root.indexes = Utils.unique([...meta.root.indexes, ...meta.indexes]);
|
|
1278
|
+
meta.root.uniques = Utils.unique([...meta.root.uniques, ...meta.uniques]);
|
|
1279
|
+
meta.root.checks = Utils.unique([...meta.root.checks, ...meta.checks]);
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* First pass of TPT initialization: sets up hierarchy relationships
|
|
1283
|
+
* (inheritanceType, tptParent, tptChildren) before properties have fieldNames.
|
|
1284
|
+
*/
|
|
1285
|
+
initTPTRelationships(meta, metadata) {
|
|
1286
|
+
if (meta.root !== meta) {
|
|
1287
|
+
meta.root = metadata.find(m => m.class === meta.root.class);
|
|
1288
|
+
}
|
|
1289
|
+
const inheritance = meta.inheritance;
|
|
1290
|
+
if (inheritance === 'tpt' && meta.root === meta) {
|
|
1291
|
+
meta.inheritanceType = 'tpt';
|
|
1292
|
+
meta.tptChildren = [];
|
|
1293
|
+
}
|
|
1294
|
+
if (meta.root.inheritanceType !== 'tpt') {
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
const parent = this.getTPTParent(meta, metadata);
|
|
1298
|
+
if (parent) {
|
|
1299
|
+
meta.tptParent = parent;
|
|
1300
|
+
meta.inheritanceType = 'tpt';
|
|
1301
|
+
parent.tptChildren ??= [];
|
|
1302
|
+
if (!parent.tptChildren.includes(meta)) {
|
|
1303
|
+
parent.tptChildren.push(meta);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
/**
|
|
1308
|
+
* Second pass of TPT initialization: re-resolves metadata references after fieldNames
|
|
1309
|
+
* are set, syncs to registry metadata, and sets up discriminators.
|
|
1310
|
+
*/
|
|
1311
|
+
finalizeTPTInheritance(meta, metadata) {
|
|
1312
|
+
if (meta.inheritanceType !== 'tpt') {
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
if (meta.tptParent) {
|
|
1316
|
+
meta.tptParent = metadata.find(m => m.class === meta.tptParent.class) ?? meta.tptParent;
|
|
1317
|
+
}
|
|
1318
|
+
if (meta.tptChildren) {
|
|
1319
|
+
meta.tptChildren = meta.tptChildren.map(child => metadata.find(m => m.class === child.class) ?? child);
|
|
1320
|
+
}
|
|
1321
|
+
const registryMeta = this.#metadata.get(meta.class);
|
|
1322
|
+
if (registryMeta && registryMeta !== meta) {
|
|
1323
|
+
registryMeta.inheritanceType = meta.inheritanceType;
|
|
1324
|
+
registryMeta.tptParent = meta.tptParent ? this.#metadata.get(meta.tptParent.class) : undefined;
|
|
1325
|
+
registryMeta.tptChildren = meta.tptChildren?.map(child => this.#metadata.get(child.class));
|
|
1326
|
+
}
|
|
1327
|
+
this.initTPTDiscriminator(meta, metadata);
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Initialize TPT discriminator map and virtual discriminator property.
|
|
1331
|
+
* Unlike STI where the discriminator is a persisted column, TPT discriminator is computed
|
|
1332
|
+
* at query time using CASE WHEN expressions based on which child table has data.
|
|
1333
|
+
*/
|
|
1334
|
+
initTPTDiscriminator(meta, metadata) {
|
|
1335
|
+
const allDescendants = this.collectAllTPTDescendants(meta, metadata);
|
|
1336
|
+
allDescendants.sort((a, b) => this.getTPTDepth(b) - this.getTPTDepth(a));
|
|
1337
|
+
meta.allTPTDescendants = allDescendants;
|
|
1338
|
+
if (meta.root !== meta) {
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
meta.root.discriminatorMap = {};
|
|
1342
|
+
for (const m of allDescendants) {
|
|
1343
|
+
const name = this.#namingStrategy.classToTableName(m.className);
|
|
1344
|
+
meta.root.discriminatorMap[name] = m.class;
|
|
1345
|
+
m.discriminatorValue = name;
|
|
1346
|
+
}
|
|
1347
|
+
if (!meta.abstract) {
|
|
1348
|
+
const name = this.#namingStrategy.classToTableName(meta.className);
|
|
1349
|
+
meta.root.discriminatorMap[name] = meta.class;
|
|
1350
|
+
meta.discriminatorValue = name;
|
|
1351
|
+
}
|
|
1352
|
+
// Virtual discriminator property - computed at query time via CASE WHEN, not persisted
|
|
1353
|
+
const discriminatorColumn = '__tpt_type';
|
|
1354
|
+
if (!meta.root.properties[discriminatorColumn]) {
|
|
1355
|
+
meta.root.addProperty({
|
|
1356
|
+
name: discriminatorColumn,
|
|
1357
|
+
type: 'string',
|
|
1358
|
+
kind: ReferenceKind.SCALAR,
|
|
1359
|
+
persist: false,
|
|
1360
|
+
userDefined: false,
|
|
1361
|
+
hidden: true,
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
meta.root.tptDiscriminatorColumn = discriminatorColumn;
|
|
1365
|
+
}
|
|
1366
|
+
/**
|
|
1367
|
+
* Recursively collect all TPT descendants (children, grandchildren, etc.)
|
|
1368
|
+
*/
|
|
1369
|
+
collectAllTPTDescendants(meta, metadata) {
|
|
1370
|
+
const descendants = [];
|
|
1371
|
+
const collect = (parent) => {
|
|
1372
|
+
for (const child of parent.tptChildren ?? []) {
|
|
1373
|
+
const resolved = metadata.find(m => m.class === child.class) ?? child;
|
|
1374
|
+
if (!resolved.abstract) {
|
|
1375
|
+
descendants.push(resolved);
|
|
1376
|
+
}
|
|
1377
|
+
collect(resolved);
|
|
1378
|
+
}
|
|
1379
|
+
};
|
|
1380
|
+
collect(meta);
|
|
1381
|
+
return descendants;
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* Computes ownProps for TPT entities - only properties defined in THIS entity,
|
|
1385
|
+
* not inherited from parent. Also creates synthetic join properties for parent/child relationships.
|
|
1386
|
+
*
|
|
1387
|
+
* Called multiple times during discovery as metadata is progressively built.
|
|
1388
|
+
* Each pass overwrites earlier results to reflect the final state of properties.
|
|
1389
|
+
*/
|
|
1390
|
+
computeTPTOwnProps(meta) {
|
|
1391
|
+
if (meta.inheritanceType !== 'tpt') {
|
|
1392
|
+
return;
|
|
1393
|
+
}
|
|
1394
|
+
const belongsToTable = (prop) => prop.persist !== false || prop.primary;
|
|
1395
|
+
// Use meta.properties (object) since meta.props (array) may not be populated yet
|
|
1396
|
+
const allProps = Object.values(meta.properties);
|
|
1397
|
+
if (!meta.tptParent) {
|
|
1398
|
+
meta.ownProps = allProps.filter(belongsToTable);
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
const parentPropNames = new Set(Object.values(meta.tptParent.properties).map(p => p.name));
|
|
1402
|
+
meta.ownProps = allProps.filter(prop => !parentPropNames.has(prop.name) && belongsToTable(prop));
|
|
1403
|
+
// Create synthetic join properties for the parent-child relationship
|
|
1404
|
+
const childFieldNames = meta.getPrimaryProps().flatMap(p => p.fieldNames);
|
|
1405
|
+
const parentFieldNames = meta.tptParent.getPrimaryProps().flatMap(p => p.fieldNames);
|
|
1406
|
+
meta.tptParentProp = {
|
|
1407
|
+
name: '[tpt:parent]',
|
|
1408
|
+
kind: ReferenceKind.MANY_TO_ONE,
|
|
1409
|
+
targetMeta: meta.tptParent,
|
|
1410
|
+
fieldNames: childFieldNames,
|
|
1411
|
+
referencedColumnNames: parentFieldNames,
|
|
1412
|
+
persist: false,
|
|
1413
|
+
};
|
|
1414
|
+
meta.tptInverseProp = {
|
|
1415
|
+
name: '[tpt:child]',
|
|
1416
|
+
kind: ReferenceKind.ONE_TO_ONE,
|
|
1417
|
+
owner: true,
|
|
1418
|
+
targetMeta: meta,
|
|
1419
|
+
fieldNames: parentFieldNames,
|
|
1420
|
+
joinColumns: parentFieldNames,
|
|
1421
|
+
referencedColumnNames: childFieldNames,
|
|
1422
|
+
persist: false,
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
/** Returns the depth of a TPT entity in its hierarchy (0 for root). */
|
|
1426
|
+
getTPTDepth(meta) {
|
|
1427
|
+
let depth = 0;
|
|
1428
|
+
let current = meta;
|
|
1429
|
+
while (current.tptParent) {
|
|
1430
|
+
depth++;
|
|
1431
|
+
current = current.tptParent;
|
|
1432
|
+
}
|
|
1433
|
+
return depth;
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* Find the direct TPT parent entity for the given entity.
|
|
1437
|
+
*/
|
|
1438
|
+
getTPTParent(meta, metadata) {
|
|
1439
|
+
if (meta.root === meta) {
|
|
1440
|
+
return undefined;
|
|
1441
|
+
}
|
|
1442
|
+
return metadata.find(m => {
|
|
1443
|
+
const ext = meta.extends;
|
|
1444
|
+
if (EntitySchema.is(ext)) {
|
|
1445
|
+
return m.class === ext.meta.class || m.className === ext.meta.className;
|
|
1446
|
+
}
|
|
1447
|
+
return m.class === ext || m.className === Utils.className(ext);
|
|
1079
1448
|
});
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
.
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
.
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
if
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
}
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
createDiscriminatorProperty(meta) {
|
|
1529
|
-
meta.addProperty({
|
|
1530
|
-
name: meta.discriminatorColumn,
|
|
1531
|
-
type: 'string',
|
|
1532
|
-
enum: true,
|
|
1533
|
-
kind: ReferenceKind.SCALAR,
|
|
1534
|
-
userDefined: false,
|
|
1535
|
-
});
|
|
1536
|
-
}
|
|
1537
|
-
initAutoincrement(meta) {
|
|
1538
|
-
const pks = meta.getPrimaryProps();
|
|
1539
|
-
if (pks.length === 1 && this.#platform.isNumericProperty(pks[0])) {
|
|
1540
|
-
/* v8 ignore next */
|
|
1541
|
-
pks[0].autoincrement ??= true;
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
createSchemaTable(meta) {
|
|
1545
|
-
const qualifiedName = meta.schema ? `${meta.schema}.${meta.tableName}` : meta.tableName;
|
|
1546
|
-
return {
|
|
1547
|
-
name: meta.tableName,
|
|
1548
|
-
schema: meta.schema,
|
|
1549
|
-
qualifiedName,
|
|
1550
|
-
toString: () => qualifiedName,
|
|
1551
|
-
};
|
|
1552
|
-
}
|
|
1553
|
-
initCheckConstraints(meta) {
|
|
1554
|
-
const columns = meta.createSchemaColumnMappingObject();
|
|
1555
|
-
const table = this.createSchemaTable(meta);
|
|
1556
|
-
for (const check of meta.checks) {
|
|
1557
|
-
const fieldNames = check.property ? meta.properties[check.property].fieldNames : [];
|
|
1558
|
-
check.name ??= this.#namingStrategy.indexName(meta.tableName, fieldNames, 'check');
|
|
1559
|
-
if (check.expression instanceof Function) {
|
|
1560
|
-
check.expression = check.expression(columns, table);
|
|
1561
|
-
}
|
|
1562
|
-
}
|
|
1563
|
-
if (this.#platform.usesEnumCheckConstraints() && !meta.embeddable) {
|
|
1564
|
-
for (const prop of meta.props) {
|
|
1565
|
-
if (prop.persist === false || prop.nativeEnumName || !prop.items?.every(item => typeof item === 'string')) {
|
|
1566
|
-
continue;
|
|
1567
|
-
}
|
|
1568
|
-
this.initFieldName(prop);
|
|
1569
|
-
let expression = null;
|
|
1570
|
-
if (prop.enum) {
|
|
1571
|
-
expression = `${this.#platform.quoteIdentifier(prop.fieldNames[0])} in ('${prop.items.join("', '")}')`;
|
|
1572
|
-
} else if (prop.array) {
|
|
1573
|
-
expression = this.#platform.getEnumArrayCheckConstraintExpression(prop.fieldNames[0], prop.items);
|
|
1574
|
-
}
|
|
1575
|
-
if (expression) {
|
|
1576
|
-
meta.checks.push({
|
|
1577
|
-
name: this.#namingStrategy.indexName(meta.tableName, prop.fieldNames, 'check'),
|
|
1578
|
-
property: prop.name,
|
|
1579
|
-
expression,
|
|
1580
|
-
});
|
|
1581
|
-
}
|
|
1582
|
-
}
|
|
1583
|
-
}
|
|
1584
|
-
}
|
|
1585
|
-
initGeneratedColumn(meta, prop) {
|
|
1586
|
-
if (!prop.generated && prop.columnTypes) {
|
|
1587
|
-
const match = /(.*) generated always as (.*)/i.exec(prop.columnTypes[0]);
|
|
1588
|
-
if (match) {
|
|
1589
|
-
prop.columnTypes[0] = match[1];
|
|
1590
|
-
prop.generated = match[2];
|
|
1591
|
-
return;
|
|
1592
|
-
}
|
|
1593
|
-
const match2 = /^as (.*)/i.exec(prop.columnTypes[0]?.trim());
|
|
1594
|
-
if (match2) {
|
|
1595
|
-
prop.generated = match2[1];
|
|
1596
|
-
}
|
|
1597
|
-
return;
|
|
1598
|
-
}
|
|
1599
|
-
if (prop.generated instanceof Function) {
|
|
1600
|
-
const columns = meta.createSchemaColumnMappingObject();
|
|
1601
|
-
prop.generated = prop.generated(columns, this.createSchemaTable(meta));
|
|
1602
|
-
}
|
|
1603
|
-
}
|
|
1604
|
-
getDefaultVersionValue(meta, prop) {
|
|
1605
|
-
if (typeof prop.defaultRaw !== 'undefined') {
|
|
1606
|
-
return prop.defaultRaw;
|
|
1607
|
-
}
|
|
1608
|
-
/* v8 ignore next */
|
|
1609
|
-
if (prop.default != null) {
|
|
1610
|
-
return '' + this.#platform.convertVersionValue(prop.default, prop);
|
|
1611
|
-
}
|
|
1612
|
-
this.initCustomType(meta, prop, true);
|
|
1613
|
-
const type = prop.customType?.runtimeType ?? prop.runtimeType ?? prop.type;
|
|
1614
|
-
if (type === 'Date') {
|
|
1615
|
-
prop.length ??= this.#platform.getDefaultVersionLength();
|
|
1616
|
-
return this.#platform.getCurrentTimestampSQL(prop.length);
|
|
1617
|
-
}
|
|
1618
|
-
return '1';
|
|
1619
|
-
}
|
|
1620
|
-
inferDefaultValue(meta, prop) {
|
|
1621
|
-
try {
|
|
1622
|
-
// try to create two entity instances to detect the value is stable
|
|
1623
|
-
const now = Date.now();
|
|
1624
|
-
const entity1 = new meta.class();
|
|
1625
|
-
const entity2 = new meta.class();
|
|
1626
|
-
// we compare the two values by reference, this will discard things like `new Date()` or `Date.now()`
|
|
1627
|
-
if (
|
|
1628
|
-
this.#config.get('discovery').inferDefaultValues &&
|
|
1629
|
-
prop.default === undefined &&
|
|
1630
|
-
entity1[prop.name] != null &&
|
|
1631
|
-
entity1[prop.name] === entity2[prop.name] &&
|
|
1632
|
-
entity1[prop.name] !== now
|
|
1633
|
-
) {
|
|
1634
|
-
prop.default ??= entity1[prop.name];
|
|
1635
|
-
}
|
|
1636
|
-
// if the default value is null, infer nullability
|
|
1637
|
-
if (entity1[prop.name] === null) {
|
|
1638
|
-
prop.nullable ??= true;
|
|
1639
|
-
}
|
|
1640
|
-
// but still use object values for type inference if not explicitly set, e.g. `createdAt = new Date()`
|
|
1641
|
-
if (prop.kind === ReferenceKind.SCALAR && prop.type == null && entity1[prop.name] != null) {
|
|
1642
|
-
prop.type = prop.runtimeType = Utils.getObjectType(entity1[prop.name]);
|
|
1643
|
-
}
|
|
1644
|
-
} catch {
|
|
1645
|
-
// ignore
|
|
1646
|
-
}
|
|
1647
|
-
}
|
|
1648
|
-
initDefaultValue(prop) {
|
|
1649
|
-
if (prop.defaultRaw || !('default' in prop)) {
|
|
1650
|
-
return;
|
|
1651
|
-
}
|
|
1652
|
-
let val = prop.default;
|
|
1653
|
-
const raw = Raw.getKnownFragment(val);
|
|
1654
|
-
if (raw) {
|
|
1655
|
-
prop.defaultRaw = this.#platform.formatQuery(raw.sql, raw.params);
|
|
1656
|
-
return;
|
|
1657
|
-
}
|
|
1658
|
-
if (Array.isArray(prop.default) && prop.customType) {
|
|
1659
|
-
val = prop.customType.convertToDatabaseValue(prop.default, this.#platform);
|
|
1660
|
-
}
|
|
1661
|
-
prop.defaultRaw = typeof val === 'string' ? `'${val}'` : '' + val;
|
|
1662
|
-
}
|
|
1663
|
-
inferTypeFromDefault(prop) {
|
|
1664
|
-
if ((prop.defaultRaw == null && prop.default == null) || prop.type !== 'any') {
|
|
1665
|
-
return;
|
|
1666
|
-
}
|
|
1667
|
-
switch (typeof prop.default) {
|
|
1668
|
-
case 'string':
|
|
1669
|
-
prop.type = prop.runtimeType = 'string';
|
|
1670
|
-
break;
|
|
1671
|
-
case 'number':
|
|
1672
|
-
prop.type = prop.runtimeType = 'number';
|
|
1673
|
-
break;
|
|
1674
|
-
case 'boolean':
|
|
1675
|
-
prop.type = prop.runtimeType = 'boolean';
|
|
1676
|
-
break;
|
|
1677
|
-
}
|
|
1678
|
-
if (prop.defaultRaw?.startsWith('current_timestamp')) {
|
|
1679
|
-
prop.type = prop.runtimeType = 'Date';
|
|
1680
|
-
}
|
|
1681
|
-
}
|
|
1682
|
-
initVersionProperty(meta, prop) {
|
|
1683
|
-
if (prop.version) {
|
|
1684
|
-
this.initDefaultValue(prop);
|
|
1685
|
-
meta.versionProperty = prop.name;
|
|
1686
|
-
prop.defaultRaw = this.getDefaultVersionValue(meta, prop);
|
|
1687
|
-
}
|
|
1688
|
-
if (prop.concurrencyCheck && !prop.primary) {
|
|
1689
|
-
meta.concurrencyCheckKeys.add(prop.name);
|
|
1690
|
-
}
|
|
1691
|
-
}
|
|
1692
|
-
initCustomType(meta, prop, simple = false, objectEmbeddable = false) {
|
|
1693
|
-
// `prop.type` might be actually instance of custom type class
|
|
1694
|
-
if (Type.isMappedType(prop.type) && !prop.customType) {
|
|
1695
|
-
prop.customType = prop.type;
|
|
1696
|
-
prop.type = prop.customType.constructor.name;
|
|
1697
|
-
}
|
|
1698
|
-
// `prop.type` might also be custom type class (not instance), so `typeof MyType` will give us `function`, not `object`
|
|
1699
|
-
if (typeof prop.type === 'function' && Type.isMappedType(prop.type.prototype) && !prop.customType) {
|
|
1700
|
-
// if the type is an ORM defined mapped type without `ensureComparable: true`,
|
|
1701
|
-
// we use just the type name, to have more performant hydration code
|
|
1702
|
-
const brand = Type.getOrmType(prop.type);
|
|
1703
|
-
const type = Utils.keys(t).find(type => {
|
|
1704
|
-
return !Type.getType(t[type]).ensureComparable(meta, prop) && (prop.type === t[type] || brand === type);
|
|
1705
|
-
});
|
|
1706
|
-
if (type) {
|
|
1707
|
-
prop.type = type === 'datetime' ? 'Date' : type;
|
|
1708
|
-
} else {
|
|
1709
|
-
prop.customType = new prop.type();
|
|
1710
|
-
prop.type = prop.customType.constructor.name;
|
|
1711
|
-
}
|
|
1712
|
-
}
|
|
1713
|
-
if (simple) {
|
|
1714
|
-
return;
|
|
1715
|
-
}
|
|
1716
|
-
if (!prop.customType && ['json', 'jsonb'].includes(prop.type?.toLowerCase())) {
|
|
1717
|
-
prop.customType = new t.json();
|
|
1718
|
-
}
|
|
1719
|
-
if (
|
|
1720
|
-
prop.kind === ReferenceKind.SCALAR &&
|
|
1721
|
-
!prop.customType &&
|
|
1722
|
-
prop.columnTypes &&
|
|
1723
|
-
['json', 'jsonb'].includes(prop.columnTypes[0])
|
|
1724
|
-
) {
|
|
1725
|
-
prop.customType = new t.json();
|
|
1726
|
-
}
|
|
1727
|
-
if (prop.kind === ReferenceKind.EMBEDDED && !prop.customType && (prop.object || prop.array)) {
|
|
1728
|
-
prop.customType = new t.json();
|
|
1729
|
-
}
|
|
1730
|
-
if (!prop.customType && prop.array && prop.items) {
|
|
1731
|
-
prop.customType = new t.enumArray(`${meta.className}.${prop.name}`, prop.items);
|
|
1732
|
-
}
|
|
1733
|
-
const isArray = prop.type?.toLowerCase() === 'array' || prop.type?.toString().endsWith('[]');
|
|
1734
|
-
if (objectEmbeddable && !prop.customType && isArray) {
|
|
1735
|
-
prop.customType = new t.json();
|
|
1736
|
-
}
|
|
1737
|
-
// for number arrays we make sure to convert the items to numbers
|
|
1738
|
-
if (!prop.customType && prop.type === 'number[]') {
|
|
1739
|
-
prop.customType = new t.array(i => +i);
|
|
1740
|
-
}
|
|
1741
|
-
// `string[]` can be returned via ts-morph, while reflect metadata will give us just `array`
|
|
1742
|
-
if (!prop.customType && isArray) {
|
|
1743
|
-
prop.customType = new t.array();
|
|
1744
|
-
}
|
|
1745
|
-
if (!prop.customType && prop.type?.toLowerCase() === 'buffer') {
|
|
1746
|
-
prop.customType = new t.blob();
|
|
1747
|
-
}
|
|
1748
|
-
if (!prop.customType && prop.type?.toLowerCase() === 'uint8array') {
|
|
1749
|
-
prop.customType = new t.uint8array();
|
|
1750
|
-
}
|
|
1751
|
-
const mappedType = this.getMappedType(prop);
|
|
1752
|
-
if (prop.fieldNames?.length === 1 && !prop.customType) {
|
|
1753
|
-
[t.bigint, t.double, t.decimal, t.interval, t.date]
|
|
1754
|
-
.filter(type => mappedType instanceof type)
|
|
1755
|
-
.forEach(type => (prop.customType = new type()));
|
|
1756
|
-
}
|
|
1757
|
-
if (prop.customType && !prop.columnTypes) {
|
|
1758
|
-
const mappedType = this.getMappedType({
|
|
1759
|
-
columnTypes: [prop.customType.getColumnType(prop, this.#platform)],
|
|
1760
|
-
});
|
|
1761
|
-
if (prop.customType.compareAsType() === 'any' && ![t.json].some(t => prop.customType instanceof t)) {
|
|
1762
|
-
prop.runtimeType ??= mappedType.runtimeType;
|
|
1763
|
-
} else {
|
|
1764
|
-
prop.runtimeType ??= prop.customType.runtimeType;
|
|
1765
|
-
}
|
|
1766
|
-
} else if (prop.runtimeType === 'object') {
|
|
1767
|
-
prop.runtimeType = mappedType.runtimeType;
|
|
1768
|
-
} else {
|
|
1769
|
-
prop.runtimeType ??= mappedType.runtimeType;
|
|
1770
|
-
}
|
|
1771
|
-
if (prop.customType) {
|
|
1772
|
-
prop.customType.platform = this.#platform;
|
|
1773
|
-
prop.customType.meta = meta;
|
|
1774
|
-
prop.customType.prop = prop;
|
|
1775
|
-
prop.columnTypes ??= [prop.customType.getColumnType(prop, this.#platform)];
|
|
1776
|
-
prop.hasConvertToJSValueSQL =
|
|
1777
|
-
!!prop.customType.convertToJSValueSQL && prop.customType.convertToJSValueSQL('', this.#platform) !== '';
|
|
1778
|
-
prop.hasConvertToDatabaseValueSQL =
|
|
1779
|
-
!!prop.customType.convertToDatabaseValueSQL &&
|
|
1780
|
-
prop.customType.convertToDatabaseValueSQL('', this.#platform) !== '';
|
|
1781
|
-
if (
|
|
1782
|
-
prop.customType instanceof t.bigint &&
|
|
1783
|
-
['string', 'bigint', 'number'].includes(prop.runtimeType.toLowerCase())
|
|
1784
|
-
) {
|
|
1785
|
-
prop.customType.mode = prop.runtimeType.toLowerCase();
|
|
1786
|
-
}
|
|
1787
|
-
}
|
|
1788
|
-
if (Type.isMappedType(prop.customType) && prop.kind === ReferenceKind.SCALAR && !isArray) {
|
|
1789
|
-
prop.type = prop.customType.name;
|
|
1790
|
-
}
|
|
1791
|
-
if (
|
|
1792
|
-
!prop.customType &&
|
|
1793
|
-
[ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) &&
|
|
1794
|
-
!prop.polymorphic &&
|
|
1795
|
-
prop.targetMeta.compositePK
|
|
1796
|
-
) {
|
|
1797
|
-
prop.customTypes = [];
|
|
1798
|
-
for (const pk of prop.targetMeta.getPrimaryProps()) {
|
|
1799
|
-
if (pk.customType) {
|
|
1800
|
-
prop.customTypes.push(pk.customType);
|
|
1801
|
-
prop.hasConvertToJSValueSQL ||=
|
|
1802
|
-
!!pk.customType.convertToJSValueSQL && pk.customType.convertToJSValueSQL('', this.#platform) !== '';
|
|
1803
|
-
/* v8 ignore next */
|
|
1804
|
-
prop.hasConvertToDatabaseValueSQL ||=
|
|
1805
|
-
!!pk.customType.convertToDatabaseValueSQL &&
|
|
1806
|
-
pk.customType.convertToDatabaseValueSQL('', this.#platform) !== '';
|
|
1807
|
-
} else {
|
|
1808
|
-
prop.customTypes.push(undefined);
|
|
1809
|
-
}
|
|
1810
|
-
}
|
|
1811
|
-
}
|
|
1812
|
-
if (prop.kind === ReferenceKind.SCALAR && !(mappedType instanceof t.unknown)) {
|
|
1813
|
-
if (
|
|
1814
|
-
prop.nativeEnumName &&
|
|
1815
|
-
meta.schema !== this.#platform.getDefaultSchemaName() &&
|
|
1816
|
-
meta.schema &&
|
|
1817
|
-
!prop.nativeEnumName.includes('.')
|
|
1818
|
-
) {
|
|
1819
|
-
const suffix = prop.columnTypes?.[0]?.endsWith('[]') ? '[]' : '';
|
|
1820
|
-
prop.columnTypes = [`${meta.schema}.${prop.nativeEnumName}${suffix}`];
|
|
1821
|
-
} else {
|
|
1822
|
-
prop.columnTypes ??= [mappedType.getColumnType(prop, this.#platform)];
|
|
1823
|
-
}
|
|
1824
|
-
// use only custom types provided by user, we don't need to use the ones provided by ORM,
|
|
1825
|
-
// with exception for ArrayType and JsonType, those two are handled in
|
|
1826
|
-
if (!Object.values(t).some(type => type === mappedType.constructor)) {
|
|
1827
|
-
prop.customType ??= mappedType;
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
}
|
|
1831
|
-
initRelation(prop) {
|
|
1832
|
-
if (prop.kind === ReferenceKind.SCALAR || prop.polymorphTargets) {
|
|
1833
|
-
return;
|
|
1834
|
-
}
|
|
1835
|
-
// when the target is a polymorphic embedded entity, `prop.target` is an array of classes, we need to get the metadata by the type name instead
|
|
1836
|
-
const meta2 = this.#metadata.find(prop.target) ?? this.#metadata.getByClassName(prop.type);
|
|
1837
|
-
// If targetKey is specified, use that property instead of PKs for referencedPKs
|
|
1838
|
-
prop.referencedPKs = prop.targetKey ? [prop.targetKey] : meta2.primaryKeys;
|
|
1839
|
-
prop.targetMeta = meta2;
|
|
1840
|
-
if (meta2.view) {
|
|
1841
|
-
prop.createForeignKeyConstraint = false;
|
|
1842
|
-
}
|
|
1843
|
-
// Auto-generate formula for persist: false relations, but only for single-column FKs
|
|
1844
|
-
// Composite FK relations need standard JOIN conditions, not formula-based
|
|
1845
|
-
if (
|
|
1846
|
-
!prop.formula &&
|
|
1847
|
-
prop.persist === false &&
|
|
1848
|
-
[ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
|
|
1849
|
-
!prop.embedded
|
|
1850
|
-
) {
|
|
1851
|
-
this.initFieldName(prop);
|
|
1852
|
-
if (prop.fieldNames?.length === 1) {
|
|
1853
|
-
prop.formula = table => `${table}.${this.#platform.quoteIdentifier(prop.fieldNames[0])}`;
|
|
1854
|
-
}
|
|
1855
|
-
}
|
|
1856
|
-
}
|
|
1857
|
-
initColumnType(prop) {
|
|
1858
|
-
this.initUnsigned(prop);
|
|
1859
|
-
// Get the target properties for FK relations - use targetKey property if specified, otherwise PKs
|
|
1860
|
-
const targetProps = prop.targetMeta
|
|
1861
|
-
? prop.targetKey
|
|
1862
|
-
? [prop.targetMeta.properties[prop.targetKey]]
|
|
1863
|
-
: prop.targetMeta.getPrimaryProps()
|
|
1864
|
-
: [];
|
|
1865
|
-
targetProps.map(targetProp => {
|
|
1866
|
-
prop.length ??= targetProp.length;
|
|
1867
|
-
prop.precision ??= targetProp.precision;
|
|
1868
|
-
prop.scale ??= targetProp.scale;
|
|
1869
|
-
});
|
|
1870
|
-
if (prop.kind === ReferenceKind.SCALAR && (prop.type == null || prop.type === 'object') && prop.columnTypes?.[0]) {
|
|
1871
|
-
delete prop.type;
|
|
1872
|
-
const mappedType = this.getMappedType(prop);
|
|
1873
|
-
prop.type = mappedType.compareAsType();
|
|
1874
|
-
}
|
|
1875
|
-
if (prop.columnTypes || !this.#schemaHelper) {
|
|
1876
|
-
return;
|
|
1877
|
-
}
|
|
1878
|
-
if (prop.kind === ReferenceKind.SCALAR) {
|
|
1879
|
-
const mappedType = this.getMappedType(prop);
|
|
1880
|
-
const SCALAR_TYPES = ['string', 'number', 'boolean', 'bigint', 'Date', 'Buffer', 'RegExp', 'any', 'unknown'];
|
|
1881
|
-
if (
|
|
1882
|
-
mappedType instanceof t.unknown &&
|
|
1883
|
-
// it could be a runtime type from reflect-metadata
|
|
1884
|
-
!SCALAR_TYPES.includes(prop.type) &&
|
|
1885
|
-
// or it might be inferred via ts-morph to some generic type alias
|
|
1886
|
-
!/[<>:"';{}]/.exec(prop.type)
|
|
1887
|
-
) {
|
|
1888
|
-
const type =
|
|
1889
|
-
prop.length != null && !prop.type.endsWith(`(${prop.length})`) ? `${prop.type}(${prop.length})` : prop.type;
|
|
1890
|
-
prop.columnTypes = [type];
|
|
1891
|
-
} else {
|
|
1892
|
-
prop.columnTypes = [mappedType.getColumnType(prop, this.#platform)];
|
|
1893
|
-
}
|
|
1894
|
-
return;
|
|
1895
|
-
}
|
|
1896
|
-
/* v8 ignore next */
|
|
1897
|
-
if (prop.kind === ReferenceKind.EMBEDDED && prop.object) {
|
|
1898
|
-
prop.columnTypes = [this.#platform.getJsonDeclarationSQL()];
|
|
1899
|
-
return;
|
|
1900
|
-
}
|
|
1901
|
-
const targetMeta = prop.targetMeta;
|
|
1902
|
-
prop.columnTypes = [];
|
|
1903
|
-
// Use targetKey property if specified, otherwise use primary key properties
|
|
1904
|
-
const referencedProps = prop.targetKey ? [targetMeta.properties[prop.targetKey]] : targetMeta.getPrimaryProps();
|
|
1905
|
-
if (prop.polymorphic && prop.polymorphTargets) {
|
|
1906
|
-
prop.columnTypes.push(this.#platform.getVarcharTypeDeclarationSQL(prop));
|
|
1907
|
-
}
|
|
1908
|
-
for (const referencedProp of referencedProps) {
|
|
1909
|
-
this.initCustomType(targetMeta, referencedProp);
|
|
1910
|
-
this.initColumnType(referencedProp);
|
|
1911
|
-
const mappedType = this.getMappedType(referencedProp);
|
|
1912
|
-
let columnTypes = referencedProp.columnTypes;
|
|
1913
|
-
if (referencedProp.autoincrement) {
|
|
1914
|
-
columnTypes = [mappedType.getColumnType({ ...referencedProp, autoincrement: false }, this.#platform)];
|
|
1915
|
-
}
|
|
1916
|
-
prop.columnTypes.push(...columnTypes);
|
|
1917
|
-
if (!targetMeta.compositePK || prop.targetKey) {
|
|
1918
|
-
prop.customType = referencedProp.customType;
|
|
1919
|
-
}
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
getMappedType(prop) {
|
|
1923
|
-
if (prop.customType) {
|
|
1924
|
-
return prop.customType;
|
|
1925
|
-
}
|
|
1926
|
-
/* v8 ignore next */
|
|
1927
|
-
let t = prop.columnTypes?.[0] ?? prop.type ?? '';
|
|
1928
|
-
if (prop.nativeEnumName) {
|
|
1929
|
-
t = 'enum';
|
|
1930
|
-
} else if (prop.enum) {
|
|
1931
|
-
t = prop.items?.every(item => typeof item === 'string') ? 'enum' : 'tinyint';
|
|
1932
|
-
}
|
|
1933
|
-
if (t === 'Date') {
|
|
1934
|
-
t = 'datetime';
|
|
1935
|
-
}
|
|
1936
|
-
return this.#platform.getMappedType(t);
|
|
1937
|
-
}
|
|
1938
|
-
getPrefix(prop, parent) {
|
|
1939
|
-
const { embeddedPath = [], fieldNames, prefix = true, prefixMode } = prop;
|
|
1940
|
-
if (prefix === true) {
|
|
1941
|
-
return (embeddedPath.length ? embeddedPath.join('_') : fieldNames[0]) + '_';
|
|
1942
|
-
}
|
|
1943
|
-
const prefixParent = parent ? this.getPrefix(parent, null) : '';
|
|
1944
|
-
if (prefix === false) {
|
|
1945
|
-
return prefixParent;
|
|
1946
|
-
}
|
|
1947
|
-
const mode = prefixMode ?? this.#config.get('embeddables').prefixMode;
|
|
1948
|
-
return mode === 'absolute' ? prefix : prefixParent + prefix;
|
|
1949
|
-
}
|
|
1950
|
-
initUnsigned(prop) {
|
|
1951
|
-
if (prop.unsigned != null) {
|
|
1952
|
-
return;
|
|
1953
|
-
}
|
|
1954
|
-
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
1955
|
-
prop.unsigned = prop.targetMeta.getPrimaryProps().some(pk => {
|
|
1956
|
-
this.initUnsigned(pk);
|
|
1957
|
-
return pk.unsigned;
|
|
1958
|
-
});
|
|
1959
|
-
return;
|
|
1960
|
-
}
|
|
1961
|
-
prop.unsigned ??=
|
|
1962
|
-
(prop.primary || prop.unsigned) && this.#platform.isNumericProperty(prop) && this.#platform.supportsUnsigned();
|
|
1963
|
-
}
|
|
1964
|
-
initIndexes(meta, prop) {
|
|
1965
|
-
const hasIndex = meta.indexes.some(idx => idx.properties?.length === 1 && idx.properties[0] === prop.name);
|
|
1966
|
-
if (prop.kind === ReferenceKind.MANY_TO_ONE && this.#platform.indexForeignKeys() && !hasIndex) {
|
|
1967
|
-
prop.index ??= true;
|
|
1968
|
-
}
|
|
1969
|
-
}
|
|
1970
|
-
shouldForceConstructorUsage(meta) {
|
|
1971
|
-
const forceConstructor = this.#config.get('forceEntityConstructor');
|
|
1972
|
-
if (Array.isArray(forceConstructor)) {
|
|
1973
|
-
return forceConstructor.some(cls => Utils.className(cls) === meta.className);
|
|
1974
|
-
}
|
|
1975
|
-
return forceConstructor;
|
|
1976
|
-
}
|
|
1449
|
+
}
|
|
1450
|
+
createDiscriminatorProperty(meta) {
|
|
1451
|
+
meta.addProperty({
|
|
1452
|
+
name: meta.discriminatorColumn,
|
|
1453
|
+
type: 'string',
|
|
1454
|
+
enum: true,
|
|
1455
|
+
kind: ReferenceKind.SCALAR,
|
|
1456
|
+
userDefined: false,
|
|
1457
|
+
});
|
|
1458
|
+
}
|
|
1459
|
+
initAutoincrement(meta) {
|
|
1460
|
+
const pks = meta.getPrimaryProps();
|
|
1461
|
+
if (pks.length === 1 && this.#platform.isNumericProperty(pks[0])) {
|
|
1462
|
+
/* v8 ignore next */
|
|
1463
|
+
pks[0].autoincrement ??= true;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
createSchemaTable(meta) {
|
|
1467
|
+
const qualifiedName = meta.schema ? `${meta.schema}.${meta.tableName}` : meta.tableName;
|
|
1468
|
+
return {
|
|
1469
|
+
name: meta.tableName,
|
|
1470
|
+
schema: meta.schema,
|
|
1471
|
+
qualifiedName,
|
|
1472
|
+
toString: () => qualifiedName,
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
initCheckConstraints(meta) {
|
|
1476
|
+
const columns = meta.createSchemaColumnMappingObject();
|
|
1477
|
+
const table = this.createSchemaTable(meta);
|
|
1478
|
+
for (const check of meta.checks) {
|
|
1479
|
+
const fieldNames = check.property ? meta.properties[check.property].fieldNames : [];
|
|
1480
|
+
check.name ??= this.#namingStrategy.indexName(meta.tableName, fieldNames, 'check');
|
|
1481
|
+
if (check.expression instanceof Function) {
|
|
1482
|
+
check.expression = check.expression(columns, table);
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
if (this.#platform.usesEnumCheckConstraints() && !meta.embeddable) {
|
|
1486
|
+
for (const prop of meta.props) {
|
|
1487
|
+
if (prop.persist === false || prop.nativeEnumName || !prop.items?.every(item => typeof item === 'string')) {
|
|
1488
|
+
continue;
|
|
1489
|
+
}
|
|
1490
|
+
this.initFieldName(prop);
|
|
1491
|
+
let expression = null;
|
|
1492
|
+
if (prop.enum) {
|
|
1493
|
+
expression = `${this.#platform.quoteIdentifier(prop.fieldNames[0])} in ('${prop.items.join("', '")}')`;
|
|
1494
|
+
}
|
|
1495
|
+
else if (prop.array) {
|
|
1496
|
+
expression = this.#platform.getEnumArrayCheckConstraintExpression(prop.fieldNames[0], prop.items);
|
|
1497
|
+
}
|
|
1498
|
+
if (expression) {
|
|
1499
|
+
meta.checks.push({
|
|
1500
|
+
name: this.#namingStrategy.indexName(meta.tableName, prop.fieldNames, 'check'),
|
|
1501
|
+
property: prop.name,
|
|
1502
|
+
expression,
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
initGeneratedColumn(meta, prop) {
|
|
1509
|
+
if (!prop.generated && prop.columnTypes) {
|
|
1510
|
+
const match = /(.*) generated always as (.*)/i.exec(prop.columnTypes[0]);
|
|
1511
|
+
if (match) {
|
|
1512
|
+
prop.columnTypes[0] = match[1];
|
|
1513
|
+
prop.generated = match[2];
|
|
1514
|
+
return;
|
|
1515
|
+
}
|
|
1516
|
+
const match2 = /^as (.*)/i.exec(prop.columnTypes[0]?.trim());
|
|
1517
|
+
if (match2) {
|
|
1518
|
+
prop.generated = match2[1];
|
|
1519
|
+
}
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
if (prop.generated instanceof Function) {
|
|
1523
|
+
const columns = meta.createSchemaColumnMappingObject();
|
|
1524
|
+
prop.generated = prop.generated(columns, this.createSchemaTable(meta));
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
getDefaultVersionValue(meta, prop) {
|
|
1528
|
+
if (typeof prop.defaultRaw !== 'undefined') {
|
|
1529
|
+
return prop.defaultRaw;
|
|
1530
|
+
}
|
|
1531
|
+
/* v8 ignore next */
|
|
1532
|
+
if (prop.default != null) {
|
|
1533
|
+
return '' + this.#platform.convertVersionValue(prop.default, prop);
|
|
1534
|
+
}
|
|
1535
|
+
this.initCustomType(meta, prop, true);
|
|
1536
|
+
const type = prop.customType?.runtimeType ?? prop.runtimeType ?? prop.type;
|
|
1537
|
+
if (type === 'Date') {
|
|
1538
|
+
prop.length ??= this.#platform.getDefaultVersionLength();
|
|
1539
|
+
return this.#platform.getCurrentTimestampSQL(prop.length);
|
|
1540
|
+
}
|
|
1541
|
+
return '1';
|
|
1542
|
+
}
|
|
1543
|
+
inferDefaultValue(meta, prop) {
|
|
1544
|
+
try {
|
|
1545
|
+
// try to create two entity instances to detect the value is stable
|
|
1546
|
+
const now = Date.now();
|
|
1547
|
+
const entity1 = new meta.class();
|
|
1548
|
+
const entity2 = new meta.class();
|
|
1549
|
+
// we compare the two values by reference, this will discard things like `new Date()` or `Date.now()`
|
|
1550
|
+
if (this.#config.get('discovery').inferDefaultValues &&
|
|
1551
|
+
prop.default === undefined &&
|
|
1552
|
+
entity1[prop.name] != null &&
|
|
1553
|
+
entity1[prop.name] === entity2[prop.name] &&
|
|
1554
|
+
entity1[prop.name] !== now) {
|
|
1555
|
+
prop.default ??= entity1[prop.name];
|
|
1556
|
+
}
|
|
1557
|
+
// if the default value is null, infer nullability
|
|
1558
|
+
if (entity1[prop.name] === null) {
|
|
1559
|
+
prop.nullable ??= true;
|
|
1560
|
+
}
|
|
1561
|
+
// but still use object values for type inference if not explicitly set, e.g. `createdAt = new Date()`
|
|
1562
|
+
if (prop.kind === ReferenceKind.SCALAR && prop.type == null && entity1[prop.name] != null) {
|
|
1563
|
+
prop.type = prop.runtimeType = Utils.getObjectType(entity1[prop.name]);
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
catch {
|
|
1567
|
+
// ignore
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
initDefaultValue(prop) {
|
|
1571
|
+
if (prop.defaultRaw || !('default' in prop)) {
|
|
1572
|
+
return;
|
|
1573
|
+
}
|
|
1574
|
+
let val = prop.default;
|
|
1575
|
+
const raw = Raw.getKnownFragment(val);
|
|
1576
|
+
if (raw) {
|
|
1577
|
+
prop.defaultRaw = this.#platform.formatQuery(raw.sql, raw.params);
|
|
1578
|
+
return;
|
|
1579
|
+
}
|
|
1580
|
+
if (Array.isArray(prop.default) && prop.customType) {
|
|
1581
|
+
val = prop.customType.convertToDatabaseValue(prop.default, this.#platform);
|
|
1582
|
+
}
|
|
1583
|
+
prop.defaultRaw = typeof val === 'string' ? `'${val}'` : '' + val;
|
|
1584
|
+
}
|
|
1585
|
+
inferTypeFromDefault(prop) {
|
|
1586
|
+
if ((prop.defaultRaw == null && prop.default == null) || prop.type !== 'any') {
|
|
1587
|
+
return;
|
|
1588
|
+
}
|
|
1589
|
+
switch (typeof prop.default) {
|
|
1590
|
+
case 'string':
|
|
1591
|
+
prop.type = prop.runtimeType = 'string';
|
|
1592
|
+
break;
|
|
1593
|
+
case 'number':
|
|
1594
|
+
prop.type = prop.runtimeType = 'number';
|
|
1595
|
+
break;
|
|
1596
|
+
case 'boolean':
|
|
1597
|
+
prop.type = prop.runtimeType = 'boolean';
|
|
1598
|
+
break;
|
|
1599
|
+
}
|
|
1600
|
+
if (prop.defaultRaw?.startsWith('current_timestamp')) {
|
|
1601
|
+
prop.type = prop.runtimeType = 'Date';
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
initVersionProperty(meta, prop) {
|
|
1605
|
+
if (prop.version) {
|
|
1606
|
+
this.initDefaultValue(prop);
|
|
1607
|
+
meta.versionProperty = prop.name;
|
|
1608
|
+
prop.defaultRaw = this.getDefaultVersionValue(meta, prop);
|
|
1609
|
+
}
|
|
1610
|
+
if (prop.concurrencyCheck && !prop.primary) {
|
|
1611
|
+
meta.concurrencyCheckKeys.add(prop.name);
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
initCustomType(meta, prop, simple = false, objectEmbeddable = false) {
|
|
1615
|
+
// `prop.type` might be actually instance of custom type class
|
|
1616
|
+
if (Type.isMappedType(prop.type) && !prop.customType) {
|
|
1617
|
+
prop.customType = prop.type;
|
|
1618
|
+
prop.type = prop.customType.constructor.name;
|
|
1619
|
+
}
|
|
1620
|
+
// `prop.type` might also be custom type class (not instance), so `typeof MyType` will give us `function`, not `object`
|
|
1621
|
+
if (typeof prop.type === 'function' &&
|
|
1622
|
+
Type.isMappedType(prop.type.prototype) &&
|
|
1623
|
+
!prop.customType) {
|
|
1624
|
+
// if the type is an ORM defined mapped type without `ensureComparable: true`,
|
|
1625
|
+
// we use just the type name, to have more performant hydration code
|
|
1626
|
+
const brand = Type.getOrmType(prop.type);
|
|
1627
|
+
const type = Utils.keys(t).find(type => {
|
|
1628
|
+
return (!Type.getType(t[type]).ensureComparable(meta, prop) && (prop.type === t[type] || brand === type));
|
|
1629
|
+
});
|
|
1630
|
+
if (type) {
|
|
1631
|
+
prop.type = type === 'datetime' ? 'Date' : type;
|
|
1632
|
+
}
|
|
1633
|
+
else {
|
|
1634
|
+
prop.customType = new prop.type();
|
|
1635
|
+
prop.type = prop.customType.constructor.name;
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
if (simple) {
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
if (!prop.customType && ['json', 'jsonb'].includes(prop.type?.toLowerCase())) {
|
|
1642
|
+
prop.customType = new t.json();
|
|
1643
|
+
}
|
|
1644
|
+
if (prop.kind === ReferenceKind.SCALAR &&
|
|
1645
|
+
!prop.customType &&
|
|
1646
|
+
prop.columnTypes &&
|
|
1647
|
+
['json', 'jsonb'].includes(prop.columnTypes[0])) {
|
|
1648
|
+
prop.customType = new t.json();
|
|
1649
|
+
}
|
|
1650
|
+
if (prop.kind === ReferenceKind.EMBEDDED && !prop.customType && (prop.object || prop.array)) {
|
|
1651
|
+
prop.customType = new t.json();
|
|
1652
|
+
}
|
|
1653
|
+
if (!prop.customType && prop.array && prop.items) {
|
|
1654
|
+
prop.customType = new t.enumArray(`${meta.className}.${prop.name}`, prop.items);
|
|
1655
|
+
}
|
|
1656
|
+
const isArray = prop.type?.toLowerCase() === 'array' || prop.type?.toString().endsWith('[]');
|
|
1657
|
+
if (objectEmbeddable && !prop.customType && isArray) {
|
|
1658
|
+
prop.customType = new t.json();
|
|
1659
|
+
}
|
|
1660
|
+
// for number arrays we make sure to convert the items to numbers
|
|
1661
|
+
if (!prop.customType && prop.type === 'number[]') {
|
|
1662
|
+
prop.customType = new t.array(i => +i);
|
|
1663
|
+
}
|
|
1664
|
+
// `string[]` can be returned via ts-morph, while reflect metadata will give us just `array`
|
|
1665
|
+
if (!prop.customType && isArray) {
|
|
1666
|
+
prop.customType = new t.array();
|
|
1667
|
+
}
|
|
1668
|
+
if (!prop.customType && prop.type?.toLowerCase() === 'buffer') {
|
|
1669
|
+
prop.customType = new t.blob();
|
|
1670
|
+
}
|
|
1671
|
+
if (!prop.customType && prop.type?.toLowerCase() === 'uint8array') {
|
|
1672
|
+
prop.customType = new t.uint8array();
|
|
1673
|
+
}
|
|
1674
|
+
const mappedType = this.getMappedType(prop);
|
|
1675
|
+
if (prop.fieldNames?.length === 1 && !prop.customType) {
|
|
1676
|
+
[t.bigint, t.double, t.decimal, t.interval, t.date]
|
|
1677
|
+
.filter(type => mappedType instanceof type)
|
|
1678
|
+
.forEach((type) => (prop.customType = new type()));
|
|
1679
|
+
}
|
|
1680
|
+
if (prop.customType && !prop.columnTypes) {
|
|
1681
|
+
const mappedType = this.getMappedType({
|
|
1682
|
+
columnTypes: [prop.customType.getColumnType(prop, this.#platform)],
|
|
1683
|
+
});
|
|
1684
|
+
if (prop.customType.compareAsType() === 'any' && ![t.json].some(t => prop.customType instanceof t)) {
|
|
1685
|
+
prop.runtimeType ??= mappedType.runtimeType;
|
|
1686
|
+
}
|
|
1687
|
+
else {
|
|
1688
|
+
prop.runtimeType ??= prop.customType.runtimeType;
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
else if (prop.runtimeType === 'object') {
|
|
1692
|
+
prop.runtimeType = mappedType.runtimeType;
|
|
1693
|
+
}
|
|
1694
|
+
else {
|
|
1695
|
+
prop.runtimeType ??= mappedType.runtimeType;
|
|
1696
|
+
}
|
|
1697
|
+
if (prop.customType) {
|
|
1698
|
+
prop.customType.platform = this.#platform;
|
|
1699
|
+
prop.customType.meta = meta;
|
|
1700
|
+
prop.customType.prop = prop;
|
|
1701
|
+
prop.columnTypes ??= [prop.customType.getColumnType(prop, this.#platform)];
|
|
1702
|
+
prop.hasConvertToJSValueSQL =
|
|
1703
|
+
!!prop.customType.convertToJSValueSQL && prop.customType.convertToJSValueSQL('', this.#platform) !== '';
|
|
1704
|
+
prop.hasConvertToDatabaseValueSQL =
|
|
1705
|
+
!!prop.customType.convertToDatabaseValueSQL &&
|
|
1706
|
+
prop.customType.convertToDatabaseValueSQL('', this.#platform) !== '';
|
|
1707
|
+
if (prop.customType instanceof t.bigint &&
|
|
1708
|
+
['string', 'bigint', 'number'].includes(prop.runtimeType.toLowerCase())) {
|
|
1709
|
+
prop.customType.mode = prop.runtimeType.toLowerCase();
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
if (Type.isMappedType(prop.customType) && prop.kind === ReferenceKind.SCALAR && !isArray) {
|
|
1713
|
+
prop.type = prop.customType.name;
|
|
1714
|
+
}
|
|
1715
|
+
if (!prop.customType &&
|
|
1716
|
+
[ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) &&
|
|
1717
|
+
!prop.polymorphic &&
|
|
1718
|
+
prop.targetMeta.compositePK) {
|
|
1719
|
+
prop.customTypes = [];
|
|
1720
|
+
for (const pk of prop.targetMeta.getPrimaryProps()) {
|
|
1721
|
+
if (pk.customType) {
|
|
1722
|
+
prop.customTypes.push(pk.customType);
|
|
1723
|
+
prop.hasConvertToJSValueSQL ||=
|
|
1724
|
+
!!pk.customType.convertToJSValueSQL && pk.customType.convertToJSValueSQL('', this.#platform) !== '';
|
|
1725
|
+
/* v8 ignore next */
|
|
1726
|
+
prop.hasConvertToDatabaseValueSQL ||=
|
|
1727
|
+
!!pk.customType.convertToDatabaseValueSQL &&
|
|
1728
|
+
pk.customType.convertToDatabaseValueSQL('', this.#platform) !== '';
|
|
1729
|
+
}
|
|
1730
|
+
else {
|
|
1731
|
+
prop.customTypes.push(undefined);
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
if (prop.kind === ReferenceKind.SCALAR && !(mappedType instanceof t.unknown)) {
|
|
1736
|
+
if (prop.nativeEnumName &&
|
|
1737
|
+
meta.schema !== this.#platform.getDefaultSchemaName() &&
|
|
1738
|
+
meta.schema &&
|
|
1739
|
+
!prop.nativeEnumName.includes('.')) {
|
|
1740
|
+
const suffix = prop.columnTypes?.[0]?.endsWith('[]') ? '[]' : '';
|
|
1741
|
+
prop.columnTypes = [`${meta.schema}.${prop.nativeEnumName}${suffix}`];
|
|
1742
|
+
}
|
|
1743
|
+
else {
|
|
1744
|
+
prop.columnTypes ??= [mappedType.getColumnType(prop, this.#platform)];
|
|
1745
|
+
}
|
|
1746
|
+
// use only custom types provided by user, we don't need to use the ones provided by ORM,
|
|
1747
|
+
// with exception for ArrayType and JsonType, those two are handled in
|
|
1748
|
+
if (!Object.values(t).some(type => type === mappedType.constructor)) {
|
|
1749
|
+
prop.customType ??= mappedType;
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
initRelation(prop) {
|
|
1754
|
+
if (prop.kind === ReferenceKind.SCALAR || prop.polymorphTargets) {
|
|
1755
|
+
return;
|
|
1756
|
+
}
|
|
1757
|
+
// when the target is a polymorphic embedded entity, `prop.target` is an array of classes, we need to get the metadata by the type name instead
|
|
1758
|
+
const meta2 = this.#metadata.find(prop.target) ?? this.#metadata.getByClassName(prop.type);
|
|
1759
|
+
// If targetKey is specified, use that property instead of PKs for referencedPKs
|
|
1760
|
+
prop.referencedPKs = prop.targetKey ? [prop.targetKey] : meta2.primaryKeys;
|
|
1761
|
+
prop.targetMeta = meta2;
|
|
1762
|
+
if (meta2.view) {
|
|
1763
|
+
prop.createForeignKeyConstraint = false;
|
|
1764
|
+
}
|
|
1765
|
+
// Auto-generate formula for persist: false relations, but only for single-column FKs
|
|
1766
|
+
// Composite FK relations need standard JOIN conditions, not formula-based
|
|
1767
|
+
if (!prop.formula &&
|
|
1768
|
+
prop.persist === false &&
|
|
1769
|
+
[ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
|
|
1770
|
+
!prop.embedded) {
|
|
1771
|
+
this.initFieldName(prop);
|
|
1772
|
+
if (prop.fieldNames?.length === 1) {
|
|
1773
|
+
prop.formula = table => `${table}.${this.#platform.quoteIdentifier(prop.fieldNames[0])}`;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
initColumnType(prop) {
|
|
1778
|
+
this.initUnsigned(prop);
|
|
1779
|
+
// Get the target properties for FK relations - use targetKey property if specified, otherwise PKs
|
|
1780
|
+
const targetProps = prop.targetMeta
|
|
1781
|
+
? prop.targetKey
|
|
1782
|
+
? [prop.targetMeta.properties[prop.targetKey]]
|
|
1783
|
+
: prop.targetMeta.getPrimaryProps()
|
|
1784
|
+
: [];
|
|
1785
|
+
targetProps.map(targetProp => {
|
|
1786
|
+
prop.length ??= targetProp.length;
|
|
1787
|
+
prop.precision ??= targetProp.precision;
|
|
1788
|
+
prop.scale ??= targetProp.scale;
|
|
1789
|
+
});
|
|
1790
|
+
if (prop.kind === ReferenceKind.SCALAR && (prop.type == null || prop.type === 'object') && prop.columnTypes?.[0]) {
|
|
1791
|
+
delete prop.type;
|
|
1792
|
+
const mappedType = this.getMappedType(prop);
|
|
1793
|
+
prop.type = mappedType.compareAsType();
|
|
1794
|
+
}
|
|
1795
|
+
if (prop.columnTypes || !this.#schemaHelper) {
|
|
1796
|
+
return;
|
|
1797
|
+
}
|
|
1798
|
+
if (prop.kind === ReferenceKind.SCALAR) {
|
|
1799
|
+
const mappedType = this.getMappedType(prop);
|
|
1800
|
+
const SCALAR_TYPES = ['string', 'number', 'boolean', 'bigint', 'Date', 'Buffer', 'RegExp', 'any', 'unknown'];
|
|
1801
|
+
if (mappedType instanceof t.unknown &&
|
|
1802
|
+
// it could be a runtime type from reflect-metadata
|
|
1803
|
+
!SCALAR_TYPES.includes(prop.type) &&
|
|
1804
|
+
// or it might be inferred via ts-morph to some generic type alias
|
|
1805
|
+
!/[<>:"';{}]/.exec(prop.type)) {
|
|
1806
|
+
const type = prop.length != null && !prop.type.endsWith(`(${prop.length})`) ? `${prop.type}(${prop.length})` : prop.type;
|
|
1807
|
+
prop.columnTypes = [type];
|
|
1808
|
+
}
|
|
1809
|
+
else {
|
|
1810
|
+
prop.columnTypes = [mappedType.getColumnType(prop, this.#platform)];
|
|
1811
|
+
}
|
|
1812
|
+
return;
|
|
1813
|
+
}
|
|
1814
|
+
/* v8 ignore next */
|
|
1815
|
+
if (prop.kind === ReferenceKind.EMBEDDED && prop.object) {
|
|
1816
|
+
prop.columnTypes = [this.#platform.getJsonDeclarationSQL()];
|
|
1817
|
+
return;
|
|
1818
|
+
}
|
|
1819
|
+
const targetMeta = prop.targetMeta;
|
|
1820
|
+
prop.columnTypes = [];
|
|
1821
|
+
// Use targetKey property if specified, otherwise use primary key properties
|
|
1822
|
+
const referencedProps = prop.targetKey ? [targetMeta.properties[prop.targetKey]] : targetMeta.getPrimaryProps();
|
|
1823
|
+
if (prop.polymorphic && prop.polymorphTargets) {
|
|
1824
|
+
prop.columnTypes.push(this.#platform.getVarcharTypeDeclarationSQL(prop));
|
|
1825
|
+
}
|
|
1826
|
+
for (const referencedProp of referencedProps) {
|
|
1827
|
+
this.initCustomType(targetMeta, referencedProp);
|
|
1828
|
+
this.initColumnType(referencedProp);
|
|
1829
|
+
const mappedType = this.getMappedType(referencedProp);
|
|
1830
|
+
let columnTypes = referencedProp.columnTypes;
|
|
1831
|
+
if (referencedProp.autoincrement) {
|
|
1832
|
+
columnTypes = [mappedType.getColumnType({ ...referencedProp, autoincrement: false }, this.#platform)];
|
|
1833
|
+
}
|
|
1834
|
+
prop.columnTypes.push(...columnTypes);
|
|
1835
|
+
if (!targetMeta.compositePK || prop.targetKey) {
|
|
1836
|
+
prop.customType = referencedProp.customType;
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
getMappedType(prop) {
|
|
1841
|
+
if (prop.customType) {
|
|
1842
|
+
return prop.customType;
|
|
1843
|
+
}
|
|
1844
|
+
/* v8 ignore next */
|
|
1845
|
+
let t = prop.columnTypes?.[0] ?? prop.type ?? '';
|
|
1846
|
+
if (prop.nativeEnumName) {
|
|
1847
|
+
t = 'enum';
|
|
1848
|
+
}
|
|
1849
|
+
else if (prop.enum) {
|
|
1850
|
+
t = prop.items?.every(item => typeof item === 'string') ? 'enum' : 'tinyint';
|
|
1851
|
+
}
|
|
1852
|
+
if (t === 'Date') {
|
|
1853
|
+
t = 'datetime';
|
|
1854
|
+
}
|
|
1855
|
+
return this.#platform.getMappedType(t);
|
|
1856
|
+
}
|
|
1857
|
+
getPrefix(prop, parent) {
|
|
1858
|
+
const { embeddedPath = [], fieldNames, prefix = true, prefixMode } = prop;
|
|
1859
|
+
if (prefix === true) {
|
|
1860
|
+
return (embeddedPath.length ? embeddedPath.join('_') : fieldNames[0]) + '_';
|
|
1861
|
+
}
|
|
1862
|
+
const prefixParent = parent ? this.getPrefix(parent, null) : '';
|
|
1863
|
+
if (prefix === false) {
|
|
1864
|
+
return prefixParent;
|
|
1865
|
+
}
|
|
1866
|
+
const mode = prefixMode ?? this.#config.get('embeddables').prefixMode;
|
|
1867
|
+
return mode === 'absolute' ? prefix : prefixParent + prefix;
|
|
1868
|
+
}
|
|
1869
|
+
initUnsigned(prop) {
|
|
1870
|
+
if (prop.unsigned != null) {
|
|
1871
|
+
return;
|
|
1872
|
+
}
|
|
1873
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
1874
|
+
prop.unsigned = prop.targetMeta.getPrimaryProps().some(pk => {
|
|
1875
|
+
this.initUnsigned(pk);
|
|
1876
|
+
return pk.unsigned;
|
|
1877
|
+
});
|
|
1878
|
+
return;
|
|
1879
|
+
}
|
|
1880
|
+
prop.unsigned ??=
|
|
1881
|
+
(prop.primary || prop.unsigned) && this.#platform.isNumericProperty(prop) && this.#platform.supportsUnsigned();
|
|
1882
|
+
}
|
|
1883
|
+
initIndexes(meta, prop) {
|
|
1884
|
+
const hasIndex = meta.indexes.some(idx => idx.properties?.length === 1 && idx.properties[0] === prop.name);
|
|
1885
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE && this.#platform.indexForeignKeys() && !hasIndex) {
|
|
1886
|
+
prop.index ??= true;
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
shouldForceConstructorUsage(meta) {
|
|
1890
|
+
const forceConstructor = this.#config.get('forceEntityConstructor');
|
|
1891
|
+
if (Array.isArray(forceConstructor)) {
|
|
1892
|
+
return forceConstructor.some(cls => Utils.className(cls) === meta.className);
|
|
1893
|
+
}
|
|
1894
|
+
return forceConstructor;
|
|
1895
|
+
}
|
|
1977
1896
|
}
|