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

Method setClass

packages/core/src/metadata/EntitySchema.ts:349–378  ·  view source on GitHub ↗

Sets or replaces the entity class associated with this schema.

(cls: Class)

Source from the content-addressed store, hash-verified

347
348 /** Sets or replaces the entity class associated with this schema. */
349 setClass(cls: Class) {
350 const oldClass = this._meta.class;
351 const sameClass = this._meta.class === cls;
352 this._meta.class = cls;
353 this._meta.prototype = cls.prototype;
354 this._meta.className = this._meta.name ?? cls.name;
355
356 if (!sameClass || !this._meta.constructorParams) {
357 this._meta.constructorParams = Utils.getConstructorParams(cls) as EntityKey<Entity>[];
358 }
359
360 if (!this.internal) {
361 // Remove old class from registry if it's being replaced with a different class
362 if (oldClass && oldClass !== cls && EntitySchema.REGISTRY.get(oldClass) === this) {
363 EntitySchema.REGISTRY.delete(oldClass);
364 }
365
366 EntitySchema.REGISTRY.set(cls, this);
367 }
368
369 const base = Object.getPrototypeOf(cls);
370
371 // Only set extends if the parent is NOT the auto-generated class for this same entity.
372 // When the user extends the auto-generated class (from defineEntity without a class option)
373 // and registers their custom class via setClass, we don't want to discover the
374 // auto-generated class as a separate parent entity.
375 if (base !== BaseEntity && base.name !== this._meta.className) {
376 this._meta.extends ??= base.name ? base : undefined;
377 }
378 }
379
380 /** Returns the underlying EntityMetadata. */
381 get meta(): EntityMetadata<Entity, Class> {

Calls 4

getConstructorParamsMethod · 0.80
getMethod · 0.65
deleteMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected