Defining, removing, and executing lifecycle events
There are several ways to attach lifecycle events to models and Sequelize’s behavior. Each of these methods allows us to change the attribute values that are derived from the hook’s arguments as pass-by-reference. For example, you can add additional properties to the instances returned in afterFind
by simply updating the attributes on the objects from within the lifecycle method. By default, Sequelize will treat lifecycle events as synchronous operations, but if you need asynchronous capabilities, you can return a Promise
object or an async
function.
Defining instance and model lifecycle events
Instance and model lifecycle events can be defined in several ways, including defining these events as a local hook (defined directly from the model itself). There are several ways to define a local hook; we will start with the basic example of declaring hooks during the initialization of a model:
class Receipt extends...