Class Instance

    View code

    This class represents an single instance, a database row. You might see it referred to as both Instance and instance. You should notinstantiate the Instance class directly, instead you access it using the finder and creation methods on the model.

    Instance instances operate with the concept of a property, which stores the actual values represented by the instance.By default, the values from dataValues can also be accessed directly from the Instance, that is:

    However, if getters and/or setters are defined for field they will be invoked, instead of returning the value from dataValues.Accessing properties directly or using get is preferred for regular use, getDataValue should only be used for custom getters.

    See:


    View code

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


    Model() -> Model

    Returns the Model the instance was created from.

    See:


    sequelize() -> Sequelize

    A reference to the sequelize instance

    See:


    where() -> Object

    Get an object representing the query for this instance, use with options.where


    getDataValue(key) -> any

    View code

    Get the value of the underlying data value

    Params:


    setDataValue(key, value)

    Update the underlying data value

    Params:

    NameTypeDescription
    keyString
    valueany

    get([key], [options]) -> Object|any

    View code

    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.

    Params:

    NameTypeDescription
    [key]String
    [options]Object
    [options.plain=false]BooleanIf set to true, included instances will be returned as plain objects

    View code

    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 functionwill be called instead. To bypass the setter, you can pass in the options object.

    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 associationwhile also making sure that the proper include options have been set (from .build() or .find())

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

    See:

    Params:

    NameTypeDescription
    keyString | Object
    valueany
    [options]Object
    [options.raw=false]BooleanIf set to true, field and virtual setters will be ignored
    [options.reset=false]BooleanClear all previously set data values

    Aliases: setAttributes


    changed([key]) -> Boolean|Array

    View code

    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.

    Params:

    NameTypeDescription
    [key]String

    previous([key]) -> any|Array.<any>

    Returns the previous value for key from _previousDataValues.

    If called without a key, returns the previous values for all values which have changed

    Params:


    save([options]) -> Promise.<this|Errors.ValidationError>

    View code

    Validate this instance, and if the validation passes, persist it to the database. It will only save changed fields, and do nothing if no fields have changed.

    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.

    Params:

    NameTypeDescription
    [options]Object
    [options.fields]Array.<string>An optional array of strings, representing database columns. If fields is provided, only those columns will be validated and saved.
    [options.silent=false]BooleanIf true, the updatedAt timestamp will not be updated.
    [options.validate=true]BooleanIf false, validations won't be run.
    [options.logging=false]FunctionA function that gets executed while running the query to log the sql.
    [options.transaction]Transaction
    [options.searchPath=DEFAULT]StringAn optional parameter to specify the schema search_path (Postgres only)

    reload([options]) -> 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.

    See:

    Params:

    NameTypeDescription
    [options]ObjectOptions that are passed on to Model.find
    [options.logging=false]FunctionA function that gets executed while running the query to log the sql.

    validate([options]) -> Promise.<Errors.ValidationError|undefined>

    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.

    See:

    Params:

    NameTypeDescription
    [options]ObjectOptions that are passed to the validator
    [options.skip]ArrayAn array of strings. All properties that are in this array will not be validated
    [options.fields]ArrayAn array of strings. Only the properties that are in this array will be validated.

    update(updates, options) -> Promise.<this>

    See:

    Params:

    NameTypeDescription
    updatesObjectSee set
    optionsObjectSee save

    Aliases: updateAttributes


    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.

    Params:


    restore([options={}]) -> Promise.<undefined>

    View code

    Restore the row corresponding to this instance. Only available for paranoid models.

    Params:

    NameTypeDescription
    [options={}]Object
    [options.logging=false]FunctionA function that gets executed while running the query to log the sql.
    [options.transaction]Transaction

    increment(fields, [options]) -> 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

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

    See:

    Params:

    NameTypeDescription
    fieldsString | Array | ObjectIf 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.
    [options]Object
    [options.by=1]IntegerThe number to increment by
    [options.silent=false]BooleanIf true, the updatedAt timestamp will not be updated.
    [options.logging=false]FunctionA function that gets executed while running the query to log the sql.
    [options.transaction]Transaction
    [options.searchPath=DEFAULT]StringAn optional parameter to specify the schema search_path (Postgres only)

    decrement(fields, [options]) -> Promise

    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

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

    See:

    Params:

    NameTypeDescription
    fieldsString | Array | ObjectIf 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
    [options]Object
    [options.by=1]IntegerThe number to decrement by
    [options.silent=false]BooleanIf true, the updatedAt timestamp will not be updated.
    [options.logging=false]FunctionA function that gets executed while running the query to log the sql.
    [options.transaction]Transaction
    [options.searchPath=DEFAULT]StringAn optional parameter to specify the schema search_path (Postgres only)

    equals(other) -> Boolean

    Check whether all values of this and other Instance are the same

    Params:

    NameTypeDescription
    otherInstance

    equalsOneOf(others) -> Boolean

    View code

    Check if this is equal to one of others by calling equals

    Params:


    toJSON() -> object

    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.

    See: