Sequelize - 嵌套在哪里?
Sequelize - nested where on include?
我有这个特定的代码,我想使用 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]]
放在 include 键中会出错。到目前为止,这是我当前的代码:
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 ..
等等。是否可以在 include 中使用 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;
}
我在 this_link
找到这个 $nested.column$
我有这个特定的代码,我想使用 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]]
放在 include 键中会出错。到目前为止,这是我当前的代码:
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 ..
等等。是否可以在 include 中使用 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;
}
我在 this_link
找到这个$nested.column$