Skip to content

Commit 00c6ba2

Browse files
committed
Added count and exists
1 parent fbc74f4 commit 00c6ba2

File tree

4 files changed

+96
-5
lines changed

4 files changed

+96
-5
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
##JugglingDB Adapter for DynamoDB version 0.1.9-4
1+
##JugglingDB Adapter for DynamoDB version 0.1.9-5
22
* Adapter is still in development stage. The stable release will be 0.2.0 and will offer rich functionalities along
33
with lots of tests.
44
* 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 for: lib/dynamodb.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ DynamoDB.prototype.all = function all(model, filter, callback) {
12321232
callback(null, queryResults);
12331233
logger.log("debug", "Query complete");
12341234
}
1235-
}
1235+
}
12361236
}.bind(this));
12371237
}
12381238
};
@@ -1856,3 +1856,41 @@ DynamoDB.prototype.destroyAll = function(model, callback) {
18561856
});
18571857
logger.log("warn", t.bold.red, stopTimer(timeStart).bold.cyan);
18581858
};
1859+
1860+
/**
1861+
* Get number of records matching a filter
1862+
* @param {Object} model
1863+
* @param {Function} callback
1864+
* @param {Object} where : Filter
1865+
* @return {Number} : Number of matching records
1866+
*/
1867+
DynamoDB.prototype.count = function count(model, callback, where) {
1868+
var filter = {};
1869+
filter.where = where;
1870+
this.all(model, filter, function(err, results){
1871+
if (err || !results) {
1872+
callback(err, null);
1873+
} else {
1874+
callback(null, results.length);
1875+
}
1876+
});
1877+
};
1878+
1879+
/**
1880+
* Check if a given record exists
1881+
* @param {[type]} model [description]
1882+
* @param {[type]} id [description]
1883+
* @param {Function} callback [description]
1884+
* @return {[type]} [description]
1885+
*/
1886+
DynamoDB.prototype.exists = function exists(model, id, callback) {
1887+
this.find(model, id, function (err, record){
1888+
if (err) {
1889+
callback(err, null);
1890+
} else if(isEmpty(record)) {
1891+
callback(null, false);
1892+
} else {
1893+
callback(null, true);
1894+
}
1895+
});
1896+
};

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jugglingdb-dynamodb",
33
"description": "DynamoDB adapter for JugglingDB ORM",
4-
"version": "0.1.9-4",
4+
"version": "0.1.9-5",
55
"main": "index.js",
66
"scripts": {
77
"test": "make test",

Diff for: test/basic-querying.test.js

+55-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('basic-querying', function() {
2525
describe('find', function() {
2626

2727
before(function(done) {
28-
done();
28+
done();
2929
});
3030

3131
it('should query by id: not found', function(done) {
@@ -45,7 +45,7 @@ describe('basic-querying', function() {
4545
should.not.exist(err);
4646
u.should.be.an.instanceOf(User);
4747
u.destroy(function(err) {
48-
done();
48+
done();
4949
});
5050

5151
});
@@ -140,6 +140,28 @@ describe('basic-querying', function() {
140140

141141
});
142142

143+
describe('count', function() {
144+
145+
before(seed);
146+
147+
it('should query total count', function(done) {
148+
User.count(function(err, n) {
149+
should.not.exist(err);
150+
should.exist(n);
151+
n.should.equal(6);
152+
done();
153+
});
154+
});
155+
156+
it('should query filtered count', function(done) {
157+
User.count({role: 'lead'}, function(err, n) {
158+
should.not.exist(err);
159+
should.exist(n);
160+
n.should.equal(2);
161+
done();
162+
});
163+
});
164+
});
143165

144166
describe('findOne', function() {
145167

@@ -158,6 +180,35 @@ describe('basic-querying', function() {
158180
});
159181
});
160182

183+
184+
185+
describe('exists', function() {
186+
187+
before(seed);
188+
189+
it('should check whether record exist', function(done) {
190+
User.findOne(function(e, u) {
191+
User.exists(u.id, function(err, exists) {
192+
should.not.exist(err);
193+
should.exist(exists);
194+
exists.should.be.ok;
195+
done();
196+
});
197+
});
198+
});
199+
200+
it('should check whether record not exist', function(done) {
201+
User.destroyAll(function() {
202+
User.exists("asdasd", function(err, exists) {
203+
should.not.exist(err);
204+
exists.should.not.be.ok;
205+
done();
206+
});
207+
});
208+
});
209+
210+
});
211+
161212
function seed(done) {
162213
var count = 0;
163214
var beatles = [
@@ -180,9 +231,11 @@ function seed(done) {
180231
{name: 'Stuart Sutcliffe', order: 3}
181232
];
182233

234+
User.destroyAll(function() {
183235
beatles.forEach(function(beatle) {
184236
User.create(beatle, ok);
185237
});
238+
});
186239

187240

188241
function ok() {

0 commit comments

Comments
 (0)