Skip to content

Commit 02418b6

Browse files
DvirDukhangkorland
authored andcommitted
added path support (#25)
* added path support
1 parent 4eda4c4 commit 02418b6

File tree

13 files changed

+229
-47
lines changed

13 files changed

+229
-47
lines changed

README.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,36 @@ npm install redisgraph.js
3030
# Example: Using the JavaScript Client
3131

3232
```javascript
33-
const RedisGraph = require("redisgraph.js").RedisGraph;
34-
35-
let graph = new RedisGraph('social');
36-
graph
37-
.query("CREATE (:person{name:'roi',age:32})")
38-
.then( () => {
39-
return graph.query("CREATE (:person{name:'amit',age:30})");
40-
})
41-
.then( () => {
42-
return graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)")
43-
})
44-
.then( () => {
45-
return graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a")
46-
})
47-
.then( (res) => {
48-
while(res.hasNext()){
49-
let record = res.next();
50-
console.log(record.getString('a.name'));
51-
}
52-
console.log(res.getStatistics().queryExecutionTime());
53-
});
33+
const RedisGraph = require("redisgraph.js").Graph;
34+
35+
let graph = new RedisGraph("social");
36+
37+
graph.query("CREATE (:person{name:'roi',age:32})").then(() => {
38+
graph.query("CREATE (:person{name:'amit',age:30})").then(()=>{
39+
graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)").then(()=>{
40+
graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a.name").then(res=>{
41+
while (res.hasNext()) {
42+
let record = res.next();
43+
console.log(record.get("a.name"));
44+
}
45+
console.log(res.getStatistics().queryExecutionTime());
46+
graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{
47+
while (res.hasNext()) {
48+
let record = res.next();
49+
// See path.js for more path API.
50+
console.log(record.get("p").nodeCount);
51+
graph.deleteGraph();
52+
process.exit();
53+
}
54+
})
55+
})
56+
})
57+
})
58+
})
59+
.catch(err => {
60+
console.log(err);
61+
});
62+
5463

5564
```
5665

examples/redisGraphExample.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
const RedisGraph = require("redisgraph.js").RedisGraph;
1+
const RedisGraph = require("redisgraph.js").Graph;
22

33
let graph = new RedisGraph("social");
44

5-
graph
6-
.query("CREATE (:person{name:'roi',age:32})")
7-
.then(() => {
8-
return graph.query("CREATE (:person{name:'amit',age:30})");
9-
})
10-
.then(() => {
11-
return graph.query(
12-
"MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)"
13-
);
14-
})
15-
.then(() => {
16-
return graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a");
17-
})
18-
.then(res => {
19-
while (res.hasNext()) {
20-
let record = res.next();
21-
console.log(record.getString("a.name"));
22-
}
23-
console.log(res.getStatistics().queryExecutionTime());
5+
graph.query("CREATE (:person{name:'roi',age:32})").then(() => {
6+
graph.query("CREATE (:person{name:'amit',age:30})").then(()=>{
7+
graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)").then(()=>{
8+
graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a.name").then(res=>{
9+
while (res.hasNext()) {
10+
let record = res.next();
11+
console.log(record.get("a.name"));
12+
}
13+
console.log(res.getStatistics().queryExecutionTime());
14+
graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{
15+
while (res.hasNext()) {
16+
let record = res.next();
17+
// See path.js for more path API.
18+
console.log(record.get("p").nodeCount);
19+
graph.deleteGraph();
20+
process.exit();
21+
}
22+
})
23+
})
24+
})
25+
})
2426
})
2527
.catch(err => {
2628
console.log(err);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"url": "git://github.com/redislabs/redisgraph.js.git"
1010
},
1111
"dependencies": {
12+
"deep-equal": "^1.1.0",
1213
"redis": "^2.8.0"
1314
},
1415
"devDependencies": {

src/edge.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
/**
23
* An edge connecting two nodes.
34
*/

src/graph.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
const redis = require("redis"),
23
util = require("util"),
34
ResultSet = require("./resultSet");

src/label.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
/**
23
* Different Statistics labels
34
*/

src/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
/**
23
* A node within the garph.
34
*/

src/path.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
class Path {
3+
constructor(nodes, edges){
4+
this.nodes = nodes;
5+
this.edges = edges;
6+
}
7+
8+
get Nodes(){
9+
return this.nodes;
10+
}
11+
12+
get Edges(){
13+
return this.edges;
14+
}
15+
16+
getNode(index){
17+
return this.nodes[index];
18+
}
19+
20+
getEdge(index){
21+
return this.edges[index];
22+
}
23+
24+
get firstNode(){
25+
return this.nodes[0];
26+
}
27+
28+
get lastNode(){
29+
return this.nodes[this.nodes.length -1];
30+
}
31+
32+
get nodeCount(){
33+
return this.nodes.length;
34+
}
35+
36+
get edgeCount(){
37+
return this.edges.length;
38+
}
39+
40+
toString() {
41+
return JSON.stringify(this);
42+
}
43+
44+
}
45+
46+
module.exports = Path;

src/record.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
/**
23
* Hold a query record
34
*/

src/resultSet.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
"use strict";
12
const Statistics = require("./statistics"),
2-
Record = require("./record");
3-
Node = require("./node");
4-
Edge = require("./edge");
5-
ReplyError = require("redis").ReplyError
3+
Record = require("./record"),
4+
Node = require("./node"),
5+
Edge = require("./edge"),
6+
Path = require("./path"),
7+
ReplyError = require("redis").ReplyError;
68

79
const ResultSetColumnTypes = {
810
COLUMN_UNKNOWN: 0,
@@ -20,7 +22,8 @@ const ResultSetValueTypes = {
2022
VALUE_DOUBLE: 5,
2123
VALUE_ARRAY: 6,
2224
VALUE_EDGE: 7,
23-
VALUE_NODE: 8
25+
VALUE_NODE: 8,
26+
VALUE_PATH: 9
2427
}
2528

2629
/**
@@ -190,6 +193,12 @@ class ResultSet {
190193
return rawArray;
191194
}
192195

196+
async parsePath(rawPath) {
197+
let nodes = await this.parseScalar(rawPath[0]);
198+
let edges = await this.parseScalar(rawPath[1]);
199+
return new Path(nodes, edges);
200+
}
201+
193202
async parseScalar(cell) {
194203
let scalar_type = cell[0];
195204
let value = cell[1];
@@ -224,6 +233,9 @@ class ResultSet {
224233
case ResultSetValueTypes.VALUE_EDGE:
225234
scalar = await this.parseEdge(value);
226235
break;
236+
case ResultSetValueTypes.VALUE_PATH:
237+
scalar = await this.parsePath(value);
238+
break;
227239
case ResultSetValueTypes.VALUE_UNKNOWN:
228240
console.log("Unknown scalar type\n");
229241
break;

0 commit comments

Comments
 (0)