Sequelize how to find rows with multiple where clauses and timestamp > NOW()
-
28-10-2019 - |
Question
How do I do this with Sequelize?
SELECT FROM sessions WHERE user_id = ? AND token = ? AND expires > NOW()
Here's what I'm trying to do (assume Session
is a Sequelize model):
Session.find({
where: {
user_id: someNumber,
token: someString,
//expires > NOW() (how do I do this?)
}
}).on('success', function (s) { /* things and stuff */ });
Thanks!
Solution
did you try to use the array notation of finders in sequelize?
Session.find({
where: ['user_id=? and token=? and expires > NOW()', someNumber, someString]
}).on('success', function (s) { /* things and stuff */ });
You can checkout this page: http://sequelizejs.com/?active=find-objects#find-objects
Hope this works. Otherwise it's a bug :D
OTHER TIPS
Please note that as of this posting and the most recent version of sequelize, the above answers are out of date. I am posting the solution that I got working in hopes of saving someone some time.
filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };
Note the $ and array inside the JSON object and lack of ? token replacement.
Or put in a more general way, since that might help someone wrap their head around it:
{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }
Another method:
Session.find({
where: {
user_id: someNumber,
token: someString,
expires: {
$gt: (new Date())
}
}
}).on('success', function (s) { /* things and stuff */ });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow