Class: WeaviateRecord::Connection

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

Overview

This module will act as a connection to Weaviate database

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_name = nil) ⇒ Connection

Creates a new Connection to the Weaviate database.



12
13
14
15
16
17
18
19
20
# File 'lib/weaviate_record/connection.rb', line 12

def initialize(collection_name = nil)
  @collection_name = collection_name&.to_s
  @client = Weaviate::Client.new(
    url: ENV.fetch('WEAVIATE_DATABASE_URL'),
    api_key: ENV.fetch('WEAVIATE_API_KEY', nil),
    model_service: ENV['WEAVIATE_VECTORIZER_MODULE']&.to_sym,
    model_service_api_key: ENV.fetch('WEAVIATE_VECTORIZER_API_KEY', nil)
  )
end

Instance Attribute Details

#clientObject (readonly)

Returns a Weaviate::Client object, which is used to interact with the Weaviate database.



9
10
11
# File 'lib/weaviate_record/connection.rb', line 9

def client
  @client
end

Instance Method Details

#check_existence(id) ⇒ Object



55
56
57
58
59
# File 'lib/weaviate_record/connection.rb', line 55

def check_existence(id)
  client.objects.exists?(class_name: @collection_name, id: id)
rescue Faraday::ConnectionFailed => e
  raise WeaviateRecord::Errors::DatabaseNotConnected, e.message, cause: nil
end

#create_call(properties) ⇒ Object



37
38
39
40
41
# File 'lib/weaviate_record/connection.rb', line 37

def create_call(properties)
  client.objects.create(class_name: @collection_name, properties: properties)
rescue Faraday::ConnectionFailed => e
  raise WeaviateRecord::Errors::DatabaseNotConnected, e.message, cause: nil
end

#delete_call(id) ⇒ Object



49
50
51
52
53
# File 'lib/weaviate_record/connection.rb', line 49

def delete_call(id)
  client.objects.delete(class_name: @collection_name, id: id)
rescue Faraday::ConnectionFailed => e
  raise WeaviateRecord::Errors::DatabaseNotConnected, e.message, cause: nil
end

#delete_where(condition) ⇒ Object



61
62
63
64
65
# File 'lib/weaviate_record/connection.rb', line 61

def delete_where(condition)
  client.objects.batch_delete(class_name: @collection_name, where: condition)
rescue Faraday::ConnectionFailed => e
  raise WeaviateRecord::Errors::DatabaseNotConnected, e.message, cause: nil
end

#find_call(id) ⇒ Object

:enddoc:



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

def find_call(id)
  client.objects.get(class_name: @collection_name, id: id)
rescue Faraday::ConnectionFailed => e
  raise WeaviateRecord::Errors::DatabaseNotConnected, e.message, cause: nil
end

#schema_listObject

Returns the present schema of the Weaviate database.



23
24
25
26
27
# File 'lib/weaviate_record/connection.rb', line 23

def schema_list
  client.schema.list.deep_symbolize_keys!
rescue Faraday::ConnectionFailed => e
  raise WeaviateRecord::Errors::DatabaseNotConnected, e.message, cause: nil
end

#update_call(id, properties) ⇒ Object



43
44
45
46
47
# File 'lib/weaviate_record/connection.rb', line 43

def update_call(id, properties)
  client.objects.update(class_name: @collection_name, id: id, properties: properties)
rescue Faraday::ConnectionFailed => e
  raise WeaviateRecord::Errors::DatabaseNotConnected, e.message, cause: nil
end