admin管理员组

文章数量:1295305

I'm working with indexedDB for local data storage, with Dexie.js which is pretty nice as a wrapper, especially because of the advanced queries. Actually, I would like to create to create several datastore by script, which seem plicated.

To create a new store, you would do something like :

db.version(2).stores({
    Doctors: "++" + strFields
});

If I do something like Doctors = "Hospital", it still creates the store with a name "Doctors".

Is there a way to do this?

Did anybody faced the same problem?

I'm working with indexedDB for local data storage, with Dexie.js which is pretty nice as a wrapper, especially because of the advanced queries. Actually, I would like to create to create several datastore by script, which seem plicated.

To create a new store, you would do something like :

db.version(2).stores({
    Doctors: "++" + strFields
});

If I do something like Doctors = "Hospital", it still creates the store with a name "Doctors".

Is there a way to do this?

Did anybody faced the same problem?

Share Improve this question edited Jul 30, 2018 at 15:08 SCdF 59.4k24 gold badges79 silver badges114 bronze badges asked Nov 18, 2014 at 13:42 JeeligJeelig 1022 silver badges15 bronze badges 2
  • The simplest method is to just use the indexedDB API. – Josh Commented Nov 20, 2014 at 22:48
  • 1 @Josh - Not really. I was using the direct API, and gave up on it. I am liking dexie must better. – Tom Stickel Commented Nov 21, 2019 at 4:49
Add a ment  | 

1 Answer 1

Reset to default 11

Let's say you want three object stores "Doctors", "Patients" and "Hospitals", you would write something like:

var db = new Dexie ("your-database-name");
db.version(1).stores({
    Doctors: "++id,name",
    Patients: "ssn",
    Hospitals: "++id,city"
});
db.open();

Note that you only have to specify the primary key and the required indexes. Primary key can optionally be auto-incremented ("++" prefixed). You could add as many properties to your objects as you wish without having to specify each one in the index list.

db.Doctors.add({name: "Phil", shoeSize: 83});
db.Patients.add({ssn: "721-07-446", name: "Randle Patrick McMurphy"});
db.Hospitals.add({name: "Johns Hopkins Hospital", city: "Baltimore"});

And to query different stores:

db.Doctors.where('name').startsWithIgnoreCase("ph").each(function (doctor) {
    alert ("Found doctor: " + doctor.name);
});

db.Hospitals.where('city').anyOf("Baltimore", "NYC").toArray(function (hospitals) {
   alert ("Found hospitals: " + JSON.stringify(hospitals, null, 4));
});

本文标签: javascriptIndexedDBDexie JSDynamically create storesStack Overflow