Skip to content

Commit 27d3638

Browse files
authored
2.3.3 feat: add regex expression to raw query (#60)
2 parents ccb70a1 + 6460543 commit 27d3638

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

lib/MongoQuery.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class MongoQuery {
8282
for (const key in obj) {
8383
if (!obj.hasOwnProperty(key)) continue;
8484

85-
if ($check.object(obj[key]) && !utils.isValidId(obj[key])) {
85+
if ($check.object(obj[key]) && !utils.isValidId(obj[key]) && key !== '$regularExpression') {
8686
mergeRawQueryRecursive(obj[key], obj, key);
8787
} else if ($check.array(obj[key])) {
8888
for (let i = 0; i < obj[key].length; i++) {
@@ -114,6 +114,8 @@ class MongoQuery {
114114
parent[parentKey] = queryVal;
115115
} else if (key === '$string') {
116116
parent[parentKey] = $check.assigned(obj[key]) && obj[key].toString ? obj[key].toString() : obj[key];
117+
} else if (key === '$regularExpression') {
118+
parent[parentKey] = new RegExp(obj[key].pattern, obj[key].options);
117119
}
118120
}
119121
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synatic/mongo-magic",
3-
"version": "2.3.2",
3+
"version": "2.3.3",
44
"description": "Synatic utility classes for interacting with MongoDB",
55
"main": "index.js",
66
"files": [

test/01. MonogQuery.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ describe('Mongo Query', function () {
147147
done();
148148
});
149149

150+
it('should parse a raw query with regularExpression in \'$in\' array', function (done) {
151+
const mongoQuery = new MongoQuery({$rawQuery: {
152+
field: {$in: [
153+
{$regularExpression: { pattern: ';', options: 'i' }},
154+
{$regularExpression: { pattern: '/', options: 'i' }}
155+
]}}
156+
});
157+
158+
assert(mongoQuery.parsedQuery.query.field.$in[0] instanceof RegExp, 'Invalid parsed $in RegExp query');
159+
assert(mongoQuery.parsedQuery.query.field.$in[1] instanceof RegExp, 'Invalid parsed $in RegExp query');
160+
done();
161+
});
162+
150163
it('should parse a raw query with int', function (done) {
151164
const mongoQuery = new MongoQuery({$rawQuery: {'field1.field2': {$float: '1.2'}}});
152165

0 commit comments

Comments
 (0)