Class: ForestAdminDatasourceMongoid::Utils::Pipeline::LookupGenerator
- Inherits:
-
Object
- Object
- ForestAdminDatasourceMongoid::Utils::Pipeline::LookupGenerator
- Includes:
- Schema
- Defined in:
- lib/forest_admin_datasource_mongoid/utils/pipeline/lookup_generator.rb
Class Method Summary collapse
- .add_fields(name, projection, options) ⇒ Object
-
.lookup(model, stack, projection, options) ⇒ Object
Transform a forest admin projection into a mongo pipeline that performs the lookups and transformations to target them.
- .lookup_projection(current_path, schema_stack, projection, options) ⇒ Object
- .lookup_relation(current_path, schema_stack, name, projection, options) ⇒ Object
Class Method Details
.add_fields(name, projection, options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/lookup_generator.rb', line 34 def self.add_fields(name, projection, ) return {} if [:include] && ![:include].include?(name) return {} if [:exclude]&.include?(name) projection.filter { |field| field.include?('@@@') } .map { |field_name| "#{name}.#{field_name.tr(":", ".")}" } .each_with_object({}) do |curr, acc| acc[curr] = "$#{curr.gsub("@@@", ".")}" end end |
.lookup(model, stack, projection, options) ⇒ Object
Transform a forest admin projection into a mongo pipeline that performs the lookups and transformations to target them
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/lookup_generator.rb', line 9 def self.lookup(model, stack, projection, ) schema_stack = stack.each_with_index.reduce([MongoidSchema.from_model(model)]) do |acc, (_, index)| [ *acc, MongoidSchema.from_model(model).apply_stack(stack.slice(0..(index + 1)), skip_as_models: true) ] end lookup_projection(nil, schema_stack.map(&:fields), projection, ) end |
.lookup_projection(current_path, schema_stack, projection, options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/lookup_generator.rb', line 20 def self.lookup_projection(current_path, schema_stack, projection, ) pipeline = [] fields = {} projection.relations.each do |name, relation_projection| pipeline.push(*lookup_relation(current_path, schema_stack, name, relation_projection, )) # pipeline = [*pipeline, *lookup_relation(current_path, schema_stack, name, relation_projection, options)] fields.merge!(add_fields(name, relation_projection, )) end pipeline.push({ '$addFields' => fields }) unless fields.empty? pipeline end |
.lookup_relation(current_path, schema_stack, name, projection, options) ⇒ Object
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/forest_admin_datasource_mongoid/utils/pipeline/lookup_generator.rb', line 45 def self.lookup_relation(current_path, schema_stack, name, projection, ) models = ObjectSpace .each_object(Class) .select { |klass| klass < Mongoid::Document && klass.name && !klass.name.start_with?('Mongoid::') } .to_h { |klass| [klass.name, klass] } as = current_path ? "#{current_path}.#{name}" : name last_schema = schema_stack[schema_stack.length - 1] previous_schema = schema_stack.slice(0..(schema_stack.length - 1)) return {} if [:include] && ![:include].include?(as) return {} if [:exclude]&.include?(as) # Native many to one relation identifier = '__many_to_one' if name.end_with?(identifier) foreign_key_name = name[0..(name.length - identifier.length - 1)] model = models[last_schema[foreign_key_name].[:association].class_name] from = model.name.gsub('::', '__') local_field = current_path ? "#{current_path}.#{foreign_key_name}" : foreign_key_name foreign_field = '_id' sub_schema = MongoidSchema.from_model(model).fields return [ # Push lookup to pipeline { '$lookup' => { 'from' => from, 'localField' => local_field, 'foreignField' => foreign_field, 'as' => as } }, { '$unwind' => { 'path' => "$#{as}", 'preserveNullAndEmptyArrays' => true } }, # Recurse to get relations of relations *lookup_projection(as, [*schema_stack, sub_schema], projection, ) ] end # inverse of fake relation if name == 'parent' && !previous_schema.empty? return lookup_projection(as, previous_schema, projection, ) end # fake relation return lookup_projection(as, [*schema_stack, last_schema[name]], projection, ) if last_schema[name] # We should have handled all possible cases. raise ForestAdminDatasourceToolkit::Exceptions::ForestException, "Unexpected relation: '#{name}'" end |