Module: Legion::Extensions::Neo4j::Runners::Nodes

Includes:
Helpers::Lex, Helpers::Client
Included in:
Client
Defined in:
lib/legion/extensions/neo4j/runners/nodes.rb

Instance Method Summary collapse

Methods included from Helpers::Client

#connection, #execute_cypher

Instance Method Details

#count_nodes(label: nil, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object



68
69
70
71
72
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 68

def count_nodes(label: nil, database: 'neo4j', url: nil, username: nil, password: nil, **)
  cypher = label ? "MATCH (n:#{label}) RETURN count(n) AS count" : 'MATCH (n) RETURN count(n) AS count'
  execute_cypher(cypher, parameters: {}, database: database,
                        url: url, username: username, password: password)
end

#create_node(label:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 27

def create_node(label:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil,
                read_only: false, **)
  raise ReadOnlyError, 'Write operations disabled (read_only mode)' if read_only

  execute_cypher("CREATE (n:#{label} $props) RETURN n",
                 parameters: { props: properties }, database: database,
                 url: url, username: username, password: password)
end

#delete_node(id:, detach: false, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 45

def delete_node(id:, detach: false, database: 'neo4j', url: nil, username: nil, password: nil,
                read_only: false, **)
  raise ReadOnlyError, 'Write operations disabled (read_only mode)' if read_only

  delete_keyword = detach ? 'DETACH DELETE' : 'DELETE'
  execute_cypher("MATCH (n) WHERE elementId(n) = $id #{delete_keyword} n",
                 parameters: { id: id }, database: database,
                 url: url, username: username, password: password)
end

#find_nodes(label:, properties: {}, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 12

def find_nodes(label:, properties: {}, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil, **)
  where_clause = properties.keys.map { |k| "n.#{k} = $#{k}" }.join(' AND ')
  cypher = "MATCH (n:#{label})"
  cypher += " WHERE #{where_clause}" unless where_clause.empty?
  cypher += ' RETURN n LIMIT $limit'
  execute_cypher(cypher, parameters: properties.merge(limit: limit),
                        database: database, url: url, username: username, password: password)
end

#get_node(id:, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object



21
22
23
24
25
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 21

def get_node(id:, database: 'neo4j', url: nil, username: nil, password: nil, **)
  execute_cypher('MATCH (n) WHERE elementId(n) = $id RETURN n',
                 parameters: { id: id }, database: database,
                 url: url, username: username, password: password)
end

#list_labels(database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object



74
75
76
77
78
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 74

def list_labels(database: 'neo4j', url: nil, username: nil, password: nil, **)
  execute_cypher('CALL db.labels() YIELD label RETURN label',
                 parameters: {}, database: database,
                 url: url, username: username, password: password)
end

#merge_node(label:, match_properties: {}, on_create: {}, on_match: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 55

def merge_node(label:, match_properties: {}, on_create: {}, on_match: {}, database: 'neo4j',
               url: nil, username: nil, password: nil, read_only: false, **)
  raise ReadOnlyError, 'Write operations disabled (read_only mode)' if read_only

  cypher = "MERGE (n:#{label} $match_props)"
  cypher += ' ON CREATE SET n += $on_create' unless on_create.empty?
  cypher += ' ON MATCH SET n += $on_match' unless on_match.empty?
  cypher += ' RETURN n'
  execute_cypher(cypher,
                 parameters: { match_props: match_properties, on_create: on_create, on_match: on_match },
                 database: database, url: url, username: username, password: password)
end

#update_node(id:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/neo4j/runners/nodes.rb', line 36

def update_node(id:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil,
                read_only: false, **)
  raise ReadOnlyError, 'Write operations disabled (read_only mode)' if read_only

  execute_cypher('MATCH (n) WHERE elementId(n) = $id SET n += $props RETURN n',
                 parameters: { id: id, props: properties }, database: database,
                 url: url, username: username, password: password)
end