Options
All
  • Public
  • Public/Protected
  • All
Menu

Type parameters

Hierarchy

  • Instance<T>
    • ModelInstance

Index

Properties

Model

Model: Model<this, T>

Returns the Model the instance was created from.

see

Model

isNewRecord

isNewRecord: boolean

Returns true if this instance has not yet been persisted to the database

sequelize

sequelize: Sequelize

A reference to the sequelize instance

Methods

changed

  • changed(key: string): boolean
  • changed(): boolean | string[]
  • If changed is called with a string it will return a boolean indicating whether the value of that key in dataValues is different from the value in _previousDataValues.

    If changed is called without an argument, it will return an array of keys that have changed.

    If changed is called without an argument and no keys have changed, it will return false.

    Parameters

    • key: string

    Returns boolean

  • Returns boolean | string[]

decrement

  • decrement(fields: string | string[] | Object, options?: InstanceIncrementDecrementOptions): Promise<this>
  • Decrement the value of one or more columns. This is done in the database, which means it does not use the values currently stored on the Instance. The decrement is done using a

    SET column = column - X
    

    query. To get the correct value after an decrement into the Instance you should do a reload.

    instance.decrement('number') // decrement number by 1
    instance.decrement(['number', 'count'], { by: 2 }) // decrement number and count by 2
    instance.decrement({ answer: 42, tries: 1}, { by: 2 }) // decrement answer by 42, and tries by 1.
                                                           // `by` is ignored, since each column has its own
                                                           // value
    

    Parameters

    • fields: string | string[] | Object

      If a string is provided, that column is decremented by the value of by given in options. If an array is provided, the same is true for each column. If and object is provided, each column is decremented by the value given

    • Optional options: InstanceIncrementDecrementOptions

    Returns Promise<this>

destroy

  • destroy(options?: InstanceDestroyOptions): Promise<void>
  • Destroy the row corresponding to this instance. Depending on your setting for paranoid, the row will either be completely deleted, or have its deletedAt timestamp set to the current time.

    Parameters

    • Optional options: InstanceDestroyOptions

    Returns Promise<void>

equals

  • equals(other: Instance<any>): boolean
  • Check whether all values of this and other Instance are the same

    Parameters

    • other: Instance<any>

    Returns boolean

equalsOneOf

  • equalsOneOf(others: Instance<any>[]): boolean
  • Check if this is eqaul to one of others by calling equals

    Parameters

    • others: Instance<any>[]

    Returns boolean

get

  • get(key: string, options?: object): any
  • get(options?: object): T
  • If no key is given, returns all values of the instance, also invoking virtual getters.

    If key is given and a field or virtual getter is present for the key it will call that getter - else it will return the value for key.

    Parameters

    • key: string
    • Optional options: object
      • Optional clone?: boolean
      • Optional plain?: boolean

        If set to true, included instances will be returned as plain objects

    Returns any

  • Parameters

    • Optional options: object
      • Optional clone?: boolean
      • Optional plain?: boolean

    Returns T

getDataValue

  • getDataValue(key: string): any
  • Get the value of the underlying data value

    Parameters

    • key: string

    Returns any

increment

  • increment(fields: string | string[] | Object, options?: InstanceIncrementDecrementOptions): Promise<this>
  • Increment the value of one or more columns. This is done in the database, which means it does not use the values currently stored on the Instance. The increment is done using a

    SET column = column + X
    

    query. To get the correct value after an increment into the Instance you should do a reload.

    instance.increment('number') // increment number by 1
    instance.increment(['number', 'count'], { by: 2 }) // increment number and count by 2
    instance.increment({ answer: 42, tries: 1}, { by: 2 }) // increment answer by 42, and tries by 1.
                                                           // `by` is ignored, since each column has its own
                                                           // value
    

    Parameters

    • fields: string | string[] | Object

      If a string is provided, that column is incremented by the value of by given in options. If an array is provided, the same is true for each column. If and object is provided, each column is incremented by the value given.

    • Optional options: InstanceIncrementDecrementOptions

    Returns Promise<this>

previous

  • previous(key: string): any
  • Returns the previous value for key from _previousDataValues.

    Parameters

    • key: string

    Returns any

reload

  • reload(options?: FindOptions): Promise<this>
  • Refresh the current instance in-place, i.e. update the object with current data from the DB and return the same object. This is different from doing a find(Instance.id), because that would create and return a new instance. With this method, all references to the Instance are updated with the new data and no new objects are created.

    Parameters

    • Optional options: FindOptions

    Returns Promise<this>

restore

  • restore(options?: InstanceRestoreOptions): Promise<void>
  • Restore the row corresponding to this instance. Only available for paranoid models.

    Parameters

    • Optional options: InstanceRestoreOptions

    Returns Promise<void>

save

  • save(options?: InstanceSaveOptions): Promise<this>
  • Validate this instance, and if the validation passes, persist it to the database.

    On success, the callback will be called with this instance. On validation error, the callback will be called with an instance of Sequelize.ValidationError. This error will have a property for each of the fields for which validation failed, with the error message for that field.

    Parameters

    • Optional options: InstanceSaveOptions

    Returns Promise<this>

set

  • set(key: string, value: any, options?: InstanceSetOptions): this
  • set(keys: Object, options?: InstanceSetOptions): this
  • Set is used to update values on the instance (the sequelize representation of the instance that is, remember that nothing will be persisted before you actually call save). In its most basic form set will update a value stored in the underlying dataValues object. However, if a custom setter function is defined for the key, that function will be called instead. To bypass the setter, you can pass raw: true in the options object.

    If set is called with an object, it will loop over the object, and call set recursively for each key, value pair. If you set raw to true, the underlying dataValues will either be set directly to the object passed, or used to extend dataValues, if dataValues already contain values.

    When set is called, the previous value of the field is stored and sets a changed flag(see changed).

    Set can also be used to build instances for associations, if you have values for those. When using set with associations you need to make sure the property key matches the alias of the association while also making sure that the proper include options have been set (from .build() or .find())

    If called with a dot.seperated key on a JSON/JSONB attribute it will set the value nested and flag the entire object as changed.

    Parameters

    • key: string
    • value: any
    • Optional options: InstanceSetOptions

    Returns this

  • Parameters

    • keys: Object
    • Optional options: InstanceSetOptions

    Returns this

setAttributes

  • setAttributes(key: string, value: any, options?: InstanceSetOptions): this
  • setAttributes(keys: Object, options?: InstanceSetOptions): this
  • Parameters

    • key: string
    • value: any
    • Optional options: InstanceSetOptions

    Returns this

  • Parameters

    • keys: Object
    • Optional options: InstanceSetOptions

    Returns this

setDataValue

  • setDataValue(key: string, value: any): void
  • Update the underlying data value

    Parameters

    • key: string
    • value: any

    Returns void

toJSON

  • toJSON(): T
  • Convert the instance to a JSON representation. Proxies to calling get with no keys. This means get all values gotten from the DB, and apply all custom getters.

    Returns T

update

  • update(key: string, value: any, options?: InstanceUpdateOptions): Promise<this>
  • update(keys: Object, options?: InstanceUpdateOptions): Promise<this>
  • This is the same as calling set and then calling save.

    Parameters

    • key: string
    • value: any
    • Optional options: InstanceUpdateOptions

    Returns Promise<this>

  • Parameters

    • keys: Object
    • Optional options: InstanceUpdateOptions

    Returns Promise<this>

updateAttributes

  • updateAttributes(key: string, value: any, options?: InstanceUpdateOptions): Promise<this>
  • updateAttributes(keys: Object, options?: InstanceUpdateOptions): Promise<this>
  • Parameters

    • key: string
    • value: any
    • Optional options: InstanceUpdateOptions

    Returns Promise<this>

  • Parameters

    • keys: Object
    • Optional options: InstanceUpdateOptions

    Returns Promise<this>

validate

  • validate(options?: object): Promise<ValidationError>
  • Validate the attribute of this instance according to validation rules set in the model definition.

    Emits null if and only if validation successful; otherwise an Error instance containing { field name : [error msgs] } entries.

    Parameters

    • Optional options: object
      • Optional skip?: string[]

        An array of strings. All properties that are in this array will not be validated

    Returns Promise<ValidationError>

where

  • where(): Object
  • Get an object representing the query for this instance, use with options.where

    Returns Object

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc