2015-06-10 99 views
0

由於JSONStore與Native中的Relational Database的行爲不同,我想了解JSONStore以及如何將數據存儲在JSONStore中。在JSONStore中有效存儲

例如,如果我有以下從服務器JSON響應,並想保存到離線存儲:

{ 
    "CID": "1020", 
    "CName": "Some names", 
    "Documents": [ 
     { 
      "DocumentID": "12987", 
      "FilePath": "anyPath1", 
      "Signatures": [ 
       { 
        "Path": "signPath1" 
       }, 
       { 
        "Path": "signPath2" 
       } 
      ] 
     }, 
     { 
      "DocumentID": "12988", 
      "FilePath": "anyPath2", 
      "Signatures": [ 
       { 
        "Path": "signPath3" 
       }, 
       { 
        "Path": "signPath4" 
       } 
      ] 
     } 
    ] 
} 

首先

我可以從其他論壇上學習,它說以線性方式存儲它們,但我認爲將相同的CID和DocumentID存儲多次(例如)會是多餘的,但我不確定如何存儲一些(金額無法定義)簽名:

var collections = {}; 
var collectionName = 'someName'; 
collections[collectionName] = {}; 
collections[collectionName].searchFields = 
{CID: 'string', CName: 'string', DocumentID: 'string', FilePath: 'string'}; 

或者,我可以用通常的關係型數據庫技術,做到這一點:

客戶

var customers = {}; 
var customersColl = 'customers'; 
customers[customersColl] = {}; 
customers[customersColl].searchFields = 
{CID: 'string', CName: 'string'}; 

文件

var documents = {}; 
var documentsColl = 'documents'; 
documents[documentsColl] = {}; 
documents[documentsColl].searchFields = 
{CID: 'string', DocumentID: 'string', FilePath: 'string'}; 

簽名

var signatures = {}; 
var signaturesColl = 'signatures'; 
signatures[signaturesColl] = {}; 
signatures[signaturesColl].searchFields = 
{DocumentID: 'string', SignaturePath: 'string'}; 

當使用這種技術,我相信這將是難以維持數據刪除。例如:

要刪除某個客戶,我需要在Documents中取得受尊重的客戶,並刪除Signatures中受尊重的文檔,並刪除Documents中的客戶,因爲沒有外鍵可以「自動化」它。

請給我一個建議,哪一個/什麼是最有效的方法。


從這個post我認爲最好將它們存儲線性太方法。

回答

0

您可以使用其他搜索字段來防止縮減,並且可以使文檔對象成爲實際的JSON對象。我在這裏有一個例子。

var collection = { 
    data : { 
     searchFields : {'DocumentID': 'string', 'FilePath': 'string', 'Signatures.Path': 'string'}, 
     additionalSearchFields : {'CID': 'string', 'CName': 'string'} 
    } 
}; 

var data = [ 
    { 
     'DocumentID': '1234', 
     'FilePath': 'anyPath1', 
     'Signatures': [ 
      { 
       'Path': 'signPath1' 
      }, 
      { 
       'Path': 'signPath2' 
      } 
     ] 
    }, 
    { 
     'DocumentID': '12988', 
     'FilePath': 'anyPath2', 
     'Signatures': [ 
      { 
       'Path': 'signPath3' 
      }, 
      { 
       'Path': 'signPath4' 
      } 
     ] 
    } 
]; 

var query = [ 
    { 
     'equal': [{DocumentID: '1234'}] 
    } 
]; 

WL.JSONStore.destroy() 
    .then(function (res) { 
     console.log('store destroyed') 
     return WL.JSONStore.init(collection); 
    }) 
    .then(function (res) { 
     console.log('collection was created'); 
     return WL.JSONStore.get('data').add(data, {'additionalSearchFields': {'CID': '1020', 'CName': 'Some names'}}); 
    }) 
    .then(function (res) { 
     console.log(res + ' doc(s) were added'); 
     return WL.JSONStore.get('data').advancedFind(query) 
    }) 
    .then(function (res) { 
     /* DOC1 
      [ 
       { 
        "_id": 1, 
        "json": { 
         "DocumentID": "1234", 
         "FilePath": "anyPath1", 
         "Signatures": [ 
          { 
           "Path": "signPath1" 
          }, 
          { 
           "Path": "signPath2" 
          } 
         ] 
        } 
      } 
     ] 
     */ 
     console.log('DOCS ' + JSON.stringify(res, null, 7)); 
     return WL.JSONStore.get('data').remove(res); 
    }) 
    .then(function (res) { 
     console.log(res + ' doc(s) was/were removed'); 
     return WL.JSONStore.get('data').findAll(); 
    }) 
    .then(function (res) { 
     /* 
      DOCS2 [ 
         { 
         "_id": 2, 
         "json": { 
            "DocumentID": "12988", 
            "FilePath": "anyPath2", 
            "Signatures": [ 
              { 
               "Path": "signPath3" 
              }, 
              { 
               "Path": "signPath4" 
              } 
            ] 
           } 
         } 
       ] 
     console.log('DOCS ' + JSON.stringify(res, null, 7)); 
     }) 
     .fail(function (err) { 
      console.log('ERR ' + JSON.stringify(err)); 
     }); 

讓我知道是否有問題。