IndexedDB-Dexie JS:动态创建商店 [英] IndexedDB - Dexie JS : Dynamically create stores

查看:380
本文介绍了IndexedDB-Dexie JS:动态创建商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用indexedDB进行本地数据存储,使用Dexie.js作为包装程序非常好,特别是由于高级查询. 其实,我想通过脚本创建多个数据存储,这看起来很复杂.

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 complicated.

要创建新商店,您可以执行以下操作:

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

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

如果我做Doctors ="Hospital"之类的事情,它仍然会创建一个名为"Doctors"的商店.

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?

推荐答案

假设您要三个对象存储区医生",患者"和医院",则可以这样写:

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"});

要查询其他商店:

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));
});

这篇关于IndexedDB-Dexie JS:动态创建商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆