Class: Exwiw::Adapter::MongodbAdapter

Inherits:
Base
  • Object
show all
Defined in:
lib/exwiw/adapter/mongodb_adapter.rb

Instance Attribute Summary

Attributes inherited from Base

#connection_config

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_config, logger) ⇒ MongodbAdapter

Returns a new instance of MongodbAdapter.



17
18
19
20
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 17

def initialize(connection_config, logger)
  super
  @state = {}
end

Class Method Details

.table_config_classObject



13
14
15
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 13

def self.table_config_class
  Exwiw::MongodbCollectionConfig
end

Instance Method Details

#build_query(config, dump_target, _config_by_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 22

def build_query(config, dump_target, _config_by_name)
  reject_filter!(config)

  filter =
    if config.name == dump_target.table_name
      { config.primary_key => { "$in" => coerce_ids(dump_target.ids) } }
    else
      constrained = config.belongs_tos.select do |relation|
        @state.key?(relation.table_name) && !@state[relation.table_name].empty?
      end

      if constrained.empty?
        {}
      else
        constrained.each_with_object({}) do |relation, acc|
          acc[relation.foreign_key] = { "$in" => @state[relation.table_name] }
        end
      end
    end

  Exwiw::MongoQuery::Find.new(
    collection: config.name,
    primary_key: config.primary_key,
    filter: filter,
    projection: build_projection(config),
  )
end

#execute(query) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 50

def execute(query)
  @logger.debug("  Executing Mongo find on '#{query.collection}': filter=#{query.filter.inspect} projection=#{query.projection.inspect}")

  docs = db[query.collection].find(query.filter).projection(query.projection).to_a

  @state[query.collection] = docs.map { |doc| doc[query.primary_key] }

  docs
end

#output_extensionObject



71
72
73
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 71

def output_extension
  'jsonl'
end

#supports_bulk_delete?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 75

def supports_bulk_delete?
  false
end

#to_bulk_delete(_query, _config) ⇒ Object

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 67

def to_bulk_delete(_query, _config)
  raise NotImplementedError, "MongodbAdapter does not support bulk delete"
end

#to_bulk_insert(rows, config) ⇒ Object



60
61
62
63
64
65
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 60

def to_bulk_insert(rows, config)
  rows.map do |doc|
    apply_replace_with!(doc, config)
    JSON.generate(extended_json(doc))
  end.join("\n")
end