From fda3d180c8e713f03f4eaec1a6351a001617ed19 Mon Sep 17 00:00:00 2001 From: Florent Gouget Date: Tue, 24 May 2016 15:06:22 +0200 Subject: [PATCH] enable search on multiple fields --- build/app.js | 14 +++++++++++++- dest/temp/QueryBuilder.js | 14 +++++++++++++- dest/temp/specs/angular-dao.specs.js | 12 ++++++++++++ package.json | 2 +- src/QueryBuilder.js | 18 +++++++++++++++++- tst/specs/angular-dao.specs.js | 12 ++++++++++++ 6 files changed, 68 insertions(+), 4 deletions(-) diff --git a/build/app.js b/build/app.js index 2a468c8..ac90951 100644 --- a/build/app.js +++ b/build/app.js @@ -697,7 +697,19 @@ var QueryBuilder = (function () { var field = arguments.length <= 1 || arguments[1] === undefined ? 'name' : arguments[1]; if (term) { - this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' })); + if (Array.isArray(field)) { + var q = { + $or: field.map(function (element) { + return _defineProperty({}, element, { + $regex: '.*' + term + '.*', + $options: 'i' + }); + }) + }; + this.setQuery(q); + } else { + this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' })); + } } return this; } diff --git a/dest/temp/QueryBuilder.js b/dest/temp/QueryBuilder.js index e49e494..075d06f 100644 --- a/dest/temp/QueryBuilder.js +++ b/dest/temp/QueryBuilder.js @@ -127,7 +127,19 @@ var QueryBuilder = (function () { var field = arguments.length <= 1 || arguments[1] === undefined ? 'name' : arguments[1]; if (term) { - this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' })); + if (Array.isArray(field)) { + var q = { + $or: field.map(function (element) { + return _defineProperty({}, element, { + $regex: '.*' + term + '.*', + $options: 'i' + }); + }) + }; + this.setQuery(q); + } else { + this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' })); + } } return this; } diff --git a/dest/temp/specs/angular-dao.specs.js b/dest/temp/specs/angular-dao.specs.js index e33c667..ba2b7fd 100644 --- a/dest/temp/specs/angular-dao.specs.js +++ b/dest/temp/specs/angular-dao.specs.js @@ -160,6 +160,18 @@ describe('Angular DAO', function () { httpBackend.flush(); }); + it('Should search on another field', function () { + httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"label":{"$regex":".*toto.*","$options":"i"}}')).respond([]); + ModelManager.get(ModelManager.query().search('toto', 'label')); + httpBackend.flush(); + }); + + it('Should search on multiple fields', function () { + httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"$or":[{"name":{"$regex":".*toto.*","$options":"i"}},{"label":{"$regex":".*toto.*","$options":"i"}}]}')).respond([]); + ModelManager.get(ModelManager.query().search('toto', ['name', 'label'])); + httpBackend.flush(); + }); + it('Should make subPopulate queries', function () { var model = ModelManager.create({ _id: '1234656', diff --git a/package.json b/package.json index 5b03ad8..1305c10 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "author": "flogou ", "name": "angular-orm", - "version": "1.0.25", + "version": "1.0.26", "description": "", "homepage": "", "dependencies": {}, diff --git a/src/QueryBuilder.js b/src/QueryBuilder.js index 04e6c4a..737b5a4 100644 --- a/src/QueryBuilder.js +++ b/src/QueryBuilder.js @@ -93,7 +93,23 @@ export default class QueryBuilder { search (term, field = 'name') { if (term) { - this.setQuery({ [field]: { $regex: '.*' + term + '.*', $options: 'i' } }) + if (Array.isArray(field)) { + var q = { + $or: field.map( + (element) => { + return { + [ element ]: { + $regex: `.*${term}.*`, + $options: 'i' + } + } + } + ) + } + this.setQuery(q) + } else { + this.setQuery({ [field]: { $regex: '.*' + term + '.*', $options: 'i' } }) + } } return this; } diff --git a/tst/specs/angular-dao.specs.js b/tst/specs/angular-dao.specs.js index e553320..39e510a 100644 --- a/tst/specs/angular-dao.specs.js +++ b/tst/specs/angular-dao.specs.js @@ -162,6 +162,18 @@ describe('Angular DAO', function () { httpBackend.flush(); }); + it('Should search on another field', function () { + httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"label":{"$regex":".*toto.*","$options":"i"}}')).respond([]); + ModelManager.get(ModelManager.query().search('toto', 'label')); + httpBackend.flush(); + }); + + it('Should search on multiple fields', function () { + httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"$or":[{"name":{"$regex":".*toto.*","$options":"i"}},{"label":{"$regex":".*toto.*","$options":"i"}}]}')).respond([]); + ModelManager.get(ModelManager.query().search('toto', ['name', 'label'])); + httpBackend.flush(); + }); + it('Should make subPopulate queries', function () { var model = ModelManager.create({ _id: '1234656',