2014-11-18 12:44:00 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ngModule = angular.module('woServices');
|
2014-11-21 09:06:29 -05:00
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
// expose an instance with the static dbName 'app-config' to store configuration data
|
2014-12-11 12:12:37 -05:00
|
|
|
ngModule.factory('appConfigStore', function(appConfigLawnchair) {
|
|
|
|
return new DeviceStorage(appConfigLawnchair);
|
2014-11-20 09:14:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// expose a singleton instance of DeviceStorage called 'accountStore' to persist user data
|
2014-12-11 12:12:37 -05:00
|
|
|
ngModule.factory('accountStore', function(accountLawnchair) {
|
|
|
|
return new DeviceStorage(accountLawnchair);
|
2014-11-25 11:46:33 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = DeviceStorage;
|
2014-11-20 09:14:39 -05:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
//
|
|
|
|
// Implementation
|
|
|
|
//
|
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
/**
|
2014-11-19 14:54:59 -05:00
|
|
|
* High level storage api that handles all persistence of a user's data on the device.
|
2013-04-02 09:02:57 -04:00
|
|
|
*/
|
2014-12-11 12:12:37 -05:00
|
|
|
function DeviceStorage(lawnchairDAO) {
|
2014-11-20 09:14:39 -05:00
|
|
|
this._lawnchairDAO = lawnchairDAO;
|
2014-11-18 12:44:00 -05:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
/**
|
|
|
|
* Initialize the lawnchair database
|
|
|
|
* @param {String} dbName The name of the database
|
2014-12-10 11:39:40 -05:00
|
|
|
* @return {Promise}
|
2014-11-21 09:06:29 -05:00
|
|
|
*/
|
2014-12-10 11:39:40 -05:00
|
|
|
DeviceStorage.prototype.init = function(dbName) {
|
|
|
|
return this._lawnchairDAO.init(dbName);
|
2014-10-02 16:05:44 -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'
|
2014-12-10 11:39:40 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-12-10 11:39:40 -05:00
|
|
|
DeviceStorage.prototype.storeList = function(list, type) {
|
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve) {
|
2014-12-10 11:39:40 -05:00
|
|
|
var key, items = [];
|
|
|
|
list = list || [];
|
|
|
|
|
|
|
|
// validate type
|
|
|
|
if (!type) {
|
|
|
|
throw new Error('Type is not set!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// format items for batch storing in dao
|
|
|
|
list.forEach(function(i) {
|
|
|
|
key = createKey(i, type);
|
|
|
|
|
|
|
|
items.push({
|
|
|
|
key: key,
|
|
|
|
object: i
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
|
2014-12-10 11:39:40 -05:00
|
|
|
resolve(items);
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-10 11:39:40 -05:00
|
|
|
}).then(function(items) {
|
|
|
|
// nothing to store
|
|
|
|
if (items.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-10 11:39:40 -05:00
|
|
|
return self._lawnchairDAO.batch(items);
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
2014-12-10 11:39:40 -05:00
|
|
|
* Deletes items of a certain type from storage
|
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-12-10 11:39:40 -05:00
|
|
|
DeviceStorage.prototype.removeList = function(type) {
|
|
|
|
return this._lawnchairDAO.removeList(type);
|
2014-10-02 16:05:44 -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)
|
2014-12-10 11:39:40 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-12-10 11:39:40 -05:00
|
|
|
DeviceStorage.prototype.listItems = function(type, offset, num) {
|
2014-10-02 16:05:44 -04:00
|
|
|
// fetch all items of a certain type from the data-store
|
2014-12-10 11:39:40 -05:00
|
|
|
return this._lawnchairDAO.list(type, offset, num);
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the whole device data-store
|
2014-12-10 11:39:40 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-12-10 11:39:40 -05:00
|
|
|
DeviceStorage.prototype.clear = function() {
|
|
|
|
return this._lawnchairDAO.clear();
|
2014-10-02 16:05:44 -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;
|
2013-09-28 13:04:15 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
return key;
|
2014-11-18 12:44:00 -05:00
|
|
|
}
|