2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* Handles generic caching of JSON objects in a lawnchair adapter
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
define(function(require) {
|
2013-08-16 15:21:24 -04:00
|
|
|
'use strict';
|
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
Lawnchair = require('lawnchair');
|
|
|
|
require('lawnchairSQL');
|
|
|
|
require('lawnchairIDB');
|
2013-08-16 15:21:24 -04:00
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
var LawnchairDAO = function() {};
|
2013-09-26 07:26:57 -04:00
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.init = function(dbName, callback) {
|
2013-08-16 15:21:24 -04:00
|
|
|
if (!dbName) {
|
2013-10-29 07:19:27 -04:00
|
|
|
callback({
|
|
|
|
errMsg: 'Lawnchair DB name must be specified!'
|
|
|
|
});
|
|
|
|
return;
|
2013-08-16 15:21:24 -04:00
|
|
|
}
|
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
this._db = new Lawnchair({
|
2013-08-16 15:21:24 -04:00
|
|
|
name: dbName
|
|
|
|
}, function(lc) {
|
|
|
|
if (!lc) {
|
2013-10-29 07:19:27 -04:00
|
|
|
callback({
|
|
|
|
errMsg: 'Lawnchair init failed!'
|
|
|
|
});
|
|
|
|
return;
|
2013-08-16 15:21:24 -04:00
|
|
|
}
|
2013-09-26 07:26:57 -04:00
|
|
|
|
|
|
|
callback();
|
2013-08-16 15:21:24 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create or update an object
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.persist = function(key, object, callback) {
|
|
|
|
if (!key || !object) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Key and Object must be set!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.save({
|
2013-08-16 15:21:24 -04:00
|
|
|
key: key,
|
|
|
|
object: object
|
2013-10-29 07:19:27 -04:00
|
|
|
}, function(persisted) {
|
|
|
|
if (persisted.key !== key) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Persisting failed!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
2013-08-16 15:21:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist a bunch of items at once
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.batch = function(list, callback) {
|
|
|
|
if (!(list instanceof Array)) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Input must be of type Array!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.batch(list, function(res) {
|
|
|
|
if (!res) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Persisting batch failed!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
2013-08-16 15:21:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a single item by its key
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.read = function(key, callback) {
|
|
|
|
if (!key) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Key must be specified!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._db.get(key, function(o) {
|
2013-08-16 15:21:24 -04:00
|
|
|
if (o) {
|
2013-10-29 07:19:27 -04:00
|
|
|
callback(null, o.object);
|
2013-08-16 15:21:24 -04:00
|
|
|
} else {
|
2013-08-23 10:47:36 -04:00
|
|
|
callback();
|
2013-08-16 15:21:24 -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)
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.list = function(type, offset, num, callback) {
|
|
|
|
var self = this,
|
|
|
|
i, from, to,
|
2013-08-16 15:21:24 -04:00
|
|
|
matchingKeys = [],
|
|
|
|
intervalKeys = [],
|
|
|
|
list = [];
|
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
// validate input
|
|
|
|
if (!type || typeof offset === 'undefined' || typeof num === 'undefined') {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Args not is not set!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
// get all keys
|
2013-10-29 07:19:27 -04:00
|
|
|
self._db.keys(function(keys) {
|
2013-08-16 15:21:24 -04:00
|
|
|
|
|
|
|
// check if key begins with type
|
|
|
|
keys.forEach(function(key) {
|
|
|
|
if (key.indexOf(type) === 0) {
|
|
|
|
matchingKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return if there are no matching keys
|
|
|
|
if (intervalKeys.length === 0) {
|
2013-10-29 07:19:27 -04:00
|
|
|
callback(null, list);
|
2013-08-16 15:21:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch all items from data-store with matching key
|
2013-10-29 07:19:27 -04:00
|
|
|
self._db.get(intervalKeys, function(intervalList) {
|
2013-08-16 15:21:24 -04:00
|
|
|
intervalList.forEach(function(item) {
|
|
|
|
list.push(item.object);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return only the interval between offset and num
|
2013-10-29 07:19:27 -04:00
|
|
|
callback(null, list);
|
2013-08-16 15:21:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an object liter from local storage by its key (delete)
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.remove = function(key, callback) {
|
|
|
|
this._db.remove(key, callback);
|
2013-08-16 15:21:24 -04:00
|
|
|
};
|
|
|
|
|
2013-09-28 13:04:15 -04:00
|
|
|
/**
|
|
|
|
* Removes an object liter from local storage by its key (delete)
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.removeList = function(type, callback) {
|
|
|
|
var self = this,
|
|
|
|
matchingKeys = [],
|
2013-09-28 13:04:15 -04:00
|
|
|
after;
|
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
// validate type
|
|
|
|
if (!type) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Type is not set!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-28 13:04:15 -04:00
|
|
|
// get all keys
|
2013-10-29 07:19:27 -04:00
|
|
|
self._db.keys(function(keys) {
|
2013-09-28 13:04:15 -04:00
|
|
|
// check if key begins with type
|
|
|
|
keys.forEach(function(key) {
|
|
|
|
if (key.indexOf(type) === 0) {
|
|
|
|
matchingKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matchingKeys.length < 1) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all matching keys
|
|
|
|
after = _.after(matchingKeys.length, callback);
|
|
|
|
_.each(matchingKeys, function(key) {
|
2013-10-29 07:19:27 -04:00
|
|
|
self._db.remove(key, after);
|
2013-09-28 13:04:15 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-08-16 15:21:24 -04:00
|
|
|
/**
|
|
|
|
* Clears the whole local storage cache
|
|
|
|
*/
|
2013-10-29 07:19:27 -04:00
|
|
|
LawnchairDAO.prototype.clear = function(callback) {
|
|
|
|
this._db.nuke(callback);
|
2013-08-16 15:21:24 -04:00
|
|
|
};
|
|
|
|
|
2013-10-29 07:19:27 -04:00
|
|
|
return LawnchairDAO;
|
2013-06-10 16:02:29 -04:00
|
|
|
});
|