2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* High level storage api that handles all persistence on the device. If
|
|
|
|
* SQLcipher/SQLite is available, all data is securely persisted there,
|
|
|
|
* through transparent encryption. If not, the crypto API is
|
|
|
|
* used to encrypt data on the fly before persisting via a JSON store.
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
define(function() {
|
2013-08-16 15:21:24 -04:00
|
|
|
'use strict';
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
var DeviceStorageDAO = function(localDbDao) {
|
|
|
|
this._localDbDao = localDbDao;
|
2013-09-26 07:26:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
DeviceStorageDAO.prototype.init = function(emailAddress, callback) {
|
2013-10-29 07:19:27 -04:00
|
|
|
this._localDbDao.init(emailAddress, callback);
|
2013-09-26 07:26:57 -04:00
|
|
|
};
|
2013-06-10 17:22:57 -04:00
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
/**
|
|
|
|
* Stores a list of encrypted items in the object store
|
|
|
|
* @param list [Array] The list of items to be persisted
|
|
|
|
* @param type [String] The type of item to be persisted e.g. 'email'
|
|
|
|
*/
|
2013-09-26 09:48:32 -04:00
|
|
|
DeviceStorageDAO.prototype.storeList = function(list, type, callback) {
|
2013-09-26 10:30:34 -04:00
|
|
|
var key, items = [];
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
// nothing to store
|
2013-09-26 09:48:32 -04:00
|
|
|
if (!list || list.length === 0) {
|
2013-08-16 15:21:24 -04:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2013-09-26 09:48:32 -04:00
|
|
|
// validate type
|
|
|
|
if (!type) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Type is not set!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
// format items for batch storing in dao
|
|
|
|
list.forEach(function(i) {
|
2013-09-28 13:04:15 -04:00
|
|
|
key = createKey(i, type);
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
items.push({
|
|
|
|
key: key,
|
|
|
|
object: i
|
|
|
|
});
|
|
|
|
});
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
this._localDbDao.batch(items, callback);
|
2013-08-16 15:21:24 -04:00
|
|
|
};
|
2013-04-02 09:02:57 -04:00
|
|
|
|
2013-09-28 13:04:15 -04:00
|
|
|
/**
|
|
|
|
* Deletes items of a certain type from storage
|
|
|
|
*/
|
|
|
|
DeviceStorageDAO.prototype.removeList = function(type, callback) {
|
2013-10-29 07:19:27 -04:00
|
|
|
this._localDbDao.removeList(type, callback);
|
2013-09-28 13:04:15 -04:00
|
|
|
};
|
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
/**
|
|
|
|
* List stored items of a given type
|
|
|
|
* @param type [String] The type of item e.g. 'email'
|
|
|
|
* @param offset [Number] The offset of items to fetch (0 is the last stored item)
|
|
|
|
* @param num [Number] The number of items to fetch (null means fetch all)
|
|
|
|
*/
|
2013-09-26 09:48:32 -04:00
|
|
|
DeviceStorageDAO.prototype.listItems = function(type, offset, num, callback) {
|
2013-09-28 13:04:15 -04:00
|
|
|
// fetch all items of a certain type from the data-store
|
2013-10-29 07:19:27 -04:00
|
|
|
this._localDbDao.list(type, offset, num, callback);
|
2013-08-16 15:21:24 -04:00
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
/**
|
|
|
|
* Clear the whole device data-store
|
|
|
|
*/
|
2013-09-26 07:26:57 -04:00
|
|
|
DeviceStorageDAO.prototype.clear = function(callback) {
|
2013-10-29 07:19:27 -04:00
|
|
|
this._localDbDao.clear(callback);
|
2013-08-16 15:21:24 -04:00
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-09-28 13:04:15 -04:00
|
|
|
//
|
|
|
|
// helper functions
|
|
|
|
//
|
|
|
|
|
|
|
|
function createKey(i, type) {
|
|
|
|
var key;
|
|
|
|
|
|
|
|
// put uid in key if available... for easy querying
|
|
|
|
if (i.uid) {
|
|
|
|
key = type + '_' + i.uid;
|
|
|
|
} else if (i.id) {
|
|
|
|
key = type + '_' + i.id;
|
|
|
|
} else {
|
|
|
|
key = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2013-09-26 07:26:57 -04:00
|
|
|
return DeviceStorageDAO;
|
2013-06-10 17:22:57 -04:00
|
|
|
});
|