forked from thomas4019/mongo-query-to-postgres-jsonb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (37 loc) · 927 Bytes
/
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
var quote = function(data) {
if (typeof data == 'string')
return "'" + data + "'";
return "'"+JSON.stringify(data)+"'"
}
var convertWrapper = function (prefix, query) {
return convert([prefix], query);
}
var convert = function (prefix, query) {
if (typeof query == 'object' && !Array.isArray(query)) {
str = ''
for (key in query) {
if (str != '') {
str += ' and ';
}
if (key == '$or') {
str += '('+query[key].map(function(subquery) {return convert(prefix, subquery) })
.join(' OR ')+')';
} else {
//key = key.replace(/\\./g, '->');
if (typeof query[key] == 'object') {
str += convert(prefix+'->'+ "'"+key+"'", query[key]);
} else {
str += '('+convert(prefix+'->>'+ "'"+key+"'", query[key])+')';
}
}
}
return str
} else {
if (Array.isArray(query)) {
return
} else {
return prefix + '=' + quote(query);
}
}
}
module.exports = convertWrapper;