1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-14 21:45:08 -05:00
kaiwa/clientapp/storage/archive.js

72 lines
2.0 KiB
JavaScript
Raw Normal View History

/*global, IDBKeyRange*/
2013-08-29 23:38:28 -04:00
"use strict";
function ArchiveStorage(storage) {
this.storage = storage;
}
ArchiveStorage.prototype = {
constructor: {
value: ArchiveStorage
},
setup: function (db) {
if (db.objectStoreNames.contains('archive')) {
db.deleteObjectStore('archive');
}
var store = db.createObjectStore('archive', {
keyPath: 'archivedId'
2013-08-29 23:38:28 -04:00
});
store.createIndex("owner", "owner", {unique: false});
2013-08-29 23:38:28 -04:00
},
transaction: function (mode) {
var trans = this.storage.db.transaction('archive', mode);
return trans.objectStore('archive');
},
add: function (message, cb) {
cb = cb || function () {};
var request = this.transaction('readwrite').put(message);
request.onsuccess = function () {
cb(false, message);
};
request.onerror = cb;
},
get: function (id, cb) {
cb = cb || function () {};
if (!id) {
return cb('not-found');
}
var request = this.transaction('readonly').get(id);
request.onsuccess = function (e) {
var res = request.result;
if (res === undefined) {
return cb('not-found');
}
request.result.acked = true;
cb(false, request.result);
};
request.onerror = cb;
},
getAll: function (owner, cb) {
cb = cb || function () {};
var results = [];
var store = this.transaction('readonly');
var request = store.index('owner').openCursor(IDBKeyRange.only(owner));
request.onsuccess = function (e) {
var cursor = e.target.result;
if (cursor) {
cursor.value.acked = true;
results.push(cursor.value);
cursor.continue();
} else {
cb(false, results);
}
};
request.onerror = cb;
},
2013-08-29 23:38:28 -04:00
};
module.exports = ArchiveStorage;