IDBObjectStore.autoIncrement

admin發表於2019-09-18

此屬性返回一個布林值,用於標識當前物件倉庫記錄主鍵是否是自增長。

返回true表示是自增長,返回表示不是自增長。

語法結構:

[JavaScript] 純文字檢視 複製程式碼
let isAutoIncrement = objectStore.autoIncrement;

屬性用法比較簡單,看如下程式碼例項:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script>
let request = window.indexedDB.open("antzone", 1);
request.onupgradeneeded = (ev) => {
  let db = ev.target.result;
  if (!db.objectStoreNames.contains('students')) {
    let objectStore = db.createObjectStore('students',{keyPath:"id"});
    objectStore.createIndex("xingbie","sex",{ unique: false });
  }
}

request.onsuccess = (ev) => {
  let db = ev.target.result;
  let transaction = db.transaction(['students'], 'readonly');
  let objectStore = transaction.objectStore('students');
  console.log(objectStore.autoIncrement);
}
</script>
</head>
<body>
  <p>列印物件倉庫中記錄主鍵是否是自增長</p>
</body>
</html>

程式碼執行效果截圖如下:

a:3:{s:3:\"pic\";s:43:\"portal/201909/18/214726yusqx9x9mbb9tt19.jpg\";s:5:\"thumb\";s:0:\"\";s:6:\"remote\";N;}

列印結果說明,物件倉庫的主鍵並非是自增長。

autoIncrement預設屬性值為false,要想讓主鍵自增長需要顯式定義autoIncrement:true。