From 526940ff043dc17f761aef74275e668497b6b647 Mon Sep 17 00:00:00 2001 From: tiagoalves Date: Mon, 20 Jan 2014 23:44:11 +0000 Subject: [PATCH] added support for custom `find` query objects --- README.md | 16 ++++++++++++++++ lib/mongoose-list.js | 6 ++++-- package.json | 2 +- test/mongoose-list.test.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2ec91aa..2e1ae8b 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/mongoose-list.js b/lib/mongoose-list.js index 0ee43a7..e5b6d3f 100755 --- a/lib/mongoose-list.js +++ b/lib/mongoose-list.js @@ -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){ @@ -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) diff --git a/package.json b/package.json index 38469d1..d0d2e6a 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/mongoose-list.test.js b/test/mongoose-list.test.js index bddba70..7333ce8 100755 --- a/test/mongoose-list.test.js +++ b/test/mongoose-list.test.js @@ -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){ @@ -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