Skip to content

Commit

Permalink
added support for custom find query objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoalves committed Jan 20, 2014
1 parent b20eaf3 commit 526940f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,24 @@ Uses a Mongoose style sort string eg: `+name -author'

Filter results by value applying to the `searchFields`

Alternatively, the `find` variable can also be a custom mongoose query object like the following example:

{
find: {
$or: [
{ field1: /something/ },
{ field2: new RegExp('else', 'i') }
]
}
}

This allows you to perform custom and complex queries and still make use of the remaining features of this module such as pagination.

## Changelog

### 0.1.2
* Added custom `find` object support

### 0.1.1
* Fixed bug with searching on non string fields

Expand Down
6 changes: 4 additions & 2 deletions lib/mongoose-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = exports = function list(schema,options){
search.start = search.start || 0
search.limit = search.limit || 10
search.sort = search.sort || options.sort || ''
if(search.find){
if(typeof search.find === 'string'){
var searchText = new RegExp(search.find,'i')
search.find = {$or: []}
if(options.searchFields){
Expand All @@ -28,7 +28,9 @@ module.exports = exports = function list(schema,options){
}
})
}
} else search.find = {}
} else if (typeof search.find !== 'object' || search.find === null) {
search.find = {}
}
Model
.find(search.find)
.sort(search.sort)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-list",
"version": "0.1.1",
"version": "0.1.2",
"description": "List plugin for mongoose that allows pagination, filtering, and sorting.",
"homepage": "https://github.com/snailjs/mongoose-list",
"bugs": "https://github.com/snailjs/mongoose-list/issues",
Expand Down
32 changes: 32 additions & 0 deletions test/mongoose-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ describe('MongooseList',function(){
done()
})
})
it('should return records when searching using a custom query',function(done){
Model.list(
{
find: {
$or: [{name: new RegExp('notexisting', 'i')}, {name: new RegExp('test', 'i')}]
}
},
function(err,count,results){
if(err) throw err
expect(count).to.equal(1)
expect(results.length).to.equal(1)
expect(results[0].name).to.equal('test doc')
done()
}
)
})
})
describe('[Multiple Records]',function(){
before(function(done){
Expand Down Expand Up @@ -99,6 +115,22 @@ describe('MongooseList',function(){
done()
})
})
it('should return records when searching using a custom query',function(done){
Model.list(
{
find: {
$or: [{name: new RegExp('notexisting', 'i')}, {name: new RegExp('test', 'i')}]
}
},
function(err,count,results){
if(err) throw err
expect(count).to.be.greaterThan(0)
expect(results.length).to.be.greaterThan(0)
expect(results[0].name).to.equal('test doc')
done()
}
)
})
it('should return only a few records when close to the end',function(done){
Model.list({start: 95, limit: 10, sort: 'name'},function(err,count,results){
if(err) throw err
Expand Down

0 comments on commit 526940f

Please sign in to comment.