const advancedResult =(model)=> async (req, res, next) => { //search : l'information que je cherche //fields : les colonnes je veux utiliser pour la recherche const fields = req.query.fields; const search = req.query.search; let page = req.query.page; let rows = req.query.rows; let reqQuery = {...req.query}; const noField = ['page','rows', 'fields', 'search','sort','select']; noField.forEach(param=> delete reqQuery[param]); //4. Create query string let queryStr = JSON.stringify(reqQuery); //5. Create operators mongo db query ['$lte'] = 30 queryStr = queryStr.replace(/\b(gt|gte|lt|lte|in|ne|or|nin)\b/g, match => `$${match}`); let find = JSON.parse(queryStr); console.log("varien", find); //Recherche if (fields) { //search = be // mark , model, matrcle //Recherche l'element search dans le tableau de propriété fields const query = { $or: fields.map(property => ({ [property]: {$regex: search, $options: 'i'} })) }; find = { $and: [ find, query, ] }; } //Pagination page = parseInt(page, 10) || 0; rows = parseInt(rows, 10) || 3000; const startIndex = page * rows; const endIndex = page * rows; const total = await model.find(find).countDocuments(); //declaration let query = model.find(find); query = query.skip(startIndex).limit(rows); // (5,8) 5,6,7,8,9,10,11, 12 //exécuté let results = await query; const pagination = {}; if (endIndex < total) { pagination.next = { page: page + 1, rows }; } if (startIndex > 0) { pagination.prev = { page: page - 1, rows }; } res.advancedResults = { success: true, count: results.length, pagination: pagination, data: results, startIndex: startIndex, endIndex: results.length, totalRecords: total, rows: Number(rows), page: page }; next(); }; module.exports = advancedResult;