Norseback
Norseback

Reputation: 315

Sequelize - nested where on include?

I have this particular code that I want to convert using sequelize logic. I am converting from .NET to 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;
        }

Problem is, I don't know why putting an [Op.or]] within an include key gives out an error. Here's my current code so far:

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;
}

If I placed a where: key inside the objects, Sequelize will treat it as WHERE .. AND .. WHERE .. AND .. and so on. Is it possible to use WHERE .. OR .. WHERE .. OR inside the include? Any help would be appreciated.

Upvotes: 0

Views: 1364

Answers (1)

eclat
eclat

Reputation: 46

I am not sure this is what you want. Cause I am not familiar with .Net

But...

If you want to put your [Op.or] condition in where like this...

SELECT *
FROM NotificationApp
left join Task on ~~
left join Comment on ~~ 
where NotificationAppTypeId = (value) 
AND (Task.ProjectId = ProjectId OR Comment.ProjectId = ProjectId OR... )

I think this might be work.

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;
}

I find this $nested.column$ at this_link

Upvotes: 2

Related Questions