Skip to content

Commit a9d3816

Browse files
authored
Add ilike and nilike operators (#1136)
Closes #633 Backport of #1091
1 parent da57136 commit a9d3816

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

lib/connectors/memory.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ function applyFilter(filter) {
605605
testInEquality({ lte: example.between[1] }, value));
606606
}
607607

608-
if (example.like || example.nlike) {
609-
var like = example.like || example.nlike;
608+
if (example.like || example.nlike || example.ilike || example.nilike) {
609+
var like = example.like || example.nlike || example.ilike || example.nilike;
610610
if (typeof like === 'string') {
611611
like = toRegExp(like);
612612
}
@@ -617,6 +617,14 @@ function applyFilter(filter) {
617617
if (example.nlike) {
618618
return !new RegExp(like).test(value);
619619
}
620+
621+
if (example.ilike) {
622+
return !!new RegExp(like, 'i').test(value);
623+
}
624+
625+
if (example.nilike) {
626+
return !new RegExp(like, 'i').test(value);
627+
}
620628
}
621629

622630
if (testInEquality(example, value)) {

lib/dao.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,8 @@ var operators = {
14071407
neq: '!=',
14081408
like: 'LIKE',
14091409
nlike: 'NOT LIKE',
1410+
ilike: 'ILIKE',
1411+
nilike: 'NOT ILIKE',
14101412
regexp: 'REGEXP',
14111413
};
14121414

@@ -1633,6 +1635,8 @@ DataAccessObject._coerce = function(where) {
16331635
break;
16341636
case 'like':
16351637
case 'nlike':
1638+
case 'ilike':
1639+
case 'nilike':
16361640
if (!(typeof val === 'string' || val instanceof RegExp)) {
16371641
err = new Error(g.f('The %s property has invalid clause %j', p, where[p]));
16381642
err.statusCode = 400;
@@ -1665,7 +1669,8 @@ DataAccessObject._coerce = function(where) {
16651669
operator = 'regexp';
16661670
} else if (operator === 'regexp' && val instanceof RegExp) {
16671671
// Do not coerce regex literals/objects
1668-
} else if (!((operator === 'like' || operator === 'nlike') && val instanceof RegExp)) {
1672+
} else if (!((operator === 'like' || operator === 'nlike' ||
1673+
operator === 'ilike' || operator === 'nilike') && val instanceof RegExp)) {
16691674
val = DataType(val);
16701675
}
16711676
}
@@ -1718,6 +1723,8 @@ DataAccessObject._coerce = function(where) {
17181723
* - neq: !=
17191724
* - like: LIKE
17201725
* - nlike: NOT LIKE
1726+
* - ilike: ILIKE
1727+
* - nilike: NOT ILIKE
17211728
* - regexp: REGEXP
17221729
*
17231730
* You can also use `and` and `or` operations. See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.

test/basic-querying.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,62 @@ describe('basic-querying', function() {
461461
});
462462
});
463463

464+
var itWhenIlikeSupported = connectorCapabilities.ilike ? it : it.skip.bind(it);
465+
466+
itWhenIlikeSupported('should support "like" that is satisfied', function(done) {
467+
User.find({ where: { name: { like: 'John' }}}, function(err, users) {
468+
if (err) return done(err);
469+
users.length.should.equal(1);
470+
users[0].name.should.equal('John Lennon');
471+
done();
472+
});
473+
});
474+
475+
itWhenIlikeSupported('should support "like" that is not satisfied', function(done) {
476+
User.find({ where: { name: { like: 'Bob' }}}, function(err, users) {
477+
if (err) return done(err);
478+
users.length.should.equal(0);
479+
done();
480+
});
481+
});
482+
483+
var itWhenNilikeSupported = connectorCapabilities.nilike ? it : it.skip.bind(it);
484+
485+
itWhenNilikeSupported('should support "nlike" that is satisfied', function(done) {
486+
User.find({ where: { name: { nlike: 'John' }}}, function(err, users) {
487+
if (err) return done(err);
488+
users.length.should.equal(5);
489+
users[0].name.should.equal('Paul McCartney');
490+
done();
491+
});
492+
});
493+
494+
itWhenIlikeSupported('should support "ilike" that is satisfied', function(done) {
495+
User.find({ where: { name: { ilike: 'john' }}}, function(err, users) {
496+
if (err) return done(err);
497+
users.length.should.equal(1);
498+
users[0].name.should.equal('John Lennon');
499+
done();
500+
});
501+
});
502+
503+
itWhenIlikeSupported('should support "ilike" that is not satisfied', function(done) {
504+
User.find({ where: { name: { ilike: 'bob' }}}, function(err, users) {
505+
if (err) return done(err);
506+
users.length.should.equal(0);
507+
done();
508+
});
509+
});
510+
511+
itWhenNilikeSupported('should support "nilike" that is satisfied', function(done) {
512+
User.find({ where: { name: { nilike: 'john' }}}, function(err, users) {
513+
if (err) return done(err);
514+
users.length.should.equal(5);
515+
users[0].name.should.equal('Paul McCartney');
516+
done();
517+
});
518+
});
519+
464520
it('should only include fields as specified', function(done) {
465521
var remaining = 0;
466522

test/init.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ if (!('getModelBuilder' in global)) {
3535
if (!('Promise' in global)) {
3636
global.Promise = require('bluebird');
3737
}
38+
39+
if (!('connectorCapabilities' in global)) {
40+
global.connectorCapabilities = {};
41+
}

0 commit comments

Comments
 (0)