import { Entity , IdOf } from "../EntityManager" ; import { CustomCollection } from "./CustomCollection" ; import { ManyToManyCollection } from "./ManyToManyCollection" ; import { OneToManyCollection } from "./OneToManyCollection" ; import { Relation } from "./Relation" ; /** A collection of `U` within `T`, either one-to-many or many-to-many. */ export interface Collection < T extends Entity , U extends Entity > extends Relation < T , U > { load ( opts ? ) : Promise < ReadonlyArray < U > > ; find ( id ) : Promise < U | undefined > ; add ( other ) : void ; remove ( other ) : void ; readonly isLoaded : boolean ; } /** Adds a known-safe `get` accessor. */ export interface LoadedCollection < T extends Entity , U extends Entity > extends Collection < T , U > { get : ReadonlyArray < U > ; getWithDeleted : ReadonlyArray < U > ; set ( values ) : void ; removeAll ( ) : void ; } /** Type guard utility for determining if an entity field is a Collection. */ export function isCollection ( maybeCollection ) { return ( maybeCollection instanceof OneToManyCollection || maybeCollection instanceof ManyToManyCollection || maybeCollection instanceof CustomCollection ) ; } /** Type guard utility for determining if an entity field is a loaded Collection. */ export function isLoadedCollection ( maybeCollection ) { return isCollection ( maybeCollection ) && maybeCollection . isLoaded ; }