You can specify an attribute as VIRTUAL
while
defining
the
model. A VIRTUAL
attribute is not actually stored in
the
database but is available to your NodeJS code as if it is part of
the
model.
Definition
To define a virtual attribute:
1 |
module.exports = function (sequelize, Sequelize) { |
Note: If you define the VIRTUAL
key in
migration, it
will throw an error
Getters, Setters and Validations
You can apply setters, getters and validations to the virtual field just like you would to a regular attribute:
1 |
module.exports = function (sequelize, Sequelize) { |
Return Types and Dependencies
Virtual attributes can also take return types and dependency
fields.
To
specify types and dependent fields, call the Virtual
constructor:
1 |
availableQuantity: { |
Specifying dependency fields automatically loads the dependent fields even if you haven’t referred to them while selecting attributes.