MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / propagate

Method propagate

packages/core/src/entity/EntityHelper.ts:259–332  ·  view source on GitHub ↗
(
    meta: EntityMetadata<T>,
    entity: T,
    owner: T,
    prop: EntityProperty<T>,
    value?: T[keyof T & string],
    old?: T,
  )

Source from the content-addressed store, hash-verified

257 }
258
259 static propagate<T extends object>(
260 meta: EntityMetadata<T>,
261 entity: T,
262 owner: T,
263 prop: EntityProperty<T>,
264 value?: T[keyof T & string],
265 old?: T,
266 ): void {
267 // For polymorphic relations, get bidirectional relations from the actual entity's metadata
268 let bidirectionalRelations: EntityProperty<T>[];
269 if (prop.polymorphic && prop.polymorphTargets?.length) {
270 // For polymorphic relations, we need to get the bidirectional relations from the actual value's metadata
271 if (!value) {
272 return; // No value means no propagation needed
273 }
274
275 bidirectionalRelations = helper(value).__meta.bidirectionalRelations as EntityProperty<T>[];
276 } else {
277 bidirectionalRelations = prop.targetMeta!.bidirectionalRelations;
278 }
279
280 for (const prop2 of bidirectionalRelations) {
281 if ((prop2.inversedBy || prop2.mappedBy) !== prop.name) {
282 continue;
283 }
284
285 // oxfmt-ignore
286 if (prop2.targetMeta!.abstract ? prop2.targetMeta!.root.class !== meta.root.class : prop2.targetMeta!.class !== meta.class) {
287 continue;
288 }
289
290 const inverse = value?.[prop2.name as never] as EntityValue<T>;
291
292 if (prop.ref && owner[prop.name]) {
293 // eslint-disable-next-line dot-notation
294 (owner[prop.name] as Reference<T>)['property'] = prop;
295 }
296
297 if (Utils.isCollection(inverse) && inverse.isPartial()) {
298 continue;
299 }
300
301 if (prop.kind === ReferenceKind.MANY_TO_ONE && Utils.isCollection<T, T>(inverse) && inverse.isInitialized()) {
302 inverse.addWithoutPropagation(owner);
303 helper(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
304 }
305
306 if (prop.kind === ReferenceKind.ONE_TO_ONE) {
307 if (
308 (value != null && Reference.unwrapReference(inverse!) !== owner) ||
309 (value == null && entity?.[prop2.name as EntityKey<T>] != null)
310 ) {
311 if (entity && (!prop.owner || helper(entity).__initialized)) {
312 EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old as T);
313 }
314 } else if (old && old !== value) {
315 // Inverse already points to owner — propagation is not needed,
316 // but we still need to clean up old's inverse side.

Callers 1

setMethod · 0.45

Calls 11

helperFunction · 0.85
isPartialMethod · 0.80
addWithoutPropagationMethod · 0.80
getUnitOfWorkMethod · 0.80
unwrapReferenceMethod · 0.80
propagateOneToOneMethod · 0.80
scheduleOrphanRemovalMethod · 0.80
isInitializedMethod · 0.65
getPrimaryKeyMethod · 0.65
isCollectionMethod · 0.45
cancelOrphanRemovalMethod · 0.45

Tested by

no test coverage detected