Tuesday, 2 May 2017

Sequelize query to get records not in associations

There are two tables userMedium and medium. medium hasMany userMedia. For a user, wanna get the medium records that not included in the userMedia. With the Sequelize library, it was implemented as:

const um = await Models.UserMedium.findAll({
    where: {userId},
    attributes: ['mediumId']
  }),
     media = await Models.Medium.findAll({
          where: {
            id: {
              $notIn: _.pluck(um, 'mediumId')
            }
          }
        });

Wonder is there a better way to run the query by using the eager loading?



via Yujun Wu

No comments:

Post a Comment