1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | (function (root, factory) {
|
12 | if (typeof define === 'function' && define.amd) {
|
13 |
|
14 | define([], factory);
|
15 | } else if (typeof exports === 'object') {
|
16 |
|
17 | module.exports = factory();
|
18 | } else {
|
19 |
|
20 | root.LokiIndexedAdapter = factory();
|
21 | }
|
22 | }(this, function () {
|
23 | return (function() {
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function LokiIndexedAdapter(appname)
|
42 | {
|
43 | this.app = 'loki';
|
44 |
|
45 | if (typeof (appname) !== 'undefined')
|
46 | {
|
47 | this.app = appname;
|
48 | }
|
49 |
|
50 |
|
51 | this.catalog = null;
|
52 |
|
53 | if (!this.checkAvailability()) {
|
54 | throw new Error('indexedDB does not seem to be supported for your environment');
|
55 | }
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | LokiIndexedAdapter.prototype.checkAvailability = function()
|
65 | {
|
66 | if (typeof indexedDB !== 'undefined' && indexedDB) return true;
|
67 |
|
68 | return false;
|
69 | };
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | LokiIndexedAdapter.prototype.loadDatabase = function(dbname, callback)
|
87 | {
|
88 | var appName = this.app;
|
89 | var adapter = this;
|
90 |
|
91 |
|
92 | if (this.catalog === null || this.catalog.db === null) {
|
93 | this.catalog = new LokiCatalog(function(cat) {
|
94 | adapter.catalog = cat;
|
95 |
|
96 | adapter.loadDatabase(dbname, callback);
|
97 | });
|
98 |
|
99 | return;
|
100 | }
|
101 |
|
102 |
|
103 | this.catalog.getAppKey(appName, dbname, function(result) {
|
104 | if (typeof (callback) === 'function') {
|
105 | if (result.id === 0) {
|
106 | callback(null);
|
107 | return;
|
108 | }
|
109 | callback(result.val);
|
110 | }
|
111 | else {
|
112 |
|
113 | console.log(result.val);
|
114 | }
|
115 | });
|
116 | };
|
117 |
|
118 |
|
119 | LokiIndexedAdapter.prototype.loadKey = LokiIndexedAdapter.prototype.loadDatabase;
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | LokiIndexedAdapter.prototype.saveDatabase = function(dbname, dbstring, callback)
|
138 | {
|
139 | var appName = this.app;
|
140 | var adapter = this;
|
141 |
|
142 | function saveCallback(result) {
|
143 | if (result && result.success === true) {
|
144 | callback(null);
|
145 | }
|
146 | else {
|
147 | callback(new Error("Error saving database"));
|
148 | }
|
149 | }
|
150 |
|
151 |
|
152 | if (this.catalog === null || this.catalog.db === null) {
|
153 | this.catalog = new LokiCatalog(function(cat) {
|
154 | adapter.catalog = cat;
|
155 |
|
156 |
|
157 | cat.setAppKey(appName, dbname, dbstring, saveCallback);
|
158 | });
|
159 |
|
160 | return;
|
161 | }
|
162 |
|
163 |
|
164 | this.catalog.setAppKey(appName, dbname, dbstring, saveCallback);
|
165 | };
|
166 |
|
167 |
|
168 | LokiIndexedAdapter.prototype.saveKey = LokiIndexedAdapter.prototype.saveDatabase;
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | LokiIndexedAdapter.prototype.deleteDatabase = function(dbname, callback)
|
185 | {
|
186 | var appName = this.app;
|
187 | var adapter = this;
|
188 |
|
189 |
|
190 | if (this.catalog === null || this.catalog.db === null) {
|
191 | this.catalog = new LokiCatalog(function(cat) {
|
192 | adapter.catalog = cat;
|
193 |
|
194 | adapter.deleteDatabase(dbname, callback);
|
195 | });
|
196 |
|
197 | return;
|
198 | }
|
199 |
|
200 |
|
201 | this.catalog.getAppKey(appName, dbname, function(result) {
|
202 | var id = result.id;
|
203 |
|
204 | if (id !== 0) {
|
205 | adapter.catalog.deleteAppKey(id);
|
206 | }
|
207 |
|
208 | if (typeof (callback) === 'function') {
|
209 | callback();
|
210 | }
|
211 | });
|
212 | };
|
213 |
|
214 |
|
215 | LokiIndexedAdapter.prototype.deleteKey = LokiIndexedAdapter.prototype.deleteDatabase;
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | LokiIndexedAdapter.prototype.deleteDatabasePartitions = function(dbname) {
|
225 | var self=this;
|
226 | this.getDatabaseList(function(result) {
|
227 | result.forEach(function(str) {
|
228 | if (str.startsWith(dbname)) {
|
229 | self.deleteDatabase(str);
|
230 | }
|
231 | });
|
232 | });
|
233 | };
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | LokiIndexedAdapter.prototype.getDatabaseList = function(callback)
|
250 | {
|
251 | var appName = this.app;
|
252 | var adapter = this;
|
253 |
|
254 |
|
255 | if (this.catalog === null || this.catalog.db === null) {
|
256 | this.catalog = new LokiCatalog(function(cat) {
|
257 | adapter.catalog = cat;
|
258 |
|
259 | adapter.getDatabaseList(callback);
|
260 | });
|
261 |
|
262 | return;
|
263 | }
|
264 |
|
265 |
|
266 |
|
267 | this.catalog.getAppKeys(appName, function(results) {
|
268 | var names = [];
|
269 |
|
270 | for(var idx = 0; idx < results.length; idx++) {
|
271 | names.push(results[idx].key);
|
272 | }
|
273 |
|
274 | if (typeof (callback) === 'function') {
|
275 | callback(names);
|
276 | }
|
277 | else {
|
278 | names.forEach(function(obj) {
|
279 | console.log(obj);
|
280 | });
|
281 | }
|
282 | });
|
283 | };
|
284 |
|
285 |
|
286 | LokiIndexedAdapter.prototype.getKeyList = LokiIndexedAdapter.prototype.getDatabaseList;
|
287 |
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | LokiIndexedAdapter.prototype.getCatalogSummary = function(callback)
|
295 | {
|
296 | var appName = this.app;
|
297 | var adapter = this;
|
298 |
|
299 |
|
300 | if (this.catalog === null || this.catalog.db === null) {
|
301 | this.catalog = new LokiCatalog(function(cat) {
|
302 | adapter.catalog = cat;
|
303 |
|
304 | adapter.getCatalogSummary(callback);
|
305 | });
|
306 |
|
307 | return;
|
308 | }
|
309 |
|
310 |
|
311 |
|
312 | this.catalog.getAllKeys(function(results) {
|
313 | var entries = [];
|
314 | var obj,
|
315 | size,
|
316 | oapp,
|
317 | okey,
|
318 | oval;
|
319 |
|
320 | for(var idx = 0; idx < results.length; idx++) {
|
321 | obj = results[idx];
|
322 | oapp = obj.app || '';
|
323 | okey = obj.key || '';
|
324 | oval = obj.val || '';
|
325 |
|
326 |
|
327 | size = oapp.length * 2 + okey.length * 2 + oval.length + 1;
|
328 |
|
329 | entries.push({ "app": obj.app, "key": obj.key, "size": size });
|
330 | }
|
331 |
|
332 | if (typeof (callback) === 'function') {
|
333 | callback(entries);
|
334 | }
|
335 | else {
|
336 | entries.forEach(function(obj) {
|
337 | console.log(obj);
|
338 | });
|
339 | }
|
340 | });
|
341 | };
|
342 |
|
343 | |
344 |
|
345 |
|
346 |
|
347 |
|
348 | function LokiCatalog(callback)
|
349 | {
|
350 | this.db = null;
|
351 | this.initializeLokiCatalog(callback);
|
352 | }
|
353 |
|
354 | LokiCatalog.prototype.initializeLokiCatalog = function(callback) {
|
355 | var openRequest = indexedDB.open('LokiCatalog', 1);
|
356 | var cat = this;
|
357 |
|
358 |
|
359 | openRequest.onupgradeneeded = function(e) {
|
360 | var thisDB = e.target.result;
|
361 | if (thisDB.objectStoreNames.contains('LokiAKV')) {
|
362 | thisDB.deleteObjectStore('LokiAKV');
|
363 | }
|
364 |
|
365 | if(!thisDB.objectStoreNames.contains('LokiAKV')) {
|
366 | var objectStore = thisDB.createObjectStore('LokiAKV', { keyPath: 'id', autoIncrement:true });
|
367 | objectStore.createIndex('app', 'app', {unique:false});
|
368 | objectStore.createIndex('key', 'key', {unique:false});
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | objectStore.createIndex('appkey', 'appkey', {unique:true});
|
374 | }
|
375 | };
|
376 |
|
377 | openRequest.onsuccess = function(e) {
|
378 | cat.db = e.target.result;
|
379 |
|
380 | if (typeof (callback) === 'function') callback(cat);
|
381 | };
|
382 |
|
383 | openRequest.onerror = function(e) {
|
384 | throw e;
|
385 | };
|
386 | };
|
387 |
|
388 | LokiCatalog.prototype.getAppKey = function(app, key, callback) {
|
389 | var transaction = this.db.transaction(['LokiAKV'], 'readonly');
|
390 | var store = transaction.objectStore('LokiAKV');
|
391 | var index = store.index('appkey');
|
392 | var appkey = app + "," + key;
|
393 | var request = index.get(appkey);
|
394 |
|
395 | request.onsuccess = (function(usercallback) {
|
396 | return function(e) {
|
397 | var lres = e.target.result;
|
398 |
|
399 | if (lres === null || typeof(lres) === 'undefined') {
|
400 | lres = {
|
401 | id: 0,
|
402 | success: false
|
403 | };
|
404 | }
|
405 |
|
406 | if (typeof(usercallback) === 'function') {
|
407 | usercallback(lres);
|
408 | }
|
409 | else {
|
410 | console.log(lres);
|
411 | }
|
412 | };
|
413 | })(callback);
|
414 |
|
415 | request.onerror = (function(usercallback) {
|
416 | return function(e) {
|
417 | if (typeof(usercallback) === 'function') {
|
418 | usercallback({ id: 0, success: false });
|
419 | }
|
420 | else {
|
421 | throw e;
|
422 | }
|
423 | };
|
424 | })(callback);
|
425 | };
|
426 |
|
427 | LokiCatalog.prototype.getAppKeyById = function (id, callback, data) {
|
428 | var transaction = this.db.transaction(['LokiAKV'], 'readonly');
|
429 | var store = transaction.objectStore('LokiAKV');
|
430 | var request = store.get(id);
|
431 |
|
432 | request.onsuccess = (function(data, usercallback){
|
433 | return function(e) {
|
434 | if (typeof(usercallback) === 'function') {
|
435 | usercallback(e.target.result, data);
|
436 | }
|
437 | else {
|
438 | console.log(e.target.result);
|
439 | }
|
440 | };
|
441 | })(data, callback);
|
442 | };
|
443 |
|
444 | LokiCatalog.prototype.setAppKey = function (app, key, val, callback) {
|
445 | var transaction = this.db.transaction(['LokiAKV'], 'readwrite');
|
446 | var store = transaction.objectStore('LokiAKV');
|
447 | var index = store.index('appkey');
|
448 | var appkey = app + "," + key;
|
449 | var request = index.get(appkey);
|
450 |
|
451 |
|
452 |
|
453 | request.onsuccess = function(e) {
|
454 | var res = e.target.result;
|
455 |
|
456 | if (res === null || res === undefined) {
|
457 | res = {
|
458 | app:app,
|
459 | key:key,
|
460 | appkey: app + ',' + key,
|
461 | val:val
|
462 | };
|
463 | }
|
464 | else {
|
465 | res.val = val;
|
466 | }
|
467 |
|
468 | var requestPut = store.put(res);
|
469 |
|
470 | requestPut.onerror = (function(usercallback) {
|
471 | return function(e) {
|
472 | if (typeof(usercallback) === 'function') {
|
473 | usercallback({ success: false });
|
474 | }
|
475 | else {
|
476 | console.error('LokiCatalog.setAppKey (set) onerror');
|
477 | console.error(request.error);
|
478 | }
|
479 | };
|
480 |
|
481 | })(callback);
|
482 |
|
483 | requestPut.onsuccess = (function(usercallback) {
|
484 | return function(e) {
|
485 | if (typeof(usercallback) === 'function') {
|
486 | usercallback({ success: true });
|
487 | }
|
488 | };
|
489 | })(callback);
|
490 | };
|
491 |
|
492 | request.onerror = (function(usercallback) {
|
493 | return function(e) {
|
494 | if (typeof(usercallback) === 'function') {
|
495 | usercallback({ success: false });
|
496 | }
|
497 | else {
|
498 | console.error('LokiCatalog.setAppKey (get) onerror');
|
499 | console.error(request.error);
|
500 | }
|
501 | };
|
502 | })(callback);
|
503 | };
|
504 |
|
505 | LokiCatalog.prototype.deleteAppKey = function (id, callback) {
|
506 | var transaction = this.db.transaction(['LokiAKV'], 'readwrite');
|
507 | var store = transaction.objectStore('LokiAKV');
|
508 | var request = store.delete(id);
|
509 |
|
510 | request.onsuccess = (function(usercallback) {
|
511 | return function(evt) {
|
512 | if (typeof(usercallback) === 'function') usercallback({ success: true });
|
513 | };
|
514 | })(callback);
|
515 |
|
516 | request.onerror = (function(usercallback) {
|
517 | return function(evt) {
|
518 | if (typeof(usercallback) === 'function') {
|
519 | usercallback(false);
|
520 | }
|
521 | else {
|
522 | console.error('LokiCatalog.deleteAppKey raised onerror');
|
523 | console.error(request.error);
|
524 | }
|
525 | };
|
526 | })(callback);
|
527 | };
|
528 |
|
529 | LokiCatalog.prototype.getAppKeys = function(app, callback) {
|
530 | var transaction = this.db.transaction(['LokiAKV'], 'readonly');
|
531 | var store = transaction.objectStore('LokiAKV');
|
532 | var index = store.index('app');
|
533 |
|
534 |
|
535 | var singleKeyRange = IDBKeyRange.only(app);
|
536 |
|
537 |
|
538 | var cursor = index.openCursor(singleKeyRange);
|
539 |
|
540 |
|
541 |
|
542 | var localdata = [];
|
543 |
|
544 | cursor.onsuccess = (function(data, callback) {
|
545 | return function(e) {
|
546 | var cursor = e.target.result;
|
547 | if (cursor) {
|
548 | var currObject = cursor.value;
|
549 |
|
550 | data.push(currObject);
|
551 |
|
552 | cursor.continue();
|
553 | }
|
554 | else {
|
555 | if (typeof(callback) === 'function') {
|
556 | callback(data);
|
557 | }
|
558 | else {
|
559 | console.log(data);
|
560 | }
|
561 | }
|
562 | };
|
563 | })(localdata, callback);
|
564 |
|
565 | cursor.onerror = (function(usercallback) {
|
566 | return function(e) {
|
567 | if (typeof(usercallback) === 'function') {
|
568 | usercallback(null);
|
569 | }
|
570 | else {
|
571 | console.error('LokiCatalog.getAppKeys raised onerror');
|
572 | console.error(e);
|
573 | }
|
574 | };
|
575 | })(callback);
|
576 |
|
577 | };
|
578 |
|
579 |
|
580 | LokiCatalog.prototype.getAllKeys = function (callback) {
|
581 | var transaction = this.db.transaction(['LokiAKV'], 'readonly');
|
582 | var store = transaction.objectStore('LokiAKV');
|
583 | var cursor = store.openCursor();
|
584 |
|
585 | var localdata = [];
|
586 |
|
587 | cursor.onsuccess = (function(data, callback) {
|
588 | return function(e) {
|
589 | var cursor = e.target.result;
|
590 | if (cursor) {
|
591 | var currObject = cursor.value;
|
592 |
|
593 | data.push(currObject);
|
594 |
|
595 | cursor.continue();
|
596 | }
|
597 | else {
|
598 | if (typeof(callback) === 'function') {
|
599 | callback(data);
|
600 | }
|
601 | else {
|
602 | console.log(data);
|
603 | }
|
604 | }
|
605 | };
|
606 | })(localdata, callback);
|
607 |
|
608 | cursor.onerror = (function(usercallback) {
|
609 | return function(e) {
|
610 | if (typeof(usercallback) === 'function') usercallback(null);
|
611 | };
|
612 | })(callback);
|
613 |
|
614 | };
|
615 |
|
616 | return LokiIndexedAdapter;
|
617 |
|
618 | }());
|
619 | }));
|