1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-12-25 17:08:54 -05:00
kaiwa/clientapp/storage/avatars.js
2013-08-29 20:38:28 -07:00

60 lines
1.5 KiB
JavaScript

"use strict";
// SCHEMA
// id: 'sha1 hash',
// dataURI: '...'
function AvatarStorage(storage) {
this.storage = storage;
}
AvatarStorage.prototype = {
constructor: {
value: AvatarStorage
},
setup: function (db) {
db.createObjectStore('avatars', {
keyPath: 'id'
});
},
transaction: function (mode) {
var trans = this.storage.db.transaction('avatars', mode);
return trans.objectStore('avatars');
},
add: function (avatar, cb) {
cb = cb || function () {};
var request = this.transaction('readwrite').put(avatar);
request.onsuccess = function () {
cb(false, avatar);
};
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');
}
cb(false, request.result);
};
request.onerror = cb;
},
remove: function (id, cb) {
cb = cb || function () {};
var request = this.transaction('readwrite')['delete'](id);
request.onsuccess = function (e) {
cb(false, request.result);
};
request.onerror = cb;
}
};
module.exports = AvatarStorage;