@@ -63,95 +63,148 @@ driver.close();
6363
6464## Usage examples
6565
66+ Driver creation:
6667``` javascript
67-
6868// Create a driver instance, for the user neo4j with password neo4j.
6969// It should be enough to have a single driver per database per application.
7070var driver = neo4j .driver (" bolt://localhost" , neo4j .auth .basic (" neo4j" , " neo4j" ));
7171
7272// Register a callback to know if driver creation was successful:
73- driver .onCompleted = function () {
73+ driver .onCompleted = function () {
7474 // proceed with using the driver, it was successfully instantiated
7575};
7676
7777// Register a callback to know if driver creation failed.
7878// This could happen due to wrong credentials or database unavailability:
79- driver .onError = function (error ) {
79+ driver .onError = function (error ) {
8080 console .log (' Driver instantiation failed' , error);
8181};
8282
83+ // Close the driver when application exits.
84+ // This closes all used network connections.
85+ driver .close ();
86+ ```
87+
88+ Session API:
89+ ``` javascript
8390// Create a session to run Cypher statements in.
8491// Note: Always make sure to close sessions when you are done using them!
8592var session = driver .session ();
8693
8794// Run a Cypher statement, reading the result in a streaming manner as records arrive:
8895session
89- .run (" MERGE (alice:Person {name : {nameParam} }) RETURN alice.name" , { nameParam: ' Alice' })
96+ .run (' MERGE (alice:Person {name : {nameParam} }) RETURN alice.name AS name ' , {nameParam: ' Alice' })
9097 .subscribe ({
91- onNext : function (record ) {
92- console .log (record ._fields );
98+ onNext : function (record ) {
99+ console .log (record .get ( ' name ' ) );
93100 },
94- onCompleted : function () {
95- // Completed!
101+ onCompleted : function () {
96102 session .close ();
97103 },
98- onError : function (error ) {
104+ onError : function (error ) {
99105 console .log (error);
100106 }
101107 });
102108
103109// or
104110// the Promise way, where the complete result is collected before we act on it:
105111session
106- .run (" MERGE (james:Person {name : {nameParam} }) RETURN james.name" , { nameParam: ' James' })
107- .then (function (result ){
108- result .records .forEach (function (record ) {
109- console .log (record ._fields );
112+ .run (' MERGE (james:Person {name : {nameParam} }) RETURN james.name AS name ' , {nameParam: ' James' })
113+ .then (function (result ) {
114+ result .records .forEach (function (record ) {
115+ console .log (record .get ( ' name ' ) );
110116 });
111- // Completed!
112117 session .close ();
113118 })
114- .catch (function (error ) {
119+ .catch (function (error ) {
115120 console .log (error);
116121 });
122+ ```
117123
118- // run statement in a transaction
124+ Transaction functions API:
125+ ``` javascript
126+ // Transaction functions provide a convenient API with minimal boilerplate and
127+ // retries on network fluctuations and transient errors. Maximum retry time is
128+ // configured on the driver level and is 30 seconds by default:
129+ neo4j .driver (" bolt://localhost" , neo4j .auth .basic (" neo4j" , " neo4j" ), {maxTransactionRetryTime: 30000 });
130+
131+ // It is possible to execute read transactions that will benefit from automatic
132+ // retries on both single instance ('bolt' URI scheme) and Causal Cluster
133+ // ('bolt+routing' URI scheme) and will get automatic load balancing in cluster deployments
134+ var readTxResultPromise = session .readTransaction (function (transaction ) {
135+ // used transaction will be committed automatically, no need for explicit commit/rollback
136+
137+ var result = transaction .run (' MATCH (person:Person) RETURN person.name AS name' );
138+ // at this point it is possible to either return the result or process it and return the
139+ // result of processing it is also possible to run more statements in the same transaction
140+ return result;
141+ });
142+
143+ // returned Promise can be later consumed like this:
144+ readTxResultPromise .then (function (result ) {
145+ session .close ();
146+ console .log (result .records );
147+ }).catch (function (error ) {
148+ console .log (error);
149+ });
150+
151+ // It is possible to execute write transactions that will benefit from automatic retries
152+ // on both single instance ('bolt' URI scheme) and Causal Cluster ('bolt+routing' URI scheme)
153+ var writeTxResultPromise = session .writeTransaction (function (transaction ) {
154+ // used transaction will be committed automatically, no need for explicit commit/rollback
155+
156+ var result = transaction .run (' MERGE (alice:Person {name : \' Alice\' }) RETURN alice.name AS name' );
157+ // at this point it is possible to either return the result or process it and return the
158+ // result of processing it is also possible to run more statements in the same transaction
159+ return result .records .map (function (record ) {
160+ return record .get (' name' );
161+ });
162+ });
163+
164+ // returned Promise can be later consumed like this:
165+ writeTxResultPromise .then (function (namesArray ) {
166+ session .close ();
167+ console .log (namesArray);
168+ }).catch (function (error ) {
169+ console .log (error);
170+ });
171+ ```
172+
173+ Explicit transactions API:
174+ ``` javascript
175+ // run statement in a transaction
119176var tx = session .beginTransaction ();
120- tx .run (" MERGE (bob:Person {name : {nameParam} }) RETURN bob.name" , { nameParam: ' Bob' })
177+ tx .run (" MERGE (bob:Person {name : {nameParam} }) RETURN bob.name AS name " , {nameParam: ' Bob' })
121178 .subscribe ({
122- onNext : function (record ) {
123- console .log (record ._fields );
124- },
125- onCompleted : function () {
126- // Completed!
179+ onNext : function (record ) {
180+ console .log (record .get (' name' ));
181+ },
182+ onCompleted : function () {
127183 session .close ();
128184 },
129- onError : function (error ) {
185+ onError : function (error ) {
130186 console .log (error);
131187 }
132188 });
133-
189+
134190// decide if the transaction should be committed or rolled back
135191var success = false ;
136192
137193if (success) {
138194 tx .commit ()
139195 .subscribe ({
140- onCompleted : function () {
141- // Completed!
196+ onCompleted : function () {
197+ // this transaction is now committed
142198 },
143- onError : function (error ) {
199+ onError : function (error ) {
144200 console .log (error);
145- }
146- });
147- } else {
201+ }
202+ });
203+ } else {
148204 // transaction is rolled black and nothing is created in the database
149205 console .log (' rolled back' );
150206 tx .rollback ();
151207}
152-
153- // Close the driver when application exits
154- driver .close ();
155208```
156209
157210Subscriber API allows following combinations of ` onNext ` , ` onCompleted ` and ` onError ` callback invocations:
0 commit comments