Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Collection<TData, TNested, T>

Collection class that handles documents of same type

extends

LokiEventEmitter

Type parameters

  • TData: any

    the data type

  • TNested: any

    nested properties of data type

  • T: TData & TNested

Hierarchy

Index

Type aliases

Static Transform

Transform: object | object | object | object | object | object | object | object | object | object | object | object | object

Constructors

constructor

  • see

    {@link Loki#addCollection} for normal creation of collections

    Parameters

    • name: string

      collection name

    • Default value options: Options<TData, TNested> = {}

    Returns Collection

Properties

Protected _asyncListeners

_asyncListeners: boolean = false

Determines whether or not the callbacks associated with each event should happen in an async fashion or not. Default is false, which means events are synchronous

_cloneMethod

_cloneMethod: CloneMethod

Default clone method (if enabled) is parse-stringify.

_cloneObjects

_cloneObjects: boolean

Options to clone objects when inserting them.

_data

_data: Doc<T>[] = []

_defaultLokiOperatorPackage

_defaultLokiOperatorPackage: string = "js"

_dirty

_dirty: boolean = true

In autosave scenarios we will use collection level dirty flags to determine whether save is needed. currently, if any collection is dirty we will autosave the whole database if autosave is configured. Defaulting to true since this is called from addCollection and adding a collection should trigger save.

_disableDeltaChangesApi

_disableDeltaChangesApi: boolean

Disable delta update object style on changes.

Protected _events

_events: object

A map, with each property being an array of callbacks.

_fullTextSearch

_fullTextSearch: FullTextSearch

_lokimap

_lokimap: object

Type declaration

  • [$loki: number]: Doc<T>

_rangedIndexes

_rangedIndexes: object

Type declaration

_transforms

_transforms: Dict<Transform<T>[]>

Transforms will be used to store frequently used query chains as a series of steps which itself can be stored along with the database.

_unindexedSortComparator

_unindexedSortComparator: string = "js"

name

name: string

Methods

_prepareFullDocIndex

  • _prepareFullDocIndex(): number[]

addDynamicView

  • Add a dynamic view to the collection

    Parameters

    • name: string

      name of dynamic view to add

    • Optional options: Options

      (optional) options to configure dynamic view with

    Returns DynamicView<T>

    reference to the dynamic view added

addListener

  • addListener(eventName: string | string[], listener: Function): Function

addTransform

  • addTransform(name: string, transform: Transform<T>[]): void
  • Adds a named collection transform to the collection

    Parameters

    • name: string

      name to associate with transform

    • transform: Transform<T>[]

      an array of transformation 'step' objects to save into the collection

    Returns void

avg

  • avg(field: keyof T): number
  • Calculates the average numerical value of a field

    Parameters

    • field: keyof T

      the field name

    Returns number

    average of property in all docs in the collection

by

  • by(field: keyof T, value: any): Doc<T>
  • Retrieve doc by Unique index

    Parameters

    • field: keyof T

      name of uniquely indexed property to use when doing lookup

    • value: any

      unique value to search for

    Returns Doc<T>

    document matching the value passed

chain

  • Chain method, used for beginning a series of chained find() and/or view() operations on a collection.

    Parameters

    • Optional transform: string | Transform<T>[]

      Ordered array of transform step objects similar to chain

    • Optional parameters: object

      Object containing properties representing parameters to substitute

    Returns ResultSet<T>

    (this) ResultSet, or data array if any map or join functions where called

clear

  • clear(__namedParameters?: object): void
  • Empties the collection.

    Parameters

    • Default value __namedParameters: object = {}

    Returns void

commit

  • commit(): void

commitStage

  • commitStage(stageName: string, message: string): void
  • (Staging API) re-attach all objects to the original collection, so indexes and views can be rebuilt then create a message to be inserted in the commitlog

    Parameters

    • stageName: string

      name of stage

    • message: string

    Returns void

count

  • Quickly determine number of documents in collection (or query)

    Parameters

    • Optional query: Query<Doc<T>>

      (optional) query object to count results of

    Returns number

    number of documents in the collection

Protected emit

  • emit(eventName: string, ...data: any[]): void
  • Emits a particular event with the option of passing optional parameters which are going to be processed by the callback provided signatures match (i.e. if passing emit(event, arg0, arg1) the listener should take two parameters)

    Parameters

    • eventName: string

      the name of the event

    • Rest ...data: any[]

      optional object passed with the event

    Returns void

ensureIndex

  • ensureIndex(field: string, indexTypeName?: string, comparatorName?: string): void
  • Ensure rangedIndex of a field.

    Parameters

    • field: string
    • Optional indexTypeName: string
    • Optional comparatorName: string

    Returns void

ensureRangedIndex

  • ensureRangedIndex(field: string, indexTypeName?: string, comparatorName?: string): void
  • Ensure rangedIndex of a field.

    Parameters

    • field: string

      Property to create an index on (need to look into contraining on keyof T)

    • Optional indexTypeName: string

      Name of IndexType factory within (global?) hashmap to create IRangedIndex from

    • Optional comparatorName: string

      Name of Comparator within (global?) hashmap

    Returns void

ensureUniqueIndex

eqJoin

  • Join two collections on specified properties

    Parameters

    • joinData: Collection<any> | ResultSet<any> | any[]

      array of documents to 'join' to this collection

    • leftJoinProp: string | function

      property name in collection

    • rightJoinProp: string | function

      property name in joinData

    • Optional mapFun: function

      (Optional) map function to use

        • (left: any, right: any): any
        • Parameters

          • left: any
          • right: any

          Returns any

    • Optional dataOptions: DataOptions

      options to data() before input to your map function

    Returns ResultSet<any>

    Result of the mapping operation

extract

  • extract(field: keyof T): any[]
  • Returns all values of a field.

    Parameters

    • field: keyof T

      the field name

    Returns any[]

    : the array of values

extractNumerical

  • extractNumerical(field: keyof T): number[]
  • Returns all values of a field as numbers (if possible).

    Parameters

    • field: keyof T

      the field name

    Returns number[]

    • the number array

find

  • Find method, api is similar to mongodb. for more complex queries use [chain()]{@link Collection#chain} or [where()]{@link Collection#where}.

    example

    {@tutorial Query Examples}

    Parameters

    • Optional query: Query<Doc<T>>

      'mongo-like' query object

    Returns Doc<T>[]

    Array of matching documents

findAndRemove

  • findAndRemove(filterObject: Query<Doc<T>>): void
  • Applies a 'mongo-like' find query object removes all documents which match that filter.

    Parameters

    • filterObject: Query<Doc<T>>

      'mongo-like' query object

    Returns void

findAndUpdate

  • findAndUpdate(filterObject: Query<Doc<T>>, updateFunction: function): void
  • Applies a 'mongo-like' find query object and passes all results to an update function.

    Parameters

    • filterObject: Query<Doc<T>>

      the 'mongo-like' query object

    • updateFunction: function

      the update function

        • (obj: Doc<T>): any
        • Parameters

          Returns any

    Returns void

findOne

  • Find one object by index property, by property equal to value

    Parameters

    • query: Query<Doc<T>>

      query object used to perform search with

    Returns Doc<T>

    First matching document, or null if none

findOneUnindexed

  • findOneUnindexed(prop: string, value: any): T & object
  • Find object by unindexed field by property equal to value, simply iterates and returns the first element matching the query

    Parameters

    • prop: string
    • value: any

    Returns T & object

flushChanges

  • flushChanges(): void

get

  • get(id: number): Doc<T>
  • get(id: number, returnPosition: boolean): Doc<T> | [Doc<T>, number]
  • Get by Id - faster than other methods because of the searching algorithm

    Parameters

    • id: number

      $loki id of document you want to retrieve

    Returns Doc<T>

    Object reference if document was found, null if not, or an array if 'returnPosition' was passed.

  • Parameters

    • id: number
    • returnPosition: boolean

    Returns Doc<T> | [Doc<T>, number]

getChanges

getDynamicView

  • Look up dynamic view reference from within the collection

    Parameters

    • name: string

      name of dynamic view to retrieve reference of

    Returns DynamicView<T>

    A reference to the dynamic view with that name

getStage

  • getStage(name: string): any

getTransform

insert

  • insert(doc: TData): Doc<T>
  • insert(doc: TData[]): Doc<T>[]
  • Adds object(s) to collection, ensure object(s) have meta properties, clone it if necessary, etc.

    Parameters

    • doc: TData

      the document (or array of documents) to be inserted

    Returns Doc<T>

    document or documents inserted

  • Parameters

    • doc: TData[]

    Returns Doc<T>[]

insertOne

  • insertOne(doc: TData, bulkInsert?: boolean): Doc<T>
  • Adds a single object, ensures it has meta properties, clone it if necessary, etc.

    Parameters

    • doc: TData

      the document to be inserted

    • Default value bulkInsert: boolean = false

      quiet pre-insert and insert event emits

    Returns Doc<T>

    document or 'undefined' if there was a problem inserting it

mapReduce

  • mapReduce<U1, U2>(mapFunction: function, reduceFunction: function): U2
  • Map Reduce operation

    Type parameters

    • U1

    • U2

    Parameters

    • mapFunction: function

      function to use as map function

        • (value: Doc<T>, index: number, array: Doc<T>[]): U1
        • Parameters

          • value: Doc<T>
          • index: number
          • array: Doc<T>[]

          Returns U1

    • reduceFunction: function

      function to use as reduce function

        • (array: U1[]): U2
        • Parameters

          • array: U1[]

          Returns U2

    Returns U2

    The result of your mapReduce operation

max

  • max(field: keyof T): number
  • Finds the maximum value of a field.

    Parameters

    • field: keyof T

      the field name

    Returns number

    the maximum value

maxRecord

  • maxRecord(field: keyof T): object
  • Finds the maximum value and its index of a field.

    Parameters

    • field: keyof T

      the field name

    Returns object

    • index and value

median

  • median(field: keyof T): number
  • Calculates the median of a field.

    Parameters

    • field: keyof T

      the field name

    Returns number

    the median

min

  • min(field: keyof T): number
  • Finds the minimum value of a field.

    Parameters

    • field: keyof T

      the field name

    Returns number

    the minimum value

minRecord

  • minRecord(field: keyof T): object
  • Finds the minimum value and its index of a field.

    Parameters

    • field: keyof T

      the field name

    Returns object

    • index and value

mode

  • mode(field: keyof T): number
  • Calculates the mode of a field.

    Parameters

    • field: keyof T

      the field name

    Returns number

    the mode

on

  • on(eventName: string | string[], listener: Function): Function
  • Adds a listener to the queue of callbacks associated to an event

    Parameters

    • eventName: string | string[]

      the name(s) of the event(s) to listen to

    • listener: Function

      callback function of listener to attach

    Returns Function

    the index of the callback in the array of listeners for a particular event

remove

  • remove(doc: number | Doc<T> | Doc<T>[]): void
  • Remove a document from the collection

    Parameters

    • doc: number | Doc<T> | Doc<T>[]

      document to remove from collection

    Returns void

removeDataOnly

  • removeDataOnly(): void

removeDynamicView

  • removeDynamicView(name: string): void
  • Remove a dynamic view from the collection

    Parameters

    • name: string

      name of dynamic view to remove

    Returns void

removeListener

  • removeListener(eventName: string | string[], listener: Function): void
  • Removes the listener at position 'index' from the event 'eventName'

    Parameters

    • eventName: string | string[]

      the name(s) of the event(s) which the listener is attached to

    • listener: Function

      the listener callback function to remove from emitter

    Returns void

removeTransform

  • removeTransform(name: string): void
  • Removes a named collection transform from the collection

    Parameters

    • name: string

      name of collection transform to remove

    Returns void

removeWhere

  • removeWhere(filterFunction: function): void
  • Remove all documents matching supplied filter function.

    Parameters

    • filterFunction: function

      the filter function

        • (obj: Doc<T>): boolean
        • Parameters

          Returns boolean

    Returns void

rollback

  • rollback(): void

setChangesApi

  • setChangesApi(disableChangesApi: boolean, disableDeltaChangesApi?: boolean): void
  • Enables/disables changes api.

    Parameters

    • disableChangesApi: boolean
    • Default value disableDeltaChangesApi: boolean = true

    Returns void

setTransform

  • setTransform(name: string, transform: Transform<T>[]): void
  • Updates a named collection transform to the collection

    Parameters

    • name: string

      name to associate with transform

    • transform: Transform<T>[]

      a transformation object to save into collection

    Returns void

stage

  • stage<F>(stageName: string, obj: Doc<F>): F
  • (Staging API) create a copy of an object and insert it into a stage

    Type parameters

    • F: TData

    Parameters

    • stageName: string
    • obj: Doc<F>

    Returns F

startTransaction

  • startTransaction(): void

stdDev

  • stdDev(field: keyof T): number
  • Calculate the standard deviation of a field.

    Parameters

    • field: keyof T

      the field name

    Returns number

    the standard deviation

toJSON

update

  • update(doc: Doc<T> | Doc<T>[]): void
  • Updates an object and notifies collection that the document has changed.

    Parameters

    • doc: Doc<T> | Doc<T>[]

      document to update within the collection

    Returns void

updateWhere

  • updateWhere(filterFunction: function, updateFunction: function): void
  • Applies a filter function and passes all results to an update function.

    Parameters

    • filterFunction: function

      the filter function

        • (obj: Doc<T>): boolean
        • Parameters

          Returns boolean

    • updateFunction: function

      the update function

        • Parameters

          Returns Doc<T>

    Returns void

where

  • where(fun: function): Doc<T>[]
  • Query the collection by supplying a javascript filter function.

    example

    let results = coll.where(function(obj) { return obj.legs === 8; });

    Parameters

    • fun: function

      filter function to run against all collection docs

        • (obj: Doc<T>): boolean
        • Parameters

          Returns boolean

    Returns Doc<T>[]

    all documents which pass your filter function

Static fromJSONObject

Object literals

_constraints

_constraints: object

Unique constraints contain duplicate object references, so they are not persisted. We will keep track of properties which have unique constraints applied here, and regenerate on load.

unique

unique: object

Type declaration

_ttl

_ttl: object

Option to activate a cleaner daemon - clears "aged" documents at set intervals.

age

age: null = null

daemon

daemon: null = null

ttlInterval

ttlInterval: null = null

Generated using TypeDoc