This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simpleDB.js
155 lines (152 loc) · 6.09 KB
/
simpleDB.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
(function (global) {
var SECRET = Object.create(null);
var DB_PREFIX = '$SimpleDB$';
var STORE = 'store';
function SimpleDBFactory(secret) {
if (secret !== SECRET) throw TypeError('Invalid constructor');
}
SimpleDBFactory.prototype = {
open: function (name) {
return new Promise(function (resolve, reject) {
var request = indexedDB.open(DB_PREFIX + name);
request.onupgradeneeded = function () {
var db = request.result;
db.createObjectStore(STORE);
};
request.onsuccess = function () {
var db = request.result;
resolve(new SimpleDB(SECRET, name, db));
};
request.onerror = function () {
reject(request.error);
};
});
},
delete: function (name) {
return new Promise(function (resolve, reject) {
var request = indexedDB.deleteDatabase(DB_PREFIX + name);
request.onsuccess = function () {
resolve(undefined);
};
request.onerror = function () {
reject(request.error);
};
});
}
};
function SimpleDB(secret, name, db) {
if (secret !== SECRET) throw TypeError('Invalid constructor');
this._name = name;
this._db = db;
}
SimpleDB.cmp = indexedDB.cmp;
SimpleDB.prototype = {
get name() {
return this._name;
},
get: function (key) {
var that = this;
return new Promise(function (resolve, reject) {
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
var req = store.get(key);
// NOTE: Could also use req.onsuccess/onerror
tx.oncomplete = function () { resolve(req.result); };
tx.onabort = function () { reject(tx.error); };
});
},
set: function (key, value) {
var that = this;
return new Promise(function (resolve, reject) {
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
var req = store.put(value, key);
tx.oncomplete = function () { resolve(undefined); };
tx.onabort = function () { reject(tx.error); };
});
},
delete: function (key) {
var that = this;
return new Promise(function (resolve, reject) {
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
var req = store.delete(key);
tx.oncomplete = function () { resolve(undefined); };
tx.onabort = function () { reject(tx.error); };
});
},
clear: function () {
var that = this;
return new Promise(function (resolve, reject) {
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
var request = store.clear();
tx.oncomplete = function () { resolve(undefined); };
tx.onabort = function () { reject(tx.error); };
});
},
forEach: function (callback, options) {
var that = this;
return new Promise(function (resolve, reject) {
options = options || {};
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
var request = store.openCursor(
options.range,
options.direction === 'reverse' ? 'prev' : 'next');
request.onsuccess = function () {
var cursor = request.result;
if (!cursor) return;
try {
var terminate = callback(cursor.key, cursor.value);
if (!terminate) cursor.continue();
} catch (ex) {
tx.abort(); // ???
}
};
tx.oncomplete = function () { resolve(undefined); };
tx.onabort = function () { reject(tx.error); };
});
},
getMany: function (keys) {
var that = this;
return new Promise(function (resolve, reject) {
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
var results = [];
for (var key of keys) {
store.get(key).onsuccess(function (result) {
results.push(result);
});
}
tx.oncomplete = function () { resolve(results); };
tx.onabort = function () { reject(tx.error); };
});
},
setMany: function (entries) {
var that = this;
return new Promise(function (resolve, reject) {
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
for (var entry of entries) {
store.put(entry.value, entry.key);
}
tx.oncomplete = function () { resolve(undefined); };
tx.onabort = function () { reject(tx.error); };
});
},
deleteMany: function (keys) {
var that = this;
return new Promise(function (resolve, reject) {
var tx = that._db.transaction(STORE, 'readwrite');
var store = tx.objectStore(STORE);
for (var key of keys)
store.delete(key);
tx.oncomplete = function () { resolve(undefined); };
tx.onabort = function () { reject(tx.error); };
});
}
};
global.simpleDB = new SimpleDBFactory(SECRET);
global.SimpleDBKeyRange = IDBKeyRange;
}(self));