这篇翻译不完整。请帮忙从英语翻译这篇文章

IndexedDB 中的 IDBDatabase 接口提供一个到 数据库的连接; 你可以使用 IDBDatabase 对象在数据库中打开一个transaction , 然后进行操作或者删除数据库中的对象。这是唯一一个能够访问和管理数据库版本的接口。 

 

Note: 此特性在 Web Worker 中可用。

Note: Everything you do in IndexedDB always happens in the context of a transaction, representing interactions with data in the database. All objects in IndexedDB — including object stores, indexes, and cursors — are tied to a particular transaction. Thus, you cannot execute commands, access data, or open anything outside of a transaction.

Note: Note that as of Firefox 40, IndexedDB transactions have relaxed durability guarantees to increase performance (see bug 1112702.) Previously in a readwrite transaction IDBTransaction.oncomplete was fired only when all data was guaranteed to have been flushed to disk. In Firefox 40+ the complete event is fired after the OS has been told to write the data but potentially before that data has actually been flushed to disk. The complete event may thus be delivered quicker than before, however, there exists a small chance that the entire transaction will be lost if the OS crashes or there is a loss of system power before the data is flushed to disk. Since such catastrophic events are rare most consumers should not need to concern themselves further. If you must ensure durability for some reason (e.g. you're storing critical data that cannot be recomputed later) you can force a transaction to flush to disk before delivering the complete event by creating a transaction using the experimental (non-standard) readwriteflush mode (see IDBDatabase.transaction.

方法

继承自: EventTarget

IDBDatabase.close()
在一个单独的线程中关闭数据库连接并立即返回。
IDBDatabase.createObjectStore()
创建并返回一个新的 object store 或者 index。
IDBDatabase.deleteObjectStore()
根据给定的名字,删除在当前连接的数据库中的 object store 和 相关的索引。 
IDBDatabase.transaction()
立即返回一个包含IDBTransaction.objectStore 方法的 transaction 对象。你可以用这个对象来操作object store。这个操作是在一个单独的线程中执行的。

属性

IDBDatabase.name 只读
DOMString类型,当前连接数据库名  。
IDBDatabase.version 只读
64-bit 整型数,当前连接数据库的版本 。当数据第一次被创建时,这个属性是一个空的字符串。 
IDBDatabase.objectStoreNames 只读
DOMStringList类型,当前连接连接数据库中所有的object store 名字列表。

Event handlers

IDBDatabase.onabort
Fires when access of the database is aborted.
IDBDatabase.onerror
Fires when access to the database fails.
IDBDatabase.onversionchange

Fires when a database structure change (IDBOpenDBRequest.onupgradeneeded event or IDBFactory.deleteDatabase was requested elsewhere (most probably in another window/tab on the same computer). This is different from the version change transaction (see IDBVersionChangeEvent), but it is related.

示例

在下面的代码中, 异步打开了一个数据库连接 (IDBFactory), 并处理成功或者异常事件, 如果触发了upgrade事件就需要创建一个新的object store  (IDBdatabase)。如果想看完整的例子, 可以使用 To-do Notifications 应用(view example live.)

// Let us open our database
  var DBOpenRequest = window.indexedDB.open("toDoList", 4);

  // these two event handlers act on the IDBDatabase object, when the database is opened successfully, or not
  DBOpenRequest.onerror = function(event) {
    note.innerHTML += '<li>Error loading database.</li>';
  };
 
  DBOpenRequest.onsuccess = function(event) {
    note.innerHTML += '<li>Database initialised.</li>';
    
    // store the result of opening the database in the db variable. This is used a lot later on
    db = DBOpenRequest.result;
    
    // Run the displayData() function to populate the task list with all the to-do list data already in the IDB
    displayData();
  };
 
  // This event handles the event whereby a new version of the database needs to be created
  // Either one has not been created before, or a new version number has been submitted via the
  // window.indexedDB.open line above
 
  DBOpenRequest.onupgradeneeded = function(event) {
    var db = event.target.result;
    
    db.onerror = function(event) {
      note.innerHTML += '<li>Error loading database.</li>';
    };

    // Create an objectStore for this database using IDBDatabase.createObjectStore
    
    var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" });
    
    // define what data items the objectStore will contain
    
    objectStore.createIndex("hours", "hours", { unique: false });
    objectStore.createIndex("minutes", "minutes", { unique: false });
    objectStore.createIndex("day", "day", { unique: false });
    objectStore.createIndex("month", "month", { unique: false });
    objectStore.createIndex("year", "year", { unique: false });

    objectStore.createIndex("notified", "notified", { unique: false });
    
    note.innerHTML += '<li>Object store created.</li>';
  };

This next line opens up a transaction on the Database, then opens an object store that we can then manipulate the data inside of.

    var objectStore = db.transaction('toDoList').objectStore('toDoList'); 

规范

Specification Status Comment
Indexed Database API
IDBDatabase
Recommendation  

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 23webkit
24
10 moz
16.0 (16.0)
10, partial 15 7.1
Available in workers (Yes) 37.0 (37.0) ? (Yes) ?
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support 4.4 22.0 (22.0) 1.0.1 10 22 8
Available in workers (Yes) 37.0 (37.0) (Yes) ? (Yes) ?

Be careful in Chrome as it still implements the old specification along with the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.

See also

 

文档标签和贡献者

标签: 
此页面的贡献者: luckyqiao, Monokai
最后编辑者: luckyqiao,