【问题标题】:Store in JSONStore Effectively有效存储在 JSONStore 中
【发布时间】:2026-01-08 19:05:01
【问题描述】:

由于 JSONStore 与 Native 中的关系数据库的行为不同,我试图了解 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 的方法,我认为最好也将它们线性存储。

【问题讨论】:

    标签: javascript jsonstore


    【解决方案1】:

    您可以使用额外的搜索字段来防止重复,并且可以使您的文档对象成为实际的 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));
            });
    

    如果有问题请告诉我。

    【讨论】: