Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Model<TDocument, TInstance>

An Iridium Model which represents a structured MongoDB collection. Models expose the methods you will generally use to query those collections, and ensure that the results of those queries are returned as {TInstance} instances.

class

Type parameters

  • TDocument

    The interface used to determine the schema of documents in the collection.

  • TInstance

    The interface or class used to represent collection documents in the JS world.

Hierarchy

  • Model

Index

Constructors

constructor

  • Creates a new Iridium model representing a given ISchema and backed by a collection whose name is specified

    constructor

    Parameters

    • core: Core

      The Iridium core that this model should use for database access

    • instanceType: InstanceImplementation<TDocument, TInstance>

      The class which will be instantiated for each document retrieved from the database

    Returns Model

Properties

Private _Instance

_Instance: ModelSpecificInstanceConstructor<TDocument, TInstance>

Private _cache

_cache: ModelCache

Private _cacheDirector

_cacheDirector: CacheDirector

Private _collection

_collection: string

Private _core

_core: Core

Private _handlers

_handlers: ModelHandlers<TDocument, TInstance>

Private _helpers

_helpers: ModelHelpers<TDocument, TInstance>

Private _hooks

_hooks: Hooks<TDocument, TInstance>

Private _indexes

_indexes: (IndexSpecification | Index)[]

Private _renames

_renames: RenameMap

Private _schema

_schema: Schema

Private _transforms

_transforms: Transforms

Private _validators

_validators: Validator[]

Accessors

Instance

cache

  • Gets the cache responsible for storing objects for quick retrieval under certain conditions

    Returns ModelCache

cacheDirector

collection

  • get collection(): Collection
  • Gets the underlying MongoDB collection from which this model's documents are retrieved. You can make use of this object if you require any low level access to the MongoDB collection, however we recommend you make use of the Iridium methods whereever possible, as we cannot guarantee the accuracy of the type definitions for the underlying MongoDB driver.

    Returns Collection

collectionName

  • get collectionName(): string
  • set collectionName(value: string): void
  • Gets the name of the underlying MongoDB collection from which this model's documents are retrieved

    Returns string

  • Sets the name of the underlying MongoDB collection from which this model's documents are retrieved

    Parameters

    • value: string

    Returns void

core

  • Gets the Iridium core that this model is associated with.

    Returns Core

    The Iridium core that this model is bound to

handlers

  • Provides helper methods used by Iridium for hook delegation and common processes

    Returns ModelHandlers<TDocument, TInstance>

    A set of helper methods which perform common event and response handling tasks within Iridium.

helpers

  • Provides helper methods used by Iridium for common tasks

    Returns ModelHelpers<TDocument, TInstance>

    A set of helper methods which are used within Iridium for common tasks

hooks

  • get hooks(): Hooks<TDocument, TInstance>
  • Gets the even hooks subscribed on this model for a number of different state changes. These hooks are primarily intended to allow lifecycle manipulation logic to be added in the user's model definition, allowing tasks such as the setting of default values or automatic client-side joins to take place.

    Returns Hooks<TDocument, TInstance>

indexes

renames

  • Gets the renamed fields for this model, which will result in the field names used in your code being different to those used in the database.

    Returns RenameMap

schema

  • Gets the schema dictating the data structure represented by this model. The schema is used by skmatc to validate documents before saving to the database, however until MongoDB 3.1 becomes widely available (with server side validation support) we are limited in our ability to validate certain types of updates. As such, these validations act more as a data-integrity check than anything else, unless you purely make use of Omnom updates within instances.

    Returns Schema

    The defined validation schema for this model

transforms

  • Gets the transforms which are applied whenever a document is received from the database, or prior to storing a document in the database. Tasks such as converting an ObjectID to a string and vice versa are all listed in this object.

    Returns Transforms

validators

  • get validators(): Validator[]
  • Gets the custom validation types available for this model. These validators are added to the default skmatc validators, as well as those available through plugins, for use when checking your instances.

    Returns Validator[]

Methods

aggregate

  • aggregate<T>(pipeline: Stage[], options?: AggregationPipeline.Options): Promise<T[]>
  • Runs an aggregate operation in MongoDB and returns the contents of the resulting aggregation

    Type parameters

    • T

    Parameters

    • pipeline: Stage[]

      The list of aggregation pipeline stages to be executed for this aggregation

    • Optional options: AggregationPipeline.Options

    Returns Promise<T[]>

    A promise which completes with the results of the aggregation task

count

  • count(callback?: General.Callback<number>): Promise<number>
  • count(conditions: object | Conditions<TDocument> | string, callback?: General.Callback<number>): Promise<number>
  • Counts the number of documents in the collection

    Parameters

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

  • Counts the number of documents in the collection which match the conditions provided

    Parameters

    • conditions: object | Conditions<TDocument> | string

      The conditions which determine whether an object is counted or not

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

create

  • create(object: InsertionDocument<TDocument>, callback?: General.Callback<TInstance>): Promise<TInstance>
  • create(object: InsertionDocument<TDocument>, options: CreateOptions, callback?: General.Callback<TInstance>): Promise<TInstance>
  • create(objects: InsertionDocument<TDocument>[], callback?: General.Callback<TInstance[]>): Promise<TInstance[]>
  • create(objects: InsertionDocument<TDocument>[], options: CreateOptions, callback?: General.Callback<TInstance[]>): Promise<TInstance[]>
  • Inserts an object into the collection after validating it against this model's schema

    Parameters

    • object: InsertionDocument<TDocument>

      The object to insert into the collection

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance>

  • Inserts an object into the collection after validating it against this model's schema

    Parameters

    • object: InsertionDocument<TDocument>

      The object to insert into the collection

    • options: CreateOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance>

  • Inserts the objects into the collection after validating them against this model's schema

    Parameters

    • objects: InsertionDocument<TDocument>[]

      The objects to insert into the collection

    • Optional callback: General.Callback<TInstance[]>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance[]>

  • Inserts the objects into the collection after validating them against this model's schema

    Parameters

    • objects: InsertionDocument<TDocument>[]

      The objects to insert into the collection

    • options: CreateOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance[]>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance[]>

dropIndex

  • dropIndex(name: string, callback?: General.Callback<boolean>): Promise<boolean>
  • dropIndex(index: IndexSpecification, callback?: General.Callback<boolean>): Promise<boolean>
  • Drops the index with the specified name if it exists in the collection

    Parameters

    • name: string

      The name of the index to remove

    • Optional callback: General.Callback<boolean>

      A callback which is triggered when the operation completes

    Returns Promise<boolean>

    Whether the index was dropped

  • Drops the index if it exists in the collection

    Parameters

    • index: IndexSpecification

      The index to remove

    • Optional callback: General.Callback<boolean>

      A callback which is triggered when the operation completes

    Returns Promise<boolean>

    Whether the index was dropped

dropIndexes

  • dropIndexes(callback?: General.Callback<boolean>): Promise<boolean>
  • Removes all indexes (except for _id) from the collection

    Parameters

    • Optional callback: General.Callback<boolean>

      A callback which is triggered when the operation completes

    Returns Promise<boolean>

    Whether the indexes were dropped

ensureIndex

  • ensureIndex(specification: IndexSpecification, callback?: General.Callback<string>): Promise<string>
  • ensureIndex(specification: IndexSpecification, options: IndexOptions, callback?: General.Callback<string>): Promise<string>
  • Ensures that the given index is created for the collection

    Parameters

    • specification: IndexSpecification

      The index specification object used by MongoDB

    • Optional callback: General.Callback<string>

      A callback which is triggered when the operation completes

    Returns Promise<string>

    The name of the index

  • Ensures that the given index is created for the collection

    Parameters

    • specification: IndexSpecification

      The index specification object used by MongoDB

    • options: IndexOptions

      The options dictating how the index is created and behaves

    • Optional callback: General.Callback<string>

      A callback which is triggered when the operation completes

    Returns Promise<string>

    The name of the index

ensureIndexes

  • ensureIndexes(callback?: General.Callback<string[]>): Promise<string[]>
  • Ensures that all indexes defined in the model's options are created

    Parameters

    • Optional callback: General.Callback<string[]>

      A callback which is triggered when the operation completes

    Returns Promise<string[]>

    The names of the indexes

find

  • find(): Cursor<TDocument, TInstance>
  • find(conditions: object | Conditions<TDocument> | string): Cursor<TDocument, TInstance>
  • find(conditions: object | Conditions<TDocument> | string, fields: object): Cursor<TDocument, TInstance>
  • Retrieves all documents in the collection and wraps them as instances

    Returns Cursor<TDocument, TInstance>

  • Returns all documents in the collection which match the conditions and wraps them as instances

    Parameters

    • conditions: object | Conditions<TDocument> | string

      The MongoDB query dictating which documents to return

    Returns Cursor<TDocument, TInstance>

  • Returns all documents in the collection which match the conditions

    Parameters

    • conditions: object | Conditions<TDocument> | string

      The MongoDB query dictating which documents to return

    • fields: object

      The fields to include or exclude from the document

      • [name: string]: number

    Returns Cursor<TDocument, TInstance>

findOne

  • findOne(callback?: General.Callback<TInstance>): Promise<TInstance | null>
  • findOne(id: string, callback?: General.Callback<TInstance>): Promise<TInstance | null>
  • findOne(conditions: object | Conditions<TDocument>, callback?: General.Callback<TInstance>): Promise<TInstance | null>
  • findOne(id: string, options: QueryOptions, callback?: General.Callback<TInstance>): Promise<TInstance | null>
  • findOne(conditions: object | Conditions<TDocument>, options: QueryOptions, callback?: General.Callback<TInstance>): Promise<TInstance | null>
  • Retrieves a single document from the collection and wraps it as an instance

    Parameters

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance | null>

  • Retrieves a single document from the collection with the given ID and wraps it as an instance

    Parameters

    • id: string

      The document's unique _id field value in downstream format

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance | null>

  • Retrieves a single document from the collection which matches the conditions

    Parameters

    • conditions: object | Conditions<TDocument>

      The MongoDB query dictating which document to return

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance | null>

  • Retrieves a single document from the collection with the given ID and wraps it as an instance

    Parameters

    • id: string

      The document's unique _id field value in downstream format

    • options: QueryOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance | null>

  • Retrieves a single document from the collection which matches the conditions

    Parameters

    • conditions: object | Conditions<TDocument>

      The MongoDB query dictating which document to return

    • options: QueryOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance | null>

get

  • get(callback?: General.Callback<TInstance>): Promise<TInstance>
  • get(id: string, callback?: General.Callback<TInstance>): Promise<TInstance>
  • get(conditions: object | Conditions<TDocument>, callback?: General.Callback<TInstance>): Promise<TInstance>
  • get(id: string, options: QueryOptions, callback?: General.Callback<TInstance>): Promise<TInstance>
  • get(conditions: object | Conditions<TDocument>, options: QueryOptions, callback?: General.Callback<TInstance>): Promise<TInstance>
  • Retrieves a single document from the collection and wraps it as an instance

    Parameters

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance>

  • Retrieves a single document from the collection with the given ID and wraps it as an instance

    Parameters

    • id: string

      The document's unique _id field value in downstream format

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance>

  • Retrieves a single document from the collection which matches the conditions

    Parameters

    • conditions: object | Conditions<TDocument>

      The MongoDB query dictating which document to return

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance>

  • Retrieves a single document from the collection with the given ID and wraps it as an instance

    Parameters

    • id: string

      The document's unique _id field value in downstream format

    • options: QueryOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance>

  • Retrieves a single document from the collection which matches the conditions

    Parameters

    • conditions: object | Conditions<TDocument>

      The MongoDB query dictating which document to return

    • options: QueryOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance>

      An optional callback which will be triggered when a result is available

    Returns Promise<TInstance>

insert

  • insert(object: InsertionDocument<TDocument>, callback?: General.Callback<TInstance>): Promise<TInstance>
  • insert(object: InsertionDocument<TDocument>, options: CreateOptions, callback?: General.Callback<TInstance>): Promise<TInstance>
  • insert(objects: InsertionDocument<TDocument>[], callback?: General.Callback<TInstance[]>): Promise<TInstance[]>
  • insert(objects: InsertionDocument<TDocument>[], options: CreateOptions, callback?: General.Callback<TInstance[]>): Promise<TInstance[]>
  • Inserts an object into the collection after validating it against this model's schema

    Parameters

    • object: InsertionDocument<TDocument>

      The object to insert into the collection

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance>

  • Inserts an object into the collection after validating it against this model's schema

    Parameters

    • object: InsertionDocument<TDocument>

      The object to insert into the collection

    • options: CreateOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance>

  • Inserts the objects into the collection after validating them against this model's schema

    Parameters

    • objects: InsertionDocument<TDocument>[]

      The objects to insert into the collection

    • Optional callback: General.Callback<TInstance[]>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance[]>

  • Inserts the objects into the collection after validating them against this model's schema

    Parameters

    • objects: InsertionDocument<TDocument>[]

      The objects to insert into the collection

    • options: CreateOptions

      The options dictating how this function behaves

    • Optional callback: General.Callback<TInstance[]>

      A callback which is triggered when the operation completes

    Returns Promise<TInstance[]>

Private loadExternal

  • Loads any externally available properties (generally accessed using public getters/setters).

    Parameters

    Returns void

Private loadInternal

  • loadInternal(): void
  • Loads any internally (protected/private) properties and helpers only used within Iridium itself.

    Returns void

mapReduce

  • Runs a mapReduce operation in MongoDB and returns the contents of the resulting collection.

    Type parameters

    • Key

    • Value

    Parameters

    • functions: MapReduceFunctions<TDocument, Key, Value>

      The mapReduce functions which will be passed to MongoDB to complete the operation.

    • options: MapReduceOptions

      Options used to configure how MongoDB runs the mapReduce operation on your collection.

    Returns Promise<MapReducedDocument<Key, Value>[]>

    A promise which completes when the mapReduce operation has written its results to the provided collection.

  • Runs a mapReduce operation in MongoDB and writes the results to a collection.

    Type parameters

    • Key

    • Value

    Parameters

    • instanceType: InstanceImplementation<MapReducedDocument<Key, Value>, any>

      An Iridium.Instance type whichThe mapReduce functions which will be passed to MongoDB to complete the operation.

    • options: MapReduceOptions

      Options used to configure how MongoDB runs the mapReduce operation on your collection.

    Returns Promise<void>

    A promise which completes when the mapReduce operation has written its results to the provided collection.

Private onNewModel

  • onNewModel(): void
  • Process any callbacks and plugin delegation for the creation of this model. It will generally be called whenever a new Iridium Core is created, however is more specifically tied to the lifespan of the models themselves.

    Returns void

remove

  • remove(callback?: General.Callback<number>): Promise<number>
  • remove(conditions: object | Conditions | any, callback?: General.Callback<number>): Promise<number>
  • remove(conditions: object | Conditions<TDocument> | string, options: RemoveOptions, callback?: General.Callback<number>): Promise<number>
  • Removes all documents from the collection

    Parameters

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

  • Removes all documents from the collection which match the conditions

    Parameters

    • conditions: object | Conditions | any

      The conditions determining whether an object is removed or not

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

  • Removes all documents from the collection which match the conditions

    Parameters

    • conditions: object | Conditions<TDocument> | string

      The conditions determining whether an object is removed or not

    • options: RemoveOptions

      The options controlling the way in which the function behaves

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

update

  • update(conditions: object | Conditions<TDocument> | string, changes: Changes, callback?: General.Callback<number>): Promise<number>
  • update(conditions: object | Conditions<TDocument> | string, changes: TDocument, callback?: General.Callback<number>): Promise<number>
  • update(conditions: object | Conditions<TDocument> | string, changes: Changes, options: UpdateOptions, callback?: General.Callback<number>): Promise<number>
  • update(conditions: Conditions<TDocument> | string, changes: TDocument, options: UpdateOptions, callback?: General.Callback<number>): Promise<number>
  • Updates the documents in the backing collection which match the conditions using the given update instructions

    Parameters

    • conditions: object | Conditions<TDocument> | string

      The conditions which determine which documents will be updated

    • changes: Changes

      The changes to make to the documents

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

  • Updates the documents in the backing collection which match the conditions using the given update instructions

    Parameters

    • conditions: object | Conditions<TDocument> | string

      The conditions which determine which documents will be updated

    • changes: TDocument

      The replacement document to do a full update

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

  • Updates the documents in the backing collection which match the conditions using the given update instructions

    Parameters

    • conditions: object | Conditions<TDocument> | string

      The conditions which determine which documents will be updated

    • changes: Changes

      The changes to make to the documents

    • options: UpdateOptions

      The options which dictate how this function behaves

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

  • Updates the documents in the backing collection which match the conditions using the given update instructions

    Parameters

    • conditions: Conditions<TDocument> | string

      The conditions which determine which documents will be updated

    • changes: TDocument

      The replacement document to do a full update

    • options: UpdateOptions

      The options which dictate how this function behaves

    • Optional callback: General.Callback<number>

      A callback which is triggered when the operation completes

    Returns Promise<number>

Generated using TypeDoc