This is example for Neo4jreactivity
From this repository you will find out how to use Neo4j and Meteor within Neo4jreactivity atmospherejs.com package
Additional functionality:
- User remove
- User search
Pick up Neo4j version from Official Neo4j Website After download - unzip and place into root folder of your user (name it neo4j), then in Terminal run:
cd ~/neo4j #downloaded and replaced folder
bin/neo4j start
- Note: Neo4j database should be running before Meteor *
Pick up version for your system on Official Node.js Website Follow installer instructions
Run this in Terminal:
curl https://install.meteor.com/ | sh
To install Neo4j NPM Package, run this in Terminal:
npm -g install neo4j
Run in Terminal:
git clone git@github.com:VeliovGroup/Meteor-Leaderboard-Neo4j.git
cd Meteor-Leaderboard-Neo4j
Run a line below in Terminal, inside Meteor-Leaderboard-Neo4j
folder:
meteor
- In your browser go to localhost:3000
- To see Neo4j browser, go to localhost:7474
In article below you will understand the basics of ostrio:neo4jreactivity
and ostrio:neo4driver
packages. How to translate queries and other code from MongoDB to Neo4j. How to write basic query in Neo4j. How to use reactivity in Neo4j.
We've decided to take very basic example application at Meteor - "Leaderboard" and move it from MongoDB to Neo4j.
First of all download Meteor and Neo4j onto your working machine, then run:
$ meteor create --example leaderboard
$ cd leaderboard
$ meteor
Run inside of Neo4j directory:
$ bin/neo4j start
$ meteor add ostrio:neo4jreactivity
- When server start, it is creates Players
- In template’s helper
players
- all players ordered by score - In template’s helper
selectedName
- player name comes viafindOne
method - Incrementation of score implemented via
update
method
Let’s put our hands on it
At first let’s create neo4j config file lib/neo4j.js
:
/* Allow client query execution */
Meteor.neo4j.allowClientQuery = true;
/* Custom URL to Neo4j should be here */
/* CHANGE THIS LINE IN ACCORDING TO YOUR NEO4J SETTINGS */
Meteor.neo4j.connectionURL = 'http://user:password@localhost:7474';
/* But deny all writing actions on client */
Meteor.neo4j.set.deny(neo4j.rules.write);
Let’s move to leaderboard.js
file and change this line [Isomorphic]:
/* Players = new Mongo.Collection("players"); */
var Players = Meteor.neo4j.collection('players');
This is isomorphic code, so we have all data we need inside Players
variable on both - server and client sides.
First, we need to publish some data to client [Client]:
Players.publish('allPlayers', function(){
return 'MATCH (node:Player) RETURN node ORDER BY node.score DESC';
});
And check if we have users or not, if not - create new users [Server]:
Players.publish('allPlayers', function(){
return 'MATCH (node:Player) RETURN node ORDER BY node.score DESC';
}, function(){
if (Players.find({}).count() <= 0) {
var names = [ 'Ada Lovelace',
'Grace Hopper',
'Marie Curie',
'Carl Friedrich Gauss',
'Nikola Tesla',
'Claude Shannon'];
var players = [];
_.each(names, function (name) {
players.push({
name: name,
score: Math.floor(Random.fraction() * 10) * 5,
__labels: ':Player' /* Assign Cypher label to all nodes */
});
});
Players.insert(players);
});
To generate Players we can use several methods, all methods below is reactive and works almost in same way, the players
variable is array of objects {[Object]}:
Players.insert(players);
/* or */
Meteor.neo4j.query('CREATE (a:Player {players})', {players: players});
/* or */
Meteor.N4JDB.query('CREATE (a:Player {players})', {players: players});
To increment ‘score’, we will use standard update
method as we use in mongo [Client]:
Players.update({
_id: Session.get('selectedPlayer')
},{
'$inc': {
score: 1
}
});
To create new user - we only will add the __label
property [Client]:
Players.insert({
name: $('#newPlayerName').val(),
score: 0,
__labels: ':Player'
});
To return Players list on client side, we will find all nodes with Player
label and sort them [Client]:
Template.leaderboard.helpers({
players: function () {
Players.find({'metadata.labels': 'Player'}, {
sort:{
score: -1
}
});
},
...
});