Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize how to find rows with multiple where clauses and timestamp > NOW()

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!

like image 983
lakenen Avatar asked Dec 05 '11 18:12

lakenen


3 Answers

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

like image 138
sdepold Avatar answered Oct 20 '22 00:10

sdepold


Another method:

Session.find({
  where: {
    user_id: someNumber,
    token: someString,
    expires: {
      $gt: (new Date())
    }
  }
}).on('success', function (s) { /* things and stuff */ });
like image 33
Peter Kingsbury Avatar answered Oct 19 '22 23:10

Peter Kingsbury


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"}] }
like image 2
JR Smith Avatar answered Oct 19 '22 23:10

JR Smith