Farad
Farad

Reputation: 1065

Sequelize. How to find with many parameters

I have a postgre database with table User (firstname, lastname, age, profilePicture, gender, intention), and I have an object filter:

filter = {
        firstname: String
        lastname: String,
        withPhoto: Boolean,
        minAge: Integer,
        maxAge: Integer,
        isMale: Boolean,
        intention: String //sex/friendship/communication
    }

What is the best way to select users by this filter?

Upvotes: 0

Views: 69

Answers (1)

blex
blex

Reputation: 25634

If all your filters correspond to table fields, you could simply use where:

UserModel.find({ where: filter });

But since you have a minAge and maxAge, you can add a between operator:

UserModel.find({
  where: {
    firstname: filter.firstname
    lastname: filter.lastname,
    withPhoto: filter.withPhoto,
    isMale: filter.isMale,
    intention: filter.intention,
    age: {
      [Sequelize.Op.between]: [filter.minAge, filter.maxAge]
    }
  }
});

Upvotes: 1

Related Questions