Thursday 14 April 2016

[Sequelize] Best practice for Select, Join and Aggregate function ?

I have three models, Item, User and Ratings.// mxn cardinality User.hasMany(Ratings); Ratings.belongsTo(User); Item.hasMany(Ratings); Ratings.belongsTo(Item); So basically a user can rate an item. Now what I want is something likeSELECT id, AVG(ratings.value) FROM items LEFT OUTER JOIN ratings on item.id = ratings.itemIdWhat's the best way of doing it in sequelize ? I don't want to write raw query as it steals away the benefit of ORM and might cause incompatibilities with different databases. (I have such requirement for my project)

Submitted April 14, 2016 at 08:15PM by bogas04

No comments:

Post a Comment