IDBObjectStore.autoIncrement

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

IDBObjectStore的只读属性autoIncrement接口返回当前objectStore的自增标记值(true或false)。

什么是自增呢?熟悉SQL的朋友应该知道,SQL数据里面的字段可以设置自增,当一条记录被插入时,不必传入该字段,新记录的该字段值会在前面一条记录该字段值的基础上加1.而indexedDB里面的autoIncrement的同样的意义。(译者注)

注意:每个objectStore的auto increment计数器是分开独立的。

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

句法

var myAutoIncrement = objectStore.autoIncrement;

Value

Boolean:

含义
true 当前objectStore会自增
false 当前objectStore不会自增
 

例子

在下面代码片段中,我们在数据库里打开了一个可读写的事务(transaction),并且用add()向一个objectStore中添加了一些数据。在objectStore被创建之后,我们在console中打印了objectStore.autoIncrement的值。想查看完整的例子,请查看我们的To-do Notifications应用(查看在线例子)。

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

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 below
  db = DBOpenRequest.result;
    
  // Run the addData() function to add the data to the database
  addData();
};

function addData() {
  // Create a new object ready to insert into the IDB
  var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ];

  // open a read/write db transaction, ready for adding the data
  var transaction = db.transaction(["toDoList"], "readwrite");

  // report on the success of the transaction completing, when everything is done
  transaction.oncomplete = function(event) {
    note.innerHTML += '<li>Transaction completed.</li>';
  };

  transaction.onerror = function(event) {
    note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
  };

  // create an object store on the transaction
  var objectStore = transaction.objectStore("toDoList");
  console.log(objectStore.autoIncrement);

  // Make a request to add our newItem object to the object store
  var objectStoreRequest = objectStore.add(newItem[0]);

  objectStoreRequest.onsuccess = function(event) {
    // report the success of our request
    note.innerHTML += '<li>Request successful.</li>';
  };
};

规范

规范 状态 备注
Indexed Database API
autoIncrement
Recommendation  
Indexed Database API 2.0
autoIncrement
Recommendation  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 24
Full support 24
No support 23 — 57
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support YesFirefox Full support 16
Full support 16
No support 10 — 16
Prefixed
Prefixed Requires the vendor prefix: moz
IE Full support 10
Notes
Full support 10
Notes
Notes partial
Opera Full support 15Safari Full support 7WebView Android Full support Yes
Full support Yes
No support ? — 57
Prefixed
Prefixed Requires the vendor prefix: webkit
Chrome Android Full support 25
Full support 25
No support 25 — 57
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 22Opera Android Full support 22Safari iOS Full support 8Samsung Internet Android Full support Yes
Full support Yes
No support ? — 7.0
Prefixed
Prefixed Requires the vendor prefix: webkit

Legend

Full support  
Full support
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

相关内容

文档标签和贡献者

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