mail/src/js/service/devicestorage.js

120 lines
2.9 KiB
JavaScript
Raw Normal View History

'use strict';
var ngModule = angular.module('woServices');
// 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);
});
// 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);
});
module.exports = DeviceStorage;
//
// Implementation
//
2013-04-02 09:02:57 -04: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) {
this._lawnchairDAO = lawnchairDAO;
}
2014-10-02 16:05:44 -04:00
/**
* Initialize the lawnchair database
* @param {String} dbName The name of the database
2014-12-10 11:39:40 -05:00
* @return {Promise}
*/
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
};
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
2015-03-31 09:43:42 -04:00
* @param {String/Array} query The type of item e.g. 'email'
* @param {Boolean} exactMatchOnly Specifies if only exact matches are extracted from the DB as opposed to keys that start with the query
2014-12-10 11:39:40 -05:00
* @return {Promise}
2014-10-02 16:05:44 -04:00
*/
2015-03-31 09:43:42 -04:00
DeviceStorage.prototype.listItems = function(query, exactMatchOnly) {
// fetch all items of a certain query from the data-store
return this._lawnchairDAO.list(query, exactMatchOnly);
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;
}
2014-10-02 16:05:44 -04:00
return key;
}