Skip to content

Commit fda3d18

Browse files
committed
enable search on multiple fields
1 parent 2e2271c commit fda3d18

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

build/app.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,19 @@ var QueryBuilder = (function () {
697697
var field = arguments.length <= 1 || arguments[1] === undefined ? 'name' : arguments[1];
698698

699699
if (term) {
700-
this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' }));
700+
if (Array.isArray(field)) {
701+
var q = {
702+
$or: field.map(function (element) {
703+
return _defineProperty({}, element, {
704+
$regex: '.*' + term + '.*',
705+
$options: 'i'
706+
});
707+
})
708+
};
709+
this.setQuery(q);
710+
} else {
711+
this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' }));
712+
}
701713
}
702714
return this;
703715
}

dest/temp/QueryBuilder.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,19 @@ var QueryBuilder = (function () {
127127
var field = arguments.length <= 1 || arguments[1] === undefined ? 'name' : arguments[1];
128128

129129
if (term) {
130-
this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' }));
130+
if (Array.isArray(field)) {
131+
var q = {
132+
$or: field.map(function (element) {
133+
return _defineProperty({}, element, {
134+
$regex: '.*' + term + '.*',
135+
$options: 'i'
136+
});
137+
})
138+
};
139+
this.setQuery(q);
140+
} else {
141+
this.setQuery(_defineProperty({}, field, { $regex: '.*' + term + '.*', $options: 'i' }));
142+
}
131143
}
132144
return this;
133145
}

dest/temp/specs/angular-dao.specs.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ describe('Angular DAO', function () {
160160
httpBackend.flush();
161161
});
162162

163+
it('Should search on another field', function () {
164+
httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"label":{"$regex":".*toto.*","$options":"i"}}')).respond([]);
165+
ModelManager.get(ModelManager.query().search('toto', 'label'));
166+
httpBackend.flush();
167+
});
168+
169+
it('Should search on multiple fields', function () {
170+
httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"$or":[{"name":{"$regex":".*toto.*","$options":"i"}},{"label":{"$regex":".*toto.*","$options":"i"}}]}')).respond([]);
171+
ModelManager.get(ModelManager.query().search('toto', ['name', 'label']));
172+
httpBackend.flush();
173+
});
174+
163175
it('Should make subPopulate queries', function () {
164176
var model = ModelManager.create({
165177
_id: '1234656',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "flogou <florent.gouget@gmail.com>",
33
"name": "angular-orm",
4-
"version": "1.0.25",
4+
"version": "1.0.26",
55
"description": "",
66
"homepage": "",
77
"dependencies": {},

src/QueryBuilder.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,23 @@ export default class QueryBuilder {
9393

9494
search (term, field = 'name') {
9595
if (term) {
96-
this.setQuery({ [field]: { $regex: '.*' + term + '.*', $options: 'i' } })
96+
if (Array.isArray(field)) {
97+
var q = {
98+
$or: field.map(
99+
(element) => {
100+
return {
101+
[ element ]: {
102+
$regex: `.*${term}.*`,
103+
$options: 'i'
104+
}
105+
}
106+
}
107+
)
108+
}
109+
this.setQuery(q)
110+
} else {
111+
this.setQuery({ [field]: { $regex: '.*' + term + '.*', $options: 'i' } })
112+
}
97113
}
98114
return this;
99115
}

tst/specs/angular-dao.specs.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ describe('Angular DAO', function () {
162162
httpBackend.flush();
163163
});
164164

165+
it('Should search on another field', function () {
166+
httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"label":{"$regex":".*toto.*","$options":"i"}}')).respond([]);
167+
ModelManager.get(ModelManager.query().search('toto', 'label'));
168+
httpBackend.flush();
169+
});
170+
171+
it('Should search on multiple fields', function () {
172+
httpBackend.expectGET(encodeURI('http://MOCKURL.com/model1?conditions={"$or":[{"name":{"$regex":".*toto.*","$options":"i"}},{"label":{"$regex":".*toto.*","$options":"i"}}]}')).respond([]);
173+
ModelManager.get(ModelManager.query().search('toto', ['name', 'label']));
174+
httpBackend.flush();
175+
});
176+
165177
it('Should make subPopulate queries', function () {
166178
var model = ModelManager.create({
167179
_id: '1234656',

0 commit comments

Comments
 (0)