Skip to content

Commit

Permalink
Added count and exists
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpaul committed Jun 26, 2014
1 parent fbc74f4 commit 00c6ba2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##JugglingDB Adapter for DynamoDB version 0.1.9-4
##JugglingDB Adapter for DynamoDB version 0.1.9-5
* Adapter is still in development stage. The stable release will be 0.2.0 and will offer rich functionalities along
with lots of tests.
* Always use the latest version of this adapter, preferably >= 0.1.5. The latest version has more features and lots of bug fixes. Versions
Expand Down
40 changes: 39 additions & 1 deletion lib/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ DynamoDB.prototype.all = function all(model, filter, callback) {
callback(null, queryResults);
logger.log("debug", "Query complete");
}
}
}
}.bind(this));
}
};
Expand Down Expand Up @@ -1856,3 +1856,41 @@ DynamoDB.prototype.destroyAll = function(model, callback) {
});
logger.log("warn", t.bold.red, stopTimer(timeStart).bold.cyan);
};

/**
* Get number of records matching a filter
* @param {Object} model
* @param {Function} callback
* @param {Object} where : Filter
* @return {Number} : Number of matching records
*/
DynamoDB.prototype.count = function count(model, callback, where) {
var filter = {};
filter.where = where;
this.all(model, filter, function(err, results){
if (err || !results) {
callback(err, null);
} else {
callback(null, results.length);
}
});
};

/**
* Check if a given record exists
* @param {[type]} model [description]
* @param {[type]} id [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
DynamoDB.prototype.exists = function exists(model, id, callback) {
this.find(model, id, function (err, record){
if (err) {
callback(err, null);
} else if(isEmpty(record)) {
callback(null, false);
} else {
callback(null, true);
}
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jugglingdb-dynamodb",
"description": "DynamoDB adapter for JugglingDB ORM",
"version": "0.1.9-4",
"version": "0.1.9-5",
"main": "index.js",
"scripts": {
"test": "make test",
Expand Down
57 changes: 55 additions & 2 deletions test/basic-querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('basic-querying', function() {
describe('find', function() {

before(function(done) {
done();
done();
});

it('should query by id: not found', function(done) {
Expand All @@ -45,7 +45,7 @@ describe('basic-querying', function() {
should.not.exist(err);
u.should.be.an.instanceOf(User);
u.destroy(function(err) {
done();
done();
});

});
Expand Down Expand Up @@ -140,6 +140,28 @@ describe('basic-querying', function() {

});

describe('count', function() {

before(seed);

it('should query total count', function(done) {
User.count(function(err, n) {
should.not.exist(err);
should.exist(n);
n.should.equal(6);
done();
});
});

it('should query filtered count', function(done) {
User.count({role: 'lead'}, function(err, n) {
should.not.exist(err);
should.exist(n);
n.should.equal(2);
done();
});
});
});

describe('findOne', function() {

Expand All @@ -158,6 +180,35 @@ describe('basic-querying', function() {
});
});



describe('exists', function() {

before(seed);

it('should check whether record exist', function(done) {
User.findOne(function(e, u) {
User.exists(u.id, function(err, exists) {
should.not.exist(err);
should.exist(exists);
exists.should.be.ok;
done();
});
});
});

it('should check whether record not exist', function(done) {
User.destroyAll(function() {
User.exists("asdasd", function(err, exists) {
should.not.exist(err);
exists.should.not.be.ok;
done();
});
});
});

});

function seed(done) {
var count = 0;
var beatles = [
Expand All @@ -180,9 +231,11 @@ function seed(done) {
{name: 'Stuart Sutcliffe', order: 3}
];

User.destroyAll(function() {
beatles.forEach(function(beatle) {
User.create(beatle, ok);
});
});


function ok() {
Expand Down

0 comments on commit 00c6ba2

Please sign in to comment.