From 1d1a4962f41e3076ef1db1d92a04df372c324845 Mon Sep 17 00:00:00 2001 From: DvirDukhan Date: Thu, 27 Feb 2020 15:03:19 +0200 Subject: [PATCH 1/4] close #43. Changed graph creation and docs. --- examples/ConnectionConfigurationExample.js | 51 +++++++++++++++++++ examples/ConnectionFromNodeRedisExample.js | 58 ++++++++++++++++++++++ src/graph.js | 24 +++++---- 3 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 examples/ConnectionConfigurationExample.js create mode 100644 examples/ConnectionFromNodeRedisExample.js diff --git a/examples/ConnectionConfigurationExample.js b/examples/ConnectionConfigurationExample.js new file mode 100644 index 0000000000..6eeef4ab8f --- /dev/null +++ b/examples/ConnectionConfigurationExample.js @@ -0,0 +1,51 @@ +/* Executed with: +redis-server --loadmodule path_to_redisgraph.so --port 8080 + +In Redis-CLI: +CONFIG SET requirepass "1234" +*/ + +const RedisGraph = require("redisgraph.js").Graph; +let graph = new RedisGraph("social", {port:8080, options:{password:1234}}); + +try { + (async () => { + await graph.query("CREATE (:person{name:'roi',age:32})"); + await graph.query("CREATE (:person{name:'amit',age:30})"); + await graph.query( + "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" + ); + + // Match query. + let res = await graph.query( + "MATCH (a:person)-[:knows]->(:person) RETURN a.name" + ); + while (res.hasNext()) { + let record = res.next(); + console.log(record.get("a.name")); + } + console.log(res.getStatistics().queryExecutionTime()); + + // Match with parameters. + let param = { age: 30 }; + res = await graph.query("MATCH (a {age: $age}) return a.name", param); + while (res.hasNext()) { + let record = res.next(); + console.log(record.get("a.name")); + } + + // Named paths matching. + res = await graph.query( + "MATCH p = (a:person)-[:knows]->(:person) RETURN p" + ); + while (res.hasNext()) { + let record = res.next(); + // See path.js for more path API. + console.log(record.get("p").nodeCount); + graph.deleteGraph(); + process.exit(); + } + })(); +} catch (err) { + console.log(err); +} diff --git a/examples/ConnectionFromNodeRedisExample.js b/examples/ConnectionFromNodeRedisExample.js new file mode 100644 index 0000000000..96df2401b5 --- /dev/null +++ b/examples/ConnectionFromNodeRedisExample.js @@ -0,0 +1,58 @@ +/* Executed with: +redis-server --loadmodule path_to_redisgraph.so --port 8080 + +In Redis-CLI: +CONFIG SET requirepass "1234" +*/ + +const RedisGraph = require("redisgraph.js").Graph; +const redis = require("redis") + +let options = { + port:8080, + password:1234 +}; +let rc = redis.createClient(options) +let graph = new RedisGraph("social", {host:rc}); + +try { + (async () => { + await graph.query("CREATE (:person{name:'roi',age:32})"); + await graph.query("CREATE (:person{name:'amit',age:30})"); + await graph.query( + "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" + ); + + // Match query. + let res = await graph.query( + "MATCH (a:person)-[:knows]->(:person) RETURN a.name" + ); + while (res.hasNext()) { + let record = res.next(); + console.log(record.get("a.name")); + } + console.log(res.getStatistics().queryExecutionTime()); + + // Match with parameters. + let param = { age: 30 }; + res = await graph.query("MATCH (a {age: $age}) return a.name", param); + while (res.hasNext()) { + let record = res.next(); + console.log(record.get("a.name")); + } + + // Named paths matching. + res = await graph.query( + "MATCH p = (a:person)-[:knows]->(:person) RETURN p" + ); + while (res.hasNext()) { + let record = res.next(); + // See path.js for more path API. + console.log(record.get("p").nodeCount); + graph.deleteGraph(); + process.exit(); + } + })(); +} catch (err) { + console.log(err); +} diff --git a/src/graph.js b/src/graph.js index 3a857b9a9e..9091d5e724 100644 --- a/src/graph.js +++ b/src/graph.js @@ -7,16 +7,18 @@ const redis = require("redis"), * RedisGraph client */ class Graph { - /** - * Creates a client to a specific graph running on the specific host/post - * See: node_redis for more options on createClient - * - * @param {string} graphId the graph id - * @param {string | RedisClient} [host] Redis host or node_redis client - * @param {string} [port] Redis port - * @param {ClientOpts} [options] node_redis options - */ - constructor(graphId, host, port, options) { + /** + * Creates a client to a specific graph running on the specific host/post + * See: node_redis for more options on createClient + * + * @param {string} graphId the graph id + * @param {object} [configuration] - a map of: + * @param {string | RedisClient} [configuration.host] Redis host or node_redis client + * @param {string | int} [configuration.port] Redis port + * @param {ClientOpts} [configuration.options] node_redis options + */ + constructor(graphId, {host, port, options} ={}) { + console.log("host = " + host + " port = " + port + " options = "+ options); this._graphId = graphId; // Graph ID this._labels = []; // List of node labels. this._relationshipTypes = []; // List of relation types. @@ -29,7 +31,7 @@ class Graph { let client = host instanceof redis.RedisClient ? host - : redis.createClient.apply(redis, [].slice.call(arguments, 1)); + : redis.createClient(port, host, options); this._sendCommand = util.promisify(client.send_command).bind(client); } From 71179de84e6a709ba4c266ba49e3869aa9320dba Mon Sep 17 00:00:00 2001 From: DvirDukhan Date: Sun, 1 Mar 2020 11:53:57 +0200 Subject: [PATCH 2/4] fixed PR comments --- examples/ConnectionConfigurationExample.js | 2 +- examples/ConnectionFromNodeRedisExample.js | 2 +- src/graph.js | 10 ++++------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/ConnectionConfigurationExample.js b/examples/ConnectionConfigurationExample.js index 6eeef4ab8f..cefbc35c55 100644 --- a/examples/ConnectionConfigurationExample.js +++ b/examples/ConnectionConfigurationExample.js @@ -6,7 +6,7 @@ CONFIG SET requirepass "1234" */ const RedisGraph = require("redisgraph.js").Graph; -let graph = new RedisGraph("social", {port:8080, options:{password:1234}}); +let graph = new RedisGraph("social", "127.0.0.1", 8080, {password:1234}); try { (async () => { diff --git a/examples/ConnectionFromNodeRedisExample.js b/examples/ConnectionFromNodeRedisExample.js index 96df2401b5..bd5ca5b71a 100644 --- a/examples/ConnectionFromNodeRedisExample.js +++ b/examples/ConnectionFromNodeRedisExample.js @@ -13,7 +13,7 @@ let options = { password:1234 }; let rc = redis.createClient(options) -let graph = new RedisGraph("social", {host:rc}); +let graph = new RedisGraph("social", rc); try { (async () => { diff --git a/src/graph.js b/src/graph.js index 9091d5e724..e36c2269bd 100644 --- a/src/graph.js +++ b/src/graph.js @@ -12,13 +12,11 @@ class Graph { * See: node_redis for more options on createClient * * @param {string} graphId the graph id - * @param {object} [configuration] - a map of: - * @param {string | RedisClient} [configuration.host] Redis host or node_redis client - * @param {string | int} [configuration.port] Redis port - * @param {ClientOpts} [configuration.options] node_redis options + * @param {string | RedisClient} [host] Redis host or node_redis client + * @param {string | int} [port] Redis port + * @param {ClientOpts} [options] node_redis options */ - constructor(graphId, {host, port, options} ={}) { - console.log("host = " + host + " port = " + port + " options = "+ options); + constructor(graphId, host, port, options) { this._graphId = graphId; // Graph ID this._labels = []; // List of node labels. this._relationshipTypes = []; // List of relation types. From 9a855a6e3fdbe9a4d0fd89ebcbd81656bf055963 Mon Sep 17 00:00:00 2001 From: DvirDukhan Date: Wed, 4 Mar 2020 15:24:23 +0200 Subject: [PATCH 3/4] fixed PR comments --- examples/ConnectionConfigurationExample.js | 51 ------------------- examples/ConnectionFromNodeRedisExample.js | 58 ---------------------- test/redisGraphAPITest.js | 17 ++++++- 3 files changed, 15 insertions(+), 111 deletions(-) delete mode 100644 examples/ConnectionConfigurationExample.js delete mode 100644 examples/ConnectionFromNodeRedisExample.js diff --git a/examples/ConnectionConfigurationExample.js b/examples/ConnectionConfigurationExample.js deleted file mode 100644 index cefbc35c55..0000000000 --- a/examples/ConnectionConfigurationExample.js +++ /dev/null @@ -1,51 +0,0 @@ -/* Executed with: -redis-server --loadmodule path_to_redisgraph.so --port 8080 - -In Redis-CLI: -CONFIG SET requirepass "1234" -*/ - -const RedisGraph = require("redisgraph.js").Graph; -let graph = new RedisGraph("social", "127.0.0.1", 8080, {password:1234}); - -try { - (async () => { - await graph.query("CREATE (:person{name:'roi',age:32})"); - await graph.query("CREATE (:person{name:'amit',age:30})"); - await graph.query( - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" - ); - - // Match query. - let res = await graph.query( - "MATCH (a:person)-[:knows]->(:person) RETURN a.name" - ); - while (res.hasNext()) { - let record = res.next(); - console.log(record.get("a.name")); - } - console.log(res.getStatistics().queryExecutionTime()); - - // Match with parameters. - let param = { age: 30 }; - res = await graph.query("MATCH (a {age: $age}) return a.name", param); - while (res.hasNext()) { - let record = res.next(); - console.log(record.get("a.name")); - } - - // Named paths matching. - res = await graph.query( - "MATCH p = (a:person)-[:knows]->(:person) RETURN p" - ); - while (res.hasNext()) { - let record = res.next(); - // See path.js for more path API. - console.log(record.get("p").nodeCount); - graph.deleteGraph(); - process.exit(); - } - })(); -} catch (err) { - console.log(err); -} diff --git a/examples/ConnectionFromNodeRedisExample.js b/examples/ConnectionFromNodeRedisExample.js deleted file mode 100644 index bd5ca5b71a..0000000000 --- a/examples/ConnectionFromNodeRedisExample.js +++ /dev/null @@ -1,58 +0,0 @@ -/* Executed with: -redis-server --loadmodule path_to_redisgraph.so --port 8080 - -In Redis-CLI: -CONFIG SET requirepass "1234" -*/ - -const RedisGraph = require("redisgraph.js").Graph; -const redis = require("redis") - -let options = { - port:8080, - password:1234 -}; -let rc = redis.createClient(options) -let graph = new RedisGraph("social", rc); - -try { - (async () => { - await graph.query("CREATE (:person{name:'roi',age:32})"); - await graph.query("CREATE (:person{name:'amit',age:30})"); - await graph.query( - "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" - ); - - // Match query. - let res = await graph.query( - "MATCH (a:person)-[:knows]->(:person) RETURN a.name" - ); - while (res.hasNext()) { - let record = res.next(); - console.log(record.get("a.name")); - } - console.log(res.getStatistics().queryExecutionTime()); - - // Match with parameters. - let param = { age: 30 }; - res = await graph.query("MATCH (a {age: $age}) return a.name", param); - while (res.hasNext()) { - let record = res.next(); - console.log(record.get("a.name")); - } - - // Named paths matching. - res = await graph.query( - "MATCH p = (a:person)-[:knows]->(:person) RETURN p" - ); - while (res.hasNext()) { - let record = res.next(); - // See path.js for more path API. - console.log(record.get("p").nodeCount); - graph.deleteGraph(); - process.exit(); - } - })(); -} catch (err) { - console.log(err); -} diff --git a/test/redisGraphAPITest.js b/test/redisGraphAPITest.js index 7a7835bc61..718b375d68 100644 --- a/test/redisGraphAPITest.js +++ b/test/redisGraphAPITest.js @@ -16,8 +16,21 @@ describe("RedisGraphAPI Test", () => { return api.deleteGraph().catch(() => {}); }); - it("test bring your client", () => { - return new RedisGraph("social", redis.createClient()); + it("test connection from post and host", async () => { + // Assuming this test is running against localhost:6379 with no password. + let graph = new RedisGraph("social", "127.0.0.1", 6379, {password:undefined}); + let result = await graph.query("CREATE ({name:'roi', age:34})"); + assert.ok(!result.hasNext()); + assert.equal(1, result.getStatistics().nodesCreated()); + graph.deleteGraph(); + }) + + it("test connection from client", async () => { + let graph = new RedisGraph("social", redis.createClient()); + let result = await graph.query("CREATE ({name:'roi', age:34})"); + assert.ok(!result.hasNext()); + assert.equal(1, result.getStatistics().nodesCreated()); + graph.deleteGraph(); }); it("test Create Node", async () => { From 31ed64eaf62f576b066504af9839e3c9df5dea4e Mon Sep 17 00:00:00 2001 From: DvirDukhan Date: Sun, 8 Mar 2020 10:27:23 +0200 Subject: [PATCH 4/4] fixed PR comment --- test/redisGraphAPITest.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/redisGraphAPITest.js b/test/redisGraphAPITest.js index 718b375d68..df19f60892 100644 --- a/test/redisGraphAPITest.js +++ b/test/redisGraphAPITest.js @@ -10,14 +10,15 @@ const assert = require("assert"), deepEqual = require("deep-equal"); describe("RedisGraphAPI Test", () => { + // Assuming this test is running against redis server at: localhost:6379 with no password. const api = new RedisGraph("social"); beforeEach(() => { return api.deleteGraph().catch(() => {}); }); - it("test connection from post and host", async () => { - // Assuming this test is running against localhost:6379 with no password. + it("test connection from port and host", async () => { + // Assuming this test is running against redis server at: localhost:6379 with no password. let graph = new RedisGraph("social", "127.0.0.1", 6379, {password:undefined}); let result = await graph.query("CREATE ({name:'roi', age:34})"); assert.ok(!result.hasNext()); @@ -26,6 +27,7 @@ describe("RedisGraphAPI Test", () => { }) it("test connection from client", async () => { + // Assuming this test is running against redis server at: localhost:6379 with no password. let graph = new RedisGraph("social", redis.createClient()); let result = await graph.query("CREATE ({name:'roi', age:34})"); assert.ok(!result.hasNext());