条件 Op.or、Op.ne 和直接包含在 sequelize 和 nestjs 中
Conditional Op.or, Op.ne and direct include in sequelize and nestjs
我有一个controller的incoming state,可能是draft,published of something,现在我正在分别处理这三个函数,如何将它们组合在一起,形成一个更清晰的代码
if (state === 'published') {
return this.checklistModelService.getAllWhere(
{ '$checklistRevision.publishedAt$': { [Op.ne]: null } },
undefined,
{
include: { model: ChecklistRevisionSchema },
},
);
}
if (state === 'draft') {
return this.checklistModelService.getAllWhere(
{ '$checklistRevision.publishedAt$': { [Op.is]: null } },
undefined,
{
include: { model: ChecklistRevisionSchema },
},
);
}
return this.checklistModelService.getAll(undefined, {
include: { model: ChecklistRevisionSchema },
});
您可以这样做,首先将 findOptions 存储在一个单独的变量中,然后在查询中使用它。
var findOptions = {};
state
? (findOptions = {
where: {
'$checklistRevision.publishedAt$': {
[state === 'published' ? Op.ne : Op.is]: null,
},
},
include: { model: ChecklistRevisionSchema },
})
: (findOptions = {
include: { model: ChecklistRevisionSchema },
});
return this.checklistModelService.getAll(undefined, findOptions);
我有一个controller的incoming state,可能是draft,published of something,现在我正在分别处理这三个函数,如何将它们组合在一起,形成一个更清晰的代码
if (state === 'published') {
return this.checklistModelService.getAllWhere(
{ '$checklistRevision.publishedAt$': { [Op.ne]: null } },
undefined,
{
include: { model: ChecklistRevisionSchema },
},
);
}
if (state === 'draft') {
return this.checklistModelService.getAllWhere(
{ '$checklistRevision.publishedAt$': { [Op.is]: null } },
undefined,
{
include: { model: ChecklistRevisionSchema },
},
);
}
return this.checklistModelService.getAll(undefined, {
include: { model: ChecklistRevisionSchema },
});
您可以这样做,首先将 findOptions 存储在一个单独的变量中,然后在查询中使用它。
var findOptions = {};
state
? (findOptions = {
where: {
'$checklistRevision.publishedAt$': {
[state === 'published' ? Op.ne : Op.is]: null,
},
},
include: { model: ChecklistRevisionSchema },
})
: (findOptions = {
include: { model: ChecklistRevisionSchema },
});
return this.checklistModelService.getAll(undefined, findOptions);