Skip to content

Commit

Permalink
Merge pull request #378 from strongloop/feature/allow-regexp-on-like-…
Browse files Browse the repository at this point in the history
…operator

Allow regexp on like/nlike operator
  • Loading branch information
Sakib Hasan authored Jun 21, 2017
2 parents 2485b12 + 3e24854 commit e7c8787
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,17 @@ MongoDB.prototype.buildWhere = function(model, where) {
}),
};
} else if (spec === 'like') {
query[k] = { $regex: new RegExp(cond, options) };
if (cond instanceof RegExp) {
query[k] = { $regex: cond };
} else {
query[k] = { $regex: new RegExp(cond, options) };
}
} else if (spec === 'nlike') {
query[k] = { $not: new RegExp(cond, options) };
if (cond instanceof RegExp) {
query[k] = { $not: cond };
} else {
query[k] = { $not: new RegExp(cond, options) };
}
} else if (spec === 'neq') {
query[k] = { $ne: cond };
} else if (spec === 'regexp') {
Expand Down
163 changes: 163 additions & 0 deletions test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,169 @@ describe('mongodb connector', function() {
});
});

context('like and nlike operator', function() {
before(function deleteExistingTestFixtures(done) {
Post.destroyAll(done);
});
beforeEach(function createTestFixtures(done) {
Post.create([
{ title: 'a', content: 'AAA' },
{ title: 'b', content: 'BBB' },
], done);
});
after(function deleteTestFixtures(done) {
Post.destroyAll(done);
});

context('like operator', function() {
context('with regex strings', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { like: '^A' }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});

context('using flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { like: '^a', options: 'i' }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});
});

context('with regex literals', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { like: /^A/ }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});

context('using flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { like: /^a/i }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});
});

context('with regex object', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { like: new RegExp(/^A/) }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});

context('using flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { like: new RegExp(/^a/i) }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});
});
});

context('nlike operator', function() {
context('with regex strings', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { nlike: '^A' }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('BBB');
done();
});
});
});

context('using flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { nlike: '^a', options: 'i' }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('BBB');
done();
});
});
});
});

context('with regex literals', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { nlike: /^A/ }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('BBB');
done();
});
});
});

context('using flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { nlike: /^a/i }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('BBB');
done();
});
});
});
});

context('with regex object', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { nlike: new RegExp(/^A/) }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('BBB');
done();
});
});
});

context('using flags', function() {
it('should work', function(done) {
Post.find({ where: { content: { nlike: new RegExp(/^a/i) }}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('BBB');
done();
});
});
});
});
});
});

after(function(done) {
User.destroyAll(function() {
Post.destroyAll(function() {
Expand Down

0 comments on commit e7c8787

Please sign in to comment.