Jee*_*lig 2 javascript indexeddb dexie
我正在使用indexedDB进行本地数据存储,使用Dexie.js作为包装器非常好,特别是因为高级查询.实际上,我想通过脚本创建几个数据存储区,这看起来很复杂.
要创建新商店,您可以执行以下操作:
db.version(2).stores({
Doctors: "++" + strFields
});
Run Code Online (Sandbox Code Playgroud)
如果我做了像Doctors ="Hospital"这样的事情,它仍然会创建一个名为"Doctors"的商店.
有没有办法做到这一点?
有人遇到过同样的问题吗?
假设你想要三个对象商店"医生","病人"和"医院",你会写下这样的东西:
var db = new Dexie ("your-database-name");
db.version(1).stores({
Doctors: "++id,name",
Patients: "ssn",
Hospitals: "++id,city"
});
db.open();
Run Code Online (Sandbox Code Playgroud)
请注意,您只需指定主键和所需的索引.主键可以选择自动递增("++"前缀).您可以根据需要向对象添加任意数量的属性,而无需在索引列表中指定每个属性.
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"});
Run Code Online (Sandbox Code Playgroud)
并查询不同的商店:
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));
});
Run Code Online (Sandbox Code Playgroud)