Reputation: 9072
I want to do a statement like this in Sequelize.
Select * from table WHERE completed = 0 AND tracking = 1 AND ((FromNumber = +15625554444 AND ToNumber = +17145554444) OR (FromNumber = +17145554444 AND ToNumber = +15625554444))
How would I do this query in Sequelize? I looked in the documentation but am very confused and keep running into weird queries in my attempts thus far.
My actual code attempt thus far while experimenting is this, but it doesn't mean anything since it doesn't work right now. I am not sure what the syntax should look like for this type of AND/Or Scenario.
var setNumberData = await models["texts"].findOne({
where: {
completed: 0,
agentNumber: agentData.number,
trackingNumber: obj.To,
[Op.and]: [
{to: realTo},
{[Op.or] : [{from: agentData.number}]}
],
// [Op.and]: [{to: obj.To}],
// [Op.or]: [{from: obj.To}],
// [Op.and]: [{to: obj.From}]
}
});
Upvotes: 5
Views: 3780
Reputation: 9072
I figured it out! The key is the [] are like the parenthesis for the condition in question.
var setNumberData = await models["texts"].findOne({
where: {
completed: 0,
agentNumber: agentData.number,
trackingNumber: obj.To,
[Op.or]: [
{[Op.and] : [
{from: obj.From},
{to: realTo}
]},
{[Op.and] : [
{from: realTo},
{to: obj.From}
]}
]
}
});
Upvotes: 14