Executing validations while performing asynchronous operations
Sometimes, your validations will require you to fetch an associated model’s record, call a third-party application, or some other form of request that waits for a response.
Suppose we were in a situation where we have to ensure that there was a completed and active payment before creating or updating a customer’s membership points. As long as any payment was still considered in good standing, that customer should be able to update their membership. We would use the async
and await
keywords to help us execute these requests and wait for the responses for validation:
Membership.init({ points: { type: DataTypes.INTEGER, } }, { sequelize, validate: { async accountIsActive() { const payments = await Payments.find({ where: { status...