Skip to content

Commit

Permalink
Ignore direct filtering by ids. Add support for $in query
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneKostrikov committed Jun 13, 2014
1 parent c2910f1 commit 63ec0cb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ adapter.findMany = function(model, query, projection) {
memo[{ "gt": "$gt", "gte": "$gte", "lt": "$lt", "lte": "$lte" }[op] || op] = opVal;
return memo;
}, {});

}else if (_.isArray(model.schema.tree[key]) && _.isString(val.in || val.$in)){
query[key] = {
$in: (val.in || val.$in).split(',')
};
}else if (_.isObject(val) && _.isString(val.regex)){
//regex
query[key] = {
Expand Down
2 changes: 1 addition & 1 deletion lib/fortune.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Fortune.prototype.resource = function(name, schema, options, schemaCallback) {
* @return {Object}
*/
Fortune.prototype._preprocessSchema = function (schema) {
['id', 'href', 'links'].forEach(function (reservedKey) {
['id', 'href', 'links', 'in'].forEach(function (reservedKey) {
if (schema.hasOwnProperty(reservedKey)) {
delete schema[reservedKey];
console.warn('Reserved key "' + reservedKey + '" is not allowed.');
Expand Down
10 changes: 7 additions & 3 deletions lib/querytree.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ function parseQuery(query, requestedResource){
if (_.isObject(schemaBranch) && _.isObject(q)){
//Three options: ref by array, ref by object, business PK, array field
if (_.isArray(schemaBranch) && (!!schemaBranch[0].ref || _.isString(schemaBranch[0]))){
//ref by array. Both types of declarations
freeze[key] = createSubrequest(q, schemaBranch[0].ref || schemaBranch[0]);
}else if (!!schemaBranch.ref){
//ref by array or $in query. Both types of declarations
if (q.in || q.$in){
freeze[key] = q;
}else{
freeze[key] = createSubrequest(q, schemaBranch[0].ref || schemaBranch[0]);
}
}else if (!!schemaBranch.ref && _.isObject(q)){
//ref by object
freeze[key] = createSubrequest(q, schemaBranch.ref);
}else{
Expand Down
79 changes: 79 additions & 0 deletions test/fortune/fields_and_filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ module.exports = function(options){
});
});
});
it('should support filtering by id for one-to-one relationships', function(done){
new Promise(function(resolve){
var upd = [{
op: 'replace',
path: '/people/0/soulmate',
value: ids.people[1]
}];
request(baseUrl).patch('/people/' + ids.people[0])
.set('content-type', 'application/json')
.send(JSON.stringify(upd))
.expect(200)
.end(function(err){
should.not.exist(err);
resolve();
});
})
.then(function(){
request(baseUrl).get('/people?filter[soulmate]=' + ids.people[1])
.expect(200)
.end(function(err, res){
should.not.exist(err);
var body = JSON.parse(res.text);
(body.people[0].id).should.equal(ids.people[0]);
done();
});
});
});
it('should support regex query', function(done){
request(baseUrl).get('/people?filter[email][regex]=Bert@&filter[email][options]=i')
.expect(200)
Expand All @@ -154,6 +181,58 @@ module.exports = function(options){
done();
});
});
it('should support `in` query', function(done){
new Promise(function(resolve){
var upd = [{
op: 'add',
path: '/people/0/houses/-',
value: ids.houses[0]
},{
op: 'add',
path: '/people/0/houses/-',
value: ids.houses[1]
}];
request(baseUrl).patch('/people/' + ids.people[0])
.set('content-type', 'application/json')
.send(JSON.stringify(upd))
.expect(200)
.end(function(err, res){
should.not.exist(err);
resolve();
});
})
.then(function(){
return new Promise(function(resolve){
var upd = [{
op: 'add',
path: '/people/0/houses/-',
value: ids.houses[1]
},{
op: 'add',
path: '/people/0/houses/-',
value: ids.houses[2]
}];
request(baseUrl).patch('/people/' + ids.people[1])
.set('content-type', 'application/json')
.send(JSON.stringify(upd))
.expect(200)
.end(function(err, res){
should.not.exist(err);
resolve();
});
});
})
.then(function(){
request(baseUrl).get('/people?filter[houses][in]=' + ids.houses[0] + ',' + ids.houses[1])
.expect(200)
.end(function(err, res){
should.not.exist(err);
var body = JSON.parse(res.text);
(body.people.length).should.equal(2);
done();
});
});
});
describe('filtering by related objects fields', function(){
beforeEach(function(done){
neighbourhood(adapter, ids).then(function(){
Expand Down

0 comments on commit 63ec0cb

Please sign in to comment.