Module: Legion::Extensions::Neo4j::Runners::Relationships
- Includes:
- Helpers::Lex, Helpers::Client
- Included in:
- Client
- Defined in:
- lib/legion/extensions/neo4j/runners/relationships.rb
Instance Method Summary collapse
- #create_relationship(from_id:, to_id:, type:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
- #delete_relationship(id:, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
- #find_relationships(type:, properties: {}, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
- #get_relationship(id:, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
- #list_relationship_types(database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
- #merge_relationship(from_id:, to_id:, type:, on_create: {}, on_match: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
- #neighbors(id:, direction: :both, type: nil, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
- #shortest_path(from_id:, to_id:, max_depth: 15, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
- #update_relationship(id:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
Methods included from Helpers::Client
Instance Method Details
#create_relationship(from_id:, to_id:, type:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 28 def create_relationship(from_id:, to_id:, type:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false, **) raise ReadOnlyError, 'Write operations disabled (read_only mode)' if read_only cypher = 'MATCH (a), (b) WHERE elementId(a) = $from_id AND elementId(b) = $to_id ' \ "CREATE (a)-[r:#{type} $props]->(b) RETURN a, r, b" execute_cypher(cypher, parameters: { from_id: from_id, to_id: to_id, props: properties }, database: database, url: url, username: username, password: password) end |
#delete_relationship(id:, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 48 def delete_relationship(id:, 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 ()-[r]->() WHERE elementId(r) = $id DELETE r', parameters: { id: id }, database: database, url: url, username: username, password: password) end |
#find_relationships(type:, properties: {}, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 12 def find_relationships(type:, properties: {}, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil, **) where_clause = properties.keys.map { |k| "r.#{k} = $#{k}" }.join(' AND ') cypher = "MATCH (a)-[r:#{type}]->(b)" cypher += " WHERE #{where_clause}" unless where_clause.empty? cypher += ' RETURN a, r, b LIMIT $limit' execute_cypher(cypher, parameters: properties.merge(limit: limit), database: database, url: url, username: username, password: password) end |
#get_relationship(id:, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
22 23 24 25 26 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 22 def get_relationship(id:, database: 'neo4j', url: nil, username: nil, password: nil, **) execute_cypher('MATCH (a)-[r]->(b) WHERE elementId(r) = $id RETURN a, r, b', parameters: { id: id }, database: database, url: url, username: username, password: password) end |
#list_relationship_types(database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
71 72 73 74 75 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 71 def list_relationship_types(database: 'neo4j', url: nil, username: nil, password: nil, **) execute_cypher('CALL db.relationshipTypes() YIELD relationshipType RETURN relationshipType', parameters: {}, database: database, url: url, username: username, password: password) end |
#merge_relationship(from_id:, to_id:, type:, on_create: {}, on_match: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 57 def merge_relationship(from_id:, to_id:, type:, 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 = 'MATCH (a), (b) WHERE elementId(a) = $from_id AND elementId(b) = $to_id ' \ "MERGE (a)-[r:#{type}]->(b)" cypher += ' ON CREATE SET r += $on_create' unless on_create.empty? cypher += ' ON MATCH SET r += $on_match' unless on_match.empty? cypher += ' RETURN a, r, b' execute_cypher(cypher, parameters: { from_id: from_id, to_id: to_id, on_create: on_create, on_match: on_match }, database: database, url: url, username: username, password: password) end |
#neighbors(id:, direction: :both, type: nil, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 77 def neighbors(id:, direction: :both, type: nil, limit: 25, database: 'neo4j', url: nil, username: nil, password: nil, **) rel = type ? ":#{type}" : '' pattern = case direction when :outgoing then "(a)-[r#{rel}]->(b)" when :incoming then "(a)<-[r#{rel}]-(b)" else "(a)-[r#{rel}]-(b)" end cypher = "MATCH #{pattern} WHERE elementId(a) = $id RETURN b, r LIMIT $limit" execute_cypher(cypher, parameters: { id: id, limit: limit }, database: database, url: url, username: username, password: password) end |
#shortest_path(from_id:, to_id:, max_depth: 15, database: 'neo4j', url: nil, username: nil, password: nil) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 90 def shortest_path(from_id:, to_id:, max_depth: 15, database: 'neo4j', url: nil, username: nil, password: nil, **) cypher = 'MATCH (a), (b) WHERE elementId(a) = $from_id AND elementId(b) = $to_id ' \ "MATCH p = shortestPath((a)-[*..#{max_depth}]-(b)) RETURN p" execute_cypher(cypher, parameters: { from_id: from_id, to_id: to_id }, database: database, url: url, username: username, password: password) end |
#update_relationship(id:, properties: {}, database: 'neo4j', url: nil, username: nil, password: nil, read_only: false) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/legion/extensions/neo4j/runners/relationships.rb', line 39 def update_relationship(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 ()-[r]->() WHERE elementId(r) = $id SET r += $props RETURN r', parameters: { id: id, props: properties }, database: database, url: url, username: username, password: password) end |