Skip to main content
Version: 7.0

abstractBaseEntity

Base class for entities providing convenience methods like assign(), toObject(), and populate().

Index

Constructors

constructor

Methods

assign

  • assign<Entity, Naked, Convert, Data>(data, options): MergeSelected<Entity, Naked, keyof Data & string>
  • Assigns the given data to this entity, updating its properties and relations.


    Parameters

    Returns MergeSelected<Entity, Naked, keyof Data & string>

getSchema

  • getSchema(): undefined | string
  • Returns the database schema this entity belongs to.


    Returns undefined | string

init

  • init<Entity, Hint, Fields, Excludes>(options): Promise<null | Loaded<Entity, Hint, Fields, Excludes>>
  • Initializes (refreshes) the entity by reloading it from the database. Returns null if not found.


    Parameters

    Returns Promise<null | Loaded<Entity, Hint, Fields, Excludes>>

isInitialized

  • isInitialized(): boolean
  • Returns whether the entity has been fully loaded from the database.


    Returns boolean

populate

  • populate<Entity, Hint, Fields>(populate, options): Promise<Loaded<Entity, Hint>>
  • Loads the specified relations on this entity.


    Parameters

    Returns Promise<Loaded<Entity, Hint>>

populated

  • populated(populated): void
  • Marks the entity as populated or not for serialization purposes.


    Parameters

    • populated: boolean = true

    Returns void

serialize

  • serialize<Entity, Naked, Hint, Exclude>(options): SerializeDTO<Naked, Hint, Exclude, never>
  • Serializes the entity with control over which relations and fields to include or exclude.


    Parameters

    Returns SerializeDTO<Naked, Hint, Exclude, never>

setSchema

  • setSchema(schema): void
  • Sets the database schema for this entity.


    Parameters

    • optionalschema: string

    Returns void

toObject

  • toObject<Entity>(): EntityDTO<Entity>
  • toObject<Entity>(ignoreFields): EntityDTO<Entity>
  • toObject<Entity, Ignored>(ignoreFields): Omit<EntityDTO<Entity>, Ignored>
  • Converts the entity to a plain object representation.

    Note on typing with Loaded entities: When called on a Loaded<Entity, 'relation'> type, the return type will be EntityDTO<Entity> (with relations as primary keys), not EntityDTO<Loaded<Entity, 'relation'>> (with loaded relations as nested objects). This is a TypeScript limitation - the this type resolves to the class, not the Loaded wrapper.

    For correct typing that reflects loaded relations, use wrap():

    const result = await em.find(User, {}, { populate: ['profile'] });
    // Type: EntityDTO<User> (profile is number)
    const obj1 = result[0].toObject();
    // Type: EntityDTO<Loaded<User, 'profile'>> (profile is nested object)
    const obj2 = wrap(result[0]).toObject();

    Runtime values are correct in both cases - only the static types differ.


    Returns EntityDTO<Entity>

toPOJO

  • Converts the entity to a plain object, including all properties regardless of serialization rules.


    Returns EntityDTO<Entity>

toReference