UNPKG

2.18 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2002-2018 "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
20var neo4j = require('neo4j');
21
22var statement = ['MERGE (alice:Person {name:{name_a},age:{age_a}})',
23 'MERGE (bob:Person {name:{name_b},age:{age_b}})',
24 'CREATE UNIQUE (alice)-[alice_knows_bob:KNOWS]->(bob)',
25 'RETURN alice, bob, alice_knows_bob'
26];
27
28var params = {
29 name_a: 'Alice',
30 age_a: 33,
31 name_b: 'Bob',
32 age_b: 44
33};
34
35var driver = neo4j.driver("bolt://localhost");
36
37var streamSession = driver.session();
38var streamResult = streamSession.run(statement.join(' '), params);
39streamResult.subscribe({
40 onNext: function(record) {
41 // On receipt of RECORD
42 for(var i in record) {
43 console.log(i);
44 console.log(record[i]);
45 }
46 }, onCompleted: function() {
47 var summary = streamResult.summarize();
48 //Print number of nodes created
49 console.log('');
50 console.log(summary.updateStatistics.nodesCreated());
51 streamSession.close();
52 }, onError: function(error) {
53 console.log(error);
54 }
55});
56
57var promiseSession = driver.session();
58var promiseResult = promiseSession.run(statement.join(' '), params);
59promiseResult.then(function(records) {
60 records.forEach(function(record) {
61 for(var i in record) {
62 console.log(i);
63 console.log(record[i]);
64 }
65 });
66 var summary = promiseResult.summarize();
67 //Print number of nodes created
68 console.log('');
69 console.log(summary.updateStatistics.nodesCreated());
70})
71.catch(function(error) {
72 console.log(error);
73})
74.then(function(){
75 promiseSession.close();
76});