/
index.md
101 lines (75 loc) · 3.83 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: "IDBObjectStore: autoIncrement プロパティ"
slug: Web/API/IDBObjectStore/autoIncrement
l10n:
sourceCommit: eab4066e72d5478de920e4020e5db71214dcffa6
---
{{ APIRef("IndexedDB") }}
{{domxref("IDBObjectStore")}} インターフェイスの読み取り専用プロパティ **`autoIncrement`** は、このオブジェクトストアのオートインクリメントフラグの値を返します。
なお、各オブジェクトストアにそれぞれ別のオートインクリメントカウンターがあります。
{{AvailableInWorkers}}
## 値
以下の論理値です。
| 値 | 意味 |
| ------- | ------------------------------------------------------ |
| `true` | オブジェクトストアはオートインクリメントを行います。 |
| `false` | オブジェクトストアはオートインクリメントを行いません。 |
## 例
以下のコード断片では、データベースの読み書きトランザクションを開き、`add()` を用いてデータをオブジェクトストアに追加します。オブジェクトストアの生成後、コンソールに `objectStore.autoIncrement` を記録します。動く例全体は、[To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) アプリケーションを参照してください。([動く例を見る](https://mdn.github.io/dom-examples/to-do-notifications/))
```js
// データベースを開く
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = (event) => {
note.innerHTML += "<li>データベースを初期化しました。</li>";
// データベースを開いた結果を変数 db に格納する
// これは後でよく使う
db = DBOpenRequest.result;
// addData() 関数を実行し、データをデータベースに追加する
addData();
};
function addData() {
// IDB に挿入できる新しいオブジェクトを生成する
const newItem = [
{
taskTitle: "Walk dog",
hours: 19,
minutes: 30,
day: 24,
month: "December",
year: 2013,
notified: "no",
},
];
// 読み書きトランザクションを開き、データを追加する準備をする
const transaction = db.transaction(["toDoList"], "readwrite");
// すべて完了し、トランザクションが正常終了したら報告する
transaction.oncomplete = (event) => {
note.innerHTML += "<li>トランザクションが完了しました。</li>";
};
transaction.onerror = (event) => {
note.innerHTML +=
"<li>トランザクションはエラーのため開けませんでした。アイテムは重複させられません。</li>";
};
// トランザクション上でオブジェクトストアを生成する
const objectStore = transaction.objectStore("toDoList");
console.log(objectStore.autoIncrement);
// オブジェクトストアに newItem オブジェクトを追加する要求を行う
const objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = (event) => {
// 要求の成功を報告する
note.innerHTML += "<li>要求に成功しました。</li>";
};
}
```
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}
## 関連情報
- [IndexedDB の使用](/ja/docs/Web/API/IndexedDB_API/Using_IndexedDB)
- トランザクションの開始: {{domxref("IDBDatabase")}}
- トランザクションの使用: {{domxref("IDBTransaction")}}
- キー範囲の設定: {{domxref("IDBKeyRange")}}
- データの取得と変更: {{domxref("IDBObjectStore")}}
- カーソルの使用: {{domxref("IDBCursor")}}
- リファレンス例: [To-do Notifications](https://github.com/mdn/dom-examples/tree/main/to-do-notifications) ([動く例を見る](https://mdn.github.io/dom-examples/to-do-notifications/))