Skip to content

Commit

Permalink
2.3.3 feat: add regex expression to raw query (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
diVid3 authored Feb 21, 2025
2 parents ccb70a1 + 6460543 commit 27d3638
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/MongoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MongoQuery {
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;

if ($check.object(obj[key]) && !utils.isValidId(obj[key])) {
if ($check.object(obj[key]) && !utils.isValidId(obj[key]) && key !== '$regularExpression') {
mergeRawQueryRecursive(obj[key], obj, key);
} else if ($check.array(obj[key])) {
for (let i = 0; i < obj[key].length; i++) {
Expand Down Expand Up @@ -114,6 +114,8 @@ class MongoQuery {
parent[parentKey] = queryVal;
} else if (key === '$string') {
parent[parentKey] = $check.assigned(obj[key]) && obj[key].toString ? obj[key].toString() : obj[key];
} else if (key === '$regularExpression') {
parent[parentKey] = new RegExp(obj[key].pattern, obj[key].options);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@synatic/mongo-magic",
"version": "2.3.2",
"version": "2.3.3",
"description": "Synatic utility classes for interacting with MongoDB",
"main": "index.js",
"files": [
Expand Down
13 changes: 13 additions & 0 deletions test/01. MonogQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ describe('Mongo Query', function () {
done();
});

it('should parse a raw query with regularExpression in \'$in\' array', function (done) {
const mongoQuery = new MongoQuery({$rawQuery: {
field: {$in: [
{$regularExpression: { pattern: ';', options: 'i' }},
{$regularExpression: { pattern: '/', options: 'i' }}
]}}
});

assert(mongoQuery.parsedQuery.query.field.$in[0] instanceof RegExp, 'Invalid parsed $in RegExp query');
assert(mongoQuery.parsedQuery.query.field.$in[1] instanceof RegExp, 'Invalid parsed $in RegExp query');
done();
});

it('should parse a raw query with int', function (done) {
const mongoQuery = new MongoQuery({$rawQuery: {'field1.field2': {$float: '1.2'}}});

Expand Down

0 comments on commit 27d3638

Please sign in to comment.