Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Instance<TDocument, TInstance>

The default Iridium Instance implementation which provides methods for saving, refreshing and removing the wrapped document from the collection, as well as integrating with Omnom, our built in document diff processor which allows clean, atomic, document updates to be performed without needing to write the update queries yourself.

Type parameters

  • TDocument

    The interface representing the structure of the documents in the collection.

  • TInstance

    The type of instance which wraps the documents, generally the subclass of this class.

    This class will be subclassed automatically by Iridium to create a model specific instance which takes advantage of some of v8's optimizations to boost performance significantly. The instance returned by the model, and all of this instance's methods, will be of type TInstance - which should represent the merger of TSchema and IInstance for best results.

Hierarchy

  • Instance

Index

Constructors

constructor

  • new Instance(model: Model<TDocument, TInstance>, document: TDocument, isNew?: boolean, isPartial?: boolean): Instance
  • Creates a new instance which represents the given document as a type of model

    Parameters

    • model: Model<TDocument, TInstance>

      The model that dictates the collection the document originated from as well as how validations are performed.

    • document: TDocument

      The document which should be wrapped by this instance

    • Default value isNew: boolean = true
    • Default value isPartial: boolean = false

    Returns Instance

Properties

Private _fieldCache

_fieldCache: FieldCache

Private _isNew

_isNew: boolean

Private _isPartial

_isPartial: boolean

Private _model

_model: Model<TDocument, TInstance>

Private _modified

_modified: TDocument

Private _original

_original: TDocument

Static cache

The cache director used to derive unique cache keys for documents of this type.

Static collection

collection: string

The name of the collection into which documents of this type are stored.

Static indexes

indexes: (IndexSpecification | Index)[] = []

The indexes which should be managed by Iridium for the collection used by this type.

Static Optional mapReduceOptions

mapReduceOptions: MapReduce.MapReduceFunctions<any, any, any>

mapReduce Options

Static onCreating

onCreating: function

A function which is called whenever a new document is in the process of being inserted into the database.

param

The document which will be inserted into the database.

Type declaration

    • (document: any): Promise<any> | PromiseLike<any> | void
    • Parameters

      • document: any

      Returns Promise<any> | PromiseLike<any> | void

Static onReady

onReady: function

A function which is called whenever a new instance has been created to wrap a document.

param

The instance which has been created.

Type declaration

    • (instance: Instance<any, Instance<any, any>>): Promise<any> | PromiseLike<any> | void
    • Parameters

      Returns Promise<any> | PromiseLike<any> | void

Static onRetrieved

onRetrieved: function

A function which is called whenever a document of this type is received from the database, prior to it being wrapped by an Instance object.

param

The document that was retrieved from the database.

Type declaration

    • (document: any): Promise<any> | PromiseLike<any> | void
    • Parameters

      • document: any

      Returns Promise<any> | PromiseLike<any> | void

Static onSaving

onSaving: function

A function which is called whenever an instance's save() method is called to allow you to interrogate and/or manipulate the changes which are being made.

param

The instance to which the changes are being made

param

The MongoDB change object describing the changes being made to the document.

Type declaration

Static renames

renames: RenameMap

The rename map which will be used to map code field names to DB fields

Static transforms

transforms: Transforms

The transformations which should be applied to properties of documents of this type.

Static validators

validators: Validator[] = DefaultValidators()

Additional which should be made available for use in the schema definition for this instance.

Accessors

document

  • get document(): TDocument
  • Gets the underlying document representation of this instance

    Returns TDocument

Methods

Protected _getField

  • _getField<K, V>(field: K): V
  • Type parameters

    • K: keyof TInstance

    • V: TInstance[K]

    Parameters

    • field: K

    Returns V

Protected _setField

  • _setField<K, V>(field: K, value: V): void
  • Type parameters

    • K: keyof TInstance

    • V: TInstance[K]

    Parameters

    • field: K
    • value: V

    Returns void

delete

  • delete(callback?: General.Callback<TInstance>): Promise<TInstance>
  • Removes this instance's document from the backing collection

    Parameters

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance>

first

  • first<T>(collection: T[], predicate: Predicate<this, T>): T | null
  • first<T>(collection: object, predicate: Predicate<this, T>): T | null
  • Retrieves the first element in an enumerable collection which matches the predicate

    Type parameters

    • T

    Parameters

    • collection: T[]

      The collection from which to retrieve the element

    • predicate: Predicate<this, T>

      The function which determines whether to select an element

    Returns T | null

    The first element in the array which matched the predicate.

  • Retrieves the first element in an enumerable collection which matches the predicate

    Type parameters

    • T

    Parameters

    • collection: object

      The collection from which to retrieve the element

      • [key: string]: T
    • predicate: Predicate<this, T>

      The function which determines whether to select an element

    Returns T | null

    The first element in the object which matched the predicate.

refresh

  • refresh(callback?: General.Callback<TInstance>): Promise<TInstance>
  • Updates this instance to match the latest document available in the backing collection

    Parameters

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the update completes

    Returns Promise<TInstance>

remove

  • remove(callback?: General.Callback<TInstance>): Promise<TInstance>
  • Removes this instance's document from the backing collection

    Parameters

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance>

save

  • save(callback?: General.Callback<TInstance>): Promise<TInstance>
  • save(changes: Changes, callback?: General.Callback<TInstance>): Promise<TInstance>
  • save(conditions: Conditions<TDocument>, changes: Changes, callback?: General.Callback<TInstance>): Promise<TInstance>
  • Saves any changes to this instance, using the built in diff algorithm to write the update query.

    Parameters

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the save operation completes

    Returns Promise<TInstance>

  • Saves the given changes to this instance and updates the instance to match the latest database document.

    Parameters

    • changes: Changes

      The MongoDB changes object to be used when updating this instance

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the save operation completes

    Returns Promise<TInstance>

  • Saves the given changes to this instance and updates the instance to match the latest database document.

    Parameters

    • conditions: Conditions<TDocument>

      The conditions under which the update will take place - these will be merged with an _id query

    • changes: Changes

      The MongoDB changes object to be used when updating this instance

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the save operation completes

    Returns Promise<TInstance>

select

  • select<T>(collection: T[], predicate: Predicate<this, T>): T[]
  • select<T>(collection: object, predicate: Predicate<this, T>): object
  • Retrieves a number of elements from an enumerable collection which match the predicate

    Type parameters

    • T

    Parameters

    • collection: T[]

      The collection from which elements will be plucked

    • predicate: Predicate<this, T>

      The function which determines the elements to be plucked

    Returns T[]

    A new array containing the elements in the array which matched the predicate.

  • Retrieves a number of elements from an enumerable collection which match the predicate

    Type parameters

    • T

    Parameters

    • collection: object

      The collection from which elements will be plucked

      • [key: string]: T
    • predicate: Predicate<this, T>

      The function which determines the elements to be plucked

    Returns object

    An object with the properties from the collection which matched the predicate.

    • [key: string]: T

toDB

  • toDB(): TDocument

toJSON

  • toJSON(): any

toString

  • toString(): string

update

  • update(callback?: General.Callback<TInstance>): Promise<TInstance>
  • Updates this instance to match the latest document available in the backing collection

    Parameters

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the update completes

    Returns Promise<TInstance>

Object literals

Static schema

schema: object

The schema used to validate documents of this type before being stored in the database.

_id

_id: boolean = false

Generated using TypeDoc