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:
parent
01e0529854
commit
502c6b7467
@ -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,70 +33,69 @@ 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) {
|
||||||
|
var self = this;
|
||||||
|
return self._q(function(resolve, reject) {
|
||||||
if (!key || !object) {
|
if (!key || !object) {
|
||||||
callback({
|
reject(new Error('Key and Object must be set!'));
|
||||||
errMsg: 'Key and Object must be set!'
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._db.save({
|
self._db.save({
|
||||||
key: key,
|
key: key,
|
||||||
object: object
|
object: object
|
||||||
}, function(persisted) {
|
}, function(persisted) {
|
||||||
if (persisted.key !== key) {
|
if (persisted.key !== key) {
|
||||||
callback({
|
reject(new Error('Persisting failed!'));
|
||||||
errMsg: 'Persisting failed!'
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback();
|
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) {
|
||||||
|
var self = this;
|
||||||
|
return self._q(function(resolve, reject) {
|
||||||
if (!(list instanceof Array)) {
|
if (!(list instanceof Array)) {
|
||||||
callback({
|
reject(new Error('Input must be of type Array!'));
|
||||||
errMsg: 'Input must be of type Array!'
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._db.batch(list, function(res) {
|
self._db.batch(list, function(res) {
|
||||||
if (!res) {
|
if (!res) {
|
||||||
callback({
|
reject(new Error('Persisting batch failed!'));
|
||||||
errMsg: 'Persisting batch failed!'
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback();
|
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) {
|
||||||
|
var self = this;
|
||||||
|
return self._q(function(resolve, reject) {
|
||||||
if (!key) {
|
if (!key) {
|
||||||
callback({
|
reject(new Error('Key must be specified!'));
|
||||||
errMsg: 'Key must be specified!'
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._db.get(key, function(o) {
|
self._db.get(key, function(o) {
|
||||||
if (o) {
|
if (o) {
|
||||||
callback(null, o.object);
|
resolve(o.object);
|
||||||
} else {
|
} 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 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) {
|
||||||
|
var i, from, to,
|
||||||
matchingKeys = [],
|
matchingKeys = [],
|
||||||
intervalKeys = [],
|
intervalKeys = [],
|
||||||
list = [];
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get all keys
|
// get all keys
|
||||||
self._db.keys(function(keys) {
|
self._db.keys(function(keys) {
|
||||||
|
|
||||||
// check if key begins with type
|
// check if key begins with type
|
||||||
keys.forEach(function(key) {
|
keys.forEach(function(key) {
|
||||||
if (key.indexOf(type) === 0) {
|
if (key.indexOf(type) === 0) {
|
||||||
@ -144,7 +143,7 @@ LawnchairDAO.prototype.list = function(type, offset, num, callback) {
|
|||||||
|
|
||||||
// return if there are no matching keys
|
// return if there are no matching keys
|
||||||
if (intervalKeys.length === 0) {
|
if (intervalKeys.length === 0) {
|
||||||
callback(null, list);
|
resolve(list);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,32 +154,40 @@ LawnchairDAO.prototype.list = function(type, offset, num, callback) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// return only the interval between offset and num
|
// return only the interval between offset and num
|
||||||
callback(null, list);
|
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) {
|
||||||
|
var matchingKeys = [],
|
||||||
after;
|
after;
|
||||||
|
|
||||||
// validate type
|
// validate type
|
||||||
if (!type) {
|
if (!type) {
|
||||||
callback({
|
reject(new Error('Type is not set!'));
|
||||||
errMsg: 'Type is not set!'
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,21 +201,31 @@ LawnchairDAO.prototype.removeList = function(type, callback) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (matchingKeys.length < 1) {
|
if (matchingKeys.length < 1) {
|
||||||
callback();
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove all matching keys
|
// remove all matching keys
|
||||||
after = _.after(matchingKeys.length, callback);
|
after = _.after(matchingKeys.length, resolve);
|
||||||
_.each(matchingKeys, function(key) {
|
_.each(matchingKeys, function(key) {
|
||||||
self._db.remove(key, after);
|
self._db.remove(key, after);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
@ -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();
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user