diff --git a/README.md b/README.md index 06f425e..6e0e49e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/dynamodb.js b/lib/dynamodb.js index 75f5cc0..0866d71 100644 --- a/lib/dynamodb.js +++ b/lib/dynamodb.js @@ -1232,7 +1232,7 @@ DynamoDB.prototype.all = function all(model, filter, callback) { callback(null, queryResults); logger.log("debug", "Query complete"); } - } + } }.bind(this)); } }; @@ -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); + } + }); +}; \ No newline at end of file diff --git a/package.json b/package.json index 361543a..bf9e151 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 9b26665..25d0bde 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -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) { @@ -45,7 +45,7 @@ describe('basic-querying', function() { should.not.exist(err); u.should.be.an.instanceOf(User); u.destroy(function(err) { - done(); + done(); }); }); @@ -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() { @@ -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 = [ @@ -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() {