my use case is something like this,
- Users can review the dishes.
- Each review is stored in a table called ratings.
- When a new record inserted to the ratings table, Dish tables overall rating column should be updated with the average rating value.
I go though the docs and some questions in the stack-overflow and GitHub also. What I have done so far is.
const db = require("../models");
const Sequelize = require('sequelize');
export async function create(req, res, next) {
try {
const body = req.swagger.params.body.value;
const rate = await db.ratings.create(body, {
fields: ['user_id', 'dish', 'overall', 'notes', 'createdAt', 'updatedAt']
})
const average = await Sequelize.fn('AVG', Sequelize.col('overall'))
res.sendStatus(200);
} catch (error) {
console.log(error);
res.status(500).json(error)
}
}
I think average function is
Sequelize.fn('AVG', Sequelize.col('overall'))
But I don't know how to call it on the model ratings