Skip to content

Commit 4be87d1

Browse files
author
DvirDukhan
committed
added async/await flow. edited readme
1 parent e588585 commit 4be87d1

File tree

6 files changed

+390
-394
lines changed

6 files changed

+390
-394
lines changed

README.md

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,38 @@ const RedisGraph = require("redisgraph.js").Graph;
3434

3535
let graph = new RedisGraph("social");
3636

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-
let param = {'age': 30};
47-
graph.query("MATCH (a {age: $age}) return a.name", param).then(res=>{
48-
while (res.hasNext()) {
49-
let record = res.next();
50-
console.log(record.get("a.name"));
51-
}
52-
graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{
53-
while (res.hasNext()) {
54-
let record = res.next();
55-
// See path.js for more path API.
56-
console.log(record.get("p").nodeCount);
57-
graph.deleteGraph();
58-
process.exit();
59-
}
60-
})
61-
})
62-
})
63-
})
64-
})
65-
})
66-
.catch(err => {
67-
console.log(err);
68-
});
69-
37+
(async () =>{
38+
await graph.query("CREATE (:person{name:'roi',age:32})");
39+
await graph.query("CREATE (:person{name:'amit',age:30})");
40+
await graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
41+
42+
// Match query.
43+
let res = await graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a.name");
44+
while (res.hasNext()) {
45+
let record = res.next();
46+
console.log(record.get("a.name"));
47+
}
48+
console.log(res.getStatistics().queryExecutionTime());
49+
50+
// Match with parameters.
51+
let param = {'age': 30};
52+
res = await graph.query("MATCH (a {age: $age}) return a.name", param);
53+
while (res.hasNext()) {
54+
let record = res.next();
55+
console.log(record.get("a.name"));
56+
}
57+
58+
// Named paths matching.
59+
res = await graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p");
60+
while (res.hasNext()) {
61+
let record = res.next();
62+
// See path.js for more path API.
63+
console.log(record.get("p").nodeCount);
64+
graph.deleteGraph();
65+
process.exit();
66+
}
67+
68+
})();
7069

7170
```
7271

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"url": "git://github.com/redislabs/redisgraph.js.git"
1010
},
1111
"dependencies": {
12-
"redisgraph.js": "^1.2.0"
12+
"redisgraph.js": "github:RedisGraph/redisgraph.js"
1313
},
1414
"main": "redisGraphExample.js"
1515
}

examples/redisGraphExample.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,40 @@ const RedisGraph = require("redisgraph.js").Graph;
22

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

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-
let param = {'age': 30};
15-
graph.query("MATCH (a {age: $age}) return a.name", param).then(res=>{
16-
while (res.hasNext()) {
17-
let record = res.next();
18-
console.log(record.get("a.name"));
19-
}
20-
graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{
21-
while (res.hasNext()) {
22-
let record = res.next();
23-
// See path.js for more path API.
24-
console.log(record.get("p").nodeCount);
25-
graph.deleteGraph();
26-
process.exit();
27-
}
28-
})
29-
})
30-
})
31-
})
32-
})
33-
})
34-
.catch(err => {
35-
console.log(err);
36-
});
5+
try {
6+
(async () =>{
7+
await graph.query("CREATE (:person{name:'roi',age:32})");
8+
await graph.query("CREATE (:person{name:'amit',age:30})");
9+
await graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
10+
11+
// Match query.
12+
let res = await graph.query("MATCH (a:person)-[:knows]->(:person) RETURN a.name");
13+
while (res.hasNext()) {
14+
let record = res.next();
15+
console.log(record.get("a.name"));
16+
}
17+
console.log(res.getStatistics().queryExecutionTime());
18+
19+
// Match with parameters.
20+
let param = {'age': 30};
21+
res = await graph.query("MATCH (a {age: $age}) return a.name", param);
22+
while (res.hasNext()) {
23+
let record = res.next();
24+
console.log(record.get("a.name"));
25+
}
26+
27+
// Named paths matching.
28+
res = await graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p");
29+
while (res.hasNext()) {
30+
let record = res.next();
31+
// See path.js for more path API.
32+
console.log(record.get("p").nodeCount);
33+
graph.deleteGraph();
34+
process.exit();
35+
}
36+
37+
})();
38+
}
39+
catch(err){
40+
console.log(err);
41+
}

src/graph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Graph {
4848

4949
paramToString(paramValue) {
5050
if(paramValue == null)
51-
return "null"
51+
return "null";
5252
let paramType = typeof(paramValue);
5353
if(paramType == "string") {
5454
let strValue = "";

src/resultSet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class ResultSet {
118118

119119
async parseEntityProperties(props) {
120120
// [[name, value, value type] X N]
121-
let properties = {}
121+
let properties = {};
122122
for (var i = 0; i < props.length; i++) {
123123
let prop = props[i];
124124
var propIndex = prop[0];
@@ -172,7 +172,7 @@ class ResultSet {
172172
// will try to get the right relationship type for at most 10 times
173173
var tries = 0;
174174
while (relation == undefined && tries < 10) {
175-
relation = await this._graph.fetchAndGetRelationship(cell[1])
175+
relation = await this._graph.fetchAndGetRelationship(cell[1]);
176176
tries++;
177177
}
178178
if (relation == undefined) {

0 commit comments

Comments
 (0)