Class: Minds::Datasources

Inherits:
Object
  • Object
show all
Defined in:
lib/minds/datasources.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Datasources

Returns a new instance of Datasources.



28
29
30
# File 'lib/minds/datasources.rb', line 28

def initialize(client:)
  @client = client
end

Instance Method Details

#allArray<Datasource>

Return a list of datasources

Examples:

datasources = datasources.all
datasources.each { |ds| puts ds.name }

Returns:

  • (Array<Datasource>)

    An array of Datasource objects



75
76
77
78
79
80
81
82
83
84
# File 'lib/minds/datasources.rb', line 75

def all
  data = @client.get(path: "datasources")
  return [] if data.empty?

  data.each_with_object([]) do |item, ds_list|
    next if item["engine"].nil?

    ds_list << Datasource.new(**item.transform_keys(&:to_sym))
  end
end

#create(ds_config, update = false) ⇒ Datasource

Create a new datasource and return it

Examples:

config = DatabaseConfig.new(
  name: 'sales_db',
  engine: 'postgres',
  connection_data: {
    host: 'localhost',
    port: 5432,
    user_name: "test"
    password: "test"
  }
)
datasource = datasources.create(config)

Parameters:

  • ds_config (DatabaseConfig)

    datasource configuration

  • update (Boolean) (defaults to: false)

    If true, to update datasourse if exists, default is false

Options Hash (ds_config):

  • :name (String)

    Name of the datasource

  • :engine (String)

    Type of database handler (e.g., ‘postgres’, ‘mysql’)

  • :description (String)

    Description of the database. Used by mind to understand what data can be retrieved from it.

  • :connection_data (Hash) — default: optional

    Credentials to connect to the database

  • :tables (Array<String>) — default: optional

    List of allowed tables

Returns:

Raises:



57
58
59
60
61
62
63
64
65
# File 'lib/minds/datasources.rb', line 57

def create(ds_config, update = false)
  name = ds_config.name

  path = "datasources"
  path += "/#{name}" if update

  @client.send(update ? :put : :post, path: path, parameters: ds_config.to_h)
  find(name)
end

#destroy(name, force: false) ⇒ Object

Delete a datasource

Examples:

# Simple delete
datasources.destroy('old_db')

# Force delete
datasources.destroy('old_db', force: true)

Parameters:

  • name (String)

    Datasource name to delete

  • force (Boolean) (defaults to: false)

    Whether to force delete from all minds



116
117
118
119
# File 'lib/minds/datasources.rb', line 116

def destroy(name, force: false)
  data = force ? { cascade: true } : nil
  @client.delete(path: "datasources/#{name}", parameters: data)
end

#find(name) ⇒ Datasource

Find a datasource by name

Examples:

datasource = datasources.find('sales_db')
puts datasource.engine

Parameters:

  • name (String)

    The name of the datasource to find

Returns:

Raises:



96
97
98
99
100
101
102
# File 'lib/minds/datasources.rb', line 96

def find(name)
  data = @client.get(path: "datasources/#{name}")
  if data["engine"].nil?
    raise ObjectNotSupported, "Wrong type of datasource: #{name}"
  end
  Datasource.new(**data.transform_keys(&:to_sym))
end