Class: Neo4j::Http::NodeClient

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/http/node_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cypher_client) ⇒ NodeClient

Returns a new instance of NodeClient.



10
11
12
# File 'lib/neo4j/http/node_client.rb', line 10

def initialize(cypher_client)
  @cypher_client = cypher_client
end

Class Method Details

.defaultObject



6
7
8
# File 'lib/neo4j/http/node_client.rb', line 6

def self.default
  @default ||= new(CypherClient.default)
end

Instance Method Details

#delete_node(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/neo4j/http/node_client.rb', line 29

def delete_node(node)
  cypher = <<-CYPHER
    MATCH (node:#{node.label} {#{node.key_name}: $key_value})
    WITH node
    DETACH DELETE node
    RETURN node
  CYPHER

  results = @cypher_client.execute_cypher(cypher, key_value: node.key_value)
  results.first&.fetch("node")
end

#find_node_by(label:, **attributes) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/neo4j/http/node_client.rb', line 41

def find_node_by(label:, **attributes)
  selectors = attributes.map { |key, value| "#{key}: $attributes.#{key}" }.join(", ")
  cypher = "MATCH (node:#{label} { #{selectors} }) RETURN node LIMIT 1"
  results = @cypher_client.execute_cypher(cypher, attributes: attributes.merge(access_mode: "READ"))
  return if results.empty?

  results.first&.fetch("node")
end

#find_nodes_by(label:, attributes:, limit: 100) ⇒ Object



50
51
52
53
54
55
# File 'lib/neo4j/http/node_client.rb', line 50

def find_nodes_by(label:, attributes:, limit: 100)
  selectors = build_selectors(attributes)
  cypher = "MATCH (node:#{label}) where #{selectors} RETURN node LIMIT #{limit}"
  results = @cypher_client.execute_cypher(cypher, attributes: attributes.merge(access_mode: "READ"))
  results.map { |result| result["node"] }
end

#upsert_node(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/neo4j/http/node_client.rb', line 14

def upsert_node(node)
  raise "#{node.key_name} value cannot be blank - (node keys: #{node.to_h.keys})" if node.key_value.blank?

  cypher = <<-CYPHER
    MERGE (node:#{node.label} {#{node.key_name}: $key_value})
    ON CREATE SET node += $attributes
    ON MATCH SET node += $attributes
    return node
  CYPHER

  results = @cypher_client.execute_cypher(cypher, key_value: node.key_value, attributes: node.attributes)

  results.first&.fetch("node")
end