Skip to main content
Version: Next

Collection <T, O>

Hierarchy

Index

Constructors

constructor

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

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

    Returns Collection<T, O>

Properties

readonlyinheritedowner

owner: O

Accessors

inheritedlength

  • get length(): number
  • Returns number

Methods

inherited[iterator]

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

add

  • Parameters

    Returns void

contains

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

    • item: TT | Reference<TT>
    • check: boolean = true

    Returns boolean

count

  • count(): number
  • Returns number

exists

  • exists(cb: (item: T) => boolean): boolean
  • Tests for the existence of an element that satisfies the given predicate.


    Parameters

    • cb: (item: T) => boolean

      Returns boolean

    filter

    • filter(cb: (item: T, index: number) => boolean): T[]
    • Extracts a subset of the collection items.


      Parameters

      • cb: (item: T, index: number) => boolean

        Returns T[]

      find

      • find(cb: (item: T, index: number) => boolean): undefined | T
      • Returns the first element of this collection that satisfies the predicate.


        Parameters

        • cb: (item: T, index: number) => boolean

          Returns undefined | T

        inheritedgetIdentifiers

        • getIdentifiers(field?: string): U[]
        • Parameters

          • optionalfield: string

          Returns U[]

        getItems

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


          Parameters

          • check: boolean = true

          Returns T[]

        indexBy

        • indexBy(key: K1): Record<T[K1] & PropertyKey, T>
        • indexBy(key: K1, valueKey: K2): Record<T[K1] & PropertyKey, T[K2]>
        • Maps the collection items to a dictionary, indexed by the key you specify. If there are more items with the same key, only the first one will be present.


          Parameters

          • key: K1

          Returns Record<T[K1] & PropertyKey, T>

        init

        inheritedisDirty

        • isDirty(): boolean
        • Returns boolean

        isEmpty

        • isEmpty(): boolean
        • Returns boolean

        inheritedisInitialized

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

          • fully: boolean = false

          Returns boolean

        load

        • Ensures the collection is loaded first (without reloading it if it already is loaded). Returns the Collection instance (itself), works the same as Reference.load().


          Parameters

          Returns Promise<LoadedCollection<Loaded<TT, P>>>

        loadCount

        • Gets the count of collection items from database instead of counting loaded items. The value is cached (unless you use the where option), use refresh: true to force reload it.


          Parameters

          Returns Promise<number>

        loadItems

        • Initializes the collection and returns the items


          Parameters

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

        map

        • map(mapper: (item: T, index: number) => R): R[]
        • Maps the collection items based on your provided mapper function.


          Parameters

          • mapper: (item: T, index: number) => R

            Returns R[]

          matching

          populated

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

            • populated: undefined | boolean = true

            Returns void

          inheritedreduce

          • reduce(cb: (obj: R, item: T, index: number) => R, initial?: R): R
          • Maps the collection items based on your provided mapper function to a single object.


            Parameters

            • cb: (obj: R, item: T, index: number) => R
              • initial: R = ...

              Returns R

            remove

            • remove(entity: TT | Reference<TT> | Iterable<TT | Reference<TT>, any, any> | (item: TT) => boolean, ...entities: (TT | Reference<TT>)[]): void
            • Remove specified item(s) from the collection. Note that removing item from collection does not 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

              Returns void

            inheritedremoveAll

            • removeAll(): void
            • Remove all items from the collection. Note that removing items from collection does not 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

            inheritedset

            • set(items: Iterable<T | Reference<T>, any, any>): void
            • Parameters

              Returns void

            inheritedsetDirty

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

              • dirty: boolean = true

              Returns void

            shouldPopulate

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

              • optionalpopulated: boolean

              Returns boolean

            slice

            • slice(start?: number, end?: number): T[]
            • Extracts a slice of the collection items starting at position start to end (exclusive) of the collection. If end is null it returns all elements from start to the end of the collection.


              Parameters

              • optionalstart: number
              • optionalend: number

              Returns T[]

            inheritedtoArray

            • Returns EntityDTO<TT>[]

            toJSON

            • Returns EntityDTO<TT>[]

            staticcreate

            • create(owner: O, prop: EntityKey<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)


              Parameters

              • owner: O
              • prop: EntityKey<O>
              • items: undefined | T[]
              • initialized: boolean

              Returns Collection<T, O>