Skip to content

Commit

Permalink
enable search on multiple fields
Browse files Browse the repository at this point in the history
  • Loading branch information
FloGou committed May 24, 2016
1 parent 2e2271c commit fda3d18
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
14 changes: 13 additions & 1 deletion build/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 13 additions & 1 deletion dest/temp/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions dest/temp/specs/angular-dao.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "flogou <[email protected]>",
"name": "angular-orm",
"version": "1.0.25",
"version": "1.0.26",
"description": "",
"homepage": "",
"dependencies": {},
Expand Down
18 changes: 17 additions & 1 deletion src/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions tst/specs/angular-dao.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit fda3d18

Please sign in to comment.