Class: WeaviateRecord::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/weaviate_record/schema.rb

Overview

This module contains methods that helps to build, maintain and read data from weaviate schema

Constant Summary collapse

STRUCTURE_FILE_BOILERPLATE =

:stopdoc:

lambda do |schema|
  <<~RUBY
    # frozen_string_literal: true

    module WeaviateRecord
      # This class stores the schema of all Weaviate Collections
      # Don't change it manually, use the WeaviateRecord::Schema.update! method to update the schema
      class Schema
        def self.all_collections # rubocop:disable Metrics/MethodLength
          #{schema}
        end
      end
    end
  RUBY
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Returns a new instance of Schema.



82
83
84
# File 'lib/weaviate_record/schema.rb', line 82

def initialize(schema)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

This attribute returns the schema of the collection in Hash format



96
97
98
# File 'lib/weaviate_record/schema.rb', line 96

def schema
  @schema
end

Class Method Details

.find_collection(klass) ⇒ Object

This method returns the weaviate schema for the given collection class



49
50
51
52
53
54
55
56
57
# File 'lib/weaviate_record/schema.rb', line 49

def find_collection(klass)
  load WeaviateRecord.config.schema_file_path
  collection_schema = all_collections[:classes].find { |collection| collection[:class] == klass.to_s }
  if collection_schema.nil?
    raise WeaviateRecord::Errors::CollectionNotFound, "Collection #{klass} not found in the schema"
  end

  new(collection_schema)
end

.synced?Boolean

This method checks if the local schema file is in sync with the schema in Weaviate database

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/weaviate_record/schema.rb', line 39

def synced?
  if File.exist?(WeaviateRecord.config.schema_file_path)
    load WeaviateRecord.config.schema_file_path
    WeaviateRecord::Schema.all_collections == schema_list
  else
    false
  end
end

.update!Object

This method updates the local schema file with the latest schema from Weaviate The schema file path is configured by setting WeaviateRecord.config.schema_file_path

If the rubocop is installed in the system, it will format the generated schema file too.



31
32
33
34
35
36
# File 'lib/weaviate_record/schema.rb', line 31

def update!
  create_weaviate_db_dir!
  File.write(WeaviateRecord.config.schema_file_path, STRUCTURE_FILE_BOILERPLATE[pretty_schema])
  rubocop_format_file
  nil
end

Instance Method Details

#attributes_listObject

:startdoc: This method returns the list of attributes for the collection

Usage

WeaviateRecord::Schema.find_collection(Article).attributes_list


91
92
93
# File 'lib/weaviate_record/schema.rb', line 91

def attributes_list
  @schema[:properties].map { |property| property[:name] }
end