123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Clients

Overview

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

Constant Summary

StorageOptions - Attributes & Methods

Class Attribute Summary

Clients::Sessions::ClassMethods - Extended

in_transaction?

This method should be used to detect whether a persistence operation is executed inside transaction or not.

Class Method Summary

Clients::Sessions::ClassMethods - Extended

after_commit

Sets up a callback is called after a commit of a transaction.

after_create_commit

Shortcut for after_commit :hook, on: :create.

after_destroy_commit

Shortcut for after_commit :hook, on: :destroy.

after_rollback

This callback is called after a create, update, or destroy are rolled back.

after_save_commit

Shortcut for after_commit :hook, on: [ :create, :update ].

after_update_commit

Shortcut for after_commit :hook, on: :update.

transaction

Executes a block within the context of a transaction.

with_session

Execute a block within the context of a session.

_session,
abort_transaction

Aborts the active transaction on the session, and calls after_rollback callbacks on modified documents.

assert_valid_transaction_action

Asserts that the given actions are valid for after_commit and after_rollback callbacks.

commit_transaction

Commits the active transaction on the session, and calls after_commit callbacks on modified documents.

set_options_for_callbacks!

Transforms custom options for after_commit and after_rollback callbacks into options for set_callback.

transaction_include_any_action?,
transactions_not_supported_exceptions

Driver version 2.20 introduced a new exception for reporting that transactions are not supported.

Clients::Options::ClassMethods - Extended

client_name

Get the database client name for the current persistence context of the document class.

collection

Get the collection for the current persistence context of the document class.

collection_name

Get the collection name for the current persistence context of the document class.

database_name

Get the database name for the current persistence context of the document class.

mongo_client

Get the client for the current persistence context of the document class.

persistence_context

Get the current persistence context of the document class.

with

Change the persistence context for this class during the block.

Clients::StorageOptions::ClassMethods - Extended

reset_storage_options!

Reset the store_in options.

storage_options_defaults

Get the default storage options.

store_in

Give this model specific custom default storage options.

Instance Attribute Summary

Options - Included

#persistence_context

Get the document’s current persistence context.

#persistence_context?

Returns whether a persistence context is set for the document or the document’s class.

StorageOptions - Included

#remembered_storage_options

Remembers the storage options that were active when the current object was instantiated/created.

Instance Method Summary

Sessions - Included

#ensure_client_compatibility!

If at least one session is active, this ensures that the current model’s client is compatible with one of them.

Options - Included

#collection

Get the collection for the document’s current persistence context.

#collection_name

Get the collection name for the document’s current persistence context.

#mongo_client

Get the database client for the document’s current persistence context.

#with

Change the persistence context for this object during the block.

#clear_persistence_context, #set_persistence_context

StorageOptions - Included

#remember_storage_options!

Saves the storage options from the current persistence context.

#storage_options

The storage options that apply to this record, consisting of both the class-level declared storage options (e.g.

Class Attribute Details

.storage_options (rw)

[ GitHub ]

  
# File 'lib/mongoid/clients/storage_options.rb', line 14

class_attribute :storage_options, instance_accessor: false, default: storage_options_defaults

.storage_options?Boolean (rw)

[ GitHub ]

  
# File 'lib/mongoid/clients/storage_options.rb', line 14

class_attribute :storage_options, instance_accessor: false, default: storage_options_defaults

Class Method Details

.clearArray

Clear all clients from the current thread.

Examples:

Clear all clients.

Mongoid::Clients.clear

Returns:

  • (Array)

    The empty clients.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'lib/mongoid/clients.rb', line 102

def clients
  @clients ||= {}
end

.defaultMongo::Client

Get the default client.

Examples:

Get the default client.

Mongoid::Clients.default

Returns:

  • (Mongo::Client)

    The default client.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'lib/mongoid/clients.rb', line 49

def disconnect
  clients.each_value(&:close)
  true
end

.reconnecttrue

Reconnect all active clients.

Examples:

Reconnect all active clients.

Mongoid::Clients.reconnect

Returns:

  • (true)

    True.

[ GitHub ]

  
# File 'lib/mongoid/clients.rb', line 60

def reconnect
  clients.each_value(&:reconnect)
  true
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.

[ GitHub ]

  
# File 'lib/mongoid/clients.rb', line 95

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:

Returns:

  • (Mongo::Client)

    The named client.

[ GitHub ]

  
# File 'lib/mongoid/clients.rb', line 75

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