class Dataflow::Nodes::UpsertNode

Public Instance Methods

add(records:) click to toggle source
# File lib/dataflow/nodes/upsert_node.rb, line 44
def add(records:)
  raise ArgumentError, "records must be an array of documents. Received: '#{records.class}'." unless records.is_a?(Array)
  records = records.compact
  return if records.blank?

  # TODO: create a chain of behavior "before add"
  rename_dotted_fields(records: records)
  add_internal_timestamp(records: records)

  db_adapter.save(records: records, replace_by: index_key)
  self.updated_at = Time.now
  save!
end
set_defaults() click to toggle source
Calls superclass method Dataflow::Nodes::DataNode#set_defaults
# File lib/dataflow/nodes/upsert_node.rb, line 16
def set_defaults
  super

  self.indexes ||= []
  # get rid of keys/string confusion
  self.indexes = JSON.parse(self.indexes.to_json)

  # if there is no index_key, take the first unique index
  if index_key.blank?
    first_unique_index = self.indexes.find { |x| x['unique'] }
    self.index_key = (first_unique_index || {})['key']
  end

  # add keys for the unique index keys
  if index_key.present?
    auto_generated_indexes = [{ 'key' => index_key, 'unique' => true }]

    if index_key.is_a? Array
      # generated non-unique indexes for each key in a compound index
      auto_generated_indexes += index_key.map { |idx| { 'key' => idx } }
    end
    self.indexes += auto_generated_indexes
    self.indexes.uniq!
  end

  self.updated_at ||= Time.now
end

Private Instance Methods

transform_index_key() click to toggle source
# File lib/dataflow/nodes/upsert_node.rb, line 60
def transform_index_key
  return unless index_key.is_a?(String)

  # try to split the comma separated string
  keys = index_key.split(',')
  # if there was no comma, leave as-is
  self.index_key = keys if keys.count > 1
end