Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
})
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
17 changes: 12 additions & 5 deletions examples/redisGraphExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
})
})
})
})
Expand Down
48 changes: 46 additions & 2 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions test/redisGraphAPITest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
})
})
})
});