Skip to content

Commit 17725ca

Browse files
authored
Merge pull request #14 from RedisGraph/new-resultset-struct
parse new result-set struct
2 parents 0538255 + a93665f commit 17725ca

File tree

10 files changed

+575
-144
lines changed

10 files changed

+575
-144
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
docker:
99
- image: circleci/node:10.15
1010

11-
- image: redislabs/redisgraph:edge
11+
- image: redislabs/redisgraph:2.0-edge
1212
port: 6379:6379
1313

1414
working_directory: ~/repo

.gitignore

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/.settings/
2-
/node_modules/
1+
.settings/
2+
node_modules/
3+
.vscode/
34
.project
45
package-lock.json
56
yarn.lock
6-
/examples/node_modules
7-
/coverage/
7+
examples/node_modules
8+
coverage/
9+
.DS_Store

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const Record = require("./src/record"),
2-
RedisGraph = require("./src/redisGraph"),
2+
Graph = require("./src/graph"),
33
ResultSet = require("./src/resultSet"),
44
Statistics = require("./src/statistics"),
55
Label = require("./src/label");
66

77
module.exports = {
88
Record: Record,
9-
RedisGraph: RedisGraph,
9+
Graph: Graph,
1010
ResultSet: ResultSet,
1111
Statistics: Statistics,
1212
Label: Label

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redisgraph.js",
3-
"version": "1.1.4",
3+
"version": "2.0.0",
44
"description": "Connect to RedisGraph 1.0.0 and up from JavaScript",
55
"author": "RedisLabs",
66
"license": "BSD 3",

src/edge.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* An edge connecting two nodes.
3+
*/
4+
class Edge {
5+
constructor(srcNode, relation, destNode, properties) {
6+
this.id = undefined; //edge's id - set by RedisGraph
7+
this.relation = relation; //edge's relationship type
8+
this.srcNode = srcNode; //edge's source node
9+
this.destNode = destNode; //edge's destinatio node
10+
this.properties = properties; //edge's list of properties (list of Key:Value)
11+
}
12+
13+
setId(id) {
14+
this.id = id;
15+
}
16+
toString() {
17+
return JSON.stringify(this);
18+
}
19+
}
20+
21+
module.exports = Edge;

src/graph.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
const redis = require("redis"),
2+
util = require("util"),
3+
ResultSet = require("./resultSet");
4+
5+
/**
6+
* RedisGraph client
7+
*/
8+
class Graph {
9+
/**
10+
* Creates a client to a specific graph running on the specific host/post
11+
* See: node_redis for more options on createClient
12+
*
13+
* @param graphId the graph id
14+
* @param host Redis host or node_redis client
15+
* @param port Redis port
16+
* @param options node_redis options
17+
*/
18+
constructor(graphId, host, port, options) {
19+
this._graphId = graphId; // Graph ID
20+
this._labels = []; // List of node labels.
21+
this._relationshipTypes = []; // List of relation types.
22+
this._properties = []; // List of properties.
23+
24+
this._labelsPromise = undefined; // used as a synchronization mechanizom for labels retrival
25+
this._propertyPromise = undefined; // used as a synchronization mechanizom for property names retrival
26+
this._relationshipPromise = undefined; // used as a synchronization mechanizom for relationship types retrival
27+
28+
let client =
29+
host instanceof redis.RedisClient
30+
? host
31+
: redis.createClient.apply(redis, [].slice.call(arguments, 1));
32+
this._sendCommand = util.promisify(client.send_command).bind(client);
33+
}
34+
35+
/**
36+
* auxilary function to extract string(s) data from procedures such as:
37+
* db.labels, db.propertyKeys and db.relationshipTypes
38+
* @param resultSet - a procedure result set
39+
*/
40+
_extractStrings(resultSet) {
41+
var strings = [];
42+
while (resultSet.hasNext()) {
43+
strings.push(resultSet.next().getString(0));
44+
}
45+
return strings;
46+
}
47+
48+
/**
49+
* Execute a Cypher query (async)
50+
*
51+
* @param query Cypher query
52+
* @return a promise contains a result set
53+
*/
54+
async query(query) {
55+
var res = await this._sendCommand("graph.QUERY", [this._graphId, query, "--compact"]);
56+
var resultSet = new ResultSet(this);
57+
return resultSet.parseResponse(res);
58+
}
59+
60+
/**
61+
* Deletes the entire graph (async)
62+
*
63+
* @return a promise contains the delete operation running time statistics
64+
*/
65+
async deleteGraph() {
66+
var res = await this._sendCommand("graph.DELETE", [this._graphId]);
67+
//clear internal graph state
68+
this._labels = [];
69+
this._relationshipTypes = [];
70+
this._properties = [];
71+
var resultSet = new ResultSet(this);
72+
return resultSet.parseResponse(res);
73+
}
74+
75+
/**
76+
* Calls procedure
77+
*
78+
* @param procedure Procedure to call
79+
* @param args Arguments to pass
80+
* @param y Yield outputs
81+
* @return a promise contains the procedure result set data
82+
*/
83+
callProcedure(procedure, args = new Array(), y = new Array()) {
84+
let q = "CALL " + procedure + "(" + args.join(',') + ")" + y.join(' ');
85+
return this.query(q);
86+
}
87+
88+
/**
89+
* Retrieves all labels in graph.
90+
*/
91+
async labels() {
92+
if (this._labelsPromise == undefined) {
93+
this._labelsPromise = this.callProcedure("db.labels").then(response => {
94+
return this._extractStrings(response);
95+
})
96+
this._labels = await (this._labelsPromise);
97+
this._labelsPromise = undefined;
98+
}
99+
else {
100+
await this._labelsPromise;
101+
}
102+
}
103+
104+
/**
105+
* Retrieves all relationship types in graph.
106+
*/
107+
async relationshipTypes() {
108+
if (this._relationshipPromise == undefined) {
109+
this._relationshipPromise = this.callProcedure("db.relationshipTypes").then(response => {
110+
return this._extractStrings(response);
111+
});
112+
this._relationshipTypes = await (this._relationshipPromise);
113+
this._relationshipPromise = undefined;
114+
}
115+
else {
116+
await this._relationshipPromise;
117+
}
118+
}
119+
120+
/**
121+
* Retrieves all properties in graph.
122+
*/
123+
async propertyKeys() {
124+
if (this._propertyPromise == undefined) {
125+
this._propertyPromise = this.callProcedure("db.propertyKeys").then(response => {
126+
return this._extractStrings(response);
127+
})
128+
this._properties = await this._propertyPromise;
129+
this._propertyPromise = undefined;
130+
}
131+
else{
132+
await this._propertyPromise;
133+
}
134+
135+
136+
}
137+
138+
/**
139+
* Retrieves label by ID.
140+
*
141+
* @param id internal ID of label.
142+
* @return String label.
143+
*/
144+
getLabel(id) {
145+
return this._labels[id];
146+
}
147+
148+
/**
149+
* Retrive all the labels from the graph and returns the wanted label
150+
* @param id internal ID of label.
151+
* @return String label.
152+
*/
153+
async fetchAndGetLabel(id) {
154+
await this.labels();
155+
return this._labels[id];
156+
}
157+
158+
/**
159+
* Retrieves relationship type by ID.
160+
*
161+
* @param id internal ID of relationship type.
162+
* @return String relationship type.
163+
*/
164+
getRelationship(id) {
165+
return this._relationshipTypes[id];
166+
}
167+
168+
/**
169+
* Retrives al the relationshipe types from the graph, and returns the wanted type
170+
* @param id internal ID of relationship type.
171+
* @return String relationship type.
172+
*/
173+
async fetchAndGetRelationship(id) {
174+
await this.relationshipTypes();
175+
return this._relationshipTypes[id];
176+
}
177+
178+
/**
179+
* Retrieves property name by ID.
180+
*
181+
* @param id internal ID of property.
182+
* @return String property.
183+
*/
184+
getProperty(id) {
185+
return this._properties[id];
186+
}
187+
188+
/**
189+
* Retrives al the properties from the graph, and returns the wanted property
190+
* @param id internal ID of property.
191+
* @return String property.
192+
*/
193+
async fetchAndGetProperty(id) {
194+
await this.propertyKeys();
195+
return this._properties[id];
196+
}
197+
}
198+
199+
module.exports = Graph;

src/node.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* A node within the garph.
3+
*/
4+
class Node {
5+
constructor(label, properties) {
6+
this.id = undefined; //node's id - set by RedisGraph
7+
this.label = label; //node's label
8+
this.properties = properties; //node's list of properties (list of Key:Value)
9+
}
10+
11+
setId(id){
12+
this.id = id;
13+
}
14+
15+
toString() {
16+
return JSON.stringify(this);
17+
}
18+
}
19+
20+
module.exports = Node;

src/redisGraph.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)