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

Method createEntity

packages/core/src/entity/EntityFactory.ts:406–465  ·  view source on GitHub ↗
(data: EntityData<T>, meta: EntityMetadata<T>, options: FactoryOptions)

Source from the content-addressed store, hash-verified

404 }
405
406 private createEntity<T extends object>(data: EntityData<T>, meta: EntityMetadata<T>, options: FactoryOptions): T {
407 const schema = this.#driver.getSchemaName(meta, options);
408
409 if (options.newEntity || meta.forceConstructor || meta.virtual) {
410 if (meta.polymorphs) {
411 throw new Error(`Cannot create entity ${meta.className}, class prototype is unknown`);
412 }
413
414 const params = this.extractConstructorParams<T>(meta, data, options);
415 const Entity = meta.class as Constructor<T>;
416
417 // creates new instance via constructor as this is the new entity
418 const entity = new Entity(...params);
419
420 // creating managed entity instance when `forceEntityConstructor` is enabled,
421 // we need to wipe all the values as they would cause update queries on next flush
422 if (!options.newEntity && (meta.forceConstructor || this.#config.get('forceEntityConstructor'))) {
423 meta.props
424 .filter(prop => prop.persist !== false && !prop.primary && data[prop.name] === undefined)
425 .forEach(prop => delete entity[prop.name]);
426 }
427
428 if (meta.virtual) {
429 return entity;
430 }
431
432 helper(entity).__schema = schema;
433
434 if (options.initialized) {
435 EntityHelper.ensurePropagation(entity);
436 }
437
438 return entity;
439 }
440
441 // creates new entity instance, bypassing constructor call as its already persisted entity
442 const entity = Object.create(meta.class.prototype) as T;
443 helper(entity).__managed = true;
444 helper(entity).__processing = !meta.embeddable && !meta.virtual;
445 helper(entity).__schema = schema;
446
447 if (options.merge && !options.newEntity) {
448 this.#hydrator.hydrateReference(
449 entity,
450 meta,
451 data,
452 this,
453 options.convertCustomTypes,
454 options.schema,
455 options.parentSchema,
456 );
457 this.unitOfWork.register(entity);
458 }
459
460 if (options.initialized) {
461 EntityHelper.ensurePropagation(entity);
462 }
463

Callers 2

createMethod · 0.95
createEmbeddableMethod · 0.95

Calls 9

helperFunction · 0.85
filterMethod · 0.80
ensurePropagationMethod · 0.80
getSchemaNameMethod · 0.65
getMethod · 0.65
createMethod · 0.65
hydrateReferenceMethod · 0.65
registerMethod · 0.45

Tested by

no test coverage detected