@@ -63,95 +63,148 @@ driver.close();
63
63
64
64
## Usage examples
65
65
66
+ Driver creation:
66
67
``` javascript
67
-
68
68
// Create a driver instance, for the user neo4j with password neo4j.
69
69
// It should be enough to have a single driver per database per application.
70
70
var driver = neo4j .driver (" bolt://localhost" , neo4j .auth .basic (" neo4j" , " neo4j" ));
71
71
72
72
// Register a callback to know if driver creation was successful:
73
- driver .onCompleted = function () {
73
+ driver .onCompleted = function () {
74
74
// proceed with using the driver, it was successfully instantiated
75
75
};
76
76
77
77
// Register a callback to know if driver creation failed.
78
78
// This could happen due to wrong credentials or database unavailability:
79
- driver .onError = function (error ) {
79
+ driver .onError = function (error ) {
80
80
console .log (' Driver instantiation failed' , error);
81
81
};
82
82
83
+ // Close the driver when application exits.
84
+ // This closes all used network connections.
85
+ driver .close ();
86
+ ```
87
+
88
+ Session API:
89
+ ``` javascript
83
90
// Create a session to run Cypher statements in.
84
91
// Note: Always make sure to close sessions when you are done using them!
85
92
var session = driver .session ();
86
93
87
94
// Run a Cypher statement, reading the result in a streaming manner as records arrive:
88
95
session
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' })
90
97
.subscribe ({
91
- onNext : function (record ) {
92
- console .log (record ._fields );
98
+ onNext : function (record ) {
99
+ console .log (record .get ( ' name ' ) );
93
100
},
94
- onCompleted : function () {
95
- // Completed!
101
+ onCompleted : function () {
96
102
session .close ();
97
103
},
98
- onError : function (error ) {
104
+ onError : function (error ) {
99
105
console .log (error);
100
106
}
101
107
});
102
108
103
109
// or
104
110
// the Promise way, where the complete result is collected before we act on it:
105
111
session
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 ' ) );
110
116
});
111
- // Completed!
112
117
session .close ();
113
118
})
114
- .catch (function (error ) {
119
+ .catch (function (error ) {
115
120
console .log (error);
116
121
});
122
+ ```
117
123
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
119
176
var 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' })
121
178
.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 () {
127
183
session .close ();
128
184
},
129
- onError : function (error ) {
185
+ onError : function (error ) {
130
186
console .log (error);
131
187
}
132
188
});
133
-
189
+
134
190
// decide if the transaction should be committed or rolled back
135
191
var success = false ;
136
192
137
193
if (success) {
138
194
tx .commit ()
139
195
.subscribe ({
140
- onCompleted : function () {
141
- // Completed!
196
+ onCompleted : function () {
197
+ // this transaction is now committed
142
198
},
143
- onError : function (error ) {
199
+ onError : function (error ) {
144
200
console .log (error);
145
- }
146
- });
147
- } else {
201
+ }
202
+ });
203
+ } else {
148
204
// transaction is rolled black and nothing is created in the database
149
205
console .log (' rolled back' );
150
206
tx .rollback ();
151
207
}
152
-
153
- // Close the driver when application exits
154
- driver .close ();
155
208
```
156
209
157
210
Subscriber API allows following combinations of ` onNext ` , ` onCompleted ` and ` onError ` callback invocations:
0 commit comments