Skip to main content
Version: 5.9

Collection <T, O>

Hierarchy

Index

Constructors

constructor

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

    • T: object
    • O: object = object

    Parameters

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

    Returns Collection<T, O>

Properties

readonlyowner

owner: O

Accessors

length

  • get length(): number
  • Returns number

Methods

[custom]

  • [custom](depth): string
  • Parameters

    • depth: number

    Returns string

[iterator]

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

add

  • add(entity, ...entities): void
  • Parameters

    Returns void

contains

  • contains(item, check): boolean
  • Parameters

    Returns boolean

count

  • count(): number
  • Returns number

exists

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


    Parameters

    • cb: (item) => boolean

      Returns boolean

    filter

    • filter(cb): T[]
    • Extracts a subset of the collection items.


      Parameters

      • cb: (item, index) => boolean

        Returns T[]

      find

      • find(cb): undefined | T
      • Returns the first element of this collection that satisfies the predicate.


        Parameters

        • cb: (item, index) => boolean

          Returns undefined | T

        getIdentifiers

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

          • U: IPrimaryKeyValue = Object

          Parameters

          • optionalfield: string

          Returns U[]

        getItems

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


          Parameters

          • check: boolean = true

          Returns T[]

        indexBy

        • indexBy<K1, K2>(key): Record<Object, T>
        • indexBy<K1, K2>(key, valueKey): Record<Object, 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.


          Type parameters

          • K1: string | number | symbol
          • K2: string | number | symbol = never

          Parameters

          • key: K1

          Returns Record<Object, T>

        init

        isDirty

        • isDirty(): boolean
        • Returns boolean

        isEmpty

        • isEmpty(): boolean
        • Returns boolean

        isInitialized

        • isInitialized(fully): boolean
        • Parameters

          • fully: boolean = false

          Returns boolean

        loadCount

        • loadCount(options): Promise<number>
        • 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

        • loadItems<P>(options): Promise<Loaded<T, P>[]>
        • Initializes the collection and returns the items


          Type parameters

          • P: string = never

          Parameters

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

        map

        • map<R>(mapper): R[]
        • Maps the collection items based on your provided mapper function.


          Type parameters

          • R

          Parameters

          • mapper: (item, index) => R

            Returns R[]

          matching

          • matching<P>(options): Promise<Loaded<T, P>[]>

          populated

          • populated(populated): void
          • Parameters

            • populated: boolean = true

            Returns void

          reduce

          • reduce<R>(cb, initial): R
          • Maps the collection items based on your provided mapper function to a single object.


            Type parameters

            • R

            Parameters

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

              Returns R

            remove

            • remove(entity, ...entities): 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

            removeAll

            • 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

            set

            • set(items): void
            • Parameters

              Returns void

            setDirty

            • setDirty(dirty): void
            • Parameters

              • dirty: boolean = true

              Returns void

            shouldPopulate

            • shouldPopulate(): boolean
            • Returns boolean

            slice

            • slice(start, end): 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[]

            toArray

            • Returns EntityDTO<T>[]

            toJSON

            • Returns EntityDTO<T>[]

            staticcreate

            • create<T, O>(owner, prop, items, initialized): 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: object
              • O: object = object

              Parameters

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

              Returns Collection<T, O>