首页 > 解决方案 > 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在包含内使用?任何帮助,将不胜感激。

标签: node.jssequelize.js

解决方案


我不确定这是你想要的。因为我不熟悉.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找到这个


推荐阅读