Class: Chroma::Resources::Database

Inherits:
Object
  • Object
show all
Includes:
APIOperations::Request
Defined in:
lib/chroma/resources/database.rb

Overview

The Database class provides methods for interacting with the Chroma database server.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::Request

included

Constructor Details

#initialize(id:, name:, tenant:) ⇒ Database

Returns a new instance of Database.



15
16
17
18
19
# File 'lib/chroma/resources/database.rb', line 15

def initialize(id:, name:, tenant:)
  @id = id
  @name = name
  @tenant = tenant
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/chroma/resources/database.rb', line 11

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/chroma/resources/database.rb', line 12

def name
  @name
end

#tenantObject (readonly)

Returns the value of attribute tenant.



13
14
15
# File 'lib/chroma/resources/database.rb', line 13

def tenant
  @tenant
end

Class Method Details

.auth_identityObject

Get the current user’s identity, tenant, and databases of the Chroma database server.

Returns the current user’s identity, tenant, and databases of the Chroma database server.



24
25
26
27
28
29
30
# File 'lib/chroma/resources/database.rb', line 24

def self.auth_identity
  result = execute_request(:get, "#{Chroma.api_url}/auth/identity")

  return result.success.body if result.success?

  raise_failure_error(result)
end

.create(name) ⇒ Object

Create a new database on the tenant.

name - The name of the database.

Examples

database = Chorma::Resources::Database.create("database-name")

Returns true if the database was successfully created, raises Chroma::APIError otherwise.



114
115
116
117
118
119
120
121
122
# File 'lib/chroma/resources/database.rb', line 114

def self.create(name)
  payload = {name: name}

  result = execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases", payload)

  return true if result.success?

  raise_failure_error(result)
end

.delete(database_name) ⇒ Object

Deletes a database from the tenant.

database_name - The name of the database to retrieve.

Examples

Chroma::Resources::Database.delete("database-name")

Returns true if the database was successfully deleted, raises Chroma::APIError otherwise.



153
154
155
156
157
158
159
# File 'lib/chroma/resources/database.rb', line 153

def self.delete(database_name)
  result = execute_request(:delete, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{database_name}")

  return true if result.success?

  raise_failure_error(result)
end

.get(database_name) ⇒ Object

Retrieves a database from the tenant.

database_name - The name of the database to retrieve.

Examples

Chroma::Resources::Database.get("database-name")

Returns The retrieved database object. Raises Chroma::APIError if not found.



133
134
135
136
137
138
139
140
141
142
# File 'lib/chroma/resources/database.rb', line 133

def self.get(database_name)
  result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{database_name}")

  if result.success?
    data = result.success.body
    new(id: data["id"], name: data["name"], tenant: data["tenant"])
  else
    raise_failure_error(result)
  end
end

.healthcheckObject

Check the hearlthcheck of the Chroma database server.

Return a Hash with a boolean.



57
58
59
60
61
62
63
# File 'lib/chroma/resources/database.rb', line 57

def self.healthcheck
  result = execute_request(:get, "#{Chroma.api_url}/healthcheck")

  return result.success.body if result.success?

  raise_failure_error(result)
end

.heartbeatObject

Check the heartbeat of the Chroma database server.

Return a Hash with a timestamp.



68
69
70
71
72
73
74
# File 'lib/chroma/resources/database.rb', line 68

def self.heartbeat
  result = execute_request(:get, "#{Chroma.api_url}/heartbeat")

  return result.success.body if result.success?

  raise_failure_error(result)
end

.listObject

Lists all databases from the tenant.

Examples

Chroma::Resources::Database.list

Returns an array of all databases in the tenant.



94
95
96
97
98
99
100
101
102
103
# File 'lib/chroma/resources/database.rb', line 94

def self.list
  result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases")

  if result.success?
    data = result.success.body
    data.map { |item| new(id: item["id"], name: item["name"], tenant: item["tenant"]) }
  else
    raise_failure_error(result)
  end
end

.pre_flight_checksObject

Check the pre-flight checks of the Chroma database server.

Return a Hash with a timestamp.



79
80
81
82
83
84
85
# File 'lib/chroma/resources/database.rb', line 79

def self.pre_flight_checks
  result = execute_request(:get, "#{Chroma.api_url}/pre-flight-checks")

  return result.success.body if result.success?

  raise_failure_error(result)
end

.resetObject

Reset the Chroma database server. This can’t be undone.

Returns true on success or raise a Chroma::Error on failure.



46
47
48
49
50
51
52
# File 'lib/chroma/resources/database.rb', line 46

def self.reset
  result = execute_request(:post, "#{Chroma.api_url}/reset")

  return result.success.body if result.success?

  raise_failure_error(result)
end

.versionObject

Get the version of the Chroma database server.

Returns the version of the Chroma database server.



35
36
37
38
39
40
41
# File 'lib/chroma/resources/database.rb', line 35

def self.version
  result = execute_request(:get, "#{Chroma.api_url}/version")

  return result.success.body if result.success?

  raise_failure_error(result)
end

Instance Method Details

#collections_countObject

Count the total number of collections in the database object.

Examples

database = Chroma::Resources::Database.get("database-name")
database.collections_count

Returns the count of collections in the database.



169
170
171
172
173
174
175
# File 'lib/chroma/resources/database.rb', line 169

def collections_count
  result = self.class.execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{name}/collections_count")

  return result.success.body if result.success?

  self.class.raise_failure_error(result)
end