module ActiveGraph::Node::Persistence
Public Instance Methods
TODO: This does not seem like it should be the responsibility of the node. Creates an unwrapped node in the database. @param [Hash] node_props The type-converted properties to be added to the new node. @param [Array] labels The labels to use for creating the new node. @return [ActiveGraph::Node] A CypherNode or EmbeddedNode
# File lib/active_graph/node/persistence.rb 68 def _create_node(node_props, labels = labels_for_create) 69 query = "CREATE (n:`#{Array(labels).join('`:`')}`) SET n = $props RETURN n" 70 neo4j_query(query, {props: node_props}, wrap: false).to_a[0][:n] 71 end
Increments concurrently a numeric attribute by a centain amount @param [Symbol, String] attribute name of the attribute to increment @param [Integer, Float] by amount to increment
# File lib/active_graph/node/persistence.rb 35 def concurrent_increment!(attribute, by = 1) 36 increment_by_query! query_as(:n), attribute, by 37 end
Creates a model with values matching those of the instance attributes and returns its id. @private @return true
# File lib/active_graph/node/persistence.rb 56 def create_model 57 node = _create_node(props_for_create) 58 init_on_load(node, node.properties) 59 @deferred_nodes = nil 60 true 61 end
As the name suggests, this inserts the primary key (id property) into the properties hash. The method called here, `default_property_values`, is a holdover from an earlier version of the gem. It does NOT contain the default values of properties, it contains the Default Property
, which we now refer to as the ID Property
. It will be deprecated and renamed in a coming refactor. @param [Hash] converted_props A hash of properties post-typeconversion, ready for insertion into the DB.
# File lib/active_graph/node/persistence.rb 78 def inject_primary_key!(converted_props) 79 self.class.default_property_values(self).tap do |destination_props| 80 destination_props.merge!(converted_props) if converted_props.is_a?(Hash) 81 end 82 end
@return [Array] Labels
to be set on the node during a create event
# File lib/active_graph/node/persistence.rb 85 def labels_for_create 86 self.class.mapped_label_names 87 end
Saves the model.
If the model is new a record gets created in the database, otherwise the existing record gets updated. If perform_validation is true validations run. If any of them fail the action is cancelled and save returns false. If the flag is false validations are bypassed altogether. See ActiveRecord::Validations for more information. There's a series of callbacks associated with save. If any of the before_* callbacks return false the action is cancelled and save returns false.
# File lib/active_graph/node/persistence.rb 25 def save(*) 26 cascade_save do 27 association_proxy_cache.clear 28 create_or_update 29 end 30 end
Persist the object to the database. Validations
and Callbacks
are included by default but validation can be disabled by passing :validate => false to save!
Creates a new transaction.
@raise a RecordInvalidError
if there is a problem during save. @param (see ActiveGraph::Rails::Validations#save) @return nil @see save
@see ActiveGraph::Rails::Validations ActiveGraph::Rails::Validations - for the :validate parameter @see ActiveGraph::Rails::Callbacks ActiveGraph::Rails::Callbacks - for callbacks
# File lib/active_graph/node/persistence.rb 49 def save!(*args) 50 save(*args) or fail(RecordInvalidError, self) # rubocop:disable Style/AndOr 51 end
Private Instance Methods
The pending associations are cleared during the save process, so it's necessary to build the processable hash before it begins.
# File lib/active_graph/node/persistence.rb 97 def cascade_save 98 ActiveGraph::Base.transaction do 99 yield.tap { process_unpersisted_nodes! } 100 end 101 end
# File lib/active_graph/node/persistence.rb 91 def destroy_query 92 query_as(:n).break.optional_match('(n)-[r]-()').delete(:n, :r) 93 end