ClientRepository.js

var LocalDataStore = require('./LocalDataStore');
var CustomRepository = require('./CustomRepository');
/**
 * Repository for client-side data retrieve.
 * Implement:
 *
 *  - {@link ClientRepository#select} method able to return `array of object` representation of server entity
 *  - {@link ClientRepository#selectAsArray} method able to return `array of object` representation of server entity
 *  - {@link ClientRepository#selectAsStore} method able to return {UB.ux.data.UBStore} (applicable only for Ext-based client types)
 *
 * Usually created using {@link UB#Repository UB.Repository} fabric function. Example:
 *
 *      var store = UB.Repository('my_entity').attrs(['ID', 'code'])
 *       .where('code', 'includes', ['1', '2', '3'])  // code in ('1', '2', '3')
 *       .where('name', 'contains', 'Homer'). // name like '%homer%'
 *       .where('birtday', 'geq', new Date()).where('birtday', 'leq', new Date() + 10) //(birtday >= '2012-01-01') AND (birtday <= '2012-01-02')
 *       .where('[age] -10', '>=', {age: 15}, 'byAge') // (age + 10 >= 15)
 *       .where('', 'match', 'myvalue'). // for condition match expression not need
 *       .logic('(byStrfType OR bySrfKindID)AND(dasdsa)')
 *       .select().done(function(response){
 *          // here response is in [{ID: 10, code: 'value1'}, .... {}] format
 *       });;
 *
 *
 * @class ClientRepository
 * @extends CustomRepository
 * @author pavel.mash 23.09.2014
 */

/**
 * Create a new CustomRepository
 * @constructor
 * @param {UBConnection} connection
 * @param {String} entityName name of Entity we create for
 */
function ClientRepository(connection, entityName){
    //noinspection JSUnresolvedFunction
    CustomRepository.call(this, entityName);
    this.connection = connection;
}
ClientRepository.prototype = Object.create(CustomRepository.prototype);
ClientRepository.prototype.constructor = ClientRepository;

/**
 * Asynchronously run request, constructed by Repository. Return promise, resolved to `array of object` representation of response.
 *
 *      UB.Repository('ubm_navshortcut').attrs(['ID', 'code'])
 *      .where('code', 'in', ['uba_user', 'ubs_audit'])
 *      .selectAsObj().done(function(store){
 *          console.log(store); // output is [{"ID":3000000000004,"code":"uba_user"},{"ID":3000000000039,"code":"ubs_audit"}]
 *      });
 *
 * @param {{field: alias}} [fieldAlias] Optional object to change attribute names during transform array to object
 * @return {Promise}
 */
ClientRepository.prototype.selectAsObject = function(fieldAlias){
    return this.connection.select(this.ubql()).then(function(res){
        return LocalDataStore.selectResultToArrayOfObjects(res, fieldAlias);
    });
};

/**
 * Asynchronously run request, constructed by Repository. Return promise, resolved to `array of array` representation of response.
 * Actual data is placed to `resultData` response property.
 *
 *      UB.Repository('ubm_navshortcut').attrs(['ID', 'code'])
 *      .where('code', 'in', ['uba_user', 'ubs_audit'])
 *      .select().done(UB.logDebug);
 *      // output is {"resultData":{"data":[[3000000000004,"uba_user"],[3000000000039,"ubs_audit"]],"fields":["ID","code"]},"total":2}
 *
 * Response MAY (but may not even for the same request) contain other variables, returned by server in case data retrieved not from cache
 *
 *      UB.Repository('uba_user').attrs(['ID', 'name', 'ID.name']) // since uba_user have `unity` mixin it ID property point us to parent (`uba_subject` in this case)
 *      .selectAsArray().done(UB.logDebug);
 *      // {"entity":"uba_user","fieldList":["ID","name","ID.name"],"method":"select","resultData":{"fields":["ID","name","ID.name"],"rowCount":1,"data":[[10,"admin","admin"]]},"total":1}
 *
 * But resultData is always present
 *
 * @return {Promise}
 */
ClientRepository.prototype.selectAsArray = function(){
    return this.connection.select(this.ubql());
};

/**
 * For core module (without Ext) - do the same as {ClientRepository.selectAsObj}
 *
 * For EntJS based client (actual implementation in {UB.ux.data.UBStore}) - create store based on request, constructed by Repository.
 * Return promise resolved to loaded {UB.ux.data.UBStore} instance.
 *
 *      UB.Repository('ubm_navshortcut').attrs(['ID', 'code']).where('code', 'in', ['uba_user', 'ubs_audit'])
 *      .selectAsStore().done(function(store){
 *          console.log(store.getTotalCount()); // here store is UB.ux.data.UBStore instance
 *      });
 *
 * @param {Object} [storeConfig] optional config passed to store constructor
 * @return {Promise}
 */
ClientRepository.prototype.selectAsStore = ClientRepository.prototype.selectAsObject;

/**
 * Alias to {ClientRepository.selectAsObject}
 */
ClientRepository.prototype.select = ClientRepository.prototype.selectAsObject;

module.exports = ClientRepository;