import type { AddEager, AddOptional, Dictionary, EntityClass, EntityKey, EntityProperty, Loaded, LoadedReference, Primary, Ref } from '../typings.js'; import type { FindOneOptions, FindOneOrFailOptions } from '../drivers/IDatabaseDriver.js'; /** Wrapper around an entity that provides lazy loading capabilities and identity-preserving reference semantics. */ export declare class Reference { private entity; private property?; constructor(entity: T); /** Creates a Reference wrapper for the given entity, preserving identity if one already exists. */ static create(entity: T | Ref): Ref; /** Creates a Reference wrapper for an entity identified by its primary key, wrapped in a Ref. */ static createFromPK(entityType: EntityClass, pk: Primary, options?: { schema?: string; }): Ref; /** Creates an uninitialized entity reference by primary key without wrapping it in a Reference. */ static createNakedFromPK(entityType: EntityClass, pk: Primary, options?: { schema?: string; }): T; /** * Checks whether the argument is instance of `Reference` wrapper. */ static isReference(data: any): data is Reference; /** * Wraps the entity in a `Reference` wrapper if the property is defined as `ref`. */ static wrapReference(entity: T | Reference, prop: EntityProperty): Reference | T; /** * Returns wrapped entity. */ static unwrapReference(ref: T | Reference | ScalarReference | Ref): T; /** * Ensures the underlying entity is loaded first (without reloading it if it already is loaded). Returns the entity. * If the entity is not found in the database (e.g. it was deleted in the meantime, or currently active filters disallow loading of it) * the method returns `null`. Use `loadOrFail()` if you want an error to be thrown in such a case. */ load(options?: LoadReferenceOptions): Promise | null>; /** * Ensures the underlying entity is loaded first (without reloading it if it already is loaded). * Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options). */ loadOrFail(options?: LoadReferenceOrFailOptions): Promise>; private set; /** Returns the underlying entity without checking initialization state. */ unwrap(): T; /** Returns the underlying entity, throwing an error if the reference is not initialized. */ getEntity(): T; /** Returns the value of a property on the underlying entity. Throws if the reference is not initialized. */ getProperty(prop: K): T[K]; /** Loads the entity if needed, then returns the value of the specified property. */ loadProperty(prop: K, options?: LoadReferenceOrFailOptions): Promise[K]>; /** Returns whether the underlying entity has been fully loaded from the database. */ isInitialized(): boolean; /** Marks the underlying entity as populated or not for serialization purposes. */ populated(populated?: boolean): void; /** Serializes the underlying entity to a plain JSON object. */ toJSON(...args: any[]): Dictionary; } /** Wrapper for lazy scalar properties that provides on-demand loading from the database. */ export declare class ScalarReference { #private; private value?; private entity?; constructor(value?: Value | undefined, initialized?: boolean); /** * Ensures the underlying entity is loaded first (without reloading it if it already is loaded). * Returns either the whole entity, or the requested property. */ load(options?: Omit, 'populate' | 'fields' | 'exclude'>): Promise; /** * Ensures the underlying entity is loaded first (without reloading it if it already is loaded). * Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options). */ loadOrFail(options?: Omit, 'populate' | 'fields' | 'exclude'>): Promise; /** Sets the scalar value and marks the reference as initialized. */ set(value: Value): void; /** Binds this scalar reference to a specific entity and property for lazy loading support. */ bind(entity: Entity, property: EntityKey): void; /** Returns the current scalar value, or undefined if not yet loaded. */ unwrap(): Value | undefined; /** Returns whether the scalar value has been loaded. */ isInitialized(): boolean; static isScalarReference(data: any): data is ScalarReference; } /** Options for `Reference.load()` to control how the referenced entity is loaded. */ export interface LoadReferenceOptions extends FindOneOptions { /** Whether to use the dataloader for batching reference loads. */ dataloader?: boolean; } /** Options for `Reference.loadOrFail()` which throws when the entity is not found. */ export interface LoadReferenceOrFailOptions extends FindOneOrFailOptions { /** Whether to use the dataloader for batching reference loads. */ dataloader?: boolean; } /** * shortcut for `wrap(entity).toReference()` */ export declare function ref | undefined | null, T extends I & {}>(entity: I): (Ref & LoadedReference>>) | AddOptional; /** * shortcut for `Reference.createFromPK(entityType, pk)` */ export declare function ref = Primary>(entityType: EntityClass, pk: I): Ref | AddOptional; /** * shortcut for `Reference.createNakedFromPK(entityType, pk)` */ export declare function rel>(entityType: EntityClass, pk: T | PK): T; export { Reference as Ref };