У меня возникли проблемы с поиском фильтрации, разбиения на страницы и сортировки на стороне сервера с помощью 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,
};
};