Skip to content

Instantly share code, notes, and snippets.

@willricketts
Created November 6, 2019 21:27
Show Gist options
  • Save willricketts/7606b7c5a9ca5cc21386b89b952c300e to your computer and use it in GitHub Desktop.
Save willricketts/7606b7c5a9ca5cc21386b89b952c300e to your computer and use it in GitHub Desktop.
Elixir module for persisting a list of Rhea SolarSystem structs into Neo4j
defmodule GalaxyMap.Importers.MapGraph do
@moduledoc """
Responsible for building the game map within Neo4j
- Creates each solar system within Neo4j
- Builds relationships between solar system nodes within Neo4j
"""
def run(solar_systems) do
solar_systems
|> Enum.map(fn(system) -> persist_system(system) end)
|> Enum.map(fn(system) -> build_connections(system) end)
|> Enum.filter(fn(system_query) -> system_query != nil end)
|> Enum.map(fn(system_query) -> execute_query(system_query) end)
end
def clear_graph_data do
execute_query("MATCH (n) DETACH DELETE n")
end
defp persist_system(system) do
execute_query(persist_system_query(system))
system
end
defp persist_system_query(system) do
"CREATE (s:SolarSystem:#{system.name} \
{ id: \"#{system.id}\", name: '#{system.name}',\
x: #{system.x}, y: #{system.y}, import_id: #{system.import_id},\
parent: #{system.parent}})"
end
defp neo4j_client do
Bolt.Sips.conn()
end
defp execute_query(query) do
Bolt.Sips.query!(neo4j_client(), query)
end
def build_connections(system) do
case system.import_id do
0 -> nil
2000 -> nil
_ ->
"MATCH (o:SolarSystem),(d:SolarSystem) \
WHERE o.import_id = #{system.import_id} AND d.import_id = #{system.parent} \
WITH o, d \
CREATE (o)-[x:Gate]->(d) \
WITH d, o \
CREATE (d)-[y:Gate]->(o) \
RETURN o, d;"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment