Skip to content

Commit 1ee0442

Browse files
authored
Merge pull request #1091 from duffn/ilike
Add ilike and nilike operators
2 parents c6e5d38 + a23076d commit 1ee0442

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
@@ -616,8 +616,8 @@ function applyFilter(filter) {
616616
testInEquality({lte: example.between[1]}, value));
617617
}
618618

619-
if (example.like || example.nlike) {
620-
var like = example.like || example.nlike;
619+
if (example.like || example.nlike || example.ilike || example.nilike) {
620+
var like = example.like || example.nlike || example.ilike || example.nilike;
621621
if (typeof like === 'string') {
622622
like = toRegExp(like);
623623
}
@@ -628,6 +628,14 @@ function applyFilter(filter) {
628628
if (example.nlike) {
629629
return !new RegExp(like).test(value);
630630
}
631+
632+
if (example.ilike) {
633+
return !!new RegExp(like, 'i').test(value);
634+
}
635+
636+
if (example.nilike) {
637+
return !new RegExp(like, 'i').test(value);
638+
}
631639
}
632640

633641
if (testInEquality(example, value)) {

lib/dao.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,8 @@ var operators = {
14001400
neq: '!=',
14011401
like: 'LIKE',
14021402
nlike: 'NOT LIKE',
1403+
ilike: 'ILIKE',
1404+
nilike: 'NOT ILIKE',
14031405
regexp: 'REGEXP',
14041406
};
14051407

@@ -1626,6 +1628,8 @@ DataAccessObject._coerce = function(where) {
16261628
break;
16271629
case 'like':
16281630
case 'nlike':
1631+
case 'ilike':
1632+
case 'nilike':
16291633
if (!(typeof val === 'string' || val instanceof RegExp)) {
16301634
err = new Error(g.f('The %s property has invalid clause %j', p, where[p]));
16311635
err.statusCode = 400;
@@ -1658,7 +1662,8 @@ DataAccessObject._coerce = function(where) {
16581662
operator = 'regexp';
16591663
} else if (operator === 'regexp' && val instanceof RegExp) {
16601664
// Do not coerce regex literals/objects
1661-
} else if (!((operator === 'like' || operator === 'nlike') && val instanceof RegExp)) {
1665+
} else if (!((operator === 'like' || operator === 'nlike' ||
1666+
operator === 'ilike' || operator === 'nilike') && val instanceof RegExp)) {
16621667
val = DataType(val);
16631668
}
16641669
}
@@ -1711,6 +1716,8 @@ DataAccessObject._coerce = function(where) {
17111716
* - neq: !=
17121717
* - like: LIKE
17131718
* - nlike: NOT LIKE
1719+
* - ilike: ILIKE
1720+
* - nilike: NOT ILIKE
17141721
* - regexp: REGEXP
17151722
*
17161723
* 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
@@ -469,6 +469,62 @@ describe('basic-querying', function() {
469469
});
470470
});
471471

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

test/init.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ if (!('getModelBuilder' in global)) {
3232
return new ModelBuilder();
3333
};
3434
}
35+
36+
if (!('connectorCapabilities' in global)) {
37+
global.connectorCapabilities = {};
38+
}

0 commit comments

Comments
 (0)