Skip to content

Commit 24c35fe

Browse files
author
DvirDukhan
authored
Parameters support (#26)
* added parameters support
1 parent 02418b6 commit 24c35fe

File tree

5 files changed

+105
-13
lines changed

5 files changed

+105
-13
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ graph.query("CREATE (:person{name:'roi',age:32})").then(() => {
4343
console.log(record.get("a.name"));
4444
}
4545
console.log(res.getStatistics().queryExecutionTime());
46-
graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{
46+
let param = {'age': 30};
47+
graph.query("MATCH (a {age: $age}) return a.name", param).then(res=>{
4748
while (res.hasNext()) {
4849
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();
50+
console.log(record.get("a.name"));
5351
}
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+
})
5461
})
5562
})
5663
})

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.1.3"
12+
"redisgraph.js": "^1.2.0"
1313
},
1414
"main": "redisGraphExample.js"
1515
}

examples/redisGraphExample.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ graph.query("CREATE (:person{name:'roi',age:32})").then(() => {
1111
console.log(record.get("a.name"));
1212
}
1313
console.log(res.getStatistics().queryExecutionTime());
14-
graph.query("MATCH p = (a:person)-[:knows]->(:person) RETURN p").then(res=>{
14+
let param = {'age': 30};
15+
graph.query("MATCH (a {age: $age}) return a.name", param).then(res=>{
1516
while (res.hasNext()) {
1617
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();
18+
console.log(record.get("a.name"));
2119
}
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+
})
2229
})
2330
})
2431
})

src/graph.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,59 @@ class Graph {
4444
strings.push(resultSet.next().getString(0));
4545
}
4646
return strings;
47-
}
47+
}
48+
49+
paramToString(paramValue) {
50+
if(paramValue == null)
51+
return "null"
52+
let paramType = typeof(paramValue);
53+
if(paramType == "string") {
54+
let strValue = "";
55+
if (paramValue[0] != "\"")
56+
strValue += "\"";
57+
strValue += paramValue;
58+
if(paramValue[paramValue.length-1] != "\"")
59+
strValue += "\"";
60+
return strValue;
61+
}
62+
if(Array.isArray(paramValue)) {
63+
let stringsArr = new Array(paramValue.length);
64+
for(var i = 0; i < paramValue.length; i++) {
65+
stringsArr[i] = this.paramToString(paramValue[i]);
66+
}
67+
return ["[", stringsArr.join(", "),"]"].join("");
68+
}
69+
return paramValue;
70+
}
71+
72+
/**
73+
* Extracts parameters from dictionary into cypher parameters string.
74+
*
75+
* @param params parameters dictionary.
76+
* @return a cypher parameters string.
77+
*/
78+
buildParamsHeader(params) {
79+
let paramsArray = ["CYPHER"]
80+
81+
for (var key in params) {
82+
let value = this.paramToString(params[key]);
83+
paramsArray.push(`${key}=${value}`);
84+
}
85+
paramsArray.push(' ');
86+
return paramsArray.join(' ');
87+
}
4888

4989
/**
5090
* Execute a Cypher query (async)
5191
*
5292
* @param query Cypher query
93+
* @param params Parameters map
5394
* @return a promise contains a result set
5495
*/
55-
async query(query) {
96+
async query(query, params) {
97+
if(params){
98+
query = this.buildParamsHeader(params) + query;
99+
}
56100
var res = await this._sendCommand("graph.QUERY", [this._graphId, query, "--compact"]);
57101
var resultSet = new ResultSet(this);
58102
return resultSet.parseResponse(res);

test/redisGraphAPITest.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,38 @@ describe('RedisGraphAPI Test', function () {
358358
})
359359
})
360360

361+
it('testParam', (done) => {
362+
let params = [1, 2.3, true, false, null, "str", [1,2,3], ["1", "2", "3"], null];
363+
let promises =[];
364+
for (var i =0; i < params.length; i++){
365+
let param = {'param':params[i]};
366+
promises.push(api.query("RETURN $param", param));
367+
}
368+
Promise.all(promises).then(values => {
369+
for (var i =0; i < values.length; i++) {
370+
let resultSet = values[i];
371+
let record = resultSet.next();
372+
let param = record.get(0);
373+
assert.deepEqual(param, params[i]);
374+
}
375+
done();
376+
}).catch(error => {
377+
console.log(error);
378+
})
379+
})
380+
it('testMissingParameter', (done)=> {
381+
api.query("RETURN $param").then(response => assert(false)).catch (err => {
382+
assert(err instanceof redis.ReplyError);
383+
assert.equal(err.message, "Missing parameters");
384+
api.query("RETURN $param", null).then(response => assert(false)).catch (err => {
385+
assert(err instanceof redis.ReplyError);
386+
assert.equal(err.message, "Missing parameters");
387+
api.query("RETURN $param", {}).then(response => assert(false)).catch (err => {
388+
assert(err instanceof redis.ReplyError);
389+
assert.equal(err.message, "Missing parameters");
390+
done();
391+
})
392+
})
393+
})
394+
})
361395
});

0 commit comments

Comments
 (0)