@unitybase/base/LocalDataStore

Helper class for manipulation with data, stored locally in (TubCachedData format).

This module shared between client & server. In case of server we use it together with dataLoader, in case of client - inside UBConnection#select to handle operations with entity data cached in IndexedDB.

For server-side samples see ubm_forms.doSelect method implementation.

Client-side sample:

    $App.connection.run({
            entity: 'tst_IDMapping',
            method: 'addnew',
            fieldList: ['ID', 'code']
       }).done(function(result){
            // here result in array-of-array format: [{"entity":"tst_IDMapping","method":"addnew","fieldList":["ID","code"],"__fieldListExternal":["ID","code"],"resultData":{"fields":["ID","code"],"rowCount": 1, "data":[[3500000016003,null]]}}]
            var objArray = UB.LocalDataStore.selectResultToArrayOfObjects(result); // transform array-of-array result representation to array-of-object
            console.log(objArray); // now result in more simple array-of-object format: [{ID: 12312312312, code: null}]
       });

Methods

doFilterAndSort(cachedData, ubRequest) → * static

Perform local filtration and sorting of data array according to ubRequest whereList & order list
Arguments:
  1. cachedData (TubCachedData)  Data, retrieved from cache
  2. ubRequest (TubSelectRequest)  Initial server request

byID(cachedData, IDValue) static

Just a helper for search cached data by row ID
Arguments:
  1. cachedData (TubCachedData)  Data, retrieved from cache
  2. IDValue (Number)  row ID.

doFiltration(cachedData, ubRequest) → Array.<Array> protected static

Apply ubRequest.whereList to data array and return new array contain filtered data
Arguments:
  1. cachedData (TubCachedData)  Data, retrieved from cache
  2. ubRequest (TubSelectRequest)

doSorting(filteredArray, cachedData, ubRequest) protected static

Apply ubRequest.orderList to inputArray (inputArray is modified)
Arguments:
  1. filteredArray (Array.<Array>)
  2. cachedData (TubCachedData)
  3. ubRequest (Object)

selectResultToArrayOfObjects(selectResult, fieldAliasopt) → Array.<*> static

Transform result of UBConnection#select response from Array of Array representation to Array of Object.

 LocalDataStore.selectResultToArrayOfObjects({resultData: {
     data: [['row1_attr1Val', 1], ['row2_attr2Val', 22]],
     fields: ['attrID.name', 'attr2']}
 });
 // result is:
 // [{"attrID.name": "row1_attr1Val", attr2: 1},
 //  {"attrID.name": "row2_attr2Val", attr2: 22}
 // ]

 // object keys simplify by passing fieldAliases
 LocalDataStore.selectResultToArrayOfObjects({resultData: {
     data: [['row1_attr1Val', 1], ['row2_attr2Val', 22]],
     fields: ['attrID.name', 'attr2']}
 }, {'attrID.name': 'attr1Name'});
 // result is:
 // [{attr1Name: "row1_attr1Val", attr2: 1},
 //  {attr1Name: "row2_attr2Val", attr2: 22}
 // ]
Arguments:
  1. selectResult (Object)
  2. [fieldAlias] (Object.<string, string>)  Optional object to change attribute names during transform array to object. Keys are original names, values - new names

flatten(requestedFieldList, cachedData) static

Flatten cached data (or result of LocalDataStore#doFilterAndSort.resultData ) to Object expected by TubDataStore.initialize Flatten format (faster than [{}..] format).

    //consider we have cached data in variable filteredData.resultData
    // to initialize dataStore with cached data:
    mySelectMethod = function(ctxt){
        var fieldList = ctxt.mParams.fieldList;
        resp = LocalDataStore.flatten(fieldList, filteredData.resultData);
        ctxt.dataStore.initFromJSON(resp);
    }

cachedData may contain more field or field in order not in requestedFieldList - in this case we use expectedFieldList

Arguments:
  1. requestedFieldList (Array.<string>)  Array of attributes to transform to. Can be ['*'] - in this case we return all cached attributes
  2. cachedData (TubCachedData)

arrayOfObjectsToSelectResult(arrayOfObject, attributeNames) → Array.<Array> static

Reverse conversion to LocalDataStore#selectResultToArrayOfObjects Transform array of object to array of array using passed attributes array

 LocalDataStore.arrayOfObjectsToSelectResult([{a: 1, b: 'as'}, {b: 'other', a: 12}], ['a', 'b']);
 // result is: [[1,"as"],[12,"other"]]
Arguments:
  1. arrayOfObject (Array.<Object>)
  2. attributeNames (Array.<String>)

Type Definitions

TubSelectRequest

Format for UBQ select request
Properties:
Name Type Description
fieldList Array.<String>

Array of entity attribute names

whereList Object

Where clauses

orderList Object

Order clauses

options Object

Options

ID Number

ID

TubCachedData

Format for data, stored in client-side cache
Properties:
Name Type Attributes Description
data Array.<Array>
fields Array.<String>
rowCount Number
version Number <optional>

A data version in case mi_modifyDate is in fields