node.js - Use JOIN in Sequelize -


prompt how use join in sequelize ? have following code:

        db.user.findall({         attributes: ['id'],         where: {vipend: {lt: expiresvip}, vip: true},         limit: 100,         order: 'id'     }).then(function(users) { 

in terms of 4, looks this:

select "vkid" "id"  "user" "user"  "user"."vipend" < 1469699683 --expiresvip , "user"."vip" = true  order id  limit '100'; 

how achieve such result?

select "vkid" "id"  "user" "user"   left join "notification" on "notification"."userid" = "user"."vkid"  "user"."vipend" < 1469699683 , "user"."vip" = true   , "notification"."type" = 4 , "notification"."expiresvipdate" < 1469699683  order id  limit '100'; 

thank you!

you need define association models. ex: db.user.hasmany(db.notification, {foreignkey: 'userid'}); use properties include in sequelize:

db.user.findall({ include: [{     model: db.notification,     required: true//true inner join, false left outer join - default left outer join }]}).then(function(user) {}, function(err) { res.json(err, 400);}); 

you should consult more here http://docs.sequelizejs.com/en/latest/docs/associations/


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -