如何从IndexedDb对象存储中获取多个键值 [英] How to get multiple key value from IndexedDb objectstore

查看:79
本文介绍了如何从IndexedDb对象存储中获取多个键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以建议如何从IndexedDB对象存储中获取多个键值吗?我做了多个选择,但没有一个没有用. 在此处创建indexedDb

Can somebody suggest how to get multiple key value from IndexedDB objectstore ? I have done multiple options, but didn't work none. Created indexedDb here

$provide.value('dbModel', {
        name: 'TestDB',
        version: '1',
        instance: {},
        objectStoreName: {
            dashboards: 'Appointment'
        },
        upgrade: function (e) {
            var db = e.target.result;
            if (!db.objectStoreNames.contains('Appointment')) {
                var calendarobj = db.createObjectStore('Appointment', {
                    keyPath: 'AppointmentId'
                });
                calendarobj.createIndex("AppointmentId", "AppointmentId", { multiEntry: true });
            }
        }
    });

数据看起来像

KeyPath-AppointmentId             Value
=====================             =====
1                                 Test 1
2                                 Test 2
3                                 Test 3

我有getObjectStore方法来获取objectstorename实例.

I have getObjectStore method to get the objectstorename instance.

getObjectStore: function (objectStoreName, mode) {
            var modeact = mode || _db.transactionTypes.readonly;
            var txn = _db.instance.transaction(objectStoreName, modeact);
            var store = txn.objectStore(objectStoreName);

            return store;
        }


var keys = [1,2]; // Want to get the First two record which has value 'Test 1' and 'Test 2'
var store = _db.getObjectStore(objectStoreName);
var tagIndex = store.index(store.keyPath); //AppointmentId

var request = tagIndex.openCursor(IDBKeyRange.only(keys));
//var request = tagIndex.get(keys);

request.onsuccess = function (event) {
                console.log(event.result);
}

推荐答案

按您的方式使用[1, 2]将不起作用-这是一个键,它恰好是具有两个成员的数组.索引数据库目前无法理解使用列表或键集进行查询.

Using [1, 2] as you do won't work - that's a key which happens to be an array with two members. Indexed DB currently doesn't understand using lists or sets of keys for querying.

您有很多选择:

1-并行发出两个get请求. (由于可以保证顺序,因此第二个请求将在第一个请求之后完成,并且您将知道两个结果都已返回.)

1 - Issue two get requests in parallel. (Since the order is guaranteed, the second request will finish after the first and you know both results will have been returned.)

var results = [];
store.get(1).onsuccess = function(e) {
  results.push(e.target.result);
};
store.get(2).onsuccess = function(e) {
  results.push(e.target.result);

  // results will have the values
};

2-使用光标和范围:

2 - Use a cursor and a range:

var results = [];
store.openCursor(IDBKeyRange.bound(1, 2)).onsuccess = function(e) {
  var cursor = e.target.result;
  if (cursor) {
    results.push(cursor.value);
    cursor.continue();
  } else {
    // results will have the values
  }
};

3-将getAll与某个范围一起使用(仅适用于较新的浏览器-如果不可用,则退回到光标)

3 - Use getAll with a range (newer browsers only - fall back to a cursor if not available)

store.getAll(IDBKeyRange.bound(1, 2)).onsuccess = function(e) {
  // e.target.result will have the entries
};

请注意,在使用范围的选项2和3中,如果存在记录,则还将获得键为1.5的记录.

Note that in options 2 and 3 which use a range you would also get a records with a key of 1.5 if one existed.

这篇关于如何从IndexedDb对象存储中获取多个键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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