如何在服务器端顺序过滤表的每个字段(nodejs orm)

问题描述 投票:0回答:2

我有些麻烦,要找出如何在序列化ORM中执行过滤部分。我已经做了,服务器端分页和排序,请按照Angular https://blog.angular-university.io/angular-material-data-table/的本教程>

我正在使用Sequelize 5.8.5,但不在打字稿中使用。

但是,在我的情况下,后端在使用Sequelize的nodejs中。我想在连接表的每个字段上搜索过滤器字符串参数。如果“字符串”(例如field1)或“字符串”(例如field2或....等)

这是我的router.sequelize。

router.get('', auth.checkToken, function(req, res) {
     let options = {
         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
         }]
     };
     if (req.query.filter) {
         options.where = {
             $or: [
                 { id: { $like: req.query.filter}},
                 { title: { $like: req.query.filter}},
                 { description: { $like: req.query.filter}},
                 { start_date: { $like: req.query.filter}},
             ]
         };
     }
     if (req.query.sort) {
         options.order = [['id', req.query.sort || 'DESC']];
     }
     if (req.query.page && req.query.pageSize) {
         options.offset = req.query.page * req.query.pageSize;
         options.limit = parseInt(req.query.page, 10) + parseInt(req.query.pageSize, 10);
     }
     models.intervention_maincourante
         .findAll(options)
         .then(all => {
             res.send(all);
         });
 });

此部分不起作用:

if (req.query.filter) {
     options.where = {
         $or: [
             { id: { $like: req.query.filter}},
             { title: { $like: req.query.filter}},
             { description: { $like: req.query.filter}},
             { start_date: { $like: req.query.filter}},
         ]
     };
 }

我也尝试过这种方式,因为有时运算符的别名无效吗?但这并没有改变任何东西。

[Op.or]: [
    { id: { [Op.like]: "%" + req.query.filter + "%"}},
    { title: { [Op.like]: "%" + req.query.filter + "%"}},
    { description: { [Op.like]: "%" + req.query.filter + "%"}},
    { start_date: { [Op.like]: "%" + req.query.filter + "%"}},
  ]

然后我尝试了:

options.where = {
  [Op.or]: [{
    title: {
      [Op.like]: "%" + req.query.filter + "%",
    },
    description: {
      [Op.like]: "%" + req.query.filter + "%",
    },
    start_date: {
      [Op.like]: "%" + req.query.filter + "%",
    }
  }]
};

文档中的内容:

{
    [Op.or]: [
        {
            title: {
                [Op.like]: 'Boat%'
            }
        },
        {
            description: {
                [Op.like]: '%boat%'
            }
        }
   ]
}

非常感谢,如果有人知道怎么做。

我有些麻烦,要找出如何在序列化ORM中执行过滤部分。我已经做了,服务器端分页和排序,按照Angular的本教程https://blog.angular -...

node.js sequelize.js filtering server-side
2个回答
0
投票

在Sequelize ORM中,当您使用$like运算符时,可以直接传递搜索字符串,也可以将其包装在% OR _中。如果要匹配字符串的某些部分,可以使用$substring运算符。

如果您的查询是像这样的纯字符串,例如'examplematch',则$ like运算符将查找完全匹配。


0
投票

如您在注释中所建议的那样,查询不使用过滤器进行记录。因此,假设应该有任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.