forked from prabushitha/gremlin-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-server.js
67 lines (54 loc) · 1.99 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
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,
}));
// parse application/json
app.use(bodyParser.json());
function mapToObj(inputMap) {
let obj = {};
inputMap.forEach((value, key) => {
obj[key] = value
});
return obj;
}
function edgesToJson(edgeList) {
return edgeList.map(
edge => ({
id: typeof edge.get('id') !== "string" ? JSON.stringify(edge.get('id')) : edge.get('id'),
from: edge.get('from'),
to: edge.get('to'),
label: edge.get('label'),
properties: mapToObj(edge.get('properties')),
})
);
}
function nodesToJson(nodeList) {
return nodeList.map(
node => ({
id: node.get('id'),
label: node.get('label'),
properties: mapToObj(node.get('properties')),
edges: edgesToJson(node.get('edges'))
})
);
}
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(__.valueMap().by(__.unfold())).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: 'g', 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}!`));