2

I'm on the way to build a Chrome app, dedicated to fulfill shop/marketplace orders. The orders come from marketplace APIs like Amazon MWS. The data is stored only in the app (with backup). The system need also to work in a quick way if 50.000 or 100.000 orders are stored.

So how to structure the IndexedDB for that?

Each order can have multiple ordered items. It can also have multiple parcel tracking codes and so on.

var simplifiedOrderObject = {
    "ordernumber": "123-12345-234",
    "name": "Mr. Sample",
    "address": "Foostreet 12, 12345 Bar York",
    "orderitems": [
        {
            "item": "brush",
            "price": "2.00"
        },
        {
            "item": "phone",
            "price": "30.90"
        }
    ],
    "parcels": [
        {
            "service": "DHL",
            "track": "12345"
        },
        {
            "service": "UPS",
            "track": "3254231514"
        }
    ]
}

Should i create only one objectStore, which holds the complete order data in one object/row like shown above? If i would do so: Is it possible and fast to have an index for search for a parcel number? - Again: multiple parcel numbers can be in each object/row.

Or is it better or even needed to do it like in relational DBs:

  • One ObjectStore for order
  • one for orderItem
  • one for parcels shipped ... and all of this is connected by having the same orderNumber?

Which is the faster and more efficient way?

1 Answer 1

1

Answering my own question: I have made some tests now. It looks like it is not possible to do this with that object in only 1 objectStore.

An other example object which would work:

var myObject = {
    "ordernumber": "123-12345-234",
    "name": "Mr. Sample",
    "shipping": {"method": "letter",
                 "company": "Deutsche Post AG" }
}

Creating an index will be done by:

objectStore.createIndex(objectIndexName, objectKeypath, optionalObjectParameters);

With setting objectKeypath it is possible to address a value in the main object like "name":

objectStore.createIndex("name", "name", {unique: false});

It would also be possible to address a value form a subobject of an object like "shipping.method":

objectStore.createIndex("shipping", "shipping.method", {unique: false});

BUT it is not possible to address values like the ones of "track", which are contained in objects, stored in an array. Even something like "parcels[0].track" to get the first value as index does not work.

Anyhow, it would be possible to index all simple elements of an array (but not objects).

So the following more simple structure would allow to create an index entry for each parcelnumber in the array "trackingNumbers":

var simplifiedOrderObject = {
    "ordernumber": "123-12345-234",
    "name": "Mr. Sample",
    "address": "Foostreet 12, 12345 Bar York",
    "orderitems": [
        {
            "item": "brush",
            "price": "2.00"
        },
        {
            "item": "phone",
            "price": "30.90"
        }
    ],
    "trackingNumbers": ["12345", "3254231514"]
} 

when creating the index with multiEntry set to true:

objectStore.createIndex("tracking", "trackingNumbers", {unique: false, multiEntry: true});

Anyhow, the missing of the possibility to index object values in arrays, makes using indexedDB really unneeded complicated. It's a failure in design. This forces the developer to do things like in relational DBs, while lacking all the possibilities of SQL. Really bad :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.