Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ModelHelpers<TDocument, TInstance>

A number of helper methods used commonly within Iridium, they provide a means to transform, validate, wrap and diff instances and documents. By keeping these methods in one place we help to improve testability and reduce code duplication (mouse abuse) throughout the codebase.

internal

Type parameters

  • TDocument

  • TInstance

Hierarchy

  • ModelHelpers

Index

Constructors

constructor

Properties

Private _validator

_validator: Skmatc

model

model: Model<TDocument, TInstance>

Methods

applyCachedFieldChanges

  • applyCachedFieldChanges(instance: InstanceInternals<TDocument, TInstance>): TDocument

cloneConditions

  • cloneConditions<T>(original: T): T
  • Clones the given document recursively, taking into account complex types like Buffers correctly. Optimized for working with query documents instead of true documents.

    Type parameters

    • T

    Parameters

    • original: T

    Returns T

cloneDocument

  • cloneDocument<T>(original: T): T
  • Clones the given document recursively, taking into account complex types like Buffers correctly.

    Type parameters

    • T

    Parameters

    • original: T

    Returns T

convertToDB

  • Converts the given document to its database form into a form using the transforms defined on the model.

    Type parameters

    • T

    Parameters

    • document: T

      The document to be converted

    • Default value options: TransformOptions = { properties: true, renames: true }
    • Default value clone: boolean = true

      Whether or not to clone the document before performing any transforms (performance boost)

    Returns T

    A new document cloned from the original and transformed

diff

  • diff(original: TDocument, modified: TDocument): any
  • Performs a diff operation between two documents and creates a MongoDB changes object to represent the differences

    Parameters

    • original: TDocument

      The original document prior to changes being made

    • modified: TDocument

      The document after changes were made

    Returns any

readInstanceField

  • readInstanceField<K, V>(instance: InstanceInternals<TDocument, TInstance>, field: K): V

transformFromDB

  • Converts the given document from its database form using the transforms defined on the model.

    remarks

    Unlike the transformToDB function - this method only applies document level transforms, as property level transforms are applied in their relevant instance setters.

    Parameters

    • document: TDocument

      The document to be converted.

    • Default value options: TransformOptions = { properties: true, renames: true }

    Returns TDocument

    The result of having transformed the document.

transformToDB

  • Converts the given document to its database form into a form using the transforms defined on the model.

    remarks

    This is only really called from insert/create - as

    Type parameters

    • T

    Parameters

    • document: T

      The document to be converted

    • Default value options: TransformOptions = { properties: true, renames: true }

    Returns T

    The result of having transformed the document.

validate

  • validate(document: TDocument): Result
  • Validates a document to ensure that it matches the model's ISchema requirements

    Parameters

    • document: TDocument

      The document to validate against the ISchema

    Returns Result

    The result of the validation

wrapDocument

  • wrapDocument(document: TDocument, isNew?: undefined | true | false, isPartial?: undefined | true | false): TInstance
  • Wraps the given document in an instance wrapper for use throughout the application

    Parameters

    • document: TDocument

      The document to be wrapped as an instance

    • Optional isNew: undefined | true | false

      Whether the instance originated from the database or was created by the application

    • Optional isPartial: undefined | true | false

      Whether the document supplied contains all information present in the database

    Returns TInstance

    An instance which wraps this document

writeInstanceField

  • writeInstanceField<K, V>(instance: InstanceInternals<TDocument, TInstance>, field: K, value: V): void

Generated using TypeDoc