Skip to main content
Version: 5.2

Collection <T, O>

Hierarchy

Index

Constructors

constructor

  • new Collection<T, O>(owner: O, items?: T[], initialized?: boolean): Collection<T, O>
  • Type parameters

    • T
    • O = unknown

    Parameters

    • owner: O
    • optionalitems: T[]
    • initialized: boolean = true

    Returns Collection<T, O>

Properties

readonlyowner

owner: O & Partial<O> & { [EntityRepositoryType]?: unknown; [PrimaryKeyType]?: unknown; __helper?: IWrappedEntityInternal<O, keyof O, string>; __meta?: EntityMetadata<O>; __platform?: Platform }

Accessors

length

  • get length(): number
  • Returns number

Methods

[custom]

  • [custom](depth: number): string
  • Parameters

    • depth: number

    Returns string

[iterator]

  • [iterator](): IterableIterator<T>
  • Returns IterableIterator<T>

add

  • Parameters

    Returns void

contains

  • contains(item: T | Reference<T>, check?: boolean): boolean
  • Parameters

    Returns boolean

count

  • count(): number
  • Returns number

getIdentifiers

  • getIdentifiers<U>(field?: string): U[]
  • Type parameters

    Parameters

    • optionalfield: string

    Returns U[]

getItems

  • getItems(check?: boolean): T[]
  • Returns the items (the collection must be initialized)


    Parameters

    • check: boolean = true

    Returns T[]

init

isDirty

  • isDirty(): boolean
  • Returns boolean

isInitialized

  • isInitialized(fully?: boolean): boolean
  • Parameters

    • fully: boolean = false

    Returns boolean

loadCount

  • loadCount(refresh?: boolean): Promise<number>
  • Gets the count of collection items from database instead of counting loaded items. The value is cached, use refresh = true to force reload it.


    Parameters

    • refresh: boolean = false

    Returns Promise<number>

loadItems

  • Initializes the collection and returns the items


    Type parameters

    • P: string = never

    Parameters

    Returns Promise<Loaded<T, P>[]>

matching

populated

  • populated(populated?: boolean): void
  • Parameters

    • populated: boolean = true

    Returns void

remove

  • remove(...items: (T | Reference<T> | (item: T) => boolean)[]): void
  • Remove specified item(s) from the collection. Note that removing item from collection does necessarily imply deleting the target entity, it means we are disconnecting the relation - removing items from collection, not removing entities from database - Collection.remove() is not the same as em.remove(). If we want to delete the entity by removing it from collection, we need to enable orphanRemoval: true, which tells the ORM we don’t want orphaned entities to exist, so we know those should be removed.


    Parameters

    • rest...items: (T | Reference<T> | (item: T) => boolean)[]

    Returns void

removeAll

  • removeAll(): void
  • Remove all items from the collection. Note that removing items from collection does necessarily imply deleting the target entity, it means we are disconnecting the relation - removing items from collection, not removing entities from database - Collection.remove() is not the same as em.remove(). If we want to delete the entity by removing it from collection, we need to enable orphanRemoval: true, which tells the ORM we don’t want orphaned entities to exist, so we know those should be removed.


    Returns void

set

  • Parameters

    Returns void

setDirty

  • setDirty(dirty?: boolean): void
  • Parameters

    • dirty: boolean = true

    Returns void

shouldPopulate

  • shouldPopulate(): boolean
  • Returns boolean

toArray

  • Returns EntityDTO<T>[]

toJSON

  • Returns EntityDTO<T>[]

staticcreate

  • create<T, O>(owner: O, prop: keyof O, items: undefined | T[], initialized: boolean): Collection<T, O>
  • Creates new Collection instance, assigns it to the owning entity and sets the items to it (propagating them to their inverse sides)


    Type parameters

    • T
    • O = any

    Parameters

    • owner: O
    • prop: keyof O
    • items: undefined | T[]
    • initialized: boolean

    Returns Collection<T, O>