Skip to content

Commit 29c60c7

Browse files
authored
Merge pull request #1180 from strongloop/feature/extended-operators-in-query
Honour allowExtendedOperators in "DAO.find"
2 parents 1b60245 + 779cf6a commit 29c60c7

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

lib/dao.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,13 +1658,19 @@ DataAccessObject._coerce = function(where) {
16581658
}
16591659
} else {
16601660
if (val != null) {
1661+
const dsSettings = this.getDataSource().settings;
1662+
const allowExtendedOperators = dsSettings.allowExtendedOperators;
16611663
if (operator === null && val instanceof RegExp) {
16621664
// Normalize {name: /A/} to {name: {regexp: /A/}}
16631665
operator = 'regexp';
16641666
} else if (operator === 'regexp' && val instanceof RegExp) {
16651667
// Do not coerce regex literals/objects
1666-
} else if (!((operator === 'like' || operator === 'nlike' ||
1667-
operator === 'ilike' || operator === 'nilike') && val instanceof RegExp)) {
1668+
} else if ((operator === 'like' || operator === 'nlike' ||
1669+
operator === 'ilike' || operator === 'nilike') && val instanceof RegExp) {
1670+
// Do not coerce RegExp operator value
1671+
} else if (allowExtendedOperators && typeof val === 'object') {
1672+
// Do not coerce object values when extended operators are allowed
1673+
} else {
16681674
val = DataType(val);
16691675
}
16701676
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
2+
// Node module: loopback-datasource-juggler
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
'use strict';
7+
8+
const DataSource = require('..').DataSource;
9+
const should = require('should');
10+
11+
describe('Model.settings.allowExtendedOperators', () => {
12+
context('DAO.find()', () => {
13+
it('converts extended operators to string value by default', () => {
14+
const TestModel = createTestModel();
15+
return TestModel.find(extendedQuery()).then((results) => {
16+
should(results[0].value).eql('[object Object]');
17+
});
18+
});
19+
20+
it('preserves extended operators wit allowExtendedOperators set', () => {
21+
const TestModel = createTestModel({allowExtendedOperators: true});
22+
return TestModel.find(extendedQuery()).then((results) => {
23+
should(results[0].value).eql({$exists: true});
24+
});
25+
});
26+
27+
function extendedQuery() {
28+
// datasource modifies the query,
29+
// we have to build a new object for each test
30+
return {where: {value: {$exists: true}}};
31+
}
32+
});
33+
34+
function createTestModel(connectorSettings) {
35+
const ds = createTestDataSource(connectorSettings);
36+
return ds.createModel('TestModel', {value: String});
37+
}
38+
39+
function createTestDataSource(connectorSettings) {
40+
connectorSettings = connectorSettings || {};
41+
connectorSettings.connector = {
42+
initialize: (dataSource, cb) => {
43+
dataSource.connector = new TestConnector(dataSource);
44+
},
45+
};
46+
47+
return new DataSource(connectorSettings);
48+
}
49+
50+
class TestConnector {
51+
constructor(dataSource) {
52+
}
53+
54+
all(model, filter, options, callback) {
55+
// return the raw "value" query
56+
var instanceFound = {
57+
value: filter.where.value,
58+
};
59+
callback(null, [instanceFound]);
60+
}
61+
}
62+
});

0 commit comments

Comments
 (0)