1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-05 17:15:04 -05:00
kaiwa/clientapp/storage/roster.js
2013-08-29 20:38:28 -07:00

79 lines
2.0 KiB
JavaScript

"use strict";
// SCHEMA
// jid: string
// name: string
// subscription: string
// groups: array
// rosterID: string
function RosterStorage(storage) {
this.storage = storage;
}
RosterStorage.prototype = {
constructor: {
value: RosterStorage
},
setup: function (db) {
db.createObjectStore('roster', {
keyPath: 'jid'
});
},
transaction: function (mode) {
var trans = this.storage.db.transaction('roster', mode);
return trans.objectStore('roster');
},
add: function (contact, cb) {
cb = cb || function () {};
var request = this.transaction('readwrite').put(contact);
request.onsuccess = function () {
cb(false, contact);
};
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;
},
getAll: function (cb) {
cb = cb || function () {};
var results = [];
var request = this.transaction('readonly').openCursor();
request.onsuccess = function (e) {
var cursor = e.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
cb(false, results);
}
};
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 = RosterStorage;