Home > Net >  Using Sequelize load data of every hour of a day
Using Sequelize load data of every hour of a day

Time:10-29

how to load record of transactions of every hour.

if (ExpenseGraphTimeSeries === 'today') {
        let date = new Date(Date.now());
        let createdAt = {
          [Op.lt]: new Date(date),
          [Op.gte]: new Date().setHours(0, 0, 0, 0)
        };

        ExpenseGraphCondition.createdAt = createdAt
        ExpenseGraph = await model.Expense.findAll({
          where: createdAt,
          attributes: [
            [model.sequelize.fn('sum', model.sequelize.col('Amount')), 'expense'], 'createdAt'
          ],
        });
      }

Current out put is
| Amount | createdAt | | -------- | -------------- | | 2349 | 01-01-2020 00:00:00 | | 2349 | 02-01-2020 00:00:00 |

i need record of every hour with amount of that hour. how to do this? 

CodePudding user response:

group:[] is use in sequelize to return data by groups. your code will be like this

    if (ExpenseGraphTimeSeries === 'today') {
        let date = new Date(Date.now());
        let createdAt = {
          [Op.lt]: new Date(date),
          [Op.gte]: new Date().setHours(0, 0, 0, 0)
        };

        ExpenseGraphCondition.createdAt = createdAt
        ExpenseGraph = await model.Expense.findAll({
          where: createdAt,
          attributes: [
            [model.sequelize.fn('sum', model.sequelize.col('Amount')), 'expense'], 'createdAt'
          ],
          group: [model.sequelize.fn('hour', model.sequelize.col('createdAt'))],
          order: [['id', 'asc']], //optional
        });
      }

  • Related