-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
111 lines (88 loc) · 2.86 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
100
101
102
103
104
105
106
107
108
109
110
111
let Redis = require('ioredis')
let camelCase = require('camelcase')
let _ = require('lodash')
class RedisGraph extends Redis {
constructor(graphName, ...args) {
if (typeof graphName === 'object') {
super(graphName)
this.graphName = graphName.graphName
} else {
super(...args)
this.graphName = graphName
}
if (!this.graphName || this.graphName.length < 1) {
throw new Error("Must specify a graph name in constructor")
}
// A single query returns an array with up to 3 elements:
// - the column names for each result
// - an array of result objects
// - some meta information about the query
// A single result can be a node, relation, or scalar in the case of something like (id(node))
Redis.Command.setReplyTransformer('GRAPH.QUERY', function (result) {
let metaInformation = parseMetaInformation(result.pop())
let parsedResults = []
parsedResults.meta = metaInformation
if (result.length > 1) { // if there are results to parse
let columnHeaders = result[0]
let resultSet = result[1]
parsedResults = resultSet.map((result) => {
return parseResult(columnHeaders, result)
})
}
return parsedResults
})
}
query (command) {
return this.call('GRAPH.QUERY', this.graphName, `${command}`)
}
delete () {
return this.call('GRAPH.DELETE', this.graphName)
}
explain (command) {
return this.call('GRAPH.EXPLAIN', this.graphName, `${command}`)
}
}
function parseMetaInformation (array) {
meta = {}
for (prop of array) {
let [name, value] = prop.split(': ')
if(value) {
value = value.trim()
name = camelCase(name)
meta[name] = value
}
}
return meta
}
// a single result will consist of an array with one element for each returned object in the original QUERY
// for example: "... RETURN n, l, p" <- will return multiple rows/records, each of which will have n, l, and p.
function parseResult (columnHeaders, singleResult) {
columns = columnHeaders.map((columnHeader, index) => {
let name = columnHeader
let value = singleResult[index]
if (Array.isArray(value)) {
value = _.fromPairs(value)
}
if (value.properties) {
_.defaults(value, _.fromPairs(value.properties))
delete value.properties
}
try{
return [name, JSON.parse(value)]
}catch(error){
return [name, value]
}
})
return _.fromPairs(columns)
}
// add methods to Pipeline
Redis.Pipeline.prototype.query = function (command) {
return this.call('GRAPH.QUERY', this.redis.graphName, `${command}`)
}
Redis.Pipeline.prototype.delete = function (command) {
return this.call('GRAPH.DELETE', this.redis.graphName)
}
Redis.Pipeline.prototype.explain = function (command) {
return this.call('GRAPH.EXPLAIN', this.redis.graphName, `${command}`)
}
module.exports = RedisGraph