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

Function resetUntouchedCollections

packages/core/src/utils/upsert-utils.ts:210–231  ·  view source on GitHub ↗
(meta: EntityMetadata<Entity>, entity: Entity)

Source from the content-addressed store, hash-verified

208
209/** @internal */
210export function resetUntouchedCollections<Entity extends object>(meta: EntityMetadata<Entity>, entity: Entity): void {
211 // for entities passed in via `em.create()` and then upserted, collection-kind relations
212 // were initialized as empty-but-initialized by the hydrator (newEntity=true). once the
213 // upsert resolves the entity to a possibly existing row, that state is stale and would
214 // cause `em.populate()` to skip those collections. replace them with a fresh
215 // uninitialized collection so populate can load them.
216 if (helper(entity).__originalEntityData) {
217 return;
218 }
219
220 for (const prop of meta.relations) {
221 if (prop.kind !== ReferenceKind.MANY_TO_MANY && prop.kind !== ReferenceKind.ONE_TO_MANY) {
222 continue;
223 }
224
225 const collection = entity[prop.name];
226
227 if (Utils.isCollection(collection) && collection.isInitialized() && !collection.isDirty()) {
228 Collection.create(entity, prop.name, undefined, false);
229 }
230 }
231}

Callers 2

upsertMethod · 0.85
upsertManyMethod · 0.85

Calls 5

helperFunction · 0.85
isDirtyMethod · 0.80
isInitializedMethod · 0.65
createMethod · 0.65
isCollectionMethod · 0.45

Tested by

no test coverage detected