Class: Exwiw::Adapter::MongodbAdapter
- Defined in:
- lib/exwiw/adapter/mongodb_adapter.rb
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #build_query(config, dump_target, config_by_name) ⇒ Object
- #dumpable?(config) ⇒ Boolean
- #execute(query) ⇒ Object
-
#initialize(connection_config, logger) ⇒ MongodbAdapter
constructor
A new instance of MongodbAdapter.
- #output_extension ⇒ Object
- #supports_bulk_delete? ⇒ Boolean
- #to_bulk_delete(_query, _config) ⇒ Object
-
#to_bulk_insert(rows, config) ⇒ Object
NOTE: relies on @embedded_children_by_parent set by a prior build_query call for the same config.
- #validate_as_dump_target!(config) ⇒ Object
Constructor Details
#initialize(connection_config, logger) ⇒ MongodbAdapter
Returns a new instance of MongodbAdapter.
16 17 18 19 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 16 def initialize(connection_config, logger) super @state = {} end |
Class Method Details
.table_config_class ⇒ Object
12 13 14 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 12 def self.table_config_class Exwiw::MongodbCollectionConfig end |
Instance Method Details
#build_query(config, dump_target, config_by_name) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 33 def build_query(config, dump_target, config_by_name) if config. raise NotImplementedError, "MongodbAdapter#build_query was called with embedded config '#{config.name}'. " \ "Embedded configs are masked through the parent collection." end reject_filter!(config) # Stash the embedded-children index for the matching to_bulk_insert call # below. The Adapter contract does not pass config_by_name to # to_bulk_insert (SQL adapters don't need it), so we rely on the Runner # invariant that build_query is always called before to_bulk_insert for # the same config. @embedded_children_by_parent = (config_by_name) 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 |
#dumpable?(config) ⇒ Boolean
21 22 23 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 21 def dumpable?(config) !config. end |
#execute(query) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 73 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_extension ⇒ Object
100 101 102 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 100 def output_extension 'jsonl' end |
#supports_bulk_delete? ⇒ Boolean
104 105 106 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 104 def supports_bulk_delete? false end |
#to_bulk_delete(_query, _config) ⇒ Object
96 97 98 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 96 def to_bulk_delete(_query, _config) raise NotImplementedError, "MongodbAdapter does not support bulk delete" end |
#to_bulk_insert(rows, config) ⇒ Object
NOTE: relies on @embedded_children_by_parent set by a prior build_query call for the same config. This implicit ordering exists because the Adapter contract intentionally does not thread config_by_name through to_bulk_insert (SQL adapters don’t need it). Safe in Runner, fragile in tests — call build_query first.
88 89 90 91 92 93 94 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 88 def to_bulk_insert(rows, config) rows.map do |doc| apply_replace_with!(doc, config) (doc, config) JSON.generate(extended_json(doc)) end.join("\n") end |
#validate_as_dump_target!(config) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 25 def validate_as_dump_target!(config) return unless config. raise NotImplementedError, "dump_target '#{config.name}' is an embedded MongodbCollectionConfig; " \ "specify a top-level collection instead." end |