Base class for models in the ORM.

Indexable

  • [key: string]: any

Constructors

  • Creates a new instance of the model.

    Parameters

    • Optionalrecord: ModelRecord

      The record to initialize the model with.

    Returns Model

Properties

$id: null | string = null

The primary key of the model instance.

cachedFields: FieldCache

Cached fields for the model.

dispatch: ((type: string, payload?: any) => Promise<any>)

The dispatch function for dispatching actions.

entity: string

The entity name of the model.

store: State

The state of the store.

Methods

  • Defines getters and setters for the model instance.

    Returns void

  • Returns the fields of the model instance.

    Returns Fields

    The fields of the model instance.

  • Fills the model instance with the given record.

    Parameters

    • record: ModelRecord = {}

      The record to fill the model with.

    Returns void

  • Loads the related models for the model instance.

    Returns void

  • Returns the constructor of the model.

    Returns typeof Model

    The constructor of the model.

  • Sets the index of the model instance.

    Parameters

    • id: null | string

      The index to set.

    Returns this

    The model instance.

  • Converts the model instance to an object.

    Returns ModelRecord

    The object representation of the model instance.

  • Returns all records of the model from the store.

    Type Parameters

    Parameters

    • this: T

    Returns InstanceType<T>[]

    All records of the model.

  • Creates an attribute with the given default value.

    Parameters

    • defaultValue: any

      The default value of the attribute.

    Returns Attribute

    The attribute.

  • Defines a belongsTo relationship.

    Parameters

    • relatedModel: typeof Model

      The related model.

    • foreignKey: string

      The foreign key.

    Returns Relationship

    The relationship.

  • Creates a new record in the store.

    Type Parameters

    Parameters

    • this: T
    • payload: any

      The payload containing the data to create.

    Returns any

    The created record.

  • Deletes records from the store based on the given id(s) or predicate.

    Type Parameters

    Parameters

    • this: T
    • id: string | number | (string | number)[]

      The id(s) or predicate to delete the records.

    Returns any[]

    The deleted records.

  • Type Parameters

    Parameters

    • this: T
    • predicate: Predicate<InstanceType<T>>

    Returns any[]

  • Deletes all records of the model from the store.

    Type Parameters

    Parameters

    • this: T

    Returns Promise<void>

    A promise that resolves when the records are deleted.

  • Returns the fields of the model.

    Returns Fields

    The fields of the model.

  • Finds a record by its id.

    Type Parameters

    Parameters

    • this: T
    • id: string | number

      The id of the record.

    Returns undefined | InstanceType<T>

    The found record or undefined.

  • Returns the cached fields of the model.

    Returns Fields

    The cached fields of the model.

  • Defines a hasMany relationship.

    Parameters

    • relatedModel: typeof Model

      The related model.

    • foreignKey: string

      The foreign key.

    Returns Relationship

    The relationship.

  • Initializes the model with the given state and dispatch function.

    Parameters

    • state: State

      The state of the store.

    • dispatch: ((type: string, payload?: any) => Promise<any>)

      The dispatch function.

        • (type, payload?): Promise<any>
        • Parameters

          • type: string
          • Optionalpayload: any

          Returns Promise<any>

    Returns void

  • Inserts a new record in the store.

    Type Parameters

    Parameters

    • this: T
    • payload: any

      The payload containing the data to insert.

    Returns any

    The inserted record.

  • Inserts or updates records or a record in the store.

    Type Parameters

    Parameters

    • this: T
    • payload: any

      The payload containing the data to insert or update.

    Returns any

    The inserted or updated record(s).

  • Creates a query builder for the model.

    Type Parameters

    Parameters

    • this: T

    Returns QueryBuilder<T>

    The query builder.

  • Resets the state of the store.

    Returns Promise<any>

    A promise that resolves when the state is reset.

  • Updates an existing record in the store.

    Type Parameters

    Parameters

    • this: T
    • payload: any

      The payload containing the data to update.

    Returns any

    The updated record.