Skip to content

Commit

Permalink
Extended filters
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneKostrikov committed Jun 6, 2014
1 parent a42fe4a commit 30a5462
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
26 changes: 25 additions & 1 deletion lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ adapter.findMany = function(model, query, projection) {

var pk = model.pk || "_id";

if (_.isObject(query) && !_.isArray(query)){
_.each(query, function(val, key){
var m;
if(model.schema.tree[key] === Date && _.isString(val)){
//Strict date equality
m = moment(val);

if(m.format("YYYY-MM-DD") === val){
Expand All @@ -241,8 +243,30 @@ adapter.findMany = function(model, query, projection) {
$lte: moment(val).add("days", 1).format("YYYY-MM-DD")
};
}
}
}else if ((model.schema.tree[key] === Date || model.schema.tree[key] === Number) && _.isObject(val)){
//gt/gte/lt/lte for dates and numbers
query[key] = {};
if (!_.isUndefined(val.gt)){
query[key].$gt = val.gt;
}
if (!_.isUndefined(val.gte)){
query[key].$gte = val.gte;
}
if (!_.isUndefined(val.lt)){
query[key].$lt = val.lt;
}
if (!_.isUndefined(val.lte)){
query[key].$lte = val.lte;
}
}else if ((model.schema.tree[key] === String || model.schema.tree[key].type === String) && _.isObject(val)){
//regex for strings
query[key] = {
$regex: val.regex ? val.regex : '',
$options: val.options ? val.options : ''
};
}
});
}

if(_.isObject(query)){
if(_.isArray(query)) {
Expand Down
1 change: 1 addition & 0 deletions test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = function(options, port) {
official: String,
password: String,
appearances: Number,
birthday: Date,
email: String,
pets: ['pet'],
soulmate: {ref: 'person', inverse: 'soulmate', type: String},
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
{
"name": "Dilbert",
"appearances": 3457,
"birthday": "1990-01-01",
"email": "dilbert@mailbert.com"
},
{
"name": "Wally",
"appearances": 1934,
"birthday": "1995-01-01",
"email": "wally@mailbert.com"
},
{
"name": "Robert",
"appearances": 0,
"birthday": "2000-01-01",
"email": "robert@mailbert.com"
}
],
Expand Down
119 changes: 116 additions & 3 deletions test/fortune-mongodb/mongodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RSVP.on('error', function(err){
});

module.exports = function(options){
describe('MongoDB adapter', function(){
describe.only('MongoDB adapter', function(){
var ids;

beforeEach(function(){
Expand Down Expand Up @@ -115,7 +115,7 @@ module.exports = function(options){
//hooks add their black magic here.
//See what you have in fixtures + what beforeWrite hooks assign in addiction
var keys = Object.keys(docs[0]).length;
(keys).should.equal(6);
(keys).should.equal(7);
done();
});
});
Expand Down Expand Up @@ -172,7 +172,7 @@ module.exports = function(options){
.then(function(doc){
//hooks add their black magic here.
//See what you have in fixtures + what beforeWrite hooks assign in addiction
(Object.keys(doc).length).should.equal(6);
(Object.keys(doc).length).should.equal(7);
done();
});
});
Expand All @@ -192,6 +192,119 @@ module.exports = function(options){
});
});
});
describe('Filtering', function(){
it('should be able to filter date by exact value', function(done){
adapter.findMany('person', {birthday: '2000-01-01'})
.then(function(docs){
(docs.length).should.equal(1);
(docs[0].name).should.equal('Robert');
done();
});
});
it('should be able to filter date range: exclusive', function(done){
var query = {
birthday: {
lt: '2000-02-02',
gt: '1990-01-01'
}
};
adapter.findMany('person', query)
.then(function(docs){
(docs.length).should.equal(2);
done();
});
});
it('should be able to filter date range: inclusive', function(done){
var query = {
birthday: {
gte: '1995-01-01',
lte: '2000-01-01'
}
};
adapter.findMany('person', query)
.then(function(docs){
(docs.length).should.equal(2);
done();
});
});
it('should be able to filter number range: exclusive', function(done){
var query = {
appearances: {
gt: 1934,
lt: 4000
}
};
adapter.findMany('person', query)
.then(function(docs){
(docs.length).should.equal(1);
done();
});
});
it('should be able to filter number range: inclusive', function(done){
var query = {
appearances: {
gte: 1934,
lte: 3457
}
};
adapter.findMany('person', query)
.then(function(docs){
(docs.length).should.equal(2);
done();
});
});
it('should be able to run regex query with default options', function(done){
var queryLowercase = {
email: {
regex: 'bert@'
}
};
var queryUppercase = {
email: {
regex: 'Bert@'
}
};
new Promise(function(resolve){
adapter.findMany('person', queryLowercase)
.then(function(docs){
(docs.length).should.equal(2);
resolve();
});
}).then(function(){
adapter.findMany('person',queryUppercase)
.then(function(docs){
(docs.length).should.equal(0);
done();
});
});
});
it('should be possible to specify custom options', function(done){
var query = {
name: {
regex: 'WALLY',
options: 'i'
}
};
adapter.findMany('person', query)
.then(function(docs){
(docs.length).should.equal(1);
(docs[0].name).should.equal('Wally');
done();
});
});
it('should treat empty regex as find all', function(done){
var query = {
email: {
regex: ''
}
};
adapter.findMany('person', query)
.then(function(docs){
(docs.length).should.equal(3);
done();
});
});
});
});

};

0 comments on commit 30a5462

Please sign in to comment.