mirror of
https://github.com/moparisthebest/mail
synced 2024-11-10 19:25:03 -05:00
Refactor lawnchair dao to promises
This commit is contained in:
parent
01e0529854
commit
502c6b7467
@ -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,70 +33,69 @@ LawnchairDAO.prototype.init = function(dbName, callback) {
|
||||
/**
|
||||
* Create or update an object
|
||||
*/
|
||||
LawnchairDAO.prototype.persist = function(key, object, callback) {
|
||||
LawnchairDAO.prototype.persist = function(key, object) {
|
||||
var self = this;
|
||||
return self._q(function(resolve, reject) {
|
||||
if (!key || !object) {
|
||||
callback({
|
||||
errMsg: 'Key and Object must be set!'
|
||||
});
|
||||
reject(new Error('Key and Object must be set!'));
|
||||
return;
|
||||
}
|
||||
|
||||
this._db.save({
|
||||
self._db.save({
|
||||
key: key,
|
||||
object: object
|
||||
}, function(persisted) {
|
||||
if (persisted.key !== key) {
|
||||
callback({
|
||||
errMsg: 'Persisting failed!'
|
||||
});
|
||||
reject(new Error('Persisting failed!'));
|
||||
return;
|
||||
}
|
||||
|
||||
callback();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Persist a bunch of items at once
|
||||
*/
|
||||
LawnchairDAO.prototype.batch = function(list, callback) {
|
||||
LawnchairDAO.prototype.batch = function(list) {
|
||||
var self = this;
|
||||
return self._q(function(resolve, reject) {
|
||||
if (!(list instanceof Array)) {
|
||||
callback({
|
||||
errMsg: 'Input must be of type Array!'
|
||||
});
|
||||
reject(new Error('Input must be of type Array!'));
|
||||
return;
|
||||
}
|
||||
|
||||
this._db.batch(list, function(res) {
|
||||
self._db.batch(list, function(res) {
|
||||
if (!res) {
|
||||
callback({
|
||||
errMsg: 'Persisting batch failed!'
|
||||
});
|
||||
reject(new Error('Persisting batch failed!'));
|
||||
return;
|
||||
}
|
||||
|
||||
callback();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Read a single item by its key
|
||||
*/
|
||||
LawnchairDAO.prototype.read = function(key, callback) {
|
||||
LawnchairDAO.prototype.read = function(key) {
|
||||
var self = this;
|
||||
return self._q(function(resolve, reject) {
|
||||
if (!key) {
|
||||
callback({
|
||||
errMsg: 'Key must be specified!'
|
||||
});
|
||||
reject(new Error('Key must be specified!'));
|
||||
return;
|
||||
}
|
||||
|
||||
this._db.get(key, function(o) {
|
||||
self._db.get(key, function(o) {
|
||||
if (o) {
|
||||
callback(null, o.object);
|
||||
resolve(o.object);
|
||||
} else {
|
||||
callback();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -103,24 +104,22 @@ 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,
|
||||
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!'
|
||||
});
|
||||
reject(new Error('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) {
|
||||
@ -144,7 +143,7 @@ LawnchairDAO.prototype.list = function(type, offset, num, callback) {
|
||||
|
||||
// return if there are no matching keys
|
||||
if (intervalKeys.length === 0) {
|
||||
callback(null, list);
|
||||
resolve(list);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -155,32 +154,40 @@ LawnchairDAO.prototype.list = function(type, offset, num, callback) {
|
||||
});
|
||||
|
||||
// return only the interval between offset and num
|
||||
callback(null, list);
|
||||
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 = [],
|
||||
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!'
|
||||
});
|
||||
reject(new Error('Type is not set!'));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -194,21 +201,31 @@ LawnchairDAO.prototype.removeList = function(type, callback) {
|
||||
});
|
||||
|
||||
if (matchingKeys.length < 1) {
|
||||
callback();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// remove all matching keys
|
||||
after = _.after(matchingKeys.length, callback);
|
||||
after = _.after(matchingKeys.length, resolve);
|
||||
_.each(matchingKeys, function(key) {
|
||||
self._db.remove(key, after);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user