import { Entity , IdOf } from "../EntityManager" ; import { CustomReference } from "./CustomReference" ; import { ManyToOneReferenceImpl } from "./ManyToOneReference" ; import { OneToOneReferenceImpl } from "./OneToOneReference" ; import { PolymorphicReferenceImpl } from "./PolymorphicReference" ; import { Relation } from "./Relation" ; export const ReferenceN = Symbol ( ) ; /**
 * A many-to-one / foreign key from `T` to `U`, i.e. book to author.
 *
 * The `N` generic is for whether the field is optional (i.e. the foreign key column is
 * nullable). If it is optional, `N` will be `undefined`, which makes the return types
 * `U | undefined`. If it is not optional, `N` will be `never`, making the return types
 * `U | never` which becomes just `U`.
 */ export interface Reference < T extends Entity , U extends Entity , N extends never | undefined > extends Relation < T , U > { readonly isLoaded : boolean ; load ( opts ? ) : Promise < U | N > ; set ( other ) : void ; [ ReferenceN ] : N ; } /** Adds a known-safe `get` accessor. */ export interface LoadedReference < T extends Entity , U extends Entity , N extends never | undefined > extends Reference < T , U , N > { id : IdOf < U > | N ; get : U | N ; getWithDeleted : U | N ; } /** Type guard utility for determining if an entity field is a Reference. */ export function isReference ( maybeReference ) { return ( maybeReference instanceof OneToOneReferenceImpl || maybeReference instanceof ManyToOneReferenceImpl || maybeReference instanceof CustomReference || maybeReference instanceof PolymorphicReferenceImpl ) ; } /** Type guard utility for determining if an entity field is a loaded Reference. */ export function isLoadedReference ( maybeReference ) { return isReference ( maybeReference ) && maybeReference . isLoaded ; }