diff --git a/README.md b/README.md index 2ca803e505..8541c20516 100644 --- a/README.md +++ b/README.md @@ -43,14 +43,21 @@ graph.query("CREATE (:person{name:'roi',age:32})").then(() => { console.log(record.get("a.name")); } console.log(res.getStatistics().queryExecutionTime()); - graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{ + let param = {'age': 30}; + graph.query("MATCH (a {age: $age}) return a.name", param).then(res=>{ while (res.hasNext()) { let record = res.next(); - // See path.js for more path API. - console.log(record.get("p").nodeCount); - graph.deleteGraph(); - process.exit(); + console.log(record.get("a.name")); } + graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{ + while (res.hasNext()) { + let record = res.next(); + // See path.js for more path API. + console.log(record.get("p").nodeCount); + graph.deleteGraph(); + process.exit(); + } + }) }) }) }) diff --git a/examples/package.json b/examples/package.json index 56e2af965f..f1de96eb7e 100644 --- a/examples/package.json +++ b/examples/package.json @@ -9,7 +9,7 @@ "url": "git://github.com/redislabs/redisgraph.js.git" }, "dependencies": { - "redisgraph.js": "^1.1.3" + "redisgraph.js": "^1.2.0" }, "main": "redisGraphExample.js" } diff --git a/examples/redisGraphExample.js b/examples/redisGraphExample.js index 00c92d8042..dac36f4ed9 100644 --- a/examples/redisGraphExample.js +++ b/examples/redisGraphExample.js @@ -11,14 +11,21 @@ graph.query("CREATE (:person{name:'roi',age:32})").then(() => { console.log(record.get("a.name")); } console.log(res.getStatistics().queryExecutionTime()); - graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{ + let param = {'age': 30}; + graph.query("MATCH (a {age: $age}) return a.name", param).then(res=>{ while (res.hasNext()) { let record = res.next(); - // See path.js for more path API. - console.log(record.get("p").nodeCount); - graph.deleteGraph(); - process.exit(); + console.log(record.get("a.name")); } + graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{ + while (res.hasNext()) { + let record = res.next(); + // See path.js for more path API. + console.log(record.get("p").nodeCount); + graph.deleteGraph(); + process.exit(); + } + }) }) }) }) diff --git a/src/graph.js b/src/graph.js index 4b75506d34..5467f43535 100644 --- a/src/graph.js +++ b/src/graph.js @@ -44,15 +44,59 @@ class Graph { strings.push(resultSet.next().getString(0)); } return strings; - } + } + + paramToString(paramValue) { + if(paramValue == null) + return "null" + let paramType = typeof(paramValue); + if(paramType == "string") { + let strValue = ""; + if (paramValue[0] != "\"") + strValue += "\""; + strValue += paramValue; + if(paramValue[paramValue.length-1] != "\"") + strValue += "\""; + return strValue; + } + if(Array.isArray(paramValue)) { + let stringsArr = new Array(paramValue.length); + for(var i = 0; i < paramValue.length; i++) { + stringsArr[i] = this.paramToString(paramValue[i]); + } + return ["[", stringsArr.join(", "),"]"].join(""); + } + return paramValue; + } + + /** + * Extracts parameters from dictionary into cypher parameters string. + * + * @param params parameters dictionary. + * @return a cypher parameters string. + */ + buildParamsHeader(params) { + let paramsArray = ["CYPHER"] + + for (var key in params) { + let value = this.paramToString(params[key]); + paramsArray.push(`${key}=${value}`); + } + paramsArray.push(' '); + return paramsArray.join(' '); + } /** * Execute a Cypher query (async) * * @param query Cypher query + * @param params Parameters map * @return a promise contains a result set */ - async query(query) { + async query(query, params) { + if(params){ + query = this.buildParamsHeader(params) + query; + } var res = await this._sendCommand("graph.QUERY", [this._graphId, query, "--compact"]); var resultSet = new ResultSet(this); return resultSet.parseResponse(res); diff --git a/test/redisGraphAPITest.js b/test/redisGraphAPITest.js index 687cedde76..fa99835b39 100644 --- a/test/redisGraphAPITest.js +++ b/test/redisGraphAPITest.js @@ -358,4 +358,38 @@ describe('RedisGraphAPI Test', function () { }) }) + it('testParam', (done) => { + let params = [1, 2.3, true, false, null, "str", [1,2,3], ["1", "2", "3"], null]; + let promises =[]; + for (var i =0; i < params.length; i++){ + let param = {'param':params[i]}; + promises.push(api.query("RETURN $param", param)); + } + Promise.all(promises).then(values => { + for (var i =0; i < values.length; i++) { + let resultSet = values[i]; + let record = resultSet.next(); + let param = record.get(0); + assert.deepEqual(param, params[i]); + } + done(); + }).catch(error => { + console.log(error); + }) + }) + it('testMissingParameter', (done)=> { + api.query("RETURN $param").then(response => assert(false)).catch (err => { + assert(err instanceof redis.ReplyError); + assert.equal(err.message, "Missing parameters"); + api.query("RETURN $param", null).then(response => assert(false)).catch (err => { + assert(err instanceof redis.ReplyError); + assert.equal(err.message, "Missing parameters"); + api.query("RETURN $param", {}).then(response => assert(false)).catch (err => { + assert(err instanceof redis.ReplyError); + assert.equal(err.message, "Missing parameters"); + done(); + }) + }) + }) + }) });