1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-26 19:22:18 -05:00
kaiwa/clientapp/storage/roster.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-09-11 17:58:39 -04:00
/*global, IDBKeyRange*/
2013-08-29 23:38:28 -04:00
"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) {
2013-09-11 17:58:39 -04:00
var store = db.createObjectStore('roster', {
keyPath: 'storageId'
2013-08-29 23:38:28 -04:00
});
2013-09-11 17:58:39 -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('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;
},
2013-09-11 17:58:39 -04:00
getAll: function (owner, cb) {
2013-08-29 23:38:28 -04:00
cb = cb || function () {};
var results = [];
2013-09-11 17:58:39 -04:00
var store = this.transaction('readonly');
var request = store.index('owner').openCursor(IDBKeyRange.only(owner));
2013-08-29 23:38:28 -04:00
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;