This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
ruby "2.1.1" | |
gem 'sinatra', '1.1.0' | |
gem 'neo4j-core', '3.0.0.alpha.11' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a node with on property name='andreas', and one label 'Human' | |
andreas = Neo4j::Node.create({name: 'andreas'}, :Human) | |
duchess = Neo4j::Node.create({name: 'duchess', character: 'posh'}, :Animal, :Cat) | |
kim = Neo4j::Node.create({name: 'kim', age: 1}, :Animal, :Cat) | |
# Create a relationship of type pets between andreas and duchess | |
# Relationships can also have properties | |
andreas.create_rel(:pets, duchess, since: 2014) | |
andreas.create_rel(:pets, kim, since: 2014) | |
duchess.create_rel(:kittens, kim) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cats = Neo4j::Session.query.match('(h:Human)-[:pets]->(p)').where(h: {name: 'andreas'}).pluck(:p) | |
# print the name of the cats | |
puts cats[0][:name] | |
puts cats[1][:name] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Human | |
include Neo4j::ActiveNode | |
property :name | |
# A human can have outgoing relationships of type pets to animals | |
# The name of the generated accessor method will be the same as the relationship type: pets | |
has_many :out, :pets, type: :pets, model_class: :Animal | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'neo4j' | |
Neo4j::Session.open |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal | |
include Neo4j::ActiveNode | |
has_one :in, :owner, origin: :pets, model_class: Human | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cat < Animal | |
property :name | |
has_many :out, :kittens, type: :kittens, model_class: self | |
has_one :in, :mother, origin: :kittens, model_class: self | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lets find me: | |
me = Human.where(name: 'andreas').first | |
# And my posh cat: | |
dutchess = me.pets.where(character: 'posh').first | |
# And find all cat owners that have a posh cat mummy: | |
Cat.as(:c).where("c.character='posh'").kittens(:k).owner.first # => me | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'neo4j/tasks/neo4j_server' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This SPIKE is now commited in branch rest: http://github.com/andreasronge/neo4j/tree/restful | |
# This is a old, see branch for new code, example and a spike of how RESTful Neo4j API would work | |
require 'rubygems' | |
require 'json' | |
require 'sinatra/base' | |
require 'neo4j' | |
# This mixin creates the following restful resources |
OlderNewer