如何在json商店中加入两个集合?

时间:2015-02-10 12:41:48

标签: ibm-mobilefirst jsonstore

我正在研究IBM Worklight。我需要访问两个集合中的数据。是否有任何解决方案加入2个集合并获得必填字段?

1 个答案:

答案 0 :(得分:0)

JSONSTORE无法合并集合。

然而,以下博文详细介绍了实现此目的的一种方法:https://mobilefirstplatform.ibmcloud.com/blog/2015/02/24/working-jsonstore-collections-join/

  1. 创建一个集合:

    var OrdersCollection = {
          orders: {
            searchFields: {
              order_id: 'integer',
              order_date: 'string'
            },
            additionalSearchFields: {
              customer_id: 'integer'
            }
          }
    };
    var CustomerCollection = {
          customers: {
              searchFields: {
                  customer_name : 'string',
                  contact_name : 'string',
                  country : 'string'
              },
              additionalSearchFields : {
                  customer_id : 'integer'
              }
            }
        };
    
  2. 使用其他搜索字段添加数据:

    var data = [
                            {order_id : 462, order_date : '1-1-2000'},
                            {order_id: 608, order_date : '2-2-2001'},
                            {order_id: 898, order_date : '3-3-2002'}
                            ];
                var counter = 0;
                var q = async.queue(function (task, callback) {
                    setTimeout(function () {
                        WL.JSONStore.get('orders').add(task.data, {additionalSearchFields: {customer_id: task.customer_id}});
                        callback(++counter);
                    },0);
                },1);
                for(var i = 0; i < data.length; i++){
                 q.push({data : data[i], customer_id: i+1}, function(counter){
                     console.log("Added Order Doc " + counter);
                 });
                }
            q.drain = function(){
                setTimeout(function() {
                    console.log("Finished adding order documents");
                    WL.JSONStore.get("orders").findAll({filter : ["customer_id", "order_id", "order_date"]})
                        .then(function (res) {
                            ordersCollectionData = res;
                            document.getElementById("orders").innerHTML = JSON.stringify(res, null, "\t");
                        })
                         .fail(function (err) {
                                console.log("There was a problem at " + JSON.stringify(err));
                        });
                },0);
            };
    
  3. 找到要合并的文件:

    WL.JSONStore.get('orders').findAll({filter : ['customer_id', 'order_id', 'order_date']})
                        .then(function (res) {
                            ordersCollectionData = res;
                        });
    
  4. 合并

    var shared_index = 'customer_id';
    var mergedCollection = [];
    mergedCollection =_.merge(customerCollectionData, ordersCollectionData, function(a, b){
            if(_.isObject(a) && (a[shared_index] == b[shared_index])) {
                return _.merge({}, a, b);
            }
    });