Module: Mongoid::Clients

Extended by:
ActiveSupport::Concern
Includes:
Options, Sessions, StorageOptions
Included in:
Composable
Defined in:
lib/mongoid/clients.rb,
lib/mongoid/clients/factory.rb,
lib/mongoid/clients/options.rb,
lib/mongoid/clients/sessions.rb,
lib/mongoid/clients/storage_options.rb,
lib/mongoid/clients/validators/storage.rb

Overview

Mixin module included into Mongoid::Document which adds database client connection functionality. Also contains singleton class methods related to managing database clients.

Defined Under Namespace

Modules: Factory, Options, Sessions, StorageOptions, Validators

Instance Attribute Summary

Attributes included from StorageOptions

#remembered_storage_options

Class Method Summary collapse

Methods included from Sessions

included

Methods included from Options

#collection, #collection_name, #mongo_client, #persistence_context, #persistence_context?, #with

Methods included from StorageOptions

#remember_storage_options!, #storage_options

Class Method Details

.clearArray

Clear all clients from the current thread.

Examples:

Clear all clients.

Mongoid::Clients.clear

Returns:

  • (Array)

    The empty clients.



29
30
31
# File 'lib/mongoid/clients.rb', line 29

def clear
  clients.clear
end

.clientsHash<Symbol, Mongo::Client>

Returns the stored clients indexed by name.

Returns:

  • (Hash<Symbol, Mongo::Client>)

    The index of clients.



92
93
94
# File 'lib/mongoid/clients.rb', line 92

def clients
  @clients ||= {}
end

.defaultMongo::Client

Get the default client.

Examples:

Get the default client.

Mongoid::Clients.default

Returns:

  • (Mongo::Client)

    The default client.



39
40
41
# File 'lib/mongoid/clients.rb', line 39

def default
  with_name(:default)
end

.disconnecttrue

Disconnect all active clients.

Examples:

Disconnect all active clients.

Mongoid::Clients.disconnect

Returns:

  • (true)

    True.



49
50
51
52
53
# File 'lib/mongoid/clients.rb', line 49

def disconnect
  clients.values.each do |client|
    client.close
  end
end

.set(name, client) ⇒ Mongo::Client

Store a client with the provided name.

Examples:

Set a client.

Mongoid::Clients.set(:analytics, my_client)

Parameters:

  • name (String | Symbol)

    The name of the client to set.

  • client (Mongo::Client)

    The client to set.

Returns:

  • (Mongo::Client)

    The set client.



85
86
87
# File 'lib/mongoid/clients.rb', line 85

def set(name, client)
  clients[name.to_sym] = client
end

.with_name(name) ⇒ Mongo::Client

Get a stored client with the provided name. If no client exists with the given name, a new one will be created, stored, and returned.

Examples:

Get a client with the name.

Mongoid::Clients.with_name(:replica)

Parameters:

  • name (String | Symbol)

    The name of the client.

Returns:

  • (Mongo::Client)

    The named client.



65
66
67
68
69
70
71
72
73
74
# File 'lib/mongoid/clients.rb', line 65

def with_name(name)
  name_as_symbol = name.to_sym
  return clients[name_as_symbol] if clients[name_as_symbol]
  CREATE_LOCK.synchronize do
    if (key_vault_client = Mongoid.clients.dig(name_as_symbol, :options, :auto_encryption_options, :key_vault_client))
      clients[key_vault_client.to_sym] ||= Clients::Factory.create(key_vault_client)
    end
    clients[name_as_symbol] ||= Clients::Factory.create(name)
  end
end