|
17 | 17 | * limitations under the License.
|
18 | 18 | */
|
19 | 19 |
|
| 20 | +import neo4j from './neo4j'; |
| 21 | + |
20 | 22 | /**
|
21 | 23 | * A Result instance is used for retrieving request response.
|
22 | 24 | * @access public
|
23 | 25 | */
|
24 |
| - |
25 | 26 | class Result {
|
26 | 27 | /**
|
27 | 28 | * Inject the observer to be used.
|
28 | 29 | * @constructor
|
29 | 30 | * @param {StreamObserver} streamObserver
|
30 | 31 | */
|
31 |
| - constructor(streamObserver) { |
| 32 | + constructor(streamObserver, statement, parameters) { |
32 | 33 | this._streamObserver = streamObserver;
|
33 | 34 | this._p = null;
|
| 35 | + this._statement = statement; |
| 36 | + this._parameters = parameters; |
| 37 | + this.summary = {} |
34 | 38 | }
|
35 | 39 |
|
36 | 40 | /**
|
37 | 41 | * Create and return new Promise
|
38 |
| - * @return {Primise} new Promise. |
| 42 | + * @return {Promise} new Promise. |
39 | 43 | */
|
40 | 44 | _createPromise() {
|
41 | 45 | if(this._p) {
|
@@ -87,8 +91,160 @@ class Result {
|
87 | 91 | * @return
|
88 | 92 | */
|
89 | 93 | subscribe(observer) {
|
| 94 | + let onCompletedOriginal = observer.onCompleted; |
| 95 | + let onCompletedWrapper = (metadata) => { |
| 96 | + this.summary = new ResultSummary(this._statement, this._parameters, metadata.type, metadata.stats); |
| 97 | + onCompletedOriginal(metadata); |
| 98 | + } |
| 99 | + observer.onCompleted = onCompletedWrapper; |
90 | 100 | this._streamObserver.subscribe(observer);
|
91 | 101 | }
|
| 102 | + |
| 103 | + /** |
| 104 | + * Get a metadata summary for the statement. |
| 105 | + * @return {ResultSummary} - A ResultSummary class. |
| 106 | + */ |
| 107 | + summarize() { |
| 108 | + return this.summary; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * A ResultSummary instance contains structured metadata for a {Result}. |
| 114 | + * @access public |
| 115 | + */ |
| 116 | +class ResultSummary { |
| 117 | + /** |
| 118 | + * @constructor |
| 119 | + * @param {string} statement - The statement this summary is for |
| 120 | + * @param {Object} parameters - Parameters for the statement |
| 121 | + * @param {string} statementType - How did the statement effect the database |
| 122 | + * @param {Object} statistics - Result statistics |
| 123 | + */ |
| 124 | + constructor(statement, parameters, statementType, statistics) { |
| 125 | + this.statement = {text: statement, parameters}; |
| 126 | + this.statementType = statementType; |
| 127 | + this.statistics = new StatementStatistics(statistics || {}); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Get statistical information for a {Result}. |
| 133 | + * @access public |
| 134 | + */ |
| 135 | +class StatementStatistics { |
| 136 | + /** |
| 137 | + * Structurize the statistics |
| 138 | + * @constructor |
| 139 | + * @param {Object} statistics - Result statistics |
| 140 | + */ |
| 141 | + constructor(statistics) { |
| 142 | + this._stats = { |
| 143 | + nodesCreated: 0, |
| 144 | + nodesDelete: 0, |
| 145 | + relationshipsCreated: 0, |
| 146 | + relationshipsDeleted: 0, |
| 147 | + propertiesSet: 0, |
| 148 | + labelsAdded: 0, |
| 149 | + labelsRemoved: 0, |
| 150 | + indexesAdded: 0, |
| 151 | + indexesRemoved: 0, |
| 152 | + constraintsAdded: 0, |
| 153 | + constraintsRemoved: 0 |
| 154 | + } |
| 155 | + Object.keys(statistics).forEach((index) => { |
| 156 | + let val = neo4j.isInt(statistics[index]) ? statistics[index].toInt() : statistics[index]; |
| 157 | + //To camelCase |
| 158 | + this._stats[index.replace(/(\-\w)/g, (m) => m[1].toUpperCase())] = val; |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Did the database get updated? |
| 164 | + * @return {boolean} |
| 165 | + */ |
| 166 | + containsUpdates() { |
| 167 | + return Object.keys(this._stats).reduce((last, current) => { |
| 168 | + return last + this._stats[current]; |
| 169 | + }, 0) > 0; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @return {Number} - Number of nodes created. |
| 174 | + */ |
| 175 | + nodesCreated() { |
| 176 | + return this._stats.nodesCreated; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @return {Number} - Number of nodes deleted. |
| 181 | + */ |
| 182 | + nodesDeleted() { |
| 183 | + return this._stats.nodesDeleted; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @return {Number} - Number of relationships created. |
| 188 | + */ |
| 189 | + relationshipsCreated() { |
| 190 | + return this._stats.relationshipsCreated; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @return {Number} - Number of nodes deleted. |
| 195 | + */ |
| 196 | + relationshipsDeleted() { |
| 197 | + return this._stats.relationshipsDeleted; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * @return {Number} - Number of properties set. |
| 202 | + */ |
| 203 | + propertiesSet() { |
| 204 | + return this._stats.propertiesSet; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @return {Number} - Number of labels added. |
| 209 | + */ |
| 210 | + labelsAdded() { |
| 211 | + return this._stats.labelsAdded; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * @return {Number} - Number of labels removed. |
| 216 | + */ |
| 217 | + labelsRemoved() { |
| 218 | + return this._stats.labelsRemoved; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * @return {Number} - Number of indexes added. |
| 223 | + */ |
| 224 | + indexesAdded() { |
| 225 | + return this._stats.indexesAdded; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @return {Number} - Number of indexes removed. |
| 230 | + */ |
| 231 | + indexesRemoved() { |
| 232 | + return this._stats.indexesRemoved; |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * @return {Number} - Number of contraints added. |
| 237 | + */ |
| 238 | + constraintsAdded() { |
| 239 | + return this._stats.constraintsAdded; |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * @return {Number} - Number of contraints removed. |
| 244 | + */ |
| 245 | + constraintsRemoved() { |
| 246 | + return this._stats.constraintsRemoved; |
| 247 | + } |
92 | 248 | }
|
93 | 249 |
|
94 | 250 | export default Result;
|
0 commit comments