sequelize.jseager-loading

How to hide included nested model from query results


I would like to include nested models in the findOptions interface for my Sequelize query so I can set conditions, but I don't want them displayed as result. I know this has been already asked, but none of the suggested solutions worked for me. My example situation, I have 3 tables, where a manager can have n employees and an employee has n documents

Tables
Managers -> Employees (1:n)
Employes -> Documents (1:n)

I would like to run a query that shows only IDs of documents relevant to specific managers provided as an input. This is the findOptions configuration I am using where running a FindAll command on Documents table

findOptions = {
    include: {
        model: Employee,
        required: true,
        as: 'employee',
        attributes: { include: [] },
        include: {
            model: Manager,
            as: 'manager',
            attributes: { include: [] }, 
            where: {
                managerId: { [Op.in]: authorized.managerIds }
            }
        }
    }
}; 

However, for some reason, while the documents returned as a result are correct, response contains also whole Manager and Employee objects nested inside, like this.

[{
        "documentId": 3,
        "employee": {
            "employeeId": 2,
            "manager": {
                "managerId": 3
            }
        }
}]

What am I doing wrong and how to get rid of that whole employee object nested inside, so the result only look like this?

[{ "documentId": 3 }]

Solution

  • To exclude nested model attributes simply indicate attributes: [], in include option:

    include: {
                model: Manager,
                as: 'manager',
                attributes: [], 
                where: {
                    managerId: { [Op.in]: authorized.managerIds }
                }
            }