add onlyKey opts to storage #getFirst

This commit is contained in:
Matias Alejo Garcia 2014-10-20 10:00:10 -03:00
parent 8765b02883
commit e109550d9b
4 changed files with 15 additions and 7 deletions

View File

@ -45,7 +45,7 @@ Profile.create = function(email, password, storage, cb) {
Profile.any = function(storage, cb) {
storage.getFirst(Profile.key(''), function(err, v, k) {
storage.getFirst(Profile.key(''), { onlyKey: true}, function(err, v, k) {
return cb(k ? true : false);
});
};

View File

@ -175,13 +175,21 @@ Storage.prototype.get = function(key, cb) {
})
};
Storage.prototype.getFirst = function(prefix, cb) {
Storage.prototype.getFirst = function(prefix, opts, cb) {
opts = opts || {};
var self = this;
this.db.allKeys(function(allKeys) {
var keys = _.filter(allKeys, function(k) {
if ((k === prefix) || k.indexOf(prefix) === 0) return true;
});
if (keys.length === 0) return cb(new Error('not found'));
if (keys.length === 0)
return cb(new Error('not found'));
if (opts.onlyKey)
return cb(null, null, keys[0]);
self._read(keys[0], function(v) {
if (_.isNull(v)) return cb(new Error('Could not decrypt data'), null, keys[0]);
return cb(null, v, keys[0]);
@ -206,7 +214,7 @@ Storage.prototype.delete = function(key, cb) {
Storage.prototype.deletePrefix = function(prefix, cb) {
var self = this;
this.getFirst(prefix, function(err, v, k) {
this.getFirst(prefix, {}, function(err, v, k) {
if (err || !v) return cb(err);
self.delete(k, function(err) {

View File

@ -175,7 +175,7 @@ Wallet.key = function(str) {
Wallet.any = function(storage, cb) {
storage.getFirst(Wallet.key(''), function(err, v, k) {
storage.getFirst(Wallet.key(''), { onlyKey: true}, function(err, v, k) {
return cb(k ? true : false);
});
};
@ -259,7 +259,7 @@ Wallet.read = function(walletId, readOpts, cb) {
err;
var obj = {};
storage.getFirst(Wallet.key(walletId), function(err, ret) {
storage.getFirst(Wallet.key(walletId), {}, function(err, ret) {
if (err) return cb(err);
if (!ret)

View File

@ -347,7 +347,7 @@ describe('Storage model', function() {
sinon.stub(s, '_read', function(k, cb) {
return cb(data[k]);
});
s.getFirst('wallet::id1', function(err, w) {
s.getFirst('wallet::id1', {}, function(err, w) {
should.not.exist(err);
w.should.exist;
w.hasOwnProperty('a').should.be.true;