Skip to content

Instantly share code, notes, and snippets.

View andreasronge's full-sized avatar

Andreas Ronge andreasronge

View GitHub Profile
@andreasronge
andreasronge / Gemfile
Last active August 29, 2015 14:00
Example how to deploy Neo4j.rb / neo4j-core app on heroku
source "https://rubygems.org"
ruby "2.1.1"
gem 'sinatra', '1.1.0'
gem 'neo4j-core', '3.0.0.alpha.11'
# 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)
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]
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
require 'neo4j'
Neo4j::Session.open
class Animal
include Neo4j::ActiveNode
has_one :in, :owner, origin: :pets, model_class: Human
end
class Cat < Animal
property :name
has_many :out, :kittens, type: :kittens, model_class: self
has_one :in, :mother, origin: :kittens, model_class: self
end
# 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
require 'neo4j/tasks/neo4j_server'
# 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