在包含之外对嵌套的渴望加载过滤进行排序

时间:2019-10-03 10:40:51

标签: node.js sorting sequelize.js filtering eager-loading

我很难找到如何使用sequelize对服务器端进行过滤,分页和排序的方法。

我正在将sequelize 5.8.5与javascript文件一起使用。

我找到了一个教程,该教程解释了如何创建分页处理程序。完成。现在,我想实现用于每个字段过滤的部分。

这是hasMany关联:

db.intervention.hasMany(db.equipment_intervention, {
    foreignKey: 'intervention_id'
});
db.intervention.hasMany(db.intervention_participation, {
    foreignKey: 'intervention_id'
});
db.intervention.hasMany(db.operating_range_do, {
    foreignKey: 'intervention_id'
});

这是findAll选项:

{
        raw : true, //  sequelize will not try to format the results of the query, or build an instance of a model from the result
        nested : true, // this will format your data
        where: {
          $or: [
            { id: { $like: req.params.filter }},
            { title: { $like: req.params.filter }},
            { description: { $like: req.params.filter }},
            { start_date: { $like: req.params.filter }},
          ]
        },
        offset: paginate({ page: req.params.pageNumber, pageSize: req.params.pageSize }).offset,
        limit: paginate({ page: req.params.pageNumber, pageSize: req.params.pageSize }).limit,
        order: [
          ['id', req.params.sortOrder],
        ],
        include: [{
            model: models.intervention,
            attributes: ['id','start_date','end_date','agent_id','state_id'],
            required: false,
            include: [
                {
                    model: models.agent,
                    required: false,
                },
                {
                    model: models.state,
                    required: false,
                },
                {
                    model: models.intervention_participation,
                    required: false,
                    include: [
                        {
                            model: models.agent,
                            attributes: ['id','access_token','agent_type_id','firstname','lastname','mail','password'],
                            required: false,
                            include: [
                                {
                                    model: models.shift_do,
                                    attributes: ['id','shift_id','agent_id','shift_date'],
                                    required: false,
                                    on: {
                                        'agent_id': {
                                            [Op.eq]: Sequelize.col('intervention->agent.id')
                                        },
                                        'shift_date': {
                                            [Op.eq]: Sequelize.col('intervention.start_date')
                                        },
                                    },
                                    include: [
                                        {
                                            model: models.shift,
                                            required: false,

                                        },
                                    ]
                                }
                            ]
                        },
                    ]
                },
                {
                    model: models.operating_range_do,
                    required: false,
                    include: [
                        {
                            model: models.operating_range,
                            required: false,
                        }
                    ]
                },
                {
                    model: models.equipment_intervention,
                    required: false,
                    include: [
                        {
                            model: models.equipment,
                            required: false,
                            include: [
                                {
                                    model: models.equipment_type,
                                    required: false,
                                    include: [
                                        {
                                            model: models.work_field,
                                            required: false,
                                        }
                                    ]
                                },
                                {
                                    model: models.equipment_location,
                                    required: false,
                                    include: [
                                        {
                                            model: models.structure,
                                            required: false,
                                        },
                                        {
                                            model: models.local,
                                            required: false,
                                        },
                                        {
                                            model: models.place,
                                            required: false,
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    model: models.intervention_helper,
                    required: false,
                    include: [
                        {
                            model: models.external_actor,
                            required: false,
                        }
                    ]
                }]
        },
        {
            model: models.work_type,
            required: false
        },
        {
            model: models.di_externe,
            required: false
        }

至少是分页处理程序:

const paginate = ({ page, pageSize }) => {
const offset = page * pageSize;
const limit = offset + pageSize;

return {
      offset,
      limit,
  };
};

0 个答案:

没有答案