node.js - Sequelize - 嵌套在哪里?
问题描述
我有这个特殊的代码,我想使用 sequelize 逻辑进行转换。我正在从 .NET 转换为 Node:
public async Task<IEnumerable<NotificationAppEntity>> GetPagged(NotificatioAppGetPaggedReq req, Guid userId)
{
var res = await Context.NotificationApp
.Where(x => req.Types.Contains(x.NotificationAppTypeId))
.Include(x => x.CreatedBy).ThenInclude(x => x.UserPics)
.Include(x => x.Comment)
.Include(x => x.Task)
.Include(x => x.Goal)
.Include(x => x.NotificationAppType)
.Include(x => x.NotificationAppUserRead)
.Where(x => x.Task.ProjectId == req.ProjectId.Value || x.Comment.ProjectId == req.ProjectId.Value ||x.Goal.ProjectId == req.ProjectId.Value)
.OrderByDescending(x => x.CreatedAt)
.Skip(req.PagingParameter.Skip)
.Take(req.PagingParameter.Take)
.ToListAsync();
return res;
}
问题是,我不知道为什么[Op.or]]
在包含键中放置一个会导致错误。到目前为止,这是我当前的代码:
async getPagged(userId, body) {
const notificationApp = await db.NotificationApp.findAll({
where: { NotificationAppTypeId: body.NotificationAppTypeId },
offset: body.Skip,
limit: body.Take,
include: [
{
model: await db.User,
attributes: { exclude: hideAttributes },
as: 'CreatedBy',
include: { model: await db.UserPic },
},
{
model: await db.Comment,
},
{
model: await db.Task,
},
{
model: await db.Goal,
},
{
model: await db.NotificationAppType,
},
{
model: await db.NotificationAppUserRead,
},
],
order: [['CreatedAt', 'DESC']],
});
return notificationApp;
}
如果我where:
在对象中放置了一个键,Sequelize 会WHERE .. AND .. WHERE .. AND ..
以此类推。是否可以WHERE .. OR .. WHERE .. OR
在包含内使用?任何帮助,将不胜感激。
解决方案
我不确定这是你想要的。因为我不熟悉.Net
但...
如果你想把你的 [Op.or] 条件放在这样的地方......
SELECT *
FROM NotificationApp
left join Task on ~~
left join Comment on ~~
where NotificationAppTypeId = (value)
AND (Task.ProjectId = ProjectId OR Comment.ProjectId = ProjectId OR... )
我认为这可能是工作。
async getPagged(userId, body) {
const notificationApp = await db.NotificationApp.findAll({
where: {
NotificationAppTypeId: body.NotificationAppTypeId,
[Op.or] : [
{'$Task.ProjectId$' : ProjectId},
{'$Comment.ProjectId$' : ProjectId},
...your conditions
]
},
offset: body.Skip,
limit: body.Take,
include: [
{
model: await db.User,
attributes: { exclude: hideAttributes },
as: 'CreatedBy',
include: { model: await db.UserPic },
},
{
model: await db.Comment,
},
{
model: await db.Task,
},
{
model: await db.Goal,
},
{
model: await db.NotificationAppType,
},
{
model: await db.NotificationAppUserRead,
},
],
order: [['CreatedAt', 'DESC']],
});
return notificationApp;
}
我$nested.column$
在this_link找到这个
推荐阅读
- sql - ORA-00907: 缺少 LISTAGG 函数错误的右括号
- c# - Unity (C#) 中的错误 CS1061;我想改变脚本中的精灵
- html - 错误:InvalidPipeArgument:在 invalidPipeArgumentError 处管道“AsyncPipe”的“[object Object]”
- android - 如何在一个文档中存储多个图像(Firestore)Android
- python - 电子邮件正则表达式验证在 python 中失败
- parameters - 在 Sphinx 中将特定方法或函数参数标记为已弃用
- node.js - 如何将mongodb项目放入discord bot的变量中?
- node.js - Firebase 不允许删除所有用户
- java - 退出 GoogleFit
- c# - 我可以把这段代码写得更短吗?带循环什么的