Sequelize OR condition object

后端 未结 6 1745
-上瘾入骨i
-上瘾入骨i 2020-12-08 03:38

By creating object like this

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


        
6条回答
  •  渐次进展
    2020-12-08 04:07

    In Sequelize version 5 you might also can use this way (full use Operator Sequelize) :

    var condition = 
    { 
      [Op.or]: [ 
       { 
         LastName: {
          [Op.eq]: "Doe"
          },
        },
       { 
         FirstName: {
          [Op.or]: ["John", "Jane"]
          }
       },
       {
          Age:{
            [Op.gt]: 18
          }
        }
     ]
    }
    

    And then, you must include this :

    const Op = require('Sequelize').Op
    

    and pass it in :

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

    It could beautifully generate SQL like this :

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

提交回复
热议问题