user13829188
user13829188

Reputation:

Conversion of an sql query to sequelize

I am trying to convert this sql query to sequelize. The sequelize query should have the same result as the sql query.

SELECT * FROM logs 
    WHERE createdAt >= '2020-06-24 00:00:00' 
        AND createdAt <= '2020-06-26 23:59:59'
        AND targetId = 192
        AND `type` = 1

#My current implementation

query = {
        [Op.and]: [
          { createdAt: { [Op.gte]: startDate } },
          { createdAt: { [Op.lte]: endDate } },
          { targetId: userId },
          { type: 1 },
        ],
      };

Upvotes: 0

Views: 308

Answers (1)

Rustam D9RS
Rustam D9RS

Reputation: 3491

To make a query like yours, you need something like the following:

{
    where: {
        createdAt: {
          [Op.gte]: startDate,
          [Op.lte]: endDate
        },
        targetId: userId,
        type: 1
    }
}

More details in the documentation.

Upvotes: 1

Related Questions