-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
166 lines (157 loc) · 4.92 KB
/
db.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
156
157
158
159
160
161
162
163
164
165
166
/**
* Created by sail on 2015/11/6.
*/
/*global indexedDB*/
'use strict';
(function (factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof define === 'function' && define.cmd) {
define(function (require, exports, module) {
module.exports = factory();
});
}
}(function () {
var DbPromise = function (fun) {
this.state = 'pending';
this.resolveList = [];
this.rejectList = [];
var _this = this;
fun(function () {
_this.resolve.apply(_this, arguments)
}, function () {
_this.reject.apply(_this, arguments)
});
};
DbPromise.prototype = {};
DbPromise.prototype.resolve = function (data) {
this.state = 'resolved';
this.data = data;
var _this = this;
this.resolveList.forEach(function (fun) {
_this.data = fun(_this.data)
});
};
DbPromise.prototype.reject = function (data) {
this.state = 'rejected';
this.error = data;
this.rejectList.forEach(function (fun) {
fun(data);
});
};
DbPromise.prototype.then = function (fun) {
if (this.state === 'pending') {
this.resolveList.push(fun);
} else {
this.data = fun(this.data);
}
return this;
};
DbPromise.prototype.catch = function (fun) {
if (this.state === 'pending') {
this.rejectList.push(fun);
} else {
fun(this.error);
}
return this;
};
var DB = function (name, upgrade, version) {
var _this = this;
if (DB.dbMap[name]) {
var map = DB.dbMap[name];
return _open(name, map.upgrade, map.version).then(function (db) {
_this.db = db;
return _this;
}).then(map.nextStep);
} else {
return _open(name, upgrade, version).then(function (db) {
_this.db = db;
return _this;
});
}
};
DB.prototype = {};
DB.prototype.put = function (table, data, tx) {
return _put(this.db, table, data, tx || this.tx);
};
DB.prototype.get = function (table, name, tx) {
return _get(this.db, table, name, tx || this.tx);
};
DB.prototype.clear = function (table, tx) {
return _clear(this.db, table, tx || this.tx);
};
DB.prototype.transaction = function (table, type, asDefault) {
var tx = _transaction(this.db, table, type);
if (asDefault) {
this.tx = tx;
}
return tx;
};
DB.prototype.transactionEnd = function () {
this.tx = void 0;
};
DB.prototype.getKv = function (table, k, tx) {
return _get(this.db, table, k, tx).then(o=>(o || {}).v);
};
DB.prototype.putKv = function (table, k, v, tx) {
return _put(this.db, table, {k, v}, tx);
};
DB.prototype.getKvStore = function (name) {
var _this = this;
return function (k, v) {
if (v === void 0) {
return _get(_this.db, name, k).then(o=>(o || {}).v);
} else {
return _put(_this.db, name, {k, v}).then(o=>(o || {}).v);
}
}
};
DB.dbMap = {};
return DB;
function _open(name, upgrade, version) {
return new DbPromise(function (resolve, reject) {
var request = indexedDB.open(name, version);
request.onupgradeneeded = upgrade;
request.onsuccess = function () {
resolve(request.result);
};
request.onerror = reject;
});
}
function _put(db, table, data, tx) {
return new DbPromise(function (resolve, reject) {
tx = tx || db.transaction(table, 'readwrite');
var store = tx.objectStore(table);
var request = store.put(data);
request.onsuccess = function () {
resolve(request.result);
};
request.onerror = reject;
});
}
function _clear(db, table, tx) {
return new Promise(function (resolve, reject) {
tx = tx || db.transaction(table, 'readwrite');
var store = tx.objectStore(table);
var request = store.clear();
request.onsuccess = function () {
resolve(request.result);
};
request.onerror = reject;
});
}
function _get(db, table, key, tx) {
return new DbPromise(function (resolve, reject) {
tx = tx || db.transaction(table, 'readonly');
var store = tx.objectStore(table);
var request = store.get(key);
request.onsuccess = function () {
resolve(request.result);
};
request.onerror = reject;
});
}
function _transaction(db, table, type) {
return db.transaction(table, type);
}
}));