@mikro-orm/core 7.0.7-dev.9 → 7.0.7

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