forked from thomas4019/mongo-query-to-postgres-jsonb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (91 loc) · 2.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var quote = function(data) {
if (typeof data == 'string')
return "'" + stringEscape(data) + "'";
return "'"+JSON.stringify(data)+"'::jsonb"
}
var stringEscape = function(str) {
return str.replace(/'/g, "''")
}
var pathToText = function(path, isString) {
var text = stringEscape(path[0]);
for (var i = 1; i < path.length; i++) {
text += (i == path.length-1 && isString ? '->>' : '->');
if (/\d+/.test(path[i]))
text += path[i]; //don't wrap numbers in quotes
else
text += '\'' + stringEscape(path[i]) + '\'';
}
return text;
}
var convertDotNotation = function(path, pathDotNotation) {
return pathToText([path].concat(pathDotNotation.split('.')), true);
}
// These are the simple operators.
var ops = {
$eq: '=',
$gt: '>',
$gte: '>=',
$lt: '<',
$lte: '<=',
$ne: '!=',
}
var otherOps = {
$in: true, $nin: true, $not: true, $or: true, $and: true, $elemMatch: true, $regex: true
}
var convert = function (path, query) {
if (typeof query == 'object' && !Array.isArray(query)) {
var specialKeys = Object.keys(query)
if (path.length > 1) {
specialKeys = specialKeys.filter(function(key) {
return key in ops || key in otherOps;
})
}
if (specialKeys.length > 0) {
var conditions = specialKeys.map(function(key) {
var value = query[key]
if (key == '$not') {
return '(NOT ' + convert(path, query[key]) + ')';
} else if (key == '$or' || key == '$and') {
if (query[key].length == 0) {
return key == '$or' ? 'FALSE' : 'TRUE'
} else {
return '('+query[key].map(function(subquery) {return convert(path, subquery) })
.join(key == '$or' ? ' OR ' : ' AND ')+')';
}
} else if (key == '$regex') {
var op = '~';
if (query['$options'] && query['$options'].includes('i')) {
op += '*';
}
return pathToText(path, true) + ' ' + op + ' \'' + stringEscape(value) + '\'';
} else if (key == '$elemMatch') {
return pathToText(path, false) + ' @> \'' + stringEscape(JSON.stringify(query[key])) + '\'::jsonb';
} else if (key == '$in' || key == '$nin') {
return pathToText(path, typeof query[key][0] == 'string') + (key == '$nin' ? ' NOT' : '') + ' IN (' + query[key].map(quote).join(', ') + ')';
} else if (Object.keys(ops).indexOf(key) !== -1) {
var text = pathToText(path, typeof query[key] == 'string')
return text + ops[key] + quote(query[key])
} else {
return convert(path.concat(key.split('.')), query[key]);
}
}).join(' and ');
if (specialKeys.length == 1)
return conditions;
else
return '(' + conditions + ')'
} else {
if (path.length == 1) {
return 'TRUE';
}
var text = pathToText(path, typeof query == 'string')
return text + '=' + quote(query);
}
} else {
var text = pathToText(path, typeof query == 'string')
return text + '=' + quote(query);
}
}
module.exports = function (fieldName, query) {
return convert([fieldName], query);
};
module.exports.convertDotNotation = convertDotNotation