Relationships between nodes are a key part of a graph database. They allow for finding related data. Just like nodes, relationships can have properties.
Create a relationship:
var client = new Neo4jClient("http://neo4j:7474")
# Create nodes
var andres = new NeoNode
andres["name"] = "Andres"
var kate = new NeoNode
kate["name"] = "Kate"
# Create a relationship of type `LOVES`
var loves = new NeoEdge(andres, "LOVES", kate)
client.save_edge(loves)
assert loves.is_linked
Get an edge from DB:
var url = loves.url.to_s
var edge = client.load_edge(url)
assert edge.from["name"].to_s == "Andres"
assert edge.to["name"].to_s == "Kate"
neo4j :: NeoEdge :: defaultinit
neo4j :: json_graph_store $ NeoEdge :: append_json_for
Append the JSON representation of the relationship to the specified buffer.neo4j :: NeoEntity :: append_json_for
Append the JSON representation of the entity to the specified buffer.core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
neo4j :: NeoEdge :: defaultinit
neo4j :: NeoEntity :: defaultinit
core :: Object :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).neo4j :: NeoEntity :: properties
In Neo4j, both nodes and relationships can contain properties.
# A relationship between two nodes.
# Relationships between nodes are a key part of a graph database.
# They allow for finding related data. Just like nodes, relationships can have properties.
#
# Create a relationship:
#
# var client = new Neo4jClient("http://neo4j:7474")
# # Create nodes
# var andres = new NeoNode
# andres["name"] = "Andres"
# var kate = new NeoNode
# kate["name"] = "Kate"
# # Create a relationship of type `LOVES`
# var loves = new NeoEdge(andres, "LOVES", kate)
# client.save_edge(loves)
# assert loves.is_linked
#
# Get an edge from DB:
#
# var url = loves.url.to_s
# var edge = client.load_edge(url)
# assert edge.from["name"].to_s == "Andres"
# assert edge.to["name"].to_s == "Kate"
class NeoEdge
super NeoEntity
private var internal_from: nullable NeoNode
private var internal_to: nullable NeoNode
private var internal_type: nullable String
private var internal_from_url: nullable String
private var internal_to_url: nullable String
init(from: NeoNode, rel_type: String, to: NeoNode) do
self.internal_from = from
self.internal_to = to
self.internal_type = rel_type
end
redef init from_neo(neo, url) do
super
var obj = neo.get(url).as(JsonObject)
self.internal_type = obj["type"].to_s
self.internal_from_url = obj["start"].to_s
self.internal_to_url = obj["end"].to_s
end
redef init from_json(neo, obj) do
super
self.internal_type = obj["type"].to_s
self.internal_from_url = obj["start"].to_s
self.internal_to_url = obj["end"].to_s
end
# Get `from` node
fun from: NeoNode do return internal_from or else load_from
private fun load_from: NeoNode do
var node = neo.load_node(internal_from_url.to_s)
internal_from = node
return node
end
# Get `to` node
fun to: NeoNode do return internal_to or else load_to
private fun load_to: NeoNode do
var node = neo.load_node(internal_to_url.to_s)
internal_to = node
return node
end
# Get edge type
fun rel_type: nullable String do return internal_type
# Get the JSON body of a REST request that create the relationship.
private fun to_rest: JsonObject do
var obj = new JsonObject
if to.is_linked then
obj["to"] = to.url
else
obj["to"] = "\{{to.batch_id.to_s}\}"
end
obj["type"] = rel_type
obj["data"] = properties
return obj
end
end
lib/neo4j/neo4j.nit:672,1--758,3
redef class NeoEdge
# Append the JSON representation of the relationship to the specified buffer.
#
# Use the IDs specfied by `graph.nodes`.
redef fun append_json_for(graph, v) do
v.stream.write "\{\"type\":"
rel_type.as(not null).serialize_to(v)
v.stream.write ",\"properties\":"
properties.serialize_to(v)
v.stream.write ",\"from\":"
graph.nodes.id_of(from).serialize_to(v)
v.stream.write ",\"to\":"
graph.nodes.id_of(to).serialize_to(v)
v.stream.write "}"
end
end
lib/neo4j/graph/json_graph_store.nit:290,1--306,3