forked from jakearchibald/idb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidb.js
93 lines (75 loc) · 2.28 KB
/
idb.js
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
import regeneratorRuntime from "regenerator/runtime";
import assert from "assert";
import {Promise} from "es6-promise";
self.Promise = Promise;
describe('idb interface', () => {
beforeEach(done => idb.delete('tmp-db').then(done));
it('exists on window', () => {
assert('idb' in self);
});
it('has open and delete methods', () => {
assert('open' in idb);
assert('delete' in idb);
});
// yeah yeah, I know, I need to write better tests
it('stuff', async () => {
// Open the database
let db = await idb.open('tmp-db', 1, upgradeDb => {
switch (upgradeDb.oldVersion) {
case 0:
upgradeDb.createObjectStore('key-val').put('world', 'hello');
}
});
// Add some things to the list
let tx = db.transaction('key-val', 'readwrite');
let store = tx.objectStore('key-val');
store.put(await store.get('hello'), 'foo');
await tx.complete;
tx = db.transaction('key-val');
assert.equal(await tx.objectStore('key-val').get('foo'), 'world');
db.close();
});
it('lets me itterate over a cursor', async () => {
// Open the database
let db = await idb.open('tmp-db', 1, upgradeDb => {
switch (upgradeDb.oldVersion) {
case 0:
const store = upgradeDb.createObjectStore('list', {keyPath: ''});
store.put("a");
store.put("b");
store.put("c");
store.put("d");
store.put("e");
}
});
const tx = db.transaction('list');
const values = [];
tx.objectStore('list').iterateCursor(cursor => {
if (!cursor) return;
values.push(cursor.value);
cursor.continue();
});
await tx.complete;
assert.equal(values.join(), 'a,b,c,d,e');
db.close();
});
it('rejects rather than throws', async () => {
const db = await idb.open('tmp-db', 1, upgradeDb => {
upgradeDb.createObjectStore('key-val');
});
let threw = false;
let rejected = false;
const tx = db.transaction('key-val');
const store = tx.objectStore('key-val');
let getPromise;
await tx.complete;
try {
getPromise = store.get('hello').catch(_ => rejected = true);
} catch(e) {
threw = true;
}
await getPromise;
assert(!threw, "Did not throw");
assert(rejected, "Rejected");
});
});