Skip to content

Commit 91f7f03

Browse files
committed
added basic support for regex
1 parent 7586303 commit 91f7f03

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The first parameter is the name of your jsonb column in your postgres table. The
4141
* $or, $not
4242
* [$in](https://docs.mongodb.org/manual/reference/operator/query/in/#use-the-in-operator-to-match-values-in-an-array), $nin
4343
* $elemMatch
44+
* [$regex](https://docs.mongodb.com/manual/reference/operator/query/regex/)
45+
4446

4547
## Todo
4648
* $nor

index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var pathToText = function(path, isString) {
2020
return text;
2121
}
2222

23+
// These are the simple operators.
2324
var ops = {
2425
$eq: '=',
2526
$gt: '>',
@@ -30,7 +31,7 @@ var ops = {
3031
}
3132

3233
var otherOps = {
33-
$in: true, $nin: true, $not: true, $or: true, $and: true, $elemMatch: true
34+
$in: true, $nin: true, $not: true, $or: true, $and: true, $elemMatch: true, $regex: true
3435
}
3536

3637
var convert = function (path, query) {
@@ -54,6 +55,12 @@ var convert = function (path, query) {
5455
return '('+query[key].map(function(subquery) {return convert(path, subquery) })
5556
.join(key == '$or' ? ' OR ' : ' AND ')+')';
5657
}
58+
} else if (key == '$regex') {
59+
var op = '~';
60+
if (query['$options'] && query['$options'].includes('i')) {
61+
op += '*';
62+
}
63+
return pathToText(path, true) + ' ' + op + ' \'' + stringEscape(value) + '\'';
5764
} else if (key == '$elemMatch') {
5865
return pathToText(path, false) + ' @> \'' + stringEscape(JSON.stringify(query[key])) + '\'::jsonb';
5966
} else if (key == '$in' || key == '$nin') {

package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
{
22
"name": "mongo-query-to-postgres-jsonb",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Converts MongoDB queries to postgresql queries for jsonb fields.",
55
"main": "index.js",
66
"directories": {
77
"test": "test"
88
},
9-
"dependencies": {},
9+
"dependencies": {
10+
"chai": "^3.5.0",
11+
"mocha": "^3.2.0"
12+
},
1013
"devDependencies": {},
11-
"repository" : {
12-
"type" : "git",
13-
"url" : "https://github.com/thomas4019/mongo-query-to-postgres-jsonb.git"
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/thomas4019/mongo-query-to-postgres-jsonb.git"
1417
},
1518
"scripts": {
1619
"test": "mocha"

test/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ describe('comparision operators', function() {
103103
});
104104
});
105105

106+
describe('regular expressions', function() {
107+
it('basic', function () {
108+
assert.equal("data->>'type' ~ 'food'", convert('data', { type: { $regex : 'food' } }));
109+
});
110+
it('case insensitive', function () {
111+
assert.equal("data->>'type' ~* 'food'", convert('data', { type: { $regex : 'food', $options: 'i' } }));
112+
});
113+
});
114+
106115
describe('combined tests', function () {
107116
it('should handle ANDs and ORs together', function() {
108117
assert.equal('(data->>\'type\'=\'food\' and (data->\'qty\'>\'100\'::jsonb OR data->\'price\'<\'9.95\'::jsonb))', convert('data', {

0 commit comments

Comments
 (0)