Tyler
Tyler

Reputation: 11509

Sequelize find this OR that

I'm trying to do a find with Sequelize that returns a record matching either a username or an email. Is it possible to do this with one call? I have something like:

User.find({
  where: { 
    username: "bryan", 
    email: "[email protected]"
  }
}).then(...)

But this does username AND email; I need username OR email to match.

I could easily do this in 2 DB calls, but is it possible to do it in 1?

Docs are here: Sequelize find

Upvotes: 1

Views: 284

Answers (2)

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28788

User.find({
  where: Sequelize.or({ username: 'bryan' }, { email: '[email protected]' })
});

http://sequelize.readthedocs.org/en/latest/api/sequelize/#orargs-sequelizeor

Upvotes: 1

Tyler
Tyler

Reputation: 11509

I still don't know if it's built into Sequelize, but good old SQL does the trick:

User.find({
  where: ["username = ? OR email = ?", "bryan", "[email protected]"]
}).then(...)

Open to marking a correct answer that shows it through a Sequelize syntax as well.

Upvotes: 0

Related Questions