Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct mongo-knex regexp resolution with relations #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/mongo-knex/lib/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,25 @@ class MongoToKnex {
statementValue = !_.isArray(statement.value) ? [statement.value] : statement.value;
}

innerQB[statement.whereType](statementColumn, statementOp, statementValue);
if ([compOps.$regex, compOps.$not].indexOf(statementOp) === -1) {
innerQB[statement.whereType](statementColumn, statementOp, statementValue);
} else {
// knex ILike won't work because of `COLLATION 'utf8_bin'`
// e.g. COLLATION 'utf8_bin' is not valid for CHARACTER SET 'latin1'
// for MySQL, case-sensitive LIKEs won't work without casting to BINARY
// the behavior aligns with commit 7b8798a6fa7870c98648cae5a494eb761638a208
// for an actual database agnostic solution, we need to rework everything around this

let whereType = statement.whereType;
const {source, ignoreCase} = processRegExp(statementValue);
if (!ignoreCase) {
innerQB[whereType](statementColumn, statementOp, source);
} else {
whereType += 'Raw';
debug(`(buildComparison) whereType: ${whereType}, statement: ${statement}, op: ${statement.operator}, comp: ${statementOp}, value: ${source} (REGEX/i)`);
innerQB[whereType](`lower(??) ${statementOp} ?`, [statementColumn, source]);
}
}
});

if (debugExtended.enabled) {
Expand Down
19 changes: 19 additions & 0 deletions packages/mongo-knex/test/unit/convertor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,22 @@ describe('Relations', function () {
.should.eql('select * from `posts` where `posts`.`id` not in (select `posts_tags`.`post_id` from `posts_tags` inner join `tags` on `tags`.`id` = `posts_tags`.`tag_id` where `tags`.`slug` in (\'fred\'))');
});

it('should be able to perform a regexp query on a many-to-many relation', function () {
runQuery({'tags.slug': {$regex: /foo/}})
.should.eql('select * from `posts` where `posts`.`id` in (select `posts_tags`.`post_id` from `posts_tags` inner join `tags` on `tags`.`id` = `posts_tags`.`tag_id` where `tags`.`slug` like \'%foo%\')');

runQuery({'tags.slug': {$regex: /foo/i}})
.should.eql('select * from `posts` where `posts`.`id` in (select `posts_tags`.`post_id` from `posts_tags` inner join `tags` on `tags`.`id` = `posts_tags`.`tag_id` where lower(`tags`.`slug`) like \'%foo%\')');
});

it('should be able to perform a negate regexp query on a many-to-many relation', function () {
runQuery({'tags.slug': {$not: /bar/}})
.should.eql('select * from `posts` where `posts`.`id` in (select `posts_tags`.`post_id` from `posts_tags` inner join `tags` on `tags`.`id` = `posts_tags`.`tag_id` where `tags`.`slug` not like \'%bar%\')');

runQuery({'tags.slug': {$not: /bar/i}})
.should.eql('select * from `posts` where `posts`.`id` in (select `posts_tags`.`post_id` from `posts_tags` inner join `tags` on `tags`.`id` = `posts_tags`.`tag_id` where lower(`tags`.`slug`) not like \'%bar%\')');
});

// This case doesn't work
it.skip('should be able to perform a query on a many-to-many join table alone', function () {
runQuery({'posts_tags.sort_order': 0});
Expand Down Expand Up @@ -366,6 +382,9 @@ describe('Relations', function () {

describe('RegExp/Like queries', function () {
it('are well behaved', function () {
runQuery({title: {$regex: /bar/}})
.should.eql('select * from `posts` where `posts`.`title` like \'%bar%\'');

runQuery({title: {$regex: /'/i}})
.should.eql('select * from `posts` where lower(`posts`.`title`) like \'%\\\'%\'');

Expand Down