Home > Back-end >  Query model based on value inside one of its columns
Query model based on value inside one of its columns

Time:01-30

I have a sequelize model called staff. One of the staff columns is called locations which holds the id's of all the locations this staff is available at. I want to create a query that uses a Location ID to get all staff active at that location. How can I do that using the ORM?

CodePudding user response:

In Sequelize, you can perform a query to filter staff based on their locations using the include and where options in your query.

const staff = await Staff.findAll({
  include: [
    {
      model: Location,
      as: 'locations',
      where: { id: locationId },
      through: { attributes: [] }
    }
  ]
});

CodePudding user response:

const findStaff = await StaffModel.findAll({ where: { locationId: "your locationId" } })

  • Related