2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* Handles generic caching of JSON objects in a lawnchair adapter
|
|
|
|
*/
|
|
|
|
app.dao.LawnchairDAO = function(window) {
|
2013-04-01 18:12:15 -04:00
|
|
|
'use strict';
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
var db = new Lawnchair({
|
|
|
|
name: 'data-store'
|
|
|
|
}, function() {});
|
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
/**
|
2013-04-02 09:02:57 -04:00
|
|
|
* Create or update an object
|
2013-03-13 11:58:46 -04:00
|
|
|
*/
|
2013-04-02 09:02:57 -04:00
|
|
|
this.persist = function(key, object, callback) {
|
|
|
|
db.save({
|
|
|
|
key: key,
|
|
|
|
object: object
|
|
|
|
}, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist a bunch of items at once
|
|
|
|
*/
|
|
|
|
this.batch = function(list, callback) {
|
|
|
|
db.batch(list, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a single item by its key
|
|
|
|
*/
|
|
|
|
this.read = function(key, callback) {
|
|
|
|
db.get(key, function(o) {
|
|
|
|
if (o) {
|
|
|
|
callback(o.object);
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -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)
|
|
|
|
*/
|
|
|
|
this.list = function(type, offset, num, callback) {
|
|
|
|
var i, list = [],
|
|
|
|
matchingKeys = [],
|
|
|
|
parts, timeStr, time;
|
|
|
|
|
|
|
|
// get all keys
|
|
|
|
db.keys(function(keys) {
|
|
|
|
|
|
|
|
// check if key begins with type
|
|
|
|
for (i = 0; i < keys.length; i++) {
|
|
|
|
if (keys[i].indexOf(type) === 0) {
|
|
|
|
matchingKeys.push(keys[i]);
|
2013-04-01 18:12:15 -04:00
|
|
|
}
|
2013-04-02 09:02:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort keys by type and date
|
|
|
|
matchingKeys = _.sortBy(matchingKeys, function(key) {
|
|
|
|
parts = key.split('_');
|
|
|
|
timeStr = parts[parts.length - 2];
|
|
|
|
time = parseInt(timeStr, 10);
|
|
|
|
return time;
|
|
|
|
});
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
// if num is null, list all items
|
|
|
|
num = (num !== null) ? num : matchingKeys.length;
|
|
|
|
|
|
|
|
// set window of items to fetch
|
|
|
|
if (offset + num < matchingKeys.length) {
|
|
|
|
matchingKeys = matchingKeys.splice(matchingKeys.length - offset - num, num);
|
|
|
|
} else if (offset + num >= matchingKeys.length && offset < matchingKeys.length) {
|
|
|
|
matchingKeys = matchingKeys.splice(0, matchingKeys.length - offset);
|
|
|
|
} else {
|
|
|
|
matchingKeys = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// return if there are no matching keys
|
|
|
|
if (matchingKeys.length === 0) {
|
|
|
|
callback(list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch all items from data-store with matching key
|
|
|
|
db.get(matchingKeys, function(matchingList) {
|
|
|
|
for (i = 0; i < matchingList.length; i++) {
|
|
|
|
list.push(matchingList[i].object);
|
2013-03-19 11:47:55 -04:00
|
|
|
}
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
// return only the interval between offset and num
|
|
|
|
callback(list);
|
|
|
|
});
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
});
|
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* Removes an object liter from local storage by its key (delete)
|
|
|
|
*/
|
|
|
|
this.remove = function(key, callback) {
|
|
|
|
db.remove(key, callback);
|
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* Clears the whole local storage cache
|
|
|
|
*/
|
|
|
|
this.clear = function(callback) {
|
|
|
|
db.nuke(callback);
|
2013-03-13 11:58:46 -04:00
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
};
|