@herdwatch/lokijs 1.5.8-dev.2 → 1.5.8-dev.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/loki-indexed-adapter.min.js +1 -1
- package/package.json +1 -1
- package/src/loki-indexed-adapter.js +121 -157
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(root,factory){if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof exports==="object"){module.exports=factory()}else{root.LokiIndexedAdapter=factory()}})(this,function(){return function(){function LokiIndexedAdapter(appname,options){this.app="loki";this.options=options||{};if(typeof appname!=="undefined"){this.app=appname}this.catalog=null;if(!this.checkAvailability()){throw new Error("indexedDB does not seem to be supported for your environment")}}LokiIndexedAdapter.prototype.closeDatabase=function(){if(this.catalog&&this.catalog.db){this.catalog.db.close();this.catalog.db=null}};LokiIndexedAdapter.prototype.checkAvailability=function(){if(typeof indexedDB!=="undefined"&&indexedDB)return true;return false};LokiIndexedAdapter.prototype.loadDatabase=function(dbname,callback){var appName=this.app;var adapter=this;if(this.catalog===null||this.catalog.db===null){this.catalog=new LokiCatalog(function(cat){adapter.catalog=cat;adapter.loadDatabase(dbname,callback)});return}this.catalog.getAppKey(appName,dbname,function(result){if(typeof callback==="function"){if(result.id===0){callback(null);return}callback(result.val)}else{console.log(result.val)}})};LokiIndexedAdapter.prototype.loadKey=LokiIndexedAdapter.prototype.loadDatabase;LokiIndexedAdapter.prototype.saveDatabase=function(dbname,dbstring,callback){var appName=this.app;var adapter=this;function saveCallback(result){if(result&&result.success===true){callback(null)}else{callback(new Error("Error saving database"))}if(adapter.options.closeAfterSave){adapter.closeDatabase()}}if(this.catalog===null||this.catalog.db===null){this.catalog=new LokiCatalog(function(cat){adapter.saveDatabase(dbname,dbstring,
|
|
1
|
+
(function(root,factory){if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof exports==="object"){module.exports=factory()}else{root.LokiIndexedAdapter=factory()}})(this,function(){return function(){function LokiIndexedAdapter(appname,options){this.app="loki";this.options=options||{};if(typeof appname!=="undefined"){this.app=appname}this.catalog=null;if(!this.checkAvailability()){throw new Error("indexedDB does not seem to be supported for your environment")}}LokiIndexedAdapter.prototype.closeDatabase=function(){if(this.catalog&&this.catalog.db){this.catalog.db.close();this.catalog.db=null}};LokiIndexedAdapter.prototype.checkAvailability=function(){if(typeof indexedDB!=="undefined"&&indexedDB)return true;return false};LokiIndexedAdapter.prototype.loadDatabase=function(dbname,callback){var appName=this.app;var adapter=this;if(this.catalog===null||this.catalog.db===null){this.catalog=new LokiCatalog(function(cat){adapter.catalog=cat;adapter.loadDatabase(dbname,callback)});return}this.catalog.getAppKey(appName,dbname,function(result){if(typeof callback==="function"){if(result.id===0){callback(null);return}callback(result.val)}else{console.log(result.val)}})};LokiIndexedAdapter.prototype.loadKey=LokiIndexedAdapter.prototype.loadDatabase;LokiIndexedAdapter.prototype.saveDatabase=function(dbname,dbstring,callback){var appName=this.app;var adapter=this;function saveCallback(result){if(result&&result.success===true){callback(null)}else{callback(new Error("Error saving database"))}if(adapter.options.closeAfterSave){adapter.closeDatabase()}}if(this.catalog===null||this.catalog.db===null){this.catalog=new LokiCatalog(function(cat){adapter.saveDatabase(dbname,dbstring,callback)});return}this.catalog.setAppKey(appName,dbname,dbstring,saveCallback)};LokiIndexedAdapter.prototype.saveKey=LokiIndexedAdapter.prototype.saveDatabase;LokiIndexedAdapter.prototype.deleteDatabase=function(dbname,callback){var appName=this.app;var adapter=this;if(this.catalog===null||this.catalog.db===null){this.catalog=new LokiCatalog(function(cat){adapter.catalog=cat;adapter.deleteDatabase(dbname,callback)});return}this.catalog.getAppKey(appName,dbname,function(result){var id=result.id;if(id!==0){adapter.catalog.deleteAppKey(id,callback)}else if(typeof callback==="function"){callback({success:true})}})};LokiIndexedAdapter.prototype.deleteKey=LokiIndexedAdapter.prototype.deleteDatabase;LokiIndexedAdapter.prototype.deleteDatabasePartitions=function(dbname){var self=this;this.getDatabaseList(function(result){result.forEach(function(str){if(str.startsWith(dbname)){self.deleteDatabase(str)}})})};LokiIndexedAdapter.prototype.getDatabaseList=function(callback){var appName=this.app;var adapter=this;if(this.catalog===null||this.catalog.db===null){this.catalog=new LokiCatalog(function(cat){adapter.catalog=cat;adapter.getDatabaseList(callback)});return}this.catalog.getAppKeys(appName,function(results){var names=[];for(var idx=0;idx<results.length;idx++){names.push(results[idx].key)}if(typeof callback==="function"){callback(names)}else{names.forEach(function(obj){console.log(obj)})}})};LokiIndexedAdapter.prototype.getKeyList=LokiIndexedAdapter.prototype.getDatabaseList;LokiIndexedAdapter.prototype.getCatalogSummary=function(callback){var appName=this.app;var adapter=this;if(this.catalog===null||this.catalog.db===null){this.catalog=new LokiCatalog(function(cat){adapter.catalog=cat;adapter.getCatalogSummary(callback)});return}this.catalog.getAllKeys(function(results){var entries=[];var obj,size,oapp,okey,oval;for(var idx=0;idx<results.length;idx++){obj=results[idx];oapp=obj.app||"";okey=obj.key||"";oval=obj.val||"";size=oapp.length*2+okey.length*2+oval.length+1;entries.push({app:obj.app,key:obj.key,size:size})}if(typeof callback==="function"){callback(entries)}else{entries.forEach(function(obj){console.log(obj)})}})};function LokiCatalog(callback){this.db=null;this.initializeLokiCatalog(callback)}LokiCatalog.prototype.initializeLokiCatalog=function(callback){var openRequest=indexedDB.open("LokiCatalog",1);var cat=this;openRequest.onupgradeneeded=function(e){var thisDB=e.target.result;if(thisDB.objectStoreNames.contains("LokiAKV")){thisDB.deleteObjectStore("LokiAKV")}if(!thisDB.objectStoreNames.contains("LokiAKV")){var objectStore=thisDB.createObjectStore("LokiAKV",{keyPath:"id",autoIncrement:true});objectStore.createIndex("app","app",{unique:false});objectStore.createIndex("key","key",{unique:false});objectStore.createIndex("appkey","appkey",{unique:true})}};openRequest.onsuccess=function(e){cat.db=e.target.result;if(typeof callback==="function")callback(cat)};openRequest.onerror=function(e){throw e}};LokiCatalog.prototype.getAppKey=function(app,key,callback){var transaction=this.db.transaction(["LokiAKV"],"readonly");var store=transaction.objectStore("LokiAKV");var index=store.index("appkey");var appkey=app+","+key;var request=index.get(appkey);request.onsuccess=function(usercallback){return function(e){var lres=e.target.result;if(lres===null||typeof lres==="undefined"){lres={id:0,success:false}}if(typeof usercallback==="function"){usercallback(lres)}else{console.log(lres)}}}(callback);request.onerror=function(usercallback){return function(e){if(typeof usercallback==="function"){usercallback({id:0,success:false})}else{throw e}}}(callback)};LokiCatalog.prototype.getAppKeyById=function(id,callback,data){var transaction=this.db.transaction(["LokiAKV"],"readonly");var store=transaction.objectStore("LokiAKV");var request=store.get(id);request.onsuccess=function(data,usercallback){return function(e){if(typeof usercallback==="function"){usercallback(e.target.result,data)}else{console.log(e.target.result)}}}(data,callback)};LokiCatalog.prototype.setAppKey=function(app,key,val,callback){var transaction=this.db.transaction(["LokiAKV"],"readwrite");var store=transaction.objectStore("LokiAKV");var index=store.index("appkey");var appkey=app+","+key;var request=index.get(appkey);request.onsuccess=function(e){var res=e.target.result;if(res===null||res===undefined){res={app:app,key:key,appkey:app+","+key,val:val}}else{res.val=val}var requestPut=store.put(res);requestPut.onerror=function(usercallback){return function(e){if(typeof usercallback==="function"){usercallback({success:false})}else{console.error("LokiCatalog.setAppKey (set) onerror");console.error(request.error)}}}(callback);requestPut.onsuccess=function(usercallback){return function(e){if(typeof usercallback==="function"){usercallback({success:true})}}}(callback)};request.onerror=function(usercallback){return function(e){if(typeof usercallback==="function"){usercallback({success:false})}else{console.error("LokiCatalog.setAppKey (get) onerror");console.error(request.error)}}}(callback)};LokiCatalog.prototype.deleteAppKey=function(id,callback){var transaction=this.db.transaction(["LokiAKV"],"readwrite");var store=transaction.objectStore("LokiAKV");var request=store.delete(id);request.onsuccess=function(usercallback){return function(evt){if(typeof usercallback==="function")usercallback({success:true})}}(callback);request.onerror=function(usercallback){return function(evt){if(typeof usercallback==="function"){usercallback({success:false})}else{console.error("LokiCatalog.deleteAppKey raised onerror");console.error(request.error)}}}(callback)};LokiCatalog.prototype.getAppKeys=function(app,callback){var transaction=this.db.transaction(["LokiAKV"],"readonly");var store=transaction.objectStore("LokiAKV");var index=store.index("app");var singleKeyRange=IDBKeyRange.only(app);var cursor=index.openCursor(singleKeyRange);var localdata=[];cursor.onsuccess=function(data,callback){return function(e){var cursor=e.target.result;if(cursor){var currObject=cursor.value;data.push(currObject);cursor.continue()}else{if(typeof callback==="function"){callback(data)}else{console.log(data)}}}}(localdata,callback);cursor.onerror=function(usercallback){return function(e){if(typeof usercallback==="function"){usercallback(null)}else{console.error("LokiCatalog.getAppKeys raised onerror");console.error(e)}}}(callback)};LokiCatalog.prototype.getAllKeys=function(callback){var transaction=this.db.transaction(["LokiAKV"],"readonly");var store=transaction.objectStore("LokiAKV");var cursor=store.openCursor();var localdata=[];cursor.onsuccess=function(data,callback){return function(e){var cursor=e.target.result;if(cursor){var currObject=cursor.value;data.push(currObject);cursor.continue()}else{if(typeof callback==="function"){callback(data)}else{console.log(data)}}}}(localdata,callback);cursor.onerror=function(usercallback){return function(e){if(typeof usercallback==="function")usercallback(null)}}(callback)};return LokiIndexedAdapter}()});
|
package/package.json
CHANGED
|
@@ -9,19 +9,18 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
(function (root, factory) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}(this, function () {
|
|
23
|
-
return (function() {
|
|
24
|
-
|
|
12
|
+
if (typeof define === 'function' && define.amd) {
|
|
13
|
+
// AMD
|
|
14
|
+
define([], factory);
|
|
15
|
+
} else if (typeof exports === 'object') {
|
|
16
|
+
// Node, CommonJS-like
|
|
17
|
+
module.exports = factory();
|
|
18
|
+
} else {
|
|
19
|
+
// Browser globals (root is window)
|
|
20
|
+
root.LokiIndexedAdapter = factory();
|
|
21
|
+
}
|
|
22
|
+
})(this, function () {
|
|
23
|
+
return (function () {
|
|
25
24
|
/**
|
|
26
25
|
* Loki persistence adapter class for indexedDb.
|
|
27
26
|
* This class fulfills abstract adapter interface which can be applied to other storage methods.
|
|
@@ -40,13 +39,11 @@
|
|
|
40
39
|
* @param {object=} options Configuration options for the adapter
|
|
41
40
|
* @param {boolean} options.closeAfterSave Whether the indexedDB database should be closed after saving.
|
|
42
41
|
*/
|
|
43
|
-
function LokiIndexedAdapter(appname, options)
|
|
44
|
-
{
|
|
42
|
+
function LokiIndexedAdapter(appname, options) {
|
|
45
43
|
this.app = 'loki';
|
|
46
44
|
this.options = options || {};
|
|
47
45
|
|
|
48
|
-
if (typeof
|
|
49
|
-
{
|
|
46
|
+
if (typeof appname !== 'undefined') {
|
|
50
47
|
this.app = appname;
|
|
51
48
|
}
|
|
52
49
|
|
|
@@ -61,8 +58,7 @@
|
|
|
61
58
|
/**
|
|
62
59
|
* Used for closing the indexeddb database.
|
|
63
60
|
*/
|
|
64
|
-
LokiIndexedAdapter.prototype.closeDatabase = function ()
|
|
65
|
-
{
|
|
61
|
+
LokiIndexedAdapter.prototype.closeDatabase = function () {
|
|
66
62
|
if (this.catalog && this.catalog.db) {
|
|
67
63
|
this.catalog.db.close();
|
|
68
64
|
this.catalog.db = null;
|
|
@@ -75,8 +71,7 @@
|
|
|
75
71
|
* @returns {boolean} true if indexeddb is available, false if not.
|
|
76
72
|
* @memberof LokiIndexedAdapter
|
|
77
73
|
*/
|
|
78
|
-
LokiIndexedAdapter.prototype.checkAvailability = function()
|
|
79
|
-
{
|
|
74
|
+
LokiIndexedAdapter.prototype.checkAvailability = function () {
|
|
80
75
|
if (typeof indexedDB !== 'undefined' && indexedDB) return true;
|
|
81
76
|
|
|
82
77
|
return false;
|
|
@@ -97,14 +92,13 @@
|
|
|
97
92
|
* @param {function} callback - callback should accept string param containing serialized db string.
|
|
98
93
|
* @memberof LokiIndexedAdapter
|
|
99
94
|
*/
|
|
100
|
-
LokiIndexedAdapter.prototype.loadDatabase = function(dbname, callback)
|
|
101
|
-
{
|
|
95
|
+
LokiIndexedAdapter.prototype.loadDatabase = function (dbname, callback) {
|
|
102
96
|
var appName = this.app;
|
|
103
97
|
var adapter = this;
|
|
104
98
|
|
|
105
99
|
// lazy open/create db reference so dont -need- callback in constructor
|
|
106
100
|
if (this.catalog === null || this.catalog.db === null) {
|
|
107
|
-
this.catalog = new LokiCatalog(function(cat) {
|
|
101
|
+
this.catalog = new LokiCatalog(function (cat) {
|
|
108
102
|
adapter.catalog = cat;
|
|
109
103
|
|
|
110
104
|
adapter.loadDatabase(dbname, callback);
|
|
@@ -114,15 +108,14 @@
|
|
|
114
108
|
}
|
|
115
109
|
|
|
116
110
|
// lookup up db string in AKV db
|
|
117
|
-
this.catalog.getAppKey(appName, dbname, function(result) {
|
|
118
|
-
if (typeof
|
|
111
|
+
this.catalog.getAppKey(appName, dbname, function (result) {
|
|
112
|
+
if (typeof callback === 'function') {
|
|
119
113
|
if (result.id === 0) {
|
|
120
114
|
callback(null);
|
|
121
115
|
return;
|
|
122
116
|
}
|
|
123
117
|
callback(result.val);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
118
|
+
} else {
|
|
126
119
|
// support console use of api
|
|
127
120
|
console.log(result.val);
|
|
128
121
|
}
|
|
@@ -148,17 +141,15 @@
|
|
|
148
141
|
* @param {function} callback - (Optional) callback passed obj.success with true or false
|
|
149
142
|
* @memberof LokiIndexedAdapter
|
|
150
143
|
*/
|
|
151
|
-
LokiIndexedAdapter.prototype.saveDatabase = function(dbname, dbstring, callback)
|
|
152
|
-
{
|
|
144
|
+
LokiIndexedAdapter.prototype.saveDatabase = function (dbname, dbstring, callback) {
|
|
153
145
|
var appName = this.app;
|
|
154
146
|
var adapter = this;
|
|
155
147
|
|
|
156
148
|
function saveCallback(result) {
|
|
157
149
|
if (result && result.success === true) {
|
|
158
150
|
callback(null);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
callback(new Error("Error saving database"));
|
|
151
|
+
} else {
|
|
152
|
+
callback(new Error('Error saving database'));
|
|
162
153
|
}
|
|
163
154
|
|
|
164
155
|
if (adapter.options.closeAfterSave) {
|
|
@@ -168,8 +159,8 @@
|
|
|
168
159
|
|
|
169
160
|
// lazy open/create db reference so dont -need- callback in constructor
|
|
170
161
|
if (this.catalog === null || this.catalog.db === null) {
|
|
171
|
-
this.catalog = new LokiCatalog(function(cat) {
|
|
172
|
-
adapter.saveDatabase(dbname, dbstring,
|
|
162
|
+
this.catalog = new LokiCatalog(function (cat) {
|
|
163
|
+
adapter.saveDatabase(dbname, dbstring, callback);
|
|
173
164
|
});
|
|
174
165
|
|
|
175
166
|
return;
|
|
@@ -196,14 +187,13 @@
|
|
|
196
187
|
* @param {function=} callback - (Optional) executed on database delete
|
|
197
188
|
* @memberof LokiIndexedAdapter
|
|
198
189
|
*/
|
|
199
|
-
LokiIndexedAdapter.prototype.deleteDatabase = function(dbname, callback)
|
|
200
|
-
{
|
|
190
|
+
LokiIndexedAdapter.prototype.deleteDatabase = function (dbname, callback) {
|
|
201
191
|
var appName = this.app;
|
|
202
192
|
var adapter = this;
|
|
203
193
|
|
|
204
194
|
// lazy open/create db reference and pass callback ahead
|
|
205
195
|
if (this.catalog === null || this.catalog.db === null) {
|
|
206
|
-
this.catalog = new LokiCatalog(function(cat) {
|
|
196
|
+
this.catalog = new LokiCatalog(function (cat) {
|
|
207
197
|
adapter.catalog = cat;
|
|
208
198
|
|
|
209
199
|
adapter.deleteDatabase(dbname, callback);
|
|
@@ -213,12 +203,12 @@
|
|
|
213
203
|
}
|
|
214
204
|
|
|
215
205
|
// catalog was already initialized, so just lookup object and delete by id
|
|
216
|
-
this.catalog.getAppKey(appName, dbname, function(result) {
|
|
206
|
+
this.catalog.getAppKey(appName, dbname, function (result) {
|
|
217
207
|
var id = result.id;
|
|
218
208
|
|
|
219
209
|
if (id !== 0) {
|
|
220
210
|
adapter.catalog.deleteAppKey(id, callback);
|
|
221
|
-
} else if (typeof
|
|
211
|
+
} else if (typeof callback === 'function') {
|
|
222
212
|
callback({ success: true });
|
|
223
213
|
}
|
|
224
214
|
});
|
|
@@ -234,10 +224,10 @@
|
|
|
234
224
|
* @param {string} dbname - the base filename which container, partitions, or pages are derived
|
|
235
225
|
* @memberof LokiIndexedAdapter
|
|
236
226
|
*/
|
|
237
|
-
LokiIndexedAdapter.prototype.deleteDatabasePartitions = function(dbname) {
|
|
238
|
-
var self=this;
|
|
239
|
-
this.getDatabaseList(function(result) {
|
|
240
|
-
result.forEach(function(str) {
|
|
227
|
+
LokiIndexedAdapter.prototype.deleteDatabasePartitions = function (dbname) {
|
|
228
|
+
var self = this;
|
|
229
|
+
this.getDatabaseList(function (result) {
|
|
230
|
+
result.forEach(function (str) {
|
|
241
231
|
if (str.startsWith(dbname)) {
|
|
242
232
|
self.deleteDatabase(str);
|
|
243
233
|
}
|
|
@@ -259,14 +249,13 @@
|
|
|
259
249
|
* @param {function} callback - should accept array of database names in the catalog for current app.
|
|
260
250
|
* @memberof LokiIndexedAdapter
|
|
261
251
|
*/
|
|
262
|
-
LokiIndexedAdapter.prototype.getDatabaseList = function(callback)
|
|
263
|
-
{
|
|
252
|
+
LokiIndexedAdapter.prototype.getDatabaseList = function (callback) {
|
|
264
253
|
var appName = this.app;
|
|
265
254
|
var adapter = this;
|
|
266
255
|
|
|
267
256
|
// lazy open/create db reference so dont -need- callback in constructor
|
|
268
257
|
if (this.catalog === null || this.catalog.db === null) {
|
|
269
|
-
this.catalog = new LokiCatalog(function(cat) {
|
|
258
|
+
this.catalog = new LokiCatalog(function (cat) {
|
|
270
259
|
adapter.catalog = cat;
|
|
271
260
|
|
|
272
261
|
adapter.getDatabaseList(callback);
|
|
@@ -277,18 +266,17 @@
|
|
|
277
266
|
|
|
278
267
|
// catalog already initialized
|
|
279
268
|
// get all keys for current appName, and transpose results so just string array
|
|
280
|
-
this.catalog.getAppKeys(appName, function(results) {
|
|
269
|
+
this.catalog.getAppKeys(appName, function (results) {
|
|
281
270
|
var names = [];
|
|
282
271
|
|
|
283
|
-
for(var idx = 0; idx < results.length; idx++) {
|
|
272
|
+
for (var idx = 0; idx < results.length; idx++) {
|
|
284
273
|
names.push(results[idx].key);
|
|
285
274
|
}
|
|
286
275
|
|
|
287
|
-
if (typeof
|
|
276
|
+
if (typeof callback === 'function') {
|
|
288
277
|
callback(names);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
names.forEach(function(obj) {
|
|
278
|
+
} else {
|
|
279
|
+
names.forEach(function (obj) {
|
|
292
280
|
console.log(obj);
|
|
293
281
|
});
|
|
294
282
|
}
|
|
@@ -304,14 +292,13 @@
|
|
|
304
292
|
* @param {function} callback - (Optional) callback to accept result array.
|
|
305
293
|
* @memberof LokiIndexedAdapter
|
|
306
294
|
*/
|
|
307
|
-
LokiIndexedAdapter.prototype.getCatalogSummary = function(callback)
|
|
308
|
-
{
|
|
295
|
+
LokiIndexedAdapter.prototype.getCatalogSummary = function (callback) {
|
|
309
296
|
var appName = this.app;
|
|
310
297
|
var adapter = this;
|
|
311
298
|
|
|
312
299
|
// lazy open/create db reference
|
|
313
300
|
if (this.catalog === null || this.catalog.db === null) {
|
|
314
|
-
this.catalog = new LokiCatalog(function(cat) {
|
|
301
|
+
this.catalog = new LokiCatalog(function (cat) {
|
|
315
302
|
adapter.catalog = cat;
|
|
316
303
|
|
|
317
304
|
adapter.getCatalogSummary(callback);
|
|
@@ -322,15 +309,11 @@
|
|
|
322
309
|
|
|
323
310
|
// catalog already initialized
|
|
324
311
|
// get all keys for current appName, and transpose results so just string array
|
|
325
|
-
this.catalog.getAllKeys(function(results) {
|
|
312
|
+
this.catalog.getAllKeys(function (results) {
|
|
326
313
|
var entries = [];
|
|
327
|
-
var obj,
|
|
328
|
-
size,
|
|
329
|
-
oapp,
|
|
330
|
-
okey,
|
|
331
|
-
oval;
|
|
314
|
+
var obj, size, oapp, okey, oval;
|
|
332
315
|
|
|
333
|
-
for(var idx = 0; idx < results.length; idx++) {
|
|
316
|
+
for (var idx = 0; idx < results.length; idx++) {
|
|
334
317
|
obj = results[idx];
|
|
335
318
|
oapp = obj.app || '';
|
|
336
319
|
okey = obj.key || '';
|
|
@@ -339,14 +322,13 @@
|
|
|
339
322
|
// app and key are composited into an appkey column so we will mult by 2
|
|
340
323
|
size = oapp.length * 2 + okey.length * 2 + oval.length + 1;
|
|
341
324
|
|
|
342
|
-
entries.push({
|
|
325
|
+
entries.push({ app: obj.app, key: obj.key, size: size });
|
|
343
326
|
}
|
|
344
327
|
|
|
345
|
-
if (typeof
|
|
328
|
+
if (typeof callback === 'function') {
|
|
346
329
|
callback(entries);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
entries.forEach(function(obj) {
|
|
330
|
+
} else {
|
|
331
|
+
entries.forEach(function (obj) {
|
|
350
332
|
console.log(obj);
|
|
351
333
|
});
|
|
352
334
|
}
|
|
@@ -358,79 +340,75 @@
|
|
|
358
340
|
* This non-interface class implements the actual persistence.
|
|
359
341
|
* Used by the IndexedAdapter class.
|
|
360
342
|
*/
|
|
361
|
-
function LokiCatalog(callback)
|
|
362
|
-
{
|
|
343
|
+
function LokiCatalog(callback) {
|
|
363
344
|
this.db = null;
|
|
364
345
|
this.initializeLokiCatalog(callback);
|
|
365
346
|
}
|
|
366
347
|
|
|
367
|
-
LokiCatalog.prototype.initializeLokiCatalog = function(callback) {
|
|
348
|
+
LokiCatalog.prototype.initializeLokiCatalog = function (callback) {
|
|
368
349
|
var openRequest = indexedDB.open('LokiCatalog', 1);
|
|
369
350
|
var cat = this;
|
|
370
351
|
|
|
371
352
|
// If database doesn't exist yet or its version is lower than our version specified above (2nd param in line above)
|
|
372
|
-
openRequest.onupgradeneeded = function(e) {
|
|
353
|
+
openRequest.onupgradeneeded = function (e) {
|
|
373
354
|
var thisDB = e.target.result;
|
|
374
355
|
if (thisDB.objectStoreNames.contains('LokiAKV')) {
|
|
375
356
|
thisDB.deleteObjectStore('LokiAKV');
|
|
376
357
|
}
|
|
377
358
|
|
|
378
|
-
if(!thisDB.objectStoreNames.contains('LokiAKV')) {
|
|
379
|
-
var objectStore = thisDB.createObjectStore('LokiAKV', { keyPath: 'id', autoIncrement:true });
|
|
380
|
-
objectStore.createIndex('app', 'app', {unique:false});
|
|
381
|
-
objectStore.createIndex('key', 'key', {unique:false});
|
|
359
|
+
if (!thisDB.objectStoreNames.contains('LokiAKV')) {
|
|
360
|
+
var objectStore = thisDB.createObjectStore('LokiAKV', { keyPath: 'id', autoIncrement: true });
|
|
361
|
+
objectStore.createIndex('app', 'app', { unique: false });
|
|
362
|
+
objectStore.createIndex('key', 'key', { unique: false });
|
|
382
363
|
// hack to simulate composite key since overhead is low (main size should be in val field)
|
|
383
364
|
// user (me) required to duplicate the app and key into comma delimited appkey field off object
|
|
384
365
|
// This will allow retrieving single record with that composite key as well as
|
|
385
366
|
// still supporting opening cursors on app or key alone
|
|
386
|
-
objectStore.createIndex('appkey', 'appkey', {unique:true});
|
|
367
|
+
objectStore.createIndex('appkey', 'appkey', { unique: true });
|
|
387
368
|
}
|
|
388
369
|
};
|
|
389
370
|
|
|
390
|
-
openRequest.onsuccess = function(e) {
|
|
371
|
+
openRequest.onsuccess = function (e) {
|
|
391
372
|
cat.db = e.target.result;
|
|
392
|
-
|
|
393
|
-
if (typeof (callback) === 'function') callback(cat);
|
|
373
|
+
if (typeof callback === 'function') callback(cat);
|
|
394
374
|
};
|
|
395
375
|
|
|
396
|
-
openRequest.onerror = function(e) {
|
|
376
|
+
openRequest.onerror = function (e) {
|
|
397
377
|
throw e;
|
|
398
378
|
};
|
|
399
379
|
};
|
|
400
380
|
|
|
401
|
-
LokiCatalog.prototype.getAppKey = function(app, key, callback) {
|
|
381
|
+
LokiCatalog.prototype.getAppKey = function (app, key, callback) {
|
|
402
382
|
var transaction = this.db.transaction(['LokiAKV'], 'readonly');
|
|
403
383
|
var store = transaction.objectStore('LokiAKV');
|
|
404
384
|
var index = store.index('appkey');
|
|
405
|
-
var appkey = app +
|
|
385
|
+
var appkey = app + ',' + key;
|
|
406
386
|
var request = index.get(appkey);
|
|
407
387
|
|
|
408
|
-
request.onsuccess = (function(usercallback) {
|
|
409
|
-
return function(e) {
|
|
388
|
+
request.onsuccess = (function (usercallback) {
|
|
389
|
+
return function (e) {
|
|
410
390
|
var lres = e.target.result;
|
|
411
391
|
|
|
412
|
-
if (lres === null || typeof
|
|
392
|
+
if (lres === null || typeof lres === 'undefined') {
|
|
413
393
|
lres = {
|
|
414
394
|
id: 0,
|
|
415
|
-
success: false
|
|
395
|
+
success: false,
|
|
416
396
|
};
|
|
417
397
|
}
|
|
418
398
|
|
|
419
|
-
if (typeof
|
|
399
|
+
if (typeof usercallback === 'function') {
|
|
420
400
|
usercallback(lres);
|
|
421
|
-
}
|
|
422
|
-
else {
|
|
401
|
+
} else {
|
|
423
402
|
console.log(lres);
|
|
424
403
|
}
|
|
425
404
|
};
|
|
426
405
|
})(callback);
|
|
427
406
|
|
|
428
|
-
request.onerror = (function(usercallback) {
|
|
429
|
-
return function(e) {
|
|
430
|
-
if (typeof
|
|
407
|
+
request.onerror = (function (usercallback) {
|
|
408
|
+
return function (e) {
|
|
409
|
+
if (typeof usercallback === 'function') {
|
|
431
410
|
usercallback({ id: 0, success: false });
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
411
|
+
} else {
|
|
434
412
|
throw e;
|
|
435
413
|
}
|
|
436
414
|
};
|
|
@@ -442,12 +420,11 @@
|
|
|
442
420
|
var store = transaction.objectStore('LokiAKV');
|
|
443
421
|
var request = store.get(id);
|
|
444
422
|
|
|
445
|
-
request.onsuccess = (function(data, usercallback){
|
|
446
|
-
return function(e) {
|
|
447
|
-
if (typeof
|
|
423
|
+
request.onsuccess = (function (data, usercallback) {
|
|
424
|
+
return function (e) {
|
|
425
|
+
if (typeof usercallback === 'function') {
|
|
448
426
|
usercallback(e.target.result, data);
|
|
449
|
-
}
|
|
450
|
-
else {
|
|
427
|
+
} else {
|
|
451
428
|
console.log(e.target.result);
|
|
452
429
|
}
|
|
453
430
|
};
|
|
@@ -458,56 +435,52 @@
|
|
|
458
435
|
var transaction = this.db.transaction(['LokiAKV'], 'readwrite');
|
|
459
436
|
var store = transaction.objectStore('LokiAKV');
|
|
460
437
|
var index = store.index('appkey');
|
|
461
|
-
var appkey = app +
|
|
438
|
+
var appkey = app + ',' + key;
|
|
462
439
|
var request = index.get(appkey);
|
|
463
440
|
|
|
464
441
|
// first try to retrieve an existing object by that key
|
|
465
442
|
// need to do this because to update an object you need to have id in object, otherwise it will append id with new autocounter and clash the unique index appkey
|
|
466
|
-
request.onsuccess = function(e) {
|
|
443
|
+
request.onsuccess = function (e) {
|
|
467
444
|
var res = e.target.result;
|
|
468
445
|
|
|
469
446
|
if (res === null || res === undefined) {
|
|
470
447
|
res = {
|
|
471
|
-
app:app,
|
|
472
|
-
key:key,
|
|
448
|
+
app: app,
|
|
449
|
+
key: key,
|
|
473
450
|
appkey: app + ',' + key,
|
|
474
|
-
val:val
|
|
451
|
+
val: val,
|
|
475
452
|
};
|
|
476
|
-
}
|
|
477
|
-
else {
|
|
453
|
+
} else {
|
|
478
454
|
res.val = val;
|
|
479
455
|
}
|
|
480
456
|
|
|
481
457
|
var requestPut = store.put(res);
|
|
482
458
|
|
|
483
|
-
requestPut.onerror = (function(usercallback) {
|
|
484
|
-
return function(e) {
|
|
485
|
-
if (typeof
|
|
459
|
+
requestPut.onerror = (function (usercallback) {
|
|
460
|
+
return function (e) {
|
|
461
|
+
if (typeof usercallback === 'function') {
|
|
486
462
|
usercallback({ success: false });
|
|
487
|
-
}
|
|
488
|
-
else {
|
|
463
|
+
} else {
|
|
489
464
|
console.error('LokiCatalog.setAppKey (set) onerror');
|
|
490
465
|
console.error(request.error);
|
|
491
466
|
}
|
|
492
467
|
};
|
|
493
|
-
|
|
494
468
|
})(callback);
|
|
495
469
|
|
|
496
|
-
requestPut.onsuccess = (function(usercallback) {
|
|
497
|
-
return function(e) {
|
|
498
|
-
if (typeof
|
|
470
|
+
requestPut.onsuccess = (function (usercallback) {
|
|
471
|
+
return function (e) {
|
|
472
|
+
if (typeof usercallback === 'function') {
|
|
499
473
|
usercallback({ success: true });
|
|
500
474
|
}
|
|
501
475
|
};
|
|
502
476
|
})(callback);
|
|
503
477
|
};
|
|
504
478
|
|
|
505
|
-
request.onerror = (function(usercallback) {
|
|
506
|
-
return function(e) {
|
|
507
|
-
if (typeof
|
|
479
|
+
request.onerror = (function (usercallback) {
|
|
480
|
+
return function (e) {
|
|
481
|
+
if (typeof usercallback === 'function') {
|
|
508
482
|
usercallback({ success: false });
|
|
509
|
-
}
|
|
510
|
-
else {
|
|
483
|
+
} else {
|
|
511
484
|
console.error('LokiCatalog.setAppKey (get) onerror');
|
|
512
485
|
console.error(request.error);
|
|
513
486
|
}
|
|
@@ -520,18 +493,17 @@
|
|
|
520
493
|
var store = transaction.objectStore('LokiAKV');
|
|
521
494
|
var request = store.delete(id);
|
|
522
495
|
|
|
523
|
-
request.onsuccess = (function(usercallback) {
|
|
524
|
-
return function(evt) {
|
|
525
|
-
if (typeof
|
|
496
|
+
request.onsuccess = (function (usercallback) {
|
|
497
|
+
return function (evt) {
|
|
498
|
+
if (typeof usercallback === 'function') usercallback({ success: true });
|
|
526
499
|
};
|
|
527
500
|
})(callback);
|
|
528
501
|
|
|
529
|
-
request.onerror = (function(usercallback) {
|
|
530
|
-
return function(evt) {
|
|
531
|
-
if (typeof
|
|
502
|
+
request.onerror = (function (usercallback) {
|
|
503
|
+
return function (evt) {
|
|
504
|
+
if (typeof usercallback === 'function') {
|
|
532
505
|
usercallback({ success: false });
|
|
533
|
-
}
|
|
534
|
-
else {
|
|
506
|
+
} else {
|
|
535
507
|
console.error('LokiCatalog.deleteAppKey raised onerror');
|
|
536
508
|
console.error(request.error);
|
|
537
509
|
}
|
|
@@ -539,7 +511,7 @@
|
|
|
539
511
|
})(callback);
|
|
540
512
|
};
|
|
541
513
|
|
|
542
|
-
LokiCatalog.prototype.getAppKeys = function(app, callback) {
|
|
514
|
+
LokiCatalog.prototype.getAppKeys = function (app, callback) {
|
|
543
515
|
var transaction = this.db.transaction(['LokiAKV'], 'readonly');
|
|
544
516
|
var store = transaction.objectStore('LokiAKV');
|
|
545
517
|
var index = store.index('app');
|
|
@@ -554,8 +526,8 @@
|
|
|
554
526
|
// this.data[] when done (similar to service)
|
|
555
527
|
var localdata = [];
|
|
556
528
|
|
|
557
|
-
cursor.onsuccess = (function(data, callback) {
|
|
558
|
-
return function(e) {
|
|
529
|
+
cursor.onsuccess = (function (data, callback) {
|
|
530
|
+
return function (e) {
|
|
559
531
|
var cursor = e.target.result;
|
|
560
532
|
if (cursor) {
|
|
561
533
|
var currObject = cursor.value;
|
|
@@ -563,30 +535,26 @@
|
|
|
563
535
|
data.push(currObject);
|
|
564
536
|
|
|
565
537
|
cursor.continue();
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
if (typeof(callback) === 'function') {
|
|
538
|
+
} else {
|
|
539
|
+
if (typeof callback === 'function') {
|
|
569
540
|
callback(data);
|
|
570
|
-
}
|
|
571
|
-
else {
|
|
541
|
+
} else {
|
|
572
542
|
console.log(data);
|
|
573
543
|
}
|
|
574
544
|
}
|
|
575
545
|
};
|
|
576
546
|
})(localdata, callback);
|
|
577
547
|
|
|
578
|
-
cursor.onerror = (function(usercallback) {
|
|
579
|
-
return function(e) {
|
|
580
|
-
if (typeof
|
|
548
|
+
cursor.onerror = (function (usercallback) {
|
|
549
|
+
return function (e) {
|
|
550
|
+
if (typeof usercallback === 'function') {
|
|
581
551
|
usercallback(null);
|
|
582
|
-
}
|
|
583
|
-
else {
|
|
552
|
+
} else {
|
|
584
553
|
console.error('LokiCatalog.getAppKeys raised onerror');
|
|
585
554
|
console.error(e);
|
|
586
555
|
}
|
|
587
556
|
};
|
|
588
557
|
})(callback);
|
|
589
|
-
|
|
590
558
|
};
|
|
591
559
|
|
|
592
560
|
// Hide 'cursoring' and return array of { id: id, key: key }
|
|
@@ -597,8 +565,8 @@
|
|
|
597
565
|
|
|
598
566
|
var localdata = [];
|
|
599
567
|
|
|
600
|
-
cursor.onsuccess = (function(data, callback) {
|
|
601
|
-
return function(e) {
|
|
568
|
+
cursor.onsuccess = (function (data, callback) {
|
|
569
|
+
return function (e) {
|
|
602
570
|
var cursor = e.target.result;
|
|
603
571
|
if (cursor) {
|
|
604
572
|
var currObject = cursor.value;
|
|
@@ -606,27 +574,23 @@
|
|
|
606
574
|
data.push(currObject);
|
|
607
575
|
|
|
608
576
|
cursor.continue();
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
if (typeof(callback) === 'function') {
|
|
577
|
+
} else {
|
|
578
|
+
if (typeof callback === 'function') {
|
|
612
579
|
callback(data);
|
|
613
|
-
}
|
|
614
|
-
else {
|
|
580
|
+
} else {
|
|
615
581
|
console.log(data);
|
|
616
582
|
}
|
|
617
583
|
}
|
|
618
584
|
};
|
|
619
585
|
})(localdata, callback);
|
|
620
586
|
|
|
621
|
-
cursor.onerror = (function(usercallback) {
|
|
622
|
-
return function(e) {
|
|
623
|
-
if (typeof
|
|
587
|
+
cursor.onerror = (function (usercallback) {
|
|
588
|
+
return function (e) {
|
|
589
|
+
if (typeof usercallback === 'function') usercallback(null);
|
|
624
590
|
};
|
|
625
591
|
})(callback);
|
|
626
|
-
|
|
627
592
|
};
|
|
628
593
|
|
|
629
594
|
return LokiIndexedAdapter;
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
}));
|
|
595
|
+
})();
|
|
596
|
+
});
|