IDBDatabase.createObjectStore()

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

IDBDatabase 接口的 createObjectStore() 方法创建并返回一个新的 object store 或 index。

此方法接受一个参数作为 store 的名称,也接受一个可选的参数对象让你可以定义重要的可选属性。你可以用这个属性唯一的标识此 store 中的每个对象。因为参数是一个标识符,所以 store 中的每一个对象都应有此属性并保证此属性唯一。

此方法只能在 versionchange 事务中被调用。

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

语法

var objectStore = IDBDatabase.createObjectStore(name);
var objectStore = IDBDatabase.createObjectStore(name, options);

参数

name
被创建的 object store 的名称。请注意创建空名称的 object store 是被允许的。
optionalParameters 可选

可选的对象,它的属性是此方法的可选参数,其中包括以下的属性:

Attribute Description
keyPath

key path 被用在新的 object store 上。如果为空或未指定,object store 创建时将没有 key path,而是使用 out-of-line keys 。你也能传一个数组作为 keyPath 。

autoIncrement 如果为 true,  object store 有一个 key generator. 默认为 false

未知参数将被忽略。

返回值

IDBObjectStore
新创建的 object store。

异常

This method may raise a此方法可能会抛出一个 DOMException 带有以下所列其中一种类型的 DOMError

Exception Description
InvalidStateError Occurs if the method was not called from a versionchange transaction callback. For older WebKit browsers, you must call first.
TransactionInactiveError Occurs if a request is made on a source database that doesn't exist (e.g. has been deleted or removed.) In Firefox previous to version 41, an InvalidStateError was raised in this case as well, which was misleading; this has now been fixed (see bug 1176165.)
ConstraintError An object store with the given name (based on case-sensitive comparison) already exists in the connected database.
InvalidAccessError If autoIncrement is set to true and keyPath is either an empty string or an array containing an empty string.
 

Example

 // Let us open our database
  var request = window.indexedDB.open("toDoList", 4);
 
  // This handler is called when a new version of the database
  // is created, either when one has not been created before
  // or when a new version number is submitted by calling
  // window.indexedDB.open().
  // This handler is only supported in recent browsers.
  request.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
    
    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>';
  };

Specification

Specification Status Comment
Indexed Database API
createObjectStore()
Recommendation  
Indexed Database API 2.0
createObjectStore()
Recommendation  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support

24

23 — 24 webkit

12

16

10 — 16 moz

101157
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes22228 Yes

1. partial

See also

文档标签和贡献者

此页面的贡献者: PWAEZQS
最后编辑者: PWAEZQS,