vendredi 31 mars 2023

how can i preform sorted pagination when need to fetch data from multiple micro services?

lets for example look at this 2 micro service :


Micro service that handle cars orders:

Table:

  • orders [order_id, car_id, date,...]

Api: /GET /orders


Micro service that handle cars data:

  • cars [car_id, color ,price,...]

Api: /GET /cars


Now the problem is :

if i want at my UI app to show the merged data paginate and sorted ?

[order_id ,car_id ,color ,date ,price .....]

for example :

First scenario : first 500 rows , sorted by order_id

  • need to fetch data from orders api
  • then take the car_id and enrich the data from cars api

Second scenario : first 500 rows , sorted by car_id

  • need to fetch data from cars api
  • then take the order_id and enrich the data from orders api

i implement the code the fetching data this why:

const sortByOrder=(sortedBy)=>{
    return ORDER_FIELDS.includes(sortedBy) 
}
const sortByOrCars=(sortedBy)=>{
    return ORDER_FIELDS.includes(sortedBy) 
}
async function fetchOrderData(sortedBy,batchSize){
    
    if(sortByOrder(sortedBy)){
        const orderData = await fetchOrderData(sortedBy,batchSize);
        const orderIds = orderData.map(order=>order.id);
        const cars = await fetchCars(orderIds);
        return joinOrderAndCars(orderData,cars);
    }
    
    if(sortByOrCars(sortedBy)){
        const caersData = await fetchCarsData(sortedBy,batchSize);
        const carsIds = carsData.map(car=>car.id);
        const orders = await fetchOrders(carsIds);
        return joinOrderAndCars(orders,carsData);
    }
}

this is the naive approach it hard to maintain

  • for example to enrich data from third api

looking for a more genreal solution , or pattern and to learn from others that and encounter this issue

Aucun commentaire:

Enregistrer un commentaire