Skip to main content
Version: Next

Loadable

Callable


  • Mixin that adds load() / loadOrFail() methods to an entity class. These methods ensure the entity is loaded from the database without reloading it if it already is — unlike init(), which always refreshes.

    Useful when migrating from a non-Ref-based codebase where lazy loading support is desired without the .$ / .get() indirection that the Reference wrapper requires. Opt-in so it does not conflict with entities that already define a load or loadOrFail property — applying the mixin to a base class that already has either method is a compile error to prevent silent override.

    Call without arguments (Loadable()) for a standalone base with no other inheritance, or pass a base class (Loadable(BaseEntity)) to compose. The convenience alias LoadableBaseEntity is shorthand for the latter.

    @example
    // compose with BaseEntity
    class User extends Loadable(BaseEntity) {
    @PrimaryKey()
    id!: number;
    }

    // standalone — no inherited base
    class Product extends Loadable() {
    @PrimaryKey()
    id!: number;
    }

    const user = orm.em.getReference(User, 1);
    await user.load();

    Returns LoadableConstructor<typeof EmptyBase> & typeof EmptyBase