-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindexeddb-setup.js
192 lines (164 loc) · 6.92 KB
/
indexeddb-setup.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(function($, window){
var idb = window.indexedDB,
dbReady = $.Deferred(),
db;
/**
* At this point, we'll only support unprefixed, non-experimental versions of IndexedDB, to simplify our
* lives - there are a number of differences we would need to account for if we were to attempt to support
* early attempts at IndexedDB implementations, some of which you can read about in the excellent MDN
* documentation at
* https://developer.mozilla.org/en/docs/Web/API/IndexedDB_API and
* https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB.
*/
if (!window.indexedDB) throw new Error("Expecting unprefixed IndexedDB support on window object.");
/**
* Create the 'jalic' IndexedDB db.
* For now, we handle errors merely by logging them and rejecting the associated Deferred object with the
* error event. This should be replaced by something more robust in the future...
*
* @param idb
* @returns {*}
*/
function createDatabase(idb){
var dbreq = idb.open('jalic', 1);
/**
* Something went wrong when opening our db. This could range from the user refusing the
* request to allow this site to store data, to the current version of the db being higher
* than the one we requested, to storage issues or lacking implementation.
* @param event
*/
dbreq.onerror = function(event){
console.log(event);
dbReady.reject(event);
};
/**
* The on upgrade needed event is called whenever we're opening a database with a version number
* higher than the currently existing version number, which includes when the database doesn't
* currently exist. Within this function we define the structure of the db.
* @param event
*/
dbreq.onupgradeneeded = function(event){
var objectStore;
db = event.target.result;
db.onerror = function(event){
console.log(event);
};
objectStore = db.createObjectStore('jalicData', {keyPath:"jdName"});
objectStore.createIndex('storedAt', 'storedAt', {unique:false});
objectStore.transaction.oncomplete = function(event){
dbReady.resolve();
};
};
/**
* DB was opened successfully, with no upgrade needed.
* @param event
*/
dbreq.onsuccess = function(event){
db = event.target.result;
dbReady.resolve();
};
return dbReady.promise();
}
/**
* Create the database and call any functions that are awaiting its availability when done.
*/
createDatabase(idb);
/**
* Define a simple interface mimicking the Storage interface on the jQuery object.
*/
$.jidb = {
/**
* Set an item within the jalicData objectStore, using the given jdName and data, with optionally
* a dataType parameter to store alongside the data. If dataType is not provided, it is the result of
* typeof data.
* Notice that we 'put' data, rather than add it - that means we will always overwrite data with an
* identical key (jdName), if it already exists.
* @param jdName
* @param data
* @param dataType
* @returns {$.Deferred} Returns a jQuery Deferred object, which resolves with an empty body on success,
* or else resolves with the transaction or request error on failure.
*/
setItem:function(jdName, data, dataType){
var defer = $.Deferred(),
transaction = db.transaction(['jalicData'], 'readwrite'),
objectStore = transaction.objectStore("jalicData"),
request;
dataType = dataType || typeof data;
transaction.oncomplete = function(){
return defer.resolve();
};
transaction.onerror = function(event){
console.log(event);
return defer.reject(event);
};
request = objectStore.put({jdName:jdName, storedAt:+new Date(), dataType:dataType, data:data});
request.onerror = function(event){
console.log(event);
defer.reject(event);
};
return defer.promise();
},
/**
* Retrieve an item from the jalicData objectStore, using the given jdName as the key.
* @param jdName
* @returns {$.Deferred} Returns a jQuery Deferred object, which resolves with the request result as an object
* on success, or else resolves with the transaction or request error on failure.
*/
getItem:function(jdName){
var defer = $.Deferred(),
transaction = db.transaction(['jalicData'], 'readonly'),
objectStore = transaction.objectStore('jalicData'),
request = objectStore.get(jdName);
request.onerror = function(event){
console.log(event);
defer.reject(event);
};
request.onsuccess = function(event){
defer.resolve(request.result);
};
return defer.promise();
},
/**
* Remove an item from the jalicData objectStore, using the given jdName as the key.
* @param jdName
* @returns {$.Deferred} Returns a jQuery Deferred object, which resolves with an empty body on success,
* or else resolves with the transaction or request error on failure.
*/
removeItem:function(jdName){
var defer = $.Deferred(),
transaction = db.transaction(['jalicData'], 'readwrite'),
objectStore = transaction.objectStore('jalicData'),
request = objectStore.delete(jdName);
request.onerror = function(event){
console.log(event);
defer.reject(event);
};
request.onsuccess = function(){
defer.resolve();
};
return defer.promise();
},
/**
* Delete the jalic database and recreate it.
* @returns {$.Deferred} Returns a jQuery Deferred object, which resolves with an empty body on success,
* or else resolves with the transaction or request error on failure.
*/
clear:function(){
var defer = $.Deferred(),
request = idb.deleteDatabase('jalic');
dbReady = $.Deferred();
request.onerror = function(event){
console.log(event);
defer.reject(event);
};
request.onsuccess = function(){
dbReady.done(function(){
defer.resolve();
});
createDatabase(idb);
};
return defer.promise();
}
};
})(jQuery, window);