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

最新更新