回答編集履歴
1
いろいろ修正
test
CHANGED
@@ -6,15 +6,17 @@
|
|
6
6
|
|
7
7
|
let db = null;
|
8
8
|
|
9
|
+
|
10
|
+
|
9
11
|
class IDB {
|
10
12
|
|
11
13
|
constructor(options) {
|
12
14
|
|
13
|
-
this._dbName = options.databaseName || '
|
15
|
+
this._dbName = options.databaseName || 'mydb';
|
14
16
|
|
15
17
|
this._dbVersion = options.databaseVersion || 1;
|
16
18
|
|
17
|
-
this._storeName = options.storeName || '
|
19
|
+
this._storeName = options.storeName || 'mystore';
|
18
20
|
|
19
21
|
this._keyPath = options.keyPath;
|
20
22
|
|
@@ -78,23 +80,77 @@
|
|
78
80
|
|
79
81
|
async putOne(data, storeName) {
|
80
82
|
|
81
|
-
return new Promise((resolve, reject) => {
|
83
|
+
return new Promise(async (resolve, reject) => {
|
82
|
-
|
84
|
+
|
83
|
-
storeName = storeName || this._storeName;
|
85
|
+
storeName = storeName || this._storeName;
|
84
86
|
|
85
87
|
if (!data) throw new Error('put no data');
|
86
88
|
|
87
|
-
await this.open();
|
89
|
+
await this.open();
|
88
|
-
|
90
|
+
|
89
|
-
const tx = this._db.transaction(storeName, 'readwrite');
|
91
|
+
const tx = this._db.transaction(this._storeName, 'readwrite');
|
90
|
-
|
92
|
+
|
91
|
-
const store = tx.objectStore(storeName);
|
93
|
+
const store = tx.objectStore(this._storeName);
|
92
94
|
|
93
95
|
let err = null;
|
94
96
|
|
95
|
-
const tx = store.transaction;
|
96
|
-
|
97
|
-
tx.oncomplete = evt => {
|
97
|
+
tx.oncomplete = evt => {
|
98
|
+
|
99
|
+
if (err) {
|
100
|
+
|
101
|
+
reject(err);
|
102
|
+
|
103
|
+
} else {
|
104
|
+
|
105
|
+
resolve(data);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
};
|
110
|
+
|
111
|
+
tx.onerror = evt => {
|
112
|
+
|
113
|
+
reject(tx.error);
|
114
|
+
|
115
|
+
};
|
116
|
+
|
117
|
+
const req = store.put(data);
|
118
|
+
|
119
|
+
req.onerror = evt => {
|
120
|
+
|
121
|
+
err = req.error;
|
122
|
+
|
123
|
+
err.data = data;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
req.success = evt => {
|
128
|
+
|
129
|
+
data[this._keyPath] = req.result;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
});
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
async getOne(key) {
|
140
|
+
|
141
|
+
return new Promise(async (resolve, reject) => {
|
142
|
+
|
143
|
+
await this.open();
|
144
|
+
|
145
|
+
const tx = this._db.transaction(this._storeName);
|
146
|
+
|
147
|
+
const store = tx.objectStore(this._storeName);
|
148
|
+
|
149
|
+
let res = null;
|
150
|
+
|
151
|
+
let err = null;
|
152
|
+
|
153
|
+
tx.oncomplete = evt => {
|
98
154
|
|
99
155
|
if (err) {
|
100
156
|
|
@@ -102,7 +158,7 @@
|
|
102
158
|
|
103
159
|
} else {
|
104
160
|
|
105
|
-
resolve(
|
161
|
+
resolve(res);
|
106
162
|
|
107
163
|
}
|
108
164
|
|
@@ -114,76 +170,24 @@
|
|
114
170
|
|
115
171
|
};
|
116
172
|
|
117
|
-
const req = store.
|
173
|
+
const req = store.get(key);
|
174
|
+
|
175
|
+
req.onsuccess = evt => {
|
176
|
+
|
177
|
+
res = req.result;
|
178
|
+
|
179
|
+
};
|
118
180
|
|
119
181
|
req.onerror = evt => {
|
120
182
|
|
121
183
|
err = req.error;
|
122
184
|
|
123
|
-
err.data = data;
|
124
|
-
|
125
|
-
}
|
185
|
+
};
|
126
|
-
|
127
|
-
req.success = evt => {
|
128
|
-
|
129
|
-
data[this._keyPath] = req.result;
|
130
|
-
|
131
|
-
}
|
132
186
|
|
133
187
|
});
|
134
188
|
|
135
189
|
}
|
136
190
|
|
137
|
-
|
138
|
-
|
139
|
-
async getOne(key) {
|
140
|
-
|
141
|
-
return new Promise((resolve, reject) => {
|
142
|
-
|
143
|
-
await this.open();
|
144
|
-
|
145
|
-
const tx = req.transaction;
|
146
|
-
|
147
|
-
let res = null;
|
148
|
-
|
149
|
-
let err = null;
|
150
|
-
|
151
|
-
tx.oncomplete = evt => {
|
152
|
-
|
153
|
-
if (err) {
|
154
|
-
|
155
|
-
reject(err);
|
156
|
-
|
157
|
-
} else {
|
158
|
-
|
159
|
-
resolve(res);
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
};
|
164
|
-
|
165
|
-
tx.onerror = evt => {
|
166
|
-
|
167
|
-
reject(tx.error);
|
168
|
-
|
169
|
-
};
|
170
|
-
|
171
|
-
req.onsuccess = evt => {
|
172
|
-
|
173
|
-
res = req.result;
|
174
|
-
|
175
|
-
};
|
176
|
-
|
177
|
-
req.onerror = evt => {
|
178
|
-
|
179
|
-
err = req.error;
|
180
|
-
|
181
|
-
};
|
182
|
-
|
183
|
-
});
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
191
|
}
|
188
192
|
|
189
193
|
|
@@ -192,11 +196,11 @@
|
|
192
196
|
|
193
197
|
const idb = new IDB({
|
194
198
|
|
195
|
-
databaseName: '
|
199
|
+
databaseName: 'mydb',
|
196
|
-
|
200
|
+
|
197
|
-
storeName: '
|
201
|
+
storeName: 'mystore',
|
198
|
-
|
202
|
+
|
199
|
-
keyPath: 'ID',
|
203
|
+
keyPath: 'MYID',
|
200
204
|
|
201
205
|
});
|
202
206
|
|
@@ -224,32 +228,26 @@
|
|
224
228
|
|
225
229
|
try {
|
226
230
|
|
227
|
-
const
|
231
|
+
const res = await idb.getOne(id);
|
228
|
-
|
232
|
+
|
229
|
-
return pass;
|
233
|
+
return res.pass;
|
230
234
|
|
231
235
|
} catch(err) {
|
232
236
|
|
233
237
|
console.log(err);
|
234
238
|
|
235
|
-
}
|
239
|
+
}
|
236
240
|
|
237
241
|
}
|
238
242
|
|
239
|
-
|
240
|
-
|
241
|
-
// indexedDBにユーザー登録
|
243
|
+
// indexedDBにユーザー登録
|
242
|
-
|
244
|
+
|
243
|
-
idbPut({
|
245
|
+
idbPut({MYID:'MYID', pass:'PASS'});
|
244
|
-
|
245
|
-
|
246
246
|
|
247
247
|
|
248
248
|
|
249
249
|
// indexedDBからユーザー(のパス)を取得
|
250
250
|
|
251
|
-
|
251
|
+
idbGet('MYID').then(pass => console.log(pass));
|
252
|
-
|
253
|
-
|
254
252
|
|
255
253
|
```
|