1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-17 22:55:11 -05:00
mail/src/js/dao/lawnchair-dao.js

119 lines
2.6 KiB
JavaScript
Raw Normal View History

2013-04-02 09:02:57 -04:00
/**
* Handles generic caching of JSON objects in a lawnchair adapter
*/
2013-05-31 07:09:49 -04:00
app.dao.LawnchairDAO = function(Lawnchair) {
'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'
2013-05-23 17:12:38 -04:00
}, function(lc) {
if (!lc) {
throw new Error('Lawnchair init failed!');
}
});
2013-04-02 09:02:57 -04:00
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-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-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-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-04-02 09:02:57 -04:00
// return only the interval between offset and num
callback(list);
});
2013-04-02 09:02:57 -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-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-02 09:02:57 -04:00
};