1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-29 04:12:18 -05:00

Refactor lawnchair dao to promises

This commit is contained in:
Tankred Hase 2014-11-29 16:50:24 +01:00
parent 01e0529854
commit 502c6b7467
2 changed files with 165 additions and 171 deletions

View File

@ -8,7 +8,9 @@ module.exports = LawnchairDAO;
/** /**
* Handles generic caching of JSON objects in a lawnchair adapter * Handles generic caching of JSON objects in a lawnchair adapter
*/ */
function LawnchairDAO() {} function LawnchairDAO($q) {
this._q = $q;
}
/** /**
* Initialize the lawnchair database * Initialize the lawnchair database
@ -31,69 +33,68 @@ LawnchairDAO.prototype.init = function(dbName, callback) {
/** /**
* Create or update an object * Create or update an object
*/ */
LawnchairDAO.prototype.persist = function(key, object, callback) { LawnchairDAO.prototype.persist = function(key, object) {
if (!key || !object) { var self = this;
callback({ return self._q(function(resolve, reject) {
errMsg: 'Key and Object must be set!' if (!key || !object) {
}); reject(new Error('Key and Object must be set!'));
return;
}
this._db.save({
key: key,
object: object
}, function(persisted) {
if (persisted.key !== key) {
callback({
errMsg: 'Persisting failed!'
});
return; return;
} }
callback(); self._db.save({
key: key,
object: object
}, function(persisted) {
if (persisted.key !== key) {
reject(new Error('Persisting failed!'));
return;
}
resolve();
});
}); });
}; };
/** /**
* Persist a bunch of items at once * Persist a bunch of items at once
*/ */
LawnchairDAO.prototype.batch = function(list, callback) { LawnchairDAO.prototype.batch = function(list) {
if (!(list instanceof Array)) { var self = this;
callback({ return self._q(function(resolve, reject) {
errMsg: 'Input must be of type Array!' if (!(list instanceof Array)) {
}); reject(new Error('Input must be of type Array!'));
return;
}
this._db.batch(list, function(res) {
if (!res) {
callback({
errMsg: 'Persisting batch failed!'
});
return; return;
} }
callback(); self._db.batch(list, function(res) {
if (!res) {
reject(new Error('Persisting batch failed!'));
return;
}
resolve();
});
}); });
}; };
/** /**
* Read a single item by its key * Read a single item by its key
*/ */
LawnchairDAO.prototype.read = function(key, callback) { LawnchairDAO.prototype.read = function(key) {
if (!key) { var self = this;
callback({ return self._q(function(resolve, reject) {
errMsg: 'Key must be specified!' if (!key) {
}); reject(new Error('Key must be specified!'));
return; return;
}
this._db.get(key, function(o) {
if (o) {
callback(null, o.object);
} else {
callback();
} }
self._db.get(key, function(o) {
if (o) {
resolve(o.object);
} else {
resolve();
}
});
}); });
}; };
@ -103,105 +104,112 @@ LawnchairDAO.prototype.read = function(key, callback) {
* @param offset [Number] The offset of items to fetch (0 is the last stored item) * @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) * @param num [Number] The number of items to fetch (null means fetch all)
*/ */
LawnchairDAO.prototype.list = function(type, offset, num, callback) { LawnchairDAO.prototype.list = function(type, offset, num) {
var self = this, var self = this;
i, from, to, return self._q(function(resolve, reject) {
matchingKeys = [], var i, from, to,
intervalKeys = [], matchingKeys = [],
list = []; intervalKeys = [],
list = [];
// validate input // validate input
if (!type || typeof offset === 'undefined' || typeof num === 'undefined') { if (!type || typeof offset === 'undefined' || typeof num === 'undefined') {
callback({ reject(new Error('Args not is not set!'));
errMsg: 'Args not is not set!'
});
return;
}
// 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);
}
});
// 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) {
callback(null, list);
return; return;
} }
// fetch all items from data-store with matching key // get all keys
self._db.get(intervalKeys, function(intervalList) { self._db.keys(function(keys) {
intervalList.forEach(function(item) { // check if key begins with type
list.push(item.object); keys.forEach(function(key) {
if (key.indexOf(type) === 0) {
matchingKeys.push(key);
}
}); });
// return only the interval between offset and num // sort keys
callback(null, list); 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) {
resolve(list);
return;
}
// fetch all items from data-store with matching key
self._db.get(intervalKeys, function(intervalList) {
intervalList.forEach(function(item) {
list.push(item.object);
});
// return only the interval between offset and num
resolve(list);
});
});
}); });
}; };
/** /**
* Removes an object liter from local storage by its key (delete) * Removes an object liter from local storage by its key (delete)
*/ */
LawnchairDAO.prototype.remove = function(key, callback) { LawnchairDAO.prototype.remove = function(key) {
this._db.remove(key, callback); var self = this;
return self._q(function(resolve, reject) {
self._db.remove(key, function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}; };
/** /**
* Removes an object liter from local storage by its key (delete) * Removes an object liter from local storage by its key (delete)
*/ */
LawnchairDAO.prototype.removeList = function(type, callback) { LawnchairDAO.prototype.removeList = function(type) {
var self = this, var self = this;
matchingKeys = [], return self._q(function(resolve, reject) {
after; var matchingKeys = [],
after;
// validate type // validate type
if (!type) { if (!type) {
callback({ reject(new Error('Type is not set!'));
errMsg: 'Type is not set!'
});
return;
}
// 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) {
callback();
return; return;
} }
// remove all matching keys // get all keys
after = _.after(matchingKeys.length, callback); self._db.keys(function(keys) {
_.each(matchingKeys, function(key) { // check if key begins with type
self._db.remove(key, after); 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);
});
}); });
}); });
}; };
@ -209,6 +217,15 @@ LawnchairDAO.prototype.removeList = function(type, callback) {
/** /**
* Clears the whole local storage cache * Clears the whole local storage cache
*/ */
LawnchairDAO.prototype.clear = function(callback) { LawnchairDAO.prototype.clear = function() {
this._db.nuke(callback); var self = this;
}; return self._q(function(resolve, reject) {
self._db.nuke(function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};

View File

@ -30,15 +30,12 @@ describe('Lawnchair DAO unit tests', function() {
}); });
afterEach(function(done) { afterEach(function(done) {
lawnchairDao.clear(function(err) { lawnchairDao.clear().then(done);
expect(err).to.not.exist;
done();
});
}); });
describe('read', function() { describe('read', function() {
it('should fail', function(done) { it('should fail', function(done) {
lawnchairDao.read(undefined, function(err) { lawnchairDao.read(undefined).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
@ -47,7 +44,7 @@ describe('Lawnchair DAO unit tests', function() {
describe('list', function() { describe('list', function() {
it('should fail', function(done) { it('should fail', function(done) {
lawnchairDao.list(undefined, 0, null, function(err) { lawnchairDao.list(undefined, 0, null).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
@ -56,7 +53,7 @@ describe('Lawnchair DAO unit tests', function() {
describe('remove list', function() { describe('remove list', function() {
it('should fail', function(done) { it('should fail', function(done) {
lawnchairDao.removeList(undefined, function(err) { lawnchairDao.removeList(undefined).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
@ -65,46 +62,36 @@ describe('Lawnchair DAO unit tests', function() {
describe('persist/read/remove', function() { describe('persist/read/remove', function() {
it('should fail', function(done) { it('should fail', function(done) {
lawnchairDao.persist(undefined, data, function(err) { lawnchairDao.persist(undefined, data).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
}); });
it('should fail', function(done) { it('should fail', function(done) {
lawnchairDao.persist('1234', undefined, function(err) { lawnchairDao.persist('1234', undefined).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
}); });
it('should work', function(done) { it('should work', function(done) {
lawnchairDao.persist(key, data, function(err) { lawnchairDao.persist(key, data).then(function() {
expect(err).to.not.exist; return lawnchairDao.read(key);
lawnchairDao.read(key, onRead); }).then(function(fetched) {
});
function onRead(err, fetched) {
expect(err).to.not.exist;
expect(fetched).to.deep.equal(data); expect(fetched).to.deep.equal(data);
lawnchairDao.remove(key, onRemove); return lawnchairDao.remove(key);
} }).then(function() {
return lawnchairDao.read(key);
function onRemove(err) { }).then(function(fetched) {
expect(err).to.not.exist;
lawnchairDao.read(key, onReadAgain);
}
function onReadAgain(err, fetched) {
expect(err).to.not.exist;
expect(fetched).to.not.exist; expect(fetched).to.not.exist;
done(); done();
} });
}); });
}); });
describe('batch/list/removeList', function() { describe('batch/list/removeList', function() {
it('should fails', function(done) { it('should fails', function(done) {
lawnchairDao.batch({}, function(err) { lawnchairDao.batch({}).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
@ -119,29 +106,19 @@ describe('Lawnchair DAO unit tests', function() {
object: data2 object: data2
}]; }];
lawnchairDao.batch(list, function(err) { lawnchairDao.batch(list).then(function() {
expect(err).to.not.exist; return lawnchairDao.list('type', 0, null);
lawnchairDao.list('type', 0, null, onList); }).then(function(fetched) {
});
function onList(err, fetched) {
expect(err).to.not.exist;
expect(fetched.length).to.equal(2); expect(fetched.length).to.equal(2);
expect(fetched[0]).to.deep.equal(list[0].object); expect(fetched[0]).to.deep.equal(list[0].object);
lawnchairDao.removeList('type', onRemoveList); return lawnchairDao.removeList('type');
} }).then(function() {
return lawnchairDao.list('type', 0, null);
function onRemoveList(err) { }).then(function(fetched) {
expect(err).to.not.exist;
lawnchairDao.list('type', 0, null, onListAgain);
}
function onListAgain(err, fetched) {
expect(err).to.not.exist;
expect(fetched).to.exist; expect(fetched).to.exist;
expect(fetched.length).to.equal(0); expect(fetched.length).to.equal(0);
done(); done();
} });
}); });
}); });