Sha256: 17fe15a9282c8f8782be64487b25a9431334bacf6fc639d3732b9ec5859bcb7f

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

module ForestAdminDatasourceToolkit
  module Utils
    class Schema
      def self.foreign_key?(collection, name)
        field = collection.fields[name]

        field.type == 'Column' &&
          collection.fields.any? do |_key, relation|
            relation.type == 'ManyToOne' && relation.foreign_key == name
          end
      end

      def self.primary_key?(collection, name)
        field = collection.fields[name]

        field.type == 'Column' && field.is_primary_key
      end

      def self.primary_keys(collection)
        collection.fields.keys.select do |field_name|
          field = collection.fields[field_name]
          field.type == 'Column' && field.is_primary_key
        end
      end

      def self.get_to_many_relation(collection, relation_name)
        unless collection.fields.key?(relation_name)
          raise Exceptions::ForestException, "Relation #{relation_name} not found"
        end

        relation = collection.fields[relation_name]

        if relation.type != 'OneToMany' && relation.type != 'ManyToMany'
          raise Exceptions::ForestException,
                "Relation #{relation_name} has invalid type should be one of OneToMany or ManyToMany."
        end

        relation
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
forest_admin_datasource_toolkit-1.0.0.pre.beta.23 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.22 lib/forest_admin_datasource_toolkit/utils/schema.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.21 lib/forest_admin_datasource_toolkit/utils/schema.rb