我写了以下Sequelize片段:
return context.model.siteStaff.findAll({
where: Sequelize.where(
Sequelize.fn('date', Sequelize.col('createdAt')), {
[Op.eq]: moment(obj.date).toISOString()
})
});
Sequelize将其转换为:
SELECT "id",
"siteId",
"staffId",
"timeStamp",
"createdAt",
"updatedAt"
FROM "operations"."site_staff" AS "site_staff"
WHERE 1 = 1;
正是这个“哪里1 = 1”困扰着我。如何让Sequelize生成类似以下内容的内容:
SELECT "id",
"siteId",
"staffId",
"timeStamp",
"createdAt",
"updatedAt"
FROM "operations"."site_staff" AS "site_staff"
WHERE date(createdAt) = '...';
在我看来,这里有些基本内容。
答案 0 :(得分:0)
您可以对代码进行一些小的调整,因为在需要建立数据库连接的地方,您可以像这样在其中添加OP
import Sequelize from 'sequelize';
const sequelize = new Sequelize(process.env.DATABASE_URL, {
dialectOptions: {timeout: 30},
operatorsAliases: Sequelize.Op // THE OPERATOR PARTS
});
logger.appLog('Database connection established');
这样可以简化操作员,因此您不必使用[Op.eq]
。同样,在查询中,您可能会丢失Sequelize.
,因此您可以编写更像自然语言的代码,并且更易于阅读/理解。
所以你可以写这样的东西
context.model.siteStaff.findAll({
where: {
createdAt: moment(obj.date).utc()
}
});
这应生成
的输出SELECT "id", "siteId", "staffId", "timeStamp", "createdAt", "updatedAt"
FROM "operations"."site_staff" AS "site_staff"
WHERE "site_staff"."createdAt" = '2019-02-05 09:06:43.843 +00:00';