forked from prabushitha/gremlin-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-server.js
83 lines (61 loc) · 2.26 KB
/
proxy-server.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
const express = require('express');
const bodyParser = require('body-parser');
const gremlin = require('gremlin');
const cors = require('cors');
const app = express();
const port = 3001;
app.use(cors({ credentials: true, }));
app.use(bodyParser.json());
const dataProcessingFunction = (obj) => {
if (obj instanceof Map) return dataProcessingFunction(Object.fromEntries(obj))
if (Array.isArray(obj)) {
const newObj = obj.map((el) => el instanceof Map ? dataProcessingFunction(Object.fromEntries(el)) : el)
if (newObj[0] instanceof Map) {
return dataProcessingFunction(newObj)
}
return newObj
} else if (typeof obj === 'object') {
for (let prop in obj) {
if (obj[prop][0] instanceof Map) {
obj[prop] = dataProcessingFunction(obj[prop][0])
}
else if (obj[prop] instanceof Map) {
obj[prop] = dataProcessingFunction(obj[prop])
}
else if (Array.isArray(obj[prop])) {
obj[prop] = obj[prop].map((el) => dataProcessingFunction(el))
}
}
}
return obj
}
const nodesToJson = (nodeList) => dataProcessingFunction(nodeList)
function makeQuery(query, nodeLimit) {
const nodeLimitQuery = !isNaN(nodeLimit) && Number(nodeLimit) > 0 ? `.limit(${nodeLimit})` : '';
return `${query}${nodeLimitQuery}.dedup().as('node')
.project('id', 'label', 'properties', 'edges')
.by(__.id())
.by(__.label())
.by(properties().group().by(key).by(union(__.value(),__.valueMap()).fold()).fold())
.by(__.outE()
.project('id', 'from', 'to', 'label', 'properties')
.by(__.id())
.by(__.select('node').id())
.by(__.inV().id())
.by(__.label())
.by(__.valueMap()
.by(__.unfold())
)
.fold())`;
}
app.post('/query', (req, res, next) => {
const gremlinHost = req.body.host;
const gremlinPort = req.body.port;
const nodeLimit = req.body.nodeLimit;
const query = req.body.query;
const client = new gremlin.driver.Client(`ws://${gremlinHost}:${gremlinPort}/gremlin`, { traversalSource: `${query.split('.')[0]}`, mimeType: 'application/json' });
client.submit(makeQuery(query, nodeLimit), {})
.then((result) => res.send(nodesToJson(result._items)))
.catch((err) => next(err));
});
app.listen(port, () => console.log(`Simple gremlin-proxy server listening on port ${port}!`));