class Terrestrial::RelationalStore

Attributes

datastore[R]
dump_pipeline[R]
load_pipeline[R]
mapping_name[R]
mappings[R]

Public Class Methods

new(mappings:, mapping_name:, datastore:, load_pipeline:, dump_pipeline:, dataset: nil) click to toggle source
# File lib/terrestrial/relational_store.rb, line 10
def initialize(mappings:, mapping_name:, datastore:, load_pipeline:, dump_pipeline:, dataset: nil)
  @mappings = mappings
  @mapping_name = mapping_name
  @datastore = datastore
  @dataset = dataset
  @load_pipeline = load_pipeline
  @dump_pipeline = dump_pipeline
  @eager_data = {}
end

Public Instance Methods

all() click to toggle source
# File lib/terrestrial/relational_store.rb, line 47
def all
  self
end
changes(graph) click to toggle source
# File lib/terrestrial/relational_store.rb, line 37
def changes(graph)
  changes, _ = dump_pipeline
    .take_until(:remove_unchanged_fields)
    .call(
      serialize_graph(graph)
    )

  changes
end
changes_sql(graph) click to toggle source
# File lib/terrestrial/relational_store.rb, line 31
def changes_sql(graph)
  changes(graph).map { |record|
    datastore.changes_sql(record)
  }
end
delete(object) click to toggle source
# File lib/terrestrial/relational_store.rb, line 77
def delete(object)
  dump_pipeline.call(
    serialize_graph(object)
      .select { |record| record.depth == 0 }
      .reverse
      .take(1)
      .map { |record| DeletedRecord.new(mapping, record.attributes, 0) }
  )
end
each(&block) click to toggle source
# File lib/terrestrial/relational_store.rb, line 63
def each(&block)
  dataset
    .map { |record|
      graph_loader.call(mapping_name, record, @eager_data)
    }
    .each(&block)
end
eager_load(association_name_map) click to toggle source
# File lib/terrestrial/relational_store.rb, line 71
def eager_load(association_name_map)
  @eager_data = eager_load_associations(mapping, dataset, association_name_map)

  self
end
save(graph) click to toggle source
# File lib/terrestrial/relational_store.rb, line 23
def save(graph)
  record_dump = serialize_graph(graph)

  dump_pipeline.call(record_dump)

  self
end
subset(name, *params) click to toggle source
# File lib/terrestrial/relational_store.rb, line 57
def subset(name, *params)
  new_with_dataset(
    mapping.subsets.execute(dataset, name, *params)
  )
end
where(query) click to toggle source
# File lib/terrestrial/relational_store.rb, line 51
def where(query)
  new_with_dataset(
    dataset.where(query)
  )
end

Private Instance Methods

association_root_datasets(association) click to toggle source
# File lib/terrestrial/relational_store.rb, line 122
def association_root_datasets(association)
  association
    .mapping_names
    .map { |name| mappings.fetch(name) }
    .map(&:namespace)
    .map { |ns| datastore[ns] }
end
dataset() click to toggle source
# File lib/terrestrial/relational_store.rb, line 153
def dataset
  @dataset ||= datastore[mapping.namespace]
end
eager_load_associations(mapping, parent_dataset, association_name_map) click to toggle source
# File lib/terrestrial/relational_store.rb, line 93
def eager_load_associations(mapping, parent_dataset, association_name_map)
  Hash[
    association_name_map.map { |name, deeper_association_names|
      association = mapping.associations.fetch(name)
      association_mapping = mappings.fetch(association.mapping_name)
      association_dataset = get_eager_dataset(association, parent_dataset)

      [
        name,
        {
          superset: association_dataset,
          associations: eager_load_associations(
            association_mapping,
            association_dataset,
            deeper_association_names,
          ),
        }
      ]
    }
  ]
end
get_eager_dataset(association, parent_dataset) click to toggle source
# File lib/terrestrial/relational_store.rb, line 115
def get_eager_dataset(association, parent_dataset)
  association.eager_superset(
    association_root_datasets(association),
    parent_dataset,
  )
end
graph_loader() click to toggle source
# File lib/terrestrial/relational_store.rb, line 145
def graph_loader
  GraphLoader.new(
    datasets: datastore,
    mappings: mappings,
    object_load_pipeline: load_pipeline,
  )
end
graph_serializer() click to toggle source
# File lib/terrestrial/relational_store.rb, line 141
def graph_serializer
  GraphSerializer.new(mappings: mappings)
end
inspectable_properties() click to toggle source
# File lib/terrestrial/relational_store.rb, line 161
def inspectable_properties
  [
    :mapping_name,
    :dataset,
    :eager_load,
  ]
end
mapping() click to toggle source
# File lib/terrestrial/relational_store.rb, line 157
def mapping
  mappings.fetch(mapping_name)
end
new_with_dataset(new_dataset) click to toggle source
# File lib/terrestrial/relational_store.rb, line 130
def new_with_dataset(new_dataset)
  self.class.new(
    dataset: new_dataset,
    mappings: mappings,
    mapping_name: mapping_name,
    datastore: datastore,
    load_pipeline: load_pipeline,
    dump_pipeline: dump_pipeline,
  )
end
serialize_graph(graph) click to toggle source
# File lib/terrestrial/relational_store.rb, line 89
def serialize_graph(graph)
  graph_serializer.call(mapping_name, graph)
end