Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create index on array element in indexeddb

Tags:

indexeddb

I have an object,

var _data = {
        teacher : {
            name : "Mack",
            subject : "Maths"
        },
        Student : [{
            name : "Alex",
            stud_id : 1
        },{
            name : "Jack",
            stud_id : 2
        },{
            name : "Mark",
            stud_id : 3
        }]

}

I want to create index on stud_id, how can I create?

like image 246
User 780611 Avatar asked Jan 03 '14 08:01

User 780611


1 Answers

What I would do that is to have two separate object stores inside of your database, one which represents teachers and one which represents students.

Then I'd store a teacher object like:

{
    name: 'John Smith',
    subject: 'Maths',
    students: [1,2,3,4,5]
}

Then I'd have a store for students like:

{
    id: 1,
    name: 'Jane Doe'
}

I'd then have an id key path in both stores (autogenerated or not, depending on whether it syncs somewhere) so you have a unique identifier of what the data is.

Finally you can create an index on the students property of a teacher and set the multiEntry flag to true so that it is known to be an array and you can query the object store on it as an array, see here for more info on multiEntry indexes.

Note: At time of writing IE10 & IE11 do not support multiEntry indexes in IndexedDB.

like image 60
Aaron Powell Avatar answered Sep 21 '22 13:09

Aaron Powell