Skip to content

Commit c245273

Browse files
committed
Accept a statement object in session.run
1 parent a203f50 commit c245273

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/session.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ class Session {
3939

4040
/**
4141
* Run Cypher statement
42-
* @param {string} statement - Cypher statement to execute
43-
* @param {Object} params - Map with params to use in statement
42+
* Could be called with a statement object i.e.: {statement: "MATCH ...", parameters: {param: 1}}
43+
* or with the statement and parameters as separate arguments.
44+
* @param {mixed} statement - Cypher statement to execute
45+
* @param {Object} parameters - Map with parameters to use in statement
4446
* @return {Result} - New Result
4547
*/
46-
run(statement, params) {
48+
run(statement, parameters) {
49+
if(typeof statement === 'object' && statement.text) {
50+
parameters = statement.parameters || {};
51+
statement = statement.text;
52+
}
4753
let streamObserver = new StreamObserver();
48-
this._conn.run( statement, params || {}, streamObserver );
54+
this._conn.run( statement, parameters || {}, streamObserver );
4955
this._conn.pullAll( streamObserver );
5056
this._conn.sync();
51-
return new Result( streamObserver, statement, params );
57+
return new Result( streamObserver, statement, parameters );
5258
}
5359

5460
/**

test/session.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ describe('session', function() {
2222
});
2323
});
2424

25+
it('should accept a statement object ', function(done) {
26+
// Given
27+
var driver = neo4j.driver("neo4j://localhost");
28+
var statement = {text: "RETURN 1 = {param} AS a", parameters: {param: 1}};
29+
30+
// When & Then
31+
var records = [];
32+
driver.session().run( statement ).subscribe( {
33+
onNext : function( record ) {
34+
records.push( record );
35+
},
36+
onCompleted : function( ) {
37+
expect( records.length ).toBe( 1 );
38+
expect( records[0]['a'] ).toBe( true );
39+
driver.close();
40+
done();
41+
}
42+
});
43+
});
44+
2545
it('should expose basic run/then/then/then ', function(done) {
2646
// Given
2747
var driver = neo4j.driver("neo4j://localhost");

0 commit comments

Comments
 (0)