UNPKG

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