0

I am tying to do pagination call on database query, in normal single query it is working fine but scenario is like, there is 3 DB query in single API and also there is filtering after first or second DB call so how to achieve pagination in that ? I am using skip and limit for pagination DB query. ex.:

Pagination {
   output1 = Query1
   dataForQ2 = []  
    for (output of output1){
        if (output.isValid === true){
         continue;
          }
          dataForQ2.push(output);
     }
    output2 = Query2(dataForQ2) // Query2 is depends on filtered data
    dataForQ3 = [];
     for (output of output2){
        if (output.isValid === true){
         continue;
          }
          dataForQ3.push(output);
     }
    result = Query3(dataForQ3)  // Query3 is depends on filtered data of Query2
   
}

if i set Limit = 10 and skip = 0 on first query then Query1 give 10 record second query it can be 10,8 or 7 because of filtered data of Query1, in Query3 can be 10,5,4. but i want 10 record. how to solve this.

Tech : node, mysql, mongo

VipulVyas
  • 41
  • 4
  • If you cannot join the queries because that data is from different data sources, then you cannot simply filter the first query. Knowing your data, you have to make an assumption how many records you need in the 1st query to get at least 10 in the 3rd one. And if you still got less, then you need to go back to fetch more. – Shadow Dec 16 '21 at 14:37
  • yes, I am also thinking same but for next request like limit will be 10, skip will change from 10 to number which fetched records in first query. so it is right approach ? if we take skip 10 then data will duplicate. – VipulVyas Dec 17 '21 at 04:31
  • You have to somehow maintain which record from the first query was included in the final resultset. If paging is so important for this feature, then you may have to reconsider your data design and bring everything into one datastore, so you can link the data together properly and apply the page limits. – Shadow Dec 17 '21 at 09:53
  • I guess I should do changes in data design. thanks – VipulVyas Dec 17 '21 at 10:30

0 Answers0