Search code examples
.netasp.net-core.net-coreodata

OData to fetch more columns, without refetching already fetched columns


I have a requirement in which we have to perform certain filter operations and hiding and showing some columns in a huge chunk of data, for better performance I thought to give a try to OData.

I Went through some blogs where it was mentioned that using OData we can build queries on the fly which will help if suppose we have specific no. of columns to display thus to fetch only those columns.

Here I discovered a new scenario in which initially we are displaying a certain set of columns (suppose 25 columns) if UI we gave an option to select few more columns and on that selection we need to display data for those newly added columns(10 more columns). Thus following the ODATA approach I will create a fresh query consisting of all the columns (25 old + 10 new) which will again fetch all 35 columns. Now as the data for 25 columns were already fetched is there a way to fetch only the newly required columns from the same API call which will bring all the 35 columns data without re-fetching the already existing 25 columns data.

Though this can be done if I fetch new columns data and perform some join operation and display the final data. (You may find this silly)But the motive of this question is does OData somehow keep a track of already fetched data so that it will only fetch newly required data.


Solution

  • I think you can check if your objects contain already a column. If not, then add it in a list and send request again to api.

    example :

        var data: any;
        var columns = ['col1', 'col2', ... 'col35']
        var initialQueryParams = columns.slice(0, 20) // get only 20 columns
    
        // 1. initial query : 
        api/controller/method?queryParam$select(id, col1, col2, ... col20)
        // data = result of api request
    
        var newCols: string[] = [];
        //2. check if your obj contain cols 'col21' ... 'col35'
        (!data[0].hasOwnProperty('col21')) {
          // if not, add to list
          newCols.push('col21');
        }
        // - second query: 
        api/controller/method?queryParam$select(id, newCols)
        data.forEach(item => {
               data['col21'] = result['col21'];
               ...
               data['col35'] = result['col35'];
        });
    

    For all requests, you need to have a primary key id which will be send with any request.

    You need to adjust a little the query string, I don't remind it perfectlly.