Skip to content

Commit

Permalink
update find method
Browse files Browse the repository at this point in the history
  • Loading branch information
CCharlieLi committed Apr 18, 2016
1 parent a5ea5df commit df40899
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
16 changes: 12 additions & 4 deletions lib/ioredis.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,14 @@ IORedis.prototype.all = function all(model, query, options, callback) {
id.inq.forEach(function (qid) {
tasks.push(self.findById(model, qid, options));
});
promise = Promise.all(tasks).then(function (res) {
promise = Promise.all(tasks).filter(function (task) {
if (task !== null) return true;
}).each(function (res) {
return res;
});
} else {
promise = this.findById(model, id, options).then(function (res) {
if (res === null) return [];
return [res];
});
}
Expand All @@ -256,8 +259,13 @@ IORedis.prototype.all = function all(model, query, options, callback) {
IORedis.prototype.findById = function (model, id, options) {
var self = this;
var connection = this.connect();
return connection.call('hgetall', model + ':' + id).then(function (res) {
return self.fromDb(model, res);
return this.exists(model, id).then(function (exists) {
if (!exists) {
return null;
}
return connection.call('hgetall', model + ':' + id).then(function (res) {
return self.fromDb(model, res);
});
});
};

Expand All @@ -281,7 +289,7 @@ IORedis.prototype.destroyAll = function destroyAll(model, where, options, callba
return this.all(model, { where: where }).then(function (res) {
var tasks = [];
res.forEach(function (result) {
if (result !== undefined) {
if (result !== null) {
tasks.push(self.destroy(model, result.id, options));
}
});
Expand Down
13 changes: 4 additions & 9 deletions test/10.crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ describe('Couchbase CRUD', function () {

it('cannot find an unsaved instance', function (done) {
Person.findById('1234').then(function (res) {
res.should.be.Object();
res.should.be.empty;
should.not.exist(res);
done();
}).catch(done);
});
Expand Down Expand Up @@ -334,9 +333,8 @@ describe('Couchbase CRUD', function () {

it('cannot find wrong instances by id', function (done) {
Person.findByIds(['0', 'lorem']).then(function (res) {
res.should.be.Array().with.length(2);
res.should.be.Array().with.length(1);
res[0].should.have.property('name', 'Charlie');
res[1].should.be.empty;
done();
}).catch(done);
});
Expand Down Expand Up @@ -373,9 +371,8 @@ describe('Couchbase CRUD', function () {
}
}
}).then(function (res) {
res.should.be.Array().with.length(2);
res.should.be.Array().with.length(1);
res[0].should.have.property('name', 'Charlie');
res[1].should.be.empty;
done();
}).catch(done);
});
Expand All @@ -399,9 +396,7 @@ describe('Couchbase CRUD', function () {
id: {}
}
}).then(function (res) {
res.should.be.Array().with.length(1);
res[0].should.be.Object();
res[0].should.be.empty;
res.length.should.be.equal(0);
done();
}).catch(done);
});
Expand Down

0 comments on commit df40899

Please sign in to comment.