/
index.md
120 lines (87 loc) · 4.75 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "IDBTransaction: objectStore() メソッド"
slug: Web/API/IDBTransaction/objectStore
l10n:
sourceCommit: eab4066e72d5478de920e4020e5db71214dcffa6
---
{{ APIRef("IndexedDB") }}
{{domxref("IDBTransaction")}} インターフェイスの **`objectStore()`** メソッドは、トランザクションの対象に追加済のオブジェクトストアを返します。
同じトランザクションオブジェクトで、同じ名前を指定して呼び出した場合、このメソッドを何回呼び出しても同じ {{domxref("IDBObjectStore")}} インスタンスを返します。別のトランザクションオブジェクトで呼び出した場合、別の {{domxref("IDBObjectStore")}} インスタンスを返します。
{{AvailableInWorkers}}
## 構文
```js-nolint
objectStore(name)
```
### 引数
- `name`
- : 要求するオブジェクトストアの名前です。
### 返値
オブジェクトストアにアクセスする用の {{domxref("IDBObjectStore")}} オブジェクトを返します。
### 例外
- `NotFoundError` {{domxref("DOMException")}}
- : 要求されたオブジェクトストアがトランザクションの対象に無いとき投げられます。
- `InvalidStateError` {{domxref("DOMException")}}
- : 要求が削除されたか取り除かれたソースオブジェクトに対してなされたか、トランザクションが終了済みのとき投げられます。
## 例
以下のコード断片では、データベースの読み書きトランザクションを開き、オブジェクトストアにデータを追加します。トランザクションのイベントハンドラーに設定された関数により、成功時または失敗時にトランザクションを開いた結果を報告する点にも注目してください。動く例全体は、[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 note = document.getElementById("notifications");
// IDB データを保存する用の db オブジェクトのインスタンス
let db;
// データベースを開く
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");
// オブジェクトストアに newItem オブジェクトを追加する
const objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = (event) => {
// 要求の成功を報告する (これは、アイテムがデータベースにに正常に格納されたことを意味しません。
// これを知るには、transaction.onsuccess を用いる必要があります)
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/))