Recover DB data from a certain date

Asked

Viewed 99 times

-1

I am creating an api in Node.js, using express and Sequelize as ORM. I would like to know, which expression should I use to recover DB data from a certain date. I’m using the date-fns to determine which exact date would be the beginning of this week and storing the value in a variable, called thisWeek, returning:

2019-12-29T03:00:00.000Z

How do I use this variable - thisWeek - to return only the results in the database from that date? Below is the code snippet that I am using to recover DB data.

const checkQtt = await Checkin.findAll({
      where: { student_id: id }
    });

1 answer

0

I found a solution that solved my problem:

I used Sequelize Operators. For that, I imported it:

import { Op } from "sequelize";

And in the code snippet where I retrieve the db data and store it in the variable, I used the following expression - [Op.gt] -, where I return only lines in which created_at is higher than the date of my variable thisWeek:

const checkQtt = await Checkin.findAll({
      where: {
        student_id: id,
        created_at: {
          [Op.gt]: thisWeek
        }
      }
    });

Any questions about working with operators in Sequelize can be resolved from the documentation: https://sequelize.org/master/manual/querying.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.