回答編集履歴
1
いろいろ修正
answer
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
例えば、以下のようにすればIDをキーにして、パスワードを取得できると思います。
|
3
3
|
```js
|
4
4
|
let db = null;
|
5
|
+
|
5
6
|
class IDB {
|
6
7
|
constructor(options) {
|
7
|
-
this._dbName = options.databaseName || '
|
8
|
+
this._dbName = options.databaseName || 'mydb';
|
8
9
|
this._dbVersion = options.databaseVersion || 1;
|
9
|
-
this._storeName = options.storeName || '
|
10
|
+
this._storeName = options.storeName || 'mystore';
|
10
11
|
this._keyPath = options.keyPath;
|
11
12
|
this._db = null;
|
12
13
|
this._store = null;
|
@@ -38,18 +39,17 @@
|
|
38
39
|
}
|
39
40
|
|
40
41
|
async putOne(data, storeName) {
|
41
|
-
return new Promise((resolve, reject) => {
|
42
|
+
return new Promise(async (resolve, reject) => {
|
42
|
-
storeName = storeName || this._storeName;
|
43
|
+
storeName = storeName || this._storeName;
|
43
44
|
if (!data) throw new Error('put no data');
|
44
|
-
await this.open();
|
45
|
+
await this.open();
|
45
|
-
const tx = this._db.transaction(
|
46
|
+
const tx = this._db.transaction(this._storeName, 'readwrite');
|
46
|
-
const store = tx.objectStore(
|
47
|
+
const store = tx.objectStore(this._storeName);
|
47
48
|
let err = null;
|
48
|
-
const tx = store.transaction;
|
49
|
-
tx.oncomplete = evt => {
|
49
|
+
tx.oncomplete = evt => {
|
50
|
-
if (err) {
|
50
|
+
if (err) {
|
51
|
-
reject(err);
|
51
|
+
reject(err);
|
52
|
-
} else {
|
52
|
+
} else {
|
53
53
|
resolve(data);
|
54
54
|
}
|
55
55
|
};
|
@@ -58,7 +58,7 @@
|
|
58
58
|
};
|
59
59
|
const req = store.put(data);
|
60
60
|
req.onerror = evt => {
|
61
|
-
err = req.error;
|
61
|
+
err = req.error;
|
62
62
|
err.data = data;
|
63
63
|
}
|
64
64
|
req.success = evt => {
|
@@ -68,12 +68,13 @@
|
|
68
68
|
}
|
69
69
|
|
70
70
|
async getOne(key) {
|
71
|
-
return new Promise((resolve, reject) => {
|
71
|
+
return new Promise(async (resolve, reject) => {
|
72
|
-
await this.open();
|
72
|
+
await this.open();
|
73
|
-
const tx =
|
73
|
+
const tx = this._db.transaction(this._storeName);
|
74
|
+
const store = tx.objectStore(this._storeName);
|
74
|
-
let res = null;
|
75
|
+
let res = null;
|
75
76
|
let err = null;
|
76
|
-
tx.oncomplete = evt => {
|
77
|
+
tx.oncomplete = evt => {
|
77
78
|
if (err) {
|
78
79
|
reject(err);
|
79
80
|
} else {
|
@@ -83,7 +84,8 @@
|
|
83
84
|
tx.onerror = evt => {
|
84
85
|
reject(tx.error);
|
85
86
|
};
|
87
|
+
const req = store.get(key);
|
86
|
-
req.onsuccess = evt => {
|
88
|
+
req.onsuccess = evt => {
|
87
89
|
res = req.result;
|
88
90
|
};
|
89
91
|
req.onerror = evt => {
|
@@ -95,9 +97,9 @@
|
|
95
97
|
|
96
98
|
|
97
99
|
const idb = new IDB({
|
98
|
-
databaseName: '
|
100
|
+
databaseName: 'mydb',
|
99
|
-
storeName: '
|
101
|
+
storeName: 'mystore',
|
100
|
-
keyPath: '
|
102
|
+
keyPath: 'MYID',
|
101
103
|
});
|
102
104
|
|
103
105
|
async function idbPut(data) {
|
@@ -111,18 +113,15 @@
|
|
111
113
|
|
112
114
|
async function idbGet(id) {
|
113
115
|
try {
|
114
|
-
const
|
116
|
+
const res = await idb.getOne(id);
|
115
|
-
return pass;
|
117
|
+
return res.pass;
|
116
118
|
} catch(err) {
|
117
119
|
console.log(err);
|
118
|
-
}
|
120
|
+
}
|
119
121
|
}
|
122
|
+
// indexedDBにユーザー登録
|
123
|
+
idbPut({MYID:'MYID', pass:'PASS'});
|
120
124
|
|
121
|
-
// indexedDBにユーザー登録
|
122
|
-
idbPut({id:'hoge', pass:'fuga'});
|
123
|
-
|
124
|
-
|
125
125
|
// indexedDBからユーザー(のパス)を取得
|
126
|
-
|
126
|
+
idbGet('MYID').then(pass => console.log(pass));
|
127
|
-
|
128
127
|
```
|