Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BaseModel<T>

The BaseModel is the foundational type for models, which is returned by a call to Model. It provides lifecycle hooks (which can be overridden) and methods for saving, deleting, and validating a document in Cosmos DB.

To look up a model, you'll usually use the methods on Model.partition(), or Model.crossPartitionQuery().

Type parameters

  • T: { id: string }

Hierarchy

  • BaseModel

Index

Constructors

constructor

  • new BaseModel<T>(props: T & Partial<Resource>): BaseModel<T>
  • Type parameters

    • T: { id: string }

    Parameters

    • props: T & Partial<Resource>

    Returns BaseModel<T>

Properties

Readonly getValidateFunction

getValidateFunction: () => ValidateFunction<Object> = ...

Gets the validation function for the JSON schema defined in the ISchemaFields for this model.

Type declaration

    • (): ValidateFunction<Object>
    • Returns ValidateFunction<Object>

Abstract partition

partition: (container?: Container) => Partition<T, ConstructorFor<T, BaseModel<T>>>

Gets a partition accessor for the collection. See Partition for more information.

Type declaration

props

props: T & Partial<Resource>

Raw model properties. These are also accessible on the instance directly, but you need to manually declare them if you use TypeScript.

Abstract schema

schema: BasicSchema<T>

Schema for this model.

Static ajv

ajv: Ajv = ...

Instance of ajv used for validation. You can modify or replace this as needed, to add custom validators for example. Modifications must happen before you start using models, since validation functions will be compiled and cached the first time they're used.

Static Optional cosmosContainer

cosmosContainer: Container

Default Cosmos DB container for the model. This can be populated by hand, or by the connectModels() helper function.

Static Optional cosmosDb

cosmosDb: Database

Default container for the model. This can be populated by hand, or by the connectModels() helper function.

Accessors

etag

  • get etag(): undefined | string
  • Gets the etag of the data when it was read.

    This will be undefined if the model was not yet saved and was not loaded from the database.

    Returns undefined | string

id

  • get id(): string
  • Gets the model ID. This is unique within a Cosmos DB partition.

    Returns string

updatedAt

  • get updatedAt(): undefined | Date
  • Gets the time the model was last updated.

    This will be undefined if the model was not yet saved and was not loaded from the database.

    Returns undefined | Date

Methods

afterCreate

  • afterCreate(): Promise<void>
  • Lifecycle hook called after the model is created for the first time.

    Returns Promise<void>

afterDelete

  • afterDelete(): Promise<void>
  • Lifecycle hook called after an existing model is deleted.

    Returns Promise<void>

afterPersist

  • afterPersist(): Promise<void>
  • Lifecycle hook called after the model is successfully persisted. This is called before afterCreate or afterUpdate.

    Returns Promise<void>

afterUpdate

  • afterUpdate(): Promise<void>
  • Lifecycle hook called after an existing model is updated.

    Returns Promise<void>

beforeCreate

  • beforeCreate(): Promise<void>
  • Lifecycle hook called before the model is created for the first time.

    Returns Promise<void>

beforeDelete

  • beforeDelete(): Promise<void>
  • Lifecycle hook called before an existing model is deleted.

    Returns Promise<void>

beforePersist

  • beforePersist(): Promise<void>
  • Lifecycle hook called before the model is persisted. This is called after beforeCreate or beforeUpdate.

    Returns Promise<void>

beforeUpdate

  • beforeUpdate(): Promise<void>
  • Lifecycle hook called before an existing model is updated.

    Returns Promise<void>

create

  • Creates the item, if it doesn't exist. If it does, an error is thrown.

    Parameters

    • __namedParameters: ISaveOptions = {}
    • container: Container = ...

    Returns Promise<IResourceResponse<BaseModel<T>>>

delete

  • delete(__namedParameters?: IDeleteOptions, container?: Container): Promise<ItemResponse<T>>
  • Parameters

    Returns Promise<ItemResponse<T>>

partitionKey

  • partitionKey(): string | number
  • Gets the value for the partition key in the current model.

    Returns string | number

save

  • Safely persists the model -- updating it if it was originally retrieved from the database (provided a model with the ID doesn't already exist) or creating it if not.

    Parameters

    Returns Promise<IResourceResponse<BaseModel<T>>>

toJSON

  • toJSON(): T
  • Implements JSON serialization, returns the same as BaseModel.toObject.

    Returns T

toObject

  • toObject(): T
  • Returns a shallow copy of the properties on the model, excluding internal Cosmos DB fields.

    Returns T

update

  • Persists item updates to the database. This uses etags to avoid a conflict, if the item was originally retrieved from the database. This can throw an error if another process made and update in the meantime. To avoid this behavior, pass { overwrite: true }.

    Parameters

    • __namedParameters: ISaveOptions = {}
    • container: Container = ...

    Returns Promise<IResourceResponse<BaseModel<T>>>

validate

  • validate(): void
  • Validates the current set of properties on the model.

    throws

    Ajv.ValidationError

    Returns void

Generated using TypeDoc