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'
|
|
|
|
* @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 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.list = function(type, offset, num) {
|
|
|
|
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 i, from, to,
|
|
|
|
matchingKeys = [],
|
|
|
|
intervalKeys = [],
|
|
|
|
list = [];
|
|
|
|
|
|
|
|
// validate input
|
|
|
|
if (!type || typeof offset === 'undefined' || typeof num === 'undefined') {
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2013-08-16 15:21:24 -04:00
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
// sort keys
|
|
|
|
matchingKeys.sort();
|
|
|
|
|
|
|
|
// set window of items to fetch
|
|
|
|
// if num is null, list all items
|
|
|
|
from = (num) ? matchingKeys.length - offset - num : 0;
|
|
|
|
to = matchingKeys.length - 1 - offset;
|
|
|
|
// filter items within requested interval
|
|
|
|
for (i = 0; i < matchingKeys.length; i++) {
|
|
|
|
if (i >= from && i <= to) {
|
|
|
|
intervalKeys.push(matchingKeys[i]);
|
|
|
|
}
|
2013-08-16 15:21:24 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
// return if there are no matching keys
|
|
|
|
if (intervalKeys.length === 0) {
|
|
|
|
resolve(list);
|
|
|
|
return;
|
2013-08-16 15:21:24 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
// fetch all items from data-store with matching key
|
|
|
|
self._db.get(intervalKeys, function(intervalList) {
|
|
|
|
intervalList.forEach(function(item) {
|
|
|
|
list.push(item.object);
|
|
|
|
});
|
2013-08-16 15:21:24 -04:00
|
|
|
|
2014-11-29 10:50:24 -05:00
|
|
|
// return only the interval between offset and num
|
|
|
|
resolve(list);
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|