2014-11-18 12:44:00 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ngModule = angular.module('woServices');
|
2014-11-25 11:46:33 -05:00
|
|
|
ngModule.service('appConfigLawnchair', LawnchairDAO);
|
|
|
|
ngModule.service('accountLawnchair', LawnchairDAO);
|
2014-11-18 12:44:00 -05:00
|
|
|
module.exports = LawnchairDAO;
|
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* Handles generic caching of JSON objects in a lawnchair adapter
|
|
|
|
*/
|
2014-12-11 12:12:37 -05:00
|
|
|
function LawnchairDAO() {}
|
2013-08-16 15:21:24 -04:00
|
|
|
|
2014-11-18 12:44:00 -05:00
|
|
|
/**
|
|
|
|
* Initialize the lawnchair database
|
|
|
|
* @param {String} dbName The name of the database
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-11-18 12:44:00 -05:00
|
|
|
*/
|
2014-12-10 08:16:53 -05:00
|
|
|
LawnchairDAO.prototype.init = function(dbName) {
|
2014-12-01 08:55:03 -05:00
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2014-12-10 08:16:53 -05:00
|
|
|
if (!dbName) {
|
2014-12-10 10:29:09 -05:00
|
|
|
throw new Error('Lawnchair DB name must be specified!');
|
2014-12-10 08:16:53 -05:00
|
|
|
}
|
2014-12-01 08:55:03 -05:00
|
|
|
|
2014-12-10 08:16:53 -05:00
|
|
|
self._db = new Lawnchair({
|
|
|
|
name: dbName
|
|
|
|
}, function(success) {
|
|
|
|
if (success) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(new Error('Lawnchair initialization ' + dbName + ' failed!'));
|
|
|
|
}
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2013-09-26 07:26:57 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Create or update an object
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-11-29 10:50:24 -05:00
|
|
|
LawnchairDAO.prototype.persist = function(key, object) {
|
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2014-11-29 10:50:24 -05:00
|
|
|
if (!key || !object) {
|
2014-12-10 10:29:09 -05:00
|
|
|
throw new Error('Key and Object must be set!');
|
2013-10-29 07:19:27 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
self._db.save({
|
|
|
|
key: key,
|
|
|
|
object: object
|
|
|
|
}, function(persisted) {
|
|
|
|
if (persisted.key !== key) {
|
|
|
|
reject(new Error('Persisting failed!'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Persist a bunch of items at once
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-11-29 10:50:24 -05:00
|
|
|
LawnchairDAO.prototype.batch = function(list) {
|
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2014-11-29 10:50:24 -05:00
|
|
|
if (!(list instanceof Array)) {
|
2014-12-10 10:29:09 -05:00
|
|
|
throw new Error('Input must be of type Array!');
|
2013-10-29 07:19:27 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
self._db.batch(list, function(res) {
|
|
|
|
if (!res) {
|
|
|
|
reject(new Error('Persisting batch failed!'));
|
2014-12-10 10:29:09 -05:00
|
|
|
} else {
|
|
|
|
resolve();
|
2014-11-29 10:50:24 -05:00
|
|
|
}
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Read a single item by its key
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-11-29 10:50:24 -05:00
|
|
|
LawnchairDAO.prototype.read = function(key) {
|
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve) {
|
2014-11-29 10:50:24 -05:00
|
|
|
if (!key) {
|
2014-12-10 10:29:09 -05:00
|
|
|
throw new Error('Key must be specified!');
|
2013-10-29 07:19:27 -04:00
|
|
|
}
|
2014-11-29 10:50:24 -05:00
|
|
|
|
|
|
|
self._db.get(key, function(o) {
|
|
|
|
if (o) {
|
|
|
|
resolve(o.object);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* List all the items of a certain type
|
|
|
|
* @param type [String] The type of item e.g. 'email'
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2015-03-31 09:43:42 -04:00
|
|
|
LawnchairDAO.prototype.list = function(query, exactMatchOnly) {
|
2014-11-29 10:50:24 -05:00
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve) {
|
2015-03-31 09:43:42 -04:00
|
|
|
var matchingKeys = [];
|
2014-11-29 10:50:24 -05:00
|
|
|
|
|
|
|
// validate input
|
2015-03-31 09:43:42 -04:00
|
|
|
if ((Array.isArray(query) && query.length === 0) || (!Array.isArray(query) && !query)) {
|
2014-12-10 10:29:09 -05:00
|
|
|
throw new Error('Args not is not set!');
|
2014-11-29 10:50:24 -05:00
|
|
|
}
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2015-03-31 09:43:42 -04:00
|
|
|
// this method operates on arrays of keys, so normalize input 'key' -> ['key']
|
|
|
|
if (!Array.isArray(query)) {
|
|
|
|
query = [query];
|
|
|
|
}
|
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
// get all keys
|
|
|
|
self._db.keys(function(keys) {
|
2015-03-31 09:43:42 -04:00
|
|
|
// check if there are keys in the db that start with the respective query
|
|
|
|
matchingKeys = keys.filter(function(key) {
|
|
|
|
return query.filter(function(type) {
|
|
|
|
if (exactMatchOnly) {
|
|
|
|
return key === type;
|
|
|
|
} else {
|
|
|
|
return key.indexOf(type) === 0;
|
|
|
|
}
|
|
|
|
}).length > 0;
|
2014-11-29 10:50:24 -05:00
|
|
|
});
|
2013-08-16 15:21:24 -04:00
|
|
|
|
2015-03-31 09:43:42 -04:00
|
|
|
if (matchingKeys.length === 0) {
|
|
|
|
// no matching keys, resolve
|
|
|
|
resolve([]);
|
2014-11-29 10:50:24 -05:00
|
|
|
return;
|
2013-08-16 15:21:24 -04:00
|
|
|
}
|
|
|
|
|
2015-03-31 09:43:42 -04:00
|
|
|
// fetch all items from data-store with matching keys
|
|
|
|
self._db.get(matchingKeys, function(intervalList) {
|
|
|
|
var result = intervalList.map(function(item) {
|
|
|
|
return item.object;
|
2014-11-29 10:50:24 -05:00
|
|
|
});
|
2013-08-16 15:21:24 -04:00
|
|
|
|
2015-03-31 09:43:42 -04:00
|
|
|
resolve(result);
|
2013-08-16 15:21:24 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2013-09-28 13:04:15 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Removes an object liter from local storage by its key (delete)
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-11-29 10:50:24 -05:00
|
|
|
LawnchairDAO.prototype.remove = function(key) {
|
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2014-11-29 10:50:24 -05:00
|
|
|
self._db.remove(key, function(err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an object liter from local storage by its key (delete)
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-11-29 10:50:24 -05:00
|
|
|
LawnchairDAO.prototype.removeList = function(type) {
|
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve) {
|
2014-11-29 10:50:24 -05:00
|
|
|
var matchingKeys = [],
|
|
|
|
after;
|
2013-09-28 13:04:15 -04:00
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
// validate type
|
|
|
|
if (!type) {
|
2014-12-10 10:29:09 -05:00
|
|
|
throw new Error('Type is not set!');
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
// get all keys
|
|
|
|
self._db.keys(function(keys) {
|
|
|
|
// check if key begins with type
|
|
|
|
keys.forEach(function(key) {
|
|
|
|
if (key.indexOf(type) === 0) {
|
|
|
|
matchingKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matchingKeys.length < 1) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all matching keys
|
|
|
|
after = _.after(matchingKeys.length, resolve);
|
|
|
|
_.each(matchingKeys, function(key) {
|
|
|
|
self._db.remove(key, after);
|
|
|
|
});
|
2013-09-28 13:04:15 -04:00
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2013-09-28 13:04:15 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Clears the whole local storage cache
|
2014-12-10 08:16:53 -05:00
|
|
|
* @return {Promise}
|
2014-10-02 16:05:44 -04:00
|
|
|
*/
|
2014-11-29 10:50:24 -05:00
|
|
|
LawnchairDAO.prototype.clear = function() {
|
|
|
|
var self = this;
|
2014-12-11 12:12:37 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2014-11-29 10:50:24 -05:00
|
|
|
self._db.nuke(function(err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|