IDBObjectStore 对象

IDBObjectStore 对象对应一个对象仓库(object store)。IDBDatabase.createObjectStore()方法返回的就是一个 IDBObjectStore 对象。

IDBDatabase 对象的transaction()返回一个事务对象,该对象的objectStore()方法返回 IDBObjectStore 对象,因此可以采用下面的链式写法。

  1. db.transaction(['test'], 'readonly')
  2. .objectStore('test')
  3. .get(X)
  4. .onsuccess = function (e) {}

属性

IDBObjectStore 对象有以下属性。

  • IDBObjectStore.indexNames:返回一个类似数组的对象(DOMStringList),包含了当前对象仓库的所有索引。
  • IDBObjectStore.keyPath:返回当前对象仓库的主键。
  • IDBObjectStore.name:返回当前对象仓库的名称。
  • IDBObjectStore.transaction:返回当前对象仓库所属的事务对象。
  • IDBObjectStore.autoIncrement:布尔值,表示主键是否会自动递增。

方法

IDBObjectStore 对象有以下方法。

(1)IDBObjectStore.add()

IDBObjectStore.add()用于向对象仓库添加数据,返回一个 IDBRequest 对象。该方法只用于添加数据,如果主键相同会报错,因此更新数据必须使用put()方法。

  1. objectStore.add(value, key)

该方法接受两个参数,第一个参数是键值,第二个参数是主键,该参数可选,如果省略默认为null

创建事务以后,就可以获取对象仓库,然后使用add()方法往里面添加数据了。

  1. var db;
  2. var DBOpenRequest = window.indexedDB.open('demo', 1);
  3. DBOpenRequest.onsuccess = function (event) {
  4. db = DBOpenRequest.result;
  5. var transaction = db.transaction(['items'], 'readwrite');
  6. transaction.oncomplete = function (event) {
  7. console.log('transaction success');
  8. };
  9. transaction.onerror = function (event) {
  10. console.log('transaction error: ' + transaction.error);
  11. };
  12. var objectStore = transaction.objectStore('items');
  13. var objectStoreRequest = objectStore.add({ foo: 1 });
  14. objectStoreRequest.onsuccess = function (event) {
  15. console.log('add data success');
  16. };
  17. };

(2)IDBObjectStore.put()

IDBObjectStore.put()方法用于更新某个主键对应的数据记录,如果对应的键值不存在,则插入一条新的记录。该方法返回一个 IDBRequest 对象。

  1. objectStore.put(item, key)

该方法接受两个参数,第一个参数为新数据,第二个参数为主键,该参数可选,且只在自动递增时才有必要提供,因为那时主键不包含在数据值里面。

(3)IDBObjectStore.clear()

IDBObjectStore.clear()删除当前对象仓库的所有记录。该方法返回一个 IDBRequest 对象。

  1. objectStore.clear()

该方法不需要参数。

(4)IDBObjectStore.delete()

IDBObjectStore.delete()方法用于删除指定主键的记录。该方法返回一个 IDBRequest 对象。

  1. objectStore.delete(Key)

该方法的参数为主键的值。

(5)IDBObjectStore.count()

IDBObjectStore.count()方法用于计算记录的数量。该方法返回一个 IDBRequest 对象。

  1. IDBObjectStore.count(key)

不带参数时,该方法返回当前对象仓库的所有记录数量。如果主键或 IDBKeyRange 对象作为参数,则返回对应的记录数量。

(6)IDBObjectStore.getKey()

IDBObjectStore.getKey()用于获取主键。该方法返回一个 IDBRequest 对象。

  1. objectStore.getKey(key)

该方法的参数可以是主键值或 IDBKeyRange 对象。

(7)IDBObjectStore.get()

IDBObjectStore.get()用于获取主键对应的数据记录。该方法返回一个 IDBRequest 对象。

  1. objectStore.get(key)

(8)IDBObjectStore.getAll()

DBObjectStore.getAll()用于获取对象仓库的记录。该方法返回一个 IDBRequest 对象。

  1. // 获取所有记录
  2. objectStore.getAll()
  3. // 获取所有符合指定主键或 IDBKeyRange 的记录
  4. objectStore.getAll(query)
  5. // 指定获取记录的数量
  6. objectStore.getAll(query, count)

(9)IDBObjectStore.getAllKeys()

IDBObjectStore.getAllKeys()用于获取所有符合条件的主键。该方法返回一个 IDBRequest 对象。

  1. // 获取所有记录的主键
  2. objectStore.getAllKeys()
  3. // 获取所有符合条件的主键
  4. objectStore.getAllKeys(query)
  5. // 指定获取主键的数量
  6. objectStore.getAllKeys(query, count)

(10)IDBObjectStore.index()

IDBObjectStore.index()方法返回指定名称的索引对象 IDBIndex。

  1. objectStore.index(name)

有了索引以后,就可以针对索引所在的属性读取数据。

  1. var t = db.transaction(['people'], 'readonly');
  2. var store = t.objectStore('people');
  3. var index = store.index('name');
  4. var request = index.get('foo');

上面代码打开对象仓库以后,先用index()方法指定获取name属性的索引,然后用get()方法读取某个name属性(foo)对应的数据。如果name属性不是对应唯一值,这时get()方法有可能取回多个数据对象。另外,get()是异步方法,读取成功以后,只能在success事件的监听函数中处理数据。

(11)IDBObjectStore.createIndex()

IDBObjectStore.createIndex()方法用于新建当前数据库的一个索引。该方法只能在VersionChange监听函数里面调用。

  1. objectStore.createIndex(indexName, keyPath, objectParameters)

该方法可以接受三个参数。

  • indexName:索引名
  • keyPath:主键
  • objectParameters:配置对象(可选)

第三个参数可以配置以下属性。

  • unique:如果设为true,将不允许重复的值
  • multiEntry:如果设为true,对于有多个值的主键数组,每个值将在索引里面新建一个条目,否则主键数组对应一个条目。

假定对象仓库中的数据记录都是如下的person类型。

  1. var person = {
  2. name: name,
  3. email: email,
  4. created: new Date()
  5. };

可以指定这个对象的某个属性来建立索引。

  1. var store = db.createObjectStore('people', { autoIncrement: true });
  2. store.createIndex('name', 'name', { unique: false });
  3. store.createIndex('email', 'email', { unique: true });

上面代码告诉索引对象,name属性不是唯一值,email属性是唯一值。

(12)IDBObjectStore.deleteIndex()

IDBObjectStore.deleteIndex()方法用于删除指定的索引。该方法只能在VersionChange监听函数里面调用。

  1. objectStore.deleteIndex(indexName)

(13)IDBObjectStore.openCursor()

IDBObjectStore.openCursor()用于获取一个指针对象。

  1. IDBObjectStore.openCursor()

指针对象可以用来遍历数据。该对象也是异步的,有自己的successerror事件,可以对它们指定监听函数。

  1. var t = db.transaction(['test'], 'readonly');
  2. var store = t.objectStore('test');
  3. var cursor = store.openCursor();
  4. cursor.onsuccess = function (event) {
  5. var res = event.target.result;
  6. if (res) {
  7. console.log('Key', res.key);
  8. console.dir('Data', res.value);
  9. res.continue();
  10. }
  11. }

监听函数接受一个事件对象作为参数,该对象的target.result属性指向当前数据记录。该记录的keyvalue分别返回主键和键值(即实际存入的数据)。continue()方法将光标移到下一个数据对象,如果当前数据对象已经是最后一个数据了,则光标指向null

openCursor()方法的第一个参数是主键值,或者一个 IDBKeyRange 对象。如果指定该参数,将只处理包含指定主键的记录;如果省略,将处理所有的记录。该方法还可以接受第二个参数,表示遍历方向,默认值为next,其他可能的值为prevnextuniqueprevunique。后两个值表示如果遇到重复值,会自动跳过。

(14)IDBObjectStore.openKeyCursor()

IDBObjectStore.openKeyCursor()用于获取一个主键指针对象。

  1. IDBObjectStore.openKeyCursor()