Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface InstanceImplementation<TDocument, TInstance>

This interface dictates the format of an instance class which wraps documents received from the database for a specific Iridium model.

Type parameters

  • TDocument

    The interface representing the documents stored in the database, after being passed through the transforms pipeline.

  • TInstance

    The type of object which is instantiated when calling this implementation's constructor.

    It is important to note that, when implementing this interface, each of the properties and methods should be exposed statically. That is, you would expose the collection property as a static variable on the instance implementation, since prototype methods and variables become available to consumers of the instance itself.

Hierarchy

  • InstanceImplementation

Index

Constructors

constructor

  • new InstanceImplementation(model: Model<TDocument, TInstance>, doc: TDocument, isNew?: undefined | true | false, isPartial?: undefined | true | false): TInstance
  • A constructor which creates a new instance tied to the given model and representing the given document.

    Parameters

    • model: Model<TDocument, TInstance>

      The Iridium Model which this instance should be tied to, gives the instance access to the database collection and any other context it requires.

    • doc: TDocument

      The document this instance should wrap from the database. This provides the data context for the instance.

    • Optional isNew: undefined | true | false

      Whether this document is known to exist in the database or not, for example, if the instance was generated from user input and hasn't been saved yet.

    • Optional isPartial: undefined | true | false

      Whether the document which has been given to this instance had any field restrictions imposed on it during the query, and may therefore only contain partial data.

    Returns TInstance

Properties

Optional cache

The cache controller used to determine whether a document may be cached, as well as deriving a unique cache key for the document and similarly, for a query. This works in concert with the cache implementation itself to ensure that documents are cached in an intelligent manner. By default this will simply make use of the document's _id field as the cache key - however that behaviour may be modified if you wish to query on other properties instead.

collection

collection: string

The name of the database collection from which documents are retrieved, and to which they are stored.

Optional indexes

indexes: (IndexSpecification | Index)[]

Any additional indexes on the collection which should be managed by Iridium. This field is optional, but if provided allows you to make use of the model's ensureIndexes() method to automatically generate all specified indexes.

Optional mapReduceOptions

mapReduceOptions: MapReduceFunctions<any, any, any>

mapReduce Options

Optional renames

renames: RenameMap

The map of code field names to DB field names which will be used by Iridium to rename those fields should you wish to have differing field names within the DB to those represented in your code.

schema

schema: Schema

The database schematic used for validation of instances prior to storing them in the database. This schematic should follow the guides set out in skmatc's documentation, and is used whenever you insert a new document into the collection or save an instance using the default instance type. Operations like update() (and by extension, save() when using the update operations) cannot be checked by skmatc for consistency and as a result will not have their data validated - be careful when making use of them as a result.

Optional transforms

transforms: Transforms

Any transform operations you would like to perform on documents received from the database, or prior to being sent to the database. These may include things such as converting ObjectIDs to strings for the application, and then back to ObjectIDs once they return to the database.

Optional validators

validators: Validator[]

Any additional validation types you wish to make available for use within this model's database schema. This allows you to validate using conditions otherwise not available within skmatc itself. For more information on implementing a validator, take a look at the skmatc documentation on GitHub.

Methods

Optional onCreating

  • onCreating(document: TDocument): Promise<any> | PromiseLike<any> | void
  • An optional method which will be called whenever a document is about to be inserted into the database, allowing you to set default values and do any preprocessing you wish prior to the document being inserted.

    Parameters

    • document: TDocument

      The document which will be inserted into the database.

      This method is executed synchronously, however you can perform asynchronous operations by returning a promise which resolves once your task has completed. Be aware that this method is executed for every document inserted into the database. As a result, long running tasks will have a significant impact on the performance of your inserts.

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

Optional onReady

  • onReady(instance: TInstance): Promise<any> | PromiseLike<any> | void
  • An optional method which is called whenever a new document for this model has been wrapped in an instance.

    Parameters

    • instance: TInstance

      The instance which has been created.

      This method is executed synchronously, however you can perform asynchronous operations by returning a promise which resolves once your task has completed. Be aware that this method is executed for every document retrieved from the database. As a result, long running tasks will have a significant impact on the performance of your queries.

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

Optional onRetrieved

  • onRetrieved(document: TDocument): Promise<any> | PromiseLike<any> | void
  • An optional method which is called whenever a new document is received from the model's collection and prior to the document being wrapped, can be used to perform preprocessing if necessary - however we recommend you rather make use of transforms for that task.

    Parameters

    • document: TDocument

      The document that was retrieved from the database.

      This method is executed synchronously, however you can perform asynchronous operations by returning a promise which resolves once your task has completed. Be aware that this method is executed for every document retrieved from the database. As a result, long running tasks will have a significant impact on the performance of your queries.

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

Optional onSaving

  • onSaving(instance: TInstance, changes: Changes): Promise<any> | PromiseLike<any> | void
  • An optional method which is called prior to saving an instance, it is provided with the instance itself as well as the proposed changes to the instance. This allows you to make additional changes, such as updating a lastChanged property on the document, or abort changes by throwing an error.

    Parameters

    • instance: TInstance

      The instance to which the changes are being made

    • changes: Changes

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

      This method is executed synchronously, however you can perform asynchronous operations by returning a promise which resolves once your task has completed. Be aware that this method is executed for every call to save. As a result, long running tasks will have a significant impact on how quickly your save operations are dispatched.

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

Generated using TypeDoc