9

For a Chrome app, which stores data in IndexedDB, I have an object like this:

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"
        }
    ]
}

If I store the hole object in an objectStore, can I use an index for "track", which can be contained multiple times in each order object?

Or is it needed or possibly better/faster to split each object into multiple objectStores like know from relational DBs:

  • order
  • orderitem
  • parcel

The solution should also work in a fast way with 100.000 or more objects stored.

1 Answer 1

11

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 :(

Sign up to request clarification or add additional context in comments.

4 Comments

You are correct - the current Indexed DB API doesn't make this easy. I have a tracking bug for this issue here: github.com/w3c/IndexedDB/issues/35
i would say this is no bug, because it works like it is written in the specification. But it is really a missing feature... :(
Yeah, "issue" not "bug". BTW, I'm the editor of the specification. :)
looks like YOU forgot this feature ;)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.