16

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!

lakenen
  • 3,436
  • 5
  • 27
  • 39

3 Answers3

22

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

sdepold
  • 6,171
  • 2
  • 41
  • 47
  • Ahh, my bad... I guess I didn't realize you could have more than one replacement using that method. Thanks! – lakenen Dec 05 '11 at 19:24
2

Another method:

Session.find({
  where: {
    user_id: someNumber,
    token: someString,
    expires: {
      $gt: (new Date())
    }
  }
}).on('success', function (s) { /* things and stuff */ });
2

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"}] }
JR Smith
  • 1,186
  • 15
  • 20
  • I'm happy to change the accepted answer, but I don't have time to test this right now. Perhaps if @sdepold wants to chime in? – lakenen May 05 '15 at 18:05