[英]Filter Sequelize with date-fns
我正在嘗試使用date-fns
fns 的isToday
方法來過濾今天保存在數據庫中的記錄。 我可以在 sequelize 查詢中執行此操作嗎? 在我嘗試做的方式之下:
import { isToday } from 'date-fns';
import Buy from '../models/Buy';
class VolumeController {
// Exibir o total de Bitcoins comprados e vendidos no dia
async show(req, res) {
const buys = await Buy.findAll({
where: {
created_at: isToday(created_at),
},
attributes: ['id'', 'buy_value', 'quantity', 'created_at'],
});
return res.json({ buys });
}
}
我解決了它如下。 我使用了date-fns
fns 庫的startOfDay
和endOfDay
方法。 我還需要sequelize
庫中的between
運算符。
import { Op } from 'sequelize';
import { startOfDay, endOfDay } from 'date-fns';
import Buy from '../models/Buy';
class VolumeController {
// Exibir o total de Bitcoins comprados e vendidos no dia
async show(req, res) {
const today = new Date();
const buys = await Buy.findAll({
where: {
created_at: {
[Op.between]: [startOfDay(today), endOfDay(today)],
},
},
attributes: ['id', 'account_id', 'buy_value', 'quantity', 'created_at'],
});
return res.json({ buys });
}
}
export default new VolumeController();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.