ostrio:neo4jdriver

v1.1.2Published 6 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Join the chat at https://gitter.im/VeliovGroup/ostrio-neo4jdriver

Most advanced, well documented and efficient REST client for Neo4j database, with 100% tests coverage. Fibers and Meteor.js allows to give a new level experience to developers, no more callback-hell and blocking operations. Speed and low resources consumption is top priority of neo4j-fiber package.

Neo4j Driver

About

Install to meteor

meteor add ostrio:neo4jdriver

Import

1import { Neo4jDB } from 'meteor/ostrio:neo4jdriver';
2// Full list of available classes (for reference):
3// import {Neo4jCursor, Neo4jRelationship, Neo4jNode, Neo4jData, Neo4jEndpoint, Neo4jTransaction, Neo4jDB} from 'meteor/ostrio:neo4jdriver';

Demo Apps

API

Please see full API with examples in neo4j-fiber wiki

Basic Usage

1import { Neo4jDB } from 'meteor/ostrio:neo4jdriver';
2
3const db = new Neo4jDB('http://localhost:7474', {
4  username: 'neo4j',
5  password: '1234'
6});
7
8// Create some data:
9const cities = {};
10cities['Zürich'] = db.nodes({
11  title: 'Zürich',
12  lat: 47.27,
13  long: 8.31
14}).label(['City']);
15
16cities['Tokyo'] = db.nodes({
17  title: 'Tokyo',
18  lat: 35.40,
19  long: 139.45
20}).label(['City']);
21
22cities['Athens'] = db.nodes({
23  title: 'Athens',
24  lat: 37.58,
25  long: 23.43
26}).label(['City']);
27
28cities['Cape Town'] = db.nodes({
29  title: 'Cape Town',
30  lat: 33.55,
31  long: 18.22
32}).label(['City']);
33
34
35// Add relationship between cities
36// At this example we set distance
37cities['Zürich'].to(cities['Tokyo'], "DISTANCE", {m: 9576670, km: 9576.67, mi: 5950.67});
38cities['Tokyo'].to(cities['Zürich'], "DISTANCE", {m: 9576670, km: 9576.67, mi: 5950.67});
39
40// Create route 1 (Zürich -> Athens -> Cape Town -> Tokyo)
41cities['Zürich'].to(cities['Athens'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 50});
42cities['Athens'].to(cities['Cape Town'], "ROUTE", {m: 8015080, km: 8015.08, mi: 4980.34, price: 500});
43cities['Cape Town'].to(cities['Tokyo'], "ROUTE", {m: 9505550, km: 9505.55, mi: 5906.48, price: 850});
44
45// Create route 2 (Zürich -> Cape Town -> Tokyo)
46cities['Zürich'].to(cities['Cape Town'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 550});
47cities['Cape Town'].to(cities['Tokyo'], "ROUTE", {m: 9576670, km: 9576.67, mi: 5950.67, price: 850});
48
49// Create route 3 (Zürich -> Athens -> Tokyo)
50cities['Zürich'].to(cities['Athens'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 50});
51cities['Athens'].to(cities['Tokyo'], "ROUTE", {m: 9576670, km: 9576.67, mi: 5950.67, price: 850});
52
53// Get Shortest Route (in km) between two Cities:
54const shortest  = cities['Zürich'].path(cities['Tokyo'], "ROUTE", {cost_property: 'km', algorithm: 'dijkstra'})[0];
55let shortestStr = 'Shortest from Zürich to Tokyo, via: ';
56shortest.nodes.forEach((id) => {
57  shortestStr += db.nodes(id).property('title') + ', ';
58});
59
60shortestStr += '| Distance: ' + shortest.weight + ' km';
61console.info(shortestStr); // <-- Shortest from Zürich to Tokyo, via: Zürich, Cape Town, Tokyo, | Distance: 11122.82 km
62
63// Get Cheapest Route (in notional currency) between two Cities:
64const cheapest  = cities['Zürich'].path(cities['Tokyo'], "ROUTE", {cost_property: 'price', algorithm: 'dijkstra'})[0];
65let cheapestStr = 'Cheapest from Zürich to Tokyo, via: ';
66cheapest.nodes.forEach((id) => {
67  cheapestStr += db.nodes(id).property('title') + ', ';
68});
69
70cheapestStr += '| Price: ' + cheapest.weight + ' nc';
71console.info(cheapestStr); // <-- Cheapest from Zürich to Tokyo, via: Zürich, Athens, Tokyo, | Price: 900 nc
72
73
74// Create data via cypher query (as alternative to examples above)
75const cursor = db.query('CREATE (n:City {props}) RETURN n', {
76  props {
77    title: 'Ottawa',
78    lat: 45.24,
79    long: 75.43
80  }
81});
82
83console.log(cursor.fetch());
84// Returns array of nodes:
85// [{
86//   n: {
87//     long: 75.43,
88//     lat: 45.24,
89//     title: "Ottawa",
90//     id: 8421,
91//     labels": ["City"],
92//     metadata: {
93//       id: 8421,
94//       labels": ["City"]
95//     }
96//   }
97// }]
98
99// Iterate through results as plain objects:
100cursor.forEach((node) => {
101  console.log(node)
102  // Returns node as Object:
103  // {
104  //   n: {
105  //     long: -75.683333,
106  //     lat: 45.416667,
107  //     title: "Ottawa",
108  //     id: 8421,
109  //     labels": ["City"],
110  //     metadata: {
111  //       id: 8421,
112  //       labels": ["City"]
113  //     }
114  //   }
115  // }
116});
117
118// Iterate through cursor as `Neo4jNode` instances:
119cursor.each((node) => {
120  console.log(node.n.get());
121  // {
122  //   long: -75.683333,
123  //   lat: 45.416667,
124  //   title: "Ottawa",
125  //   id: 8421,
126  //   labels": ["City"],
127  //   metadata: {
128  //     id: 8421,
129  //     labels": ["City"]
130  //   }
131  // }
132});

Testing & Dev usage

Local usage

To use the ostrio-neo4jdriver in a project and benefit from updates to the driver as they are released, you can keep your project and the driver in separate directories, and create a symlink between them.

# Stop meteor if it is running
$ cd /directory/of/your/project
# If you don't have a Meteor project yet, create a new one:
$ meteor create MyProject
$ cd MyProject
# Create `packages` directory inside project's dir
$ mkdir packages
$ cd packages
# Clone this repository to a local `packages` directory
$ git clone --bare https://github.com/VeliovGroup/ostrio-neo4jdriver.git
# If you need dev branch, switch into it
$ git checkout dev
# Go back into project's directory
$ cd ../
$ meteor add ostrio:neo4jdriver
# Do not forget to run Neo4j database, before start work with package

From now any changes in ostrio:neo4jdriver package folder will cause your project application to rebuild.

To run tests:

Before running tests - make sure it's blank Neo4j database with no records!

# Go to local package folder
$ cd packages/ostrio-neo4jdriver
# Edit line 10 of `tests.coffee` to set connection to your Neo4j database
# Default is: 'http://localhost:7474', {username: 'neo4j', password: '1234'}
# Do not forget to run Neo4j database
# Make sure database has no records
$ meteor test-packages ./