admin管理员组

文章数量:1279016

For a Chrome app, wich stores data in IndexedDB, i have a 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.

For a Chrome app, wich stores data in IndexedDB, i have a 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.

Share Improve this question edited Apr 29, 2016 at 21:29 Lutz asked Apr 28, 2016 at 17:26 LutzLutz 8501 gold badge10 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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",
                 "pany": "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 plicated. 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 :(

本文标签: javascriptIndexing array values in an object in an IndexedDBStack Overflow