Skip to content

Commit 84ba970

Browse files
committed
Move summary to callback result/onCompleted, add examples
- #summarize() lived on the ResultStream from run - which was unhelpful, since the summary is not available until the result is consumed. This instead makes a summary available as an argument to `onCompleted` and as a field on the result object we get when we ask for a full result - Add examples file for the manual.
1 parent 343470c commit 84ba970

File tree

4 files changed

+154
-29
lines changed

4 files changed

+154
-29
lines changed

src/v1/internal/connector.js

-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ class Connection {
219219
}
220220

221221
_handleMessage( msg ) {
222-
223222
switch( msg.signature ) {
224223
case RECORD:
225224
this._currentObserver.onNext( msg.fields[0] );

src/v1/result.js

+10-16
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class Result {
3838
this._p = null;
3939
this._statement = statement;
4040
this._parameters = parameters;
41-
this.summary = {};
4241
}
4342

4443
/**
@@ -54,7 +53,10 @@ class Result {
5453
let records = [];
5554
let observer = {
5655
onNext: (record) => { records.push(record); },
57-
onCompleted: () => { resolve(records); },
56+
onCompleted: (summary) => {
57+
records.summary = summary;
58+
resolve(records);
59+
},
5860
onError: (error) => { reject(error); }
5961
};
6062
self.subscribe(observer);
@@ -68,9 +70,9 @@ class Result {
6870
* @param {function(results: Object)} cb - Function to be called when all results are collected.
6971
* @return {Promise} promise.
7072
*/
71-
then(cb) {
73+
then(onFulfilled, onRejected) {
7274
this._createPromise();
73-
this._p.then(cb);
75+
this._p.then(onFulfilled, onRejected);
7476
return this._p;
7577
}
7678

@@ -80,9 +82,9 @@ class Result {
8082
* @param {function(error: Object)} cb - Function to be called upon errors.
8183
* @return {Promise} promise.
8284
*/
83-
catch(cb) {
85+
catch(onRejected) {
8486
this._createPromise();
85-
this._p.catch(cb);
87+
this._p.catch(onRejected);
8688
return this._p;
8789
}
8890

@@ -97,20 +99,12 @@ class Result {
9799
subscribe(observer) {
98100
let onCompletedOriginal = observer.onCompleted;
99101
let onCompletedWrapper = (metadata) => {
100-
this.summary = new ResultSummary(this._statement, this._parameters, metadata);
101-
onCompletedOriginal.call(observer);
102+
let sum = new ResultSummary(this._statement, this._parameters, metadata);
103+
onCompletedOriginal.call(observer, sum);
102104
};
103105
observer.onCompleted = onCompletedWrapper;
104106
this._streamObserver.subscribe(observer);
105107
}
106-
107-
/**
108-
* Get a metadata summary for the statement.
109-
* @return {ResultSummary} - A ResultSummary class.
110-
*/
111-
summarize() {
112-
return this.summary;
113-
}
114108
}
115109

116110
export default Result

test/v1/examples.test.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* Copyright (c) 2002-2015 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
var neo4j = require("../../lib/v1");
21+
22+
var _console = console;
23+
24+
describe('transaction', function() {
25+
26+
var driver, session, out, console;
27+
28+
beforeEach(function(done) {
29+
driver = neo4j.driver("bolt://localhost");
30+
session = driver.session();
31+
32+
// Override console.log, to assert on stdout output
33+
out = [];
34+
console = { log: function(msg) { out.push(msg); } };
35+
36+
session.run("MATCH (n) DETACH DELETE n").then(done);
37+
});
38+
39+
it('should document a minimum viable snippet', function(done) {
40+
// tag::minimum-snippet[]
41+
var driver = neo4j.driver("bolt://localhost");
42+
var session = driver.session();
43+
44+
session.run( "CREATE (neo:Person {name:'Neo', age:23})" );
45+
46+
session
47+
.run( "MATCH (p:Person) WHERE p.name = 'Neo' RETURN p.age" )
48+
.then( function( result ) {
49+
console.log( "Neo is " + result[0]["p.age"].toInt() + " years old." );
50+
51+
session.close();
52+
driver.close();
53+
done();
54+
});
55+
// end::minimum-snippet[]
56+
});
57+
58+
it('should document a statement', function(done) {
59+
var resultPromise =
60+
// tag::statement[]
61+
session
62+
.run( "CREATE (p:Person { name: {name} })", {"name": "The One"} )
63+
.then( function(result) {
64+
var theOnesCreated = result.summary.updateStatistics.nodesCreated();
65+
console.log("There were " + theOnesCreated + " the ones created.")
66+
});
67+
// end::statement[]
68+
69+
// Then
70+
resultPromise.then(function() {
71+
expect(out[0]).toBe("There were 1 the ones created.");
72+
done();
73+
});
74+
});
75+
76+
it('should document a statement without parameters', function(done) {
77+
var resultPromise =
78+
// tag::statement-without-parameters[]
79+
session
80+
.run( "CREATE (p:Person { name: 'The One' })" )
81+
82+
.then( function(result) {
83+
var theOnesCreated = result.summary.updateStatistics.nodesCreated();
84+
console.log("There were " + theOnesCreated + " the ones created.");
85+
});
86+
// end::statement-without-parameters[]
87+
88+
// Then
89+
resultPromise.then(function() {
90+
expect(out[0]).toBe("There were 1 the ones created.");
91+
done();
92+
})
93+
});
94+
95+
it('should document committing a transaction', function() {
96+
// tag::transaction-commit[]
97+
var tx = session.beginTransaction();
98+
tx.run( "CREATE (p:Person { name: 'The One' })" );
99+
tx.commit();
100+
// end::transaction-commit[]
101+
});
102+
103+
it('should document rolling back a transaction', function() {
104+
// tag::transaction-rollback[]
105+
var tx = session.beginTransaction();
106+
tx.run( "CREATE (p:Person { name: 'The One' })" );
107+
tx.rollback();
108+
// end::transaction-rollback[]
109+
});
110+
111+
it('should document how to require encryption', function() {
112+
// tag::tls-require-encryption[]
113+
// Unfortunately, this feature is not yet implemented for JavaScript
114+
// end::tls-require-encryption[]
115+
});
116+
117+
it('should document how to configure trust-on-first-use', function() {
118+
// tag::tls-trust-on-first-use[]
119+
// Unfortunately, this feature is not yet implemented for JavaScript
120+
// end::tls-trust-on-first-use[]
121+
});
122+
123+
it('should document how to configure a trusted signing certificate', function() {
124+
// tag::tls-signed[]
125+
// Unfortunately, this feature is not yet implemented for JavaScript
126+
// end::tls-signed[]
127+
});
128+
129+
});

test/v1/session.test.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ describe('session', function() {
134134
var statement = "CREATE (n:Label {prop:{prop}}) RETURN n";
135135
var params = {prop: "string"}
136136
// When & Then
137-
var result = driver.session().run( statement, params );
138-
result.then(function() {
139-
var sum = result.summarize();
137+
driver.session().run( statement, params )
138+
.then(function(result) {
139+
var sum = result.summary;
140140
expect(sum.statement.text).toBe( statement );
141141
expect(sum.statement.parameters).toBe( params );
142142
expect(sum.updateStatistics.containsUpdates()).toBe(true);
@@ -153,9 +153,10 @@ describe('session', function() {
153153
var statement = "EXPLAIN CREATE (n:Label {prop:{prop}}) RETURN n";
154154
var params = {prop: "string"}
155155
// When & Then
156-
var result = driver.session().run( statement, params );
157-
result.then(function() {
158-
var sum = result.summarize();
156+
driver.session()
157+
.run( statement, params )
158+
.then(function(result) {
159+
var sum = result.summary;
159160
expect(sum.hasPlan()).toBe(true);
160161
expect(sum.hasProfile()).toBe(false);
161162
expect(sum.plan.operatorType).toBe('ProduceResults');
@@ -173,9 +174,10 @@ describe('session', function() {
173174
var statement = "PROFILE MATCH (n:Label {prop:{prop}}) RETURN n";
174175
var params = {prop: "string"}
175176
// When & Then
176-
var result = driver.session().run( statement, params );
177-
result.then(function() {
178-
var sum = result.summarize();
177+
driver.session()
178+
.run( statement, params )
179+
.then(function(result) {
180+
var sum = result.summary;
179181
expect(sum.hasPlan()).toBe(true); //When there's a profile, there's a plan
180182
expect(sum.hasProfile()).toBe(true);
181183
expect(sum.profile.operatorType).toBe('ProduceResults');
@@ -194,9 +196,10 @@ describe('session', function() {
194196
var driver = neo4j.driver("bolt://localhost");
195197
var statement = "EXPLAIN MATCH (n), (m) RETURN n, m";
196198
// When & Then
197-
var result = driver.session().run( statement );
198-
result.then(function() {
199-
var sum = result.summarize();
199+
driver.session()
200+
.run( statement )
201+
.then(function(result) {
202+
var sum = result.summary;
200203
expect(sum.notifications.length).toBeGreaterThan(0);
201204
expect(sum.notifications[0].code).toBe("Neo.ClientNotification.Statement.CartesianProduct");
202205
expect(sum.notifications[0].title).toBe("This query builds a cartesian product between disconnected patterns.");

0 commit comments

Comments
 (0)