Skip to content

Commit 37424ce

Browse files
committed
close #43. Changed graph creation and docs.
1 parent 244a5bd commit 37424ce

File tree

3 files changed

+122
-11
lines changed

3 files changed

+122
-11
lines changed

Diff for: examples/ConnectionConfigurationExample.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Executed with:
2+
redis-server --loadmodule path_to_redisgraph.so --port 8080
3+
4+
In Redis-CLI:
5+
CONFIG SET requirepass "1234"
6+
*/
7+
8+
const RedisGraph = require("redisgraph.js").Graph;
9+
let graph = new RedisGraph("social", {port:8080, options:{password:1234}});
10+
11+
try {
12+
(async () => {
13+
await graph.query("CREATE (:person{name:'roi',age:32})");
14+
await graph.query("CREATE (:person{name:'amit',age:30})");
15+
await graph.query(
16+
"MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"
17+
);
18+
19+
// Match query.
20+
let res = await graph.query(
21+
"MATCH (a:person)-[:knows]->(:person) RETURN a.name"
22+
);
23+
while (res.hasNext()) {
24+
let record = res.next();
25+
console.log(record.get("a.name"));
26+
}
27+
console.log(res.getStatistics().queryExecutionTime());
28+
29+
// Match with parameters.
30+
let param = { age: 30 };
31+
res = await graph.query("MATCH (a {age: $age}) return a.name", param);
32+
while (res.hasNext()) {
33+
let record = res.next();
34+
console.log(record.get("a.name"));
35+
}
36+
37+
// Named paths matching.
38+
res = await graph.query(
39+
"MATCH p = (a:person)-[:knows]->(:person) RETURN p"
40+
);
41+
while (res.hasNext()) {
42+
let record = res.next();
43+
// See path.js for more path API.
44+
console.log(record.get("p").nodeCount);
45+
graph.deleteGraph();
46+
process.exit();
47+
}
48+
})();
49+
} catch (err) {
50+
console.log(err);
51+
}

Diff for: examples/ConnectionFromNodeRedisExample.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Executed with:
2+
redis-server --loadmodule path_to_redisgraph.so --port 8080
3+
4+
In Redis-CLI:
5+
CONFIG SET requirepass "1234"
6+
*/
7+
8+
const RedisGraph = require("redisgraph.js").Graph;
9+
const redis = require("redis")
10+
11+
let options = {
12+
port:8080,
13+
password:1234
14+
};
15+
let rc = redis.createClient(options)
16+
let graph = new RedisGraph("social", {host:rc});
17+
18+
try {
19+
(async () => {
20+
await graph.query("CREATE (:person{name:'roi',age:32})");
21+
await graph.query("CREATE (:person{name:'amit',age:30})");
22+
await graph.query(
23+
"MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"
24+
);
25+
26+
// Match query.
27+
let res = await graph.query(
28+
"MATCH (a:person)-[:knows]->(:person) RETURN a.name"
29+
);
30+
while (res.hasNext()) {
31+
let record = res.next();
32+
console.log(record.get("a.name"));
33+
}
34+
console.log(res.getStatistics().queryExecutionTime());
35+
36+
// Match with parameters.
37+
let param = { age: 30 };
38+
res = await graph.query("MATCH (a {age: $age}) return a.name", param);
39+
while (res.hasNext()) {
40+
let record = res.next();
41+
console.log(record.get("a.name"));
42+
}
43+
44+
// Named paths matching.
45+
res = await graph.query(
46+
"MATCH p = (a:person)-[:knows]->(:person) RETURN p"
47+
);
48+
while (res.hasNext()) {
49+
let record = res.next();
50+
// See path.js for more path API.
51+
console.log(record.get("p").nodeCount);
52+
graph.deleteGraph();
53+
process.exit();
54+
}
55+
})();
56+
} catch (err) {
57+
console.log(err);
58+
}

Diff for: src/graph.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ const redis = require("redis"),
77
* RedisGraph client
88
*/
99
class Graph {
10-
/**
11-
* Creates a client to a specific graph running on the specific host/post
12-
* See: node_redis for more options on createClient
13-
*
14-
* @param {string} graphId the graph id
15-
* @param {string | RedisClient} [host] Redis host or node_redis client
16-
* @param {string} [port] Redis port
17-
* @param {ClientOpts} [options] node_redis options
18-
*/
19-
constructor(graphId, host, port, options) {
10+
/**
11+
* Creates a client to a specific graph running on the specific host/post
12+
* See: node_redis for more options on createClient
13+
*
14+
* @param {string} graphId the graph id
15+
* @param {object} [configuration] - a map of:
16+
* @param {string | RedisClient} [configuration.host] Redis host or node_redis client
17+
* @param {string | int} [configuration.port] Redis port
18+
* @param {ClientOpts} [configuration.options] node_redis options
19+
*/
20+
constructor(graphId, {host, port, options} ={}) {
21+
console.log("host = " + host + " port = " + port + " options = "+ options);
2022
this._graphId = graphId; // Graph ID
2123
this._labels = []; // List of node labels.
2224
this._relationshipTypes = []; // List of relation types.
@@ -29,7 +31,7 @@ class Graph {
2931
let client =
3032
host instanceof redis.RedisClient
3133
? host
32-
: redis.createClient.apply(redis, [].slice.call(arguments, 1));
34+
: redis.createClient(port, host, options);
3335
this._sendCommand = util.promisify(client.send_command).bind(client);
3436
}
3537

0 commit comments

Comments
 (0)