Store in JSONStore Effectively

154 views Asked by At

As JSONStore behaves differently with Relational Database in Native, I am trying to understand JSONStore and how the best way to storing data in it.

For example if I have below JSON response from server and would like to store to offline storage:

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

First

What I can learn from other forum, it says to store them linearly, but I believe that it will be redundant to store same CID and DocumentID several times (for example), although I am not sure how to store some (amounts cannot be defined) Signatures:

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

Second

Or I can do it by usual Relational Database technique:

Customers

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

Documents

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

Signatures

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

When use this technique, I believe it will be hard to maintain data removal. For example:

To remove certain customer, I need to get the respected customer in Documents and remove respected document in Signatures and remove the customer in Documents as there are no Foreign Key to 'automate' it.

Please give me advise which one/what is the most efficient way to do it.


Third

Approach from this post which I believe the best to store them linearly too.

1

There are 1 answers

0
Namfo On

You could use additional search fields as to prevent redudancy and you can make your document objects actual JSON objects. I have an example here.

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));
        });

Let me know if there are questions.