Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize OR condition object

By creating object like this

var condition= {   where:   {      LastName:"Doe",      FirstName:["John","Jane"],      Age:{        gt:18      }   }     } 

and pass it in

Student.findAll(condition) .success(function(students){  }) 

It could beautifully generate SQL like this

"SELECT * FROM Student WHERE LastName='Doe' AND FirstName in ("John","Jane") AND Age>18" 

However, It is all 'AND' condition, how could I generate 'OR' condition by creating a condition object?

like image 265
Morio Avatar asked Dec 20 '13 01:12

Morio


People also ask

How do you define an object in Sequelize?

You can use JSON datatype of field in Sequelize (this corresponds to JSON datatype in PostgreSQL). const User = sequelize. define('User', { name: { type: DataTypes. STRING, allowNull: false }, age: { type: DataTypes.

How do you create a Sequelized model?

Sequelize set up Install Sequelize database driver for the database you would like to use by running one of the commands below. Install npm package Sequelize-CLI. Create a project folder. In your project folder path, run the command below to initialize Sequelize in the folder.

How do you define an array of objects in Sequelize?

The easiest would be to create another table or entity and associate them as a 1:n relationship. For that matter add a new model in Sequelize and define its associations like described here: http://sequelizejs.com/articles/getting-started#associations You might have then 2 tables. One profile table having N pictures.


1 Answers

Seems there is another format now

where: {     LastName: "Doe",     $or: [         {             FirstName:              {                 $eq: "John"             }         },          {             FirstName:              {                 $eq: "Jane"             }         },          {             Age:              {                 $gt: 18             }         }     ] } 

Will generate

WHERE LastName='Doe' AND (FirstName = 'John' OR FirstName = 'Jane' OR Age > 18) 

See the doc: http://docs.sequelizejs.com/en/latest/docs/querying/#where

like image 57
Morio Avatar answered Sep 19 '22 08:09

Morio