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
*/
function LawnchairDAO() {}
function LawnchairDAO($q) {
this._q = $q;
}
/**
* Initialize the lawnchair database
@ -31,69 +33,68 @@ LawnchairDAO.prototype.init = function(dbName, callback) {
/**
* Create or update an object
*/
LawnchairDAO.prototype.persist = function(key, object, callback) {
if (!key || !object) {
callback({
errMsg: 'Key and Object must be set!'
});
return;
}
this._db.save({
key: key,
object: object
}, function(persisted) {
if (persisted.key !== key) {
callback({
errMsg: 'Persisting failed!'
});
LawnchairDAO.prototype.persist = function(key, object) {
var self = this;
return self._q(function(resolve, reject) {
if (!key || !object) {
reject(new Error('Key and Object must be set!'));
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
*/
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!'
});
LawnchairDAO.prototype.batch = function(list) {
var self = this;
return self._q(function(resolve, reject) {
if (!(list instanceof Array)) {
reject(new Error('Input must be of type Array!'));
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
*/
LawnchairDAO.prototype.read = function(key, callback) {
if (!key) {
callback({
errMsg: 'Key must be specified!'
});
return;
}
this._db.get(key, function(o) {
if (o) {
callback(null, o.object);
} else {
callback();
LawnchairDAO.prototype.read = function(key) {
var self = this;
return self._q(function(resolve, reject) {
if (!key) {
reject(new Error('Key must be specified!'));
return;
}
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 num [Number] The number of items to fetch (null means fetch all)
*/
LawnchairDAO.prototype.list = function(type, offset, num, callback) {
var self = this,
i, from, to,
matchingKeys = [],
intervalKeys = [],
list = [];
LawnchairDAO.prototype.list = function(type, offset, num) {
var self = this;
return self._q(function(resolve, reject) {
var i, from, to,
matchingKeys = [],
intervalKeys = [],
list = [];
// validate input
if (!type || typeof offset === 'undefined' || typeof num === 'undefined') {
callback({
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);
// validate input
if (!type || typeof offset === 'undefined' || typeof num === 'undefined') {
reject(new Error('Args not is not set!'));
return;
}
// fetch all items from data-store with matching key
self._db.get(intervalKeys, function(intervalList) {
intervalList.forEach(function(item) {
list.push(item.object);
// 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);
}
});
// return only the interval between offset and num
callback(null, list);
});
// 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) {
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)
*/
LawnchairDAO.prototype.remove = function(key, callback) {
this._db.remove(key, callback);
LawnchairDAO.prototype.remove = function(key) {
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)
*/
LawnchairDAO.prototype.removeList = function(type, callback) {
var self = this,
matchingKeys = [],
after;
LawnchairDAO.prototype.removeList = function(type) {
var self = this;
return self._q(function(resolve, reject) {
var matchingKeys = [],
after;
// validate type
if (!type) {
callback({
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();
// validate type
if (!type) {
reject(new Error('Type is not set!'));
return;
}
// remove all matching keys
after = _.after(matchingKeys.length, callback);
_.each(matchingKeys, function(key) {
self._db.remove(key, after);
// 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);
});
});
});
};
@ -209,6 +217,15 @@ LawnchairDAO.prototype.removeList = function(type, callback) {
/**
* Clears the whole local storage cache
*/
LawnchairDAO.prototype.clear = function(callback) {
this._db.nuke(callback);
};
LawnchairDAO.prototype.clear = function() {
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) {
lawnchairDao.clear(function(err) {
expect(err).to.not.exist;
done();
});
lawnchairDao.clear().then(done);
});
describe('read', function() {
it('should fail', function(done) {
lawnchairDao.read(undefined, function(err) {
lawnchairDao.read(undefined).catch(function(err) {
expect(err).to.exist;
done();
});
@ -47,7 +44,7 @@ describe('Lawnchair DAO unit tests', function() {
describe('list', function() {
it('should fail', function(done) {
lawnchairDao.list(undefined, 0, null, function(err) {
lawnchairDao.list(undefined, 0, null).catch(function(err) {
expect(err).to.exist;
done();
});
@ -56,7 +53,7 @@ describe('Lawnchair DAO unit tests', function() {
describe('remove list', function() {
it('should fail', function(done) {
lawnchairDao.removeList(undefined, function(err) {
lawnchairDao.removeList(undefined).catch(function(err) {
expect(err).to.exist;
done();
});
@ -65,46 +62,36 @@ describe('Lawnchair DAO unit tests', function() {
describe('persist/read/remove', function() {
it('should fail', function(done) {
lawnchairDao.persist(undefined, data, function(err) {
lawnchairDao.persist(undefined, data).catch(function(err) {
expect(err).to.exist;
done();
});
});
it('should fail', function(done) {
lawnchairDao.persist('1234', undefined, function(err) {
lawnchairDao.persist('1234', undefined).catch(function(err) {
expect(err).to.exist;
done();
});
});
it('should work', function(done) {
lawnchairDao.persist(key, data, function(err) {
expect(err).to.not.exist;
lawnchairDao.read(key, onRead);
});
function onRead(err, fetched) {
expect(err).to.not.exist;
lawnchairDao.persist(key, data).then(function() {
return lawnchairDao.read(key);
}).then(function(fetched) {
expect(fetched).to.deep.equal(data);
lawnchairDao.remove(key, onRemove);
}
function onRemove(err) {
expect(err).to.not.exist;
lawnchairDao.read(key, onReadAgain);
}
function onReadAgain(err, fetched) {
expect(err).to.not.exist;
return lawnchairDao.remove(key);
}).then(function() {
return lawnchairDao.read(key);
}).then(function(fetched) {
expect(fetched).to.not.exist;
done();
}
});
});
});
describe('batch/list/removeList', function() {
it('should fails', function(done) {
lawnchairDao.batch({}, function(err) {
lawnchairDao.batch({}).catch(function(err) {
expect(err).to.exist;
done();
});
@ -119,29 +106,19 @@ describe('Lawnchair DAO unit tests', function() {
object: data2
}];
lawnchairDao.batch(list, function(err) {
expect(err).to.not.exist;
lawnchairDao.list('type', 0, null, onList);
});
function onList(err, fetched) {
expect(err).to.not.exist;
lawnchairDao.batch(list).then(function() {
return lawnchairDao.list('type', 0, null);
}).then(function(fetched) {
expect(fetched.length).to.equal(2);
expect(fetched[0]).to.deep.equal(list[0].object);
lawnchairDao.removeList('type', onRemoveList);
}
function onRemoveList(err) {
expect(err).to.not.exist;
lawnchairDao.list('type', 0, null, onListAgain);
}
function onListAgain(err, fetched) {
expect(err).to.not.exist;
return lawnchairDao.removeList('type');
}).then(function() {
return lawnchairDao.list('type', 0, null);
}).then(function(fetched) {
expect(fetched).to.exist;
expect(fetched.length).to.equal(0);
done();
}
});
});
});