Module: RubyLLM::Mongoid::Transaction

Included in:
ChatMethods
Defined in:
lib/ruby_llm/mongoid/transaction.rb

Overview

Wraps a block in a MongoDB multi-document transaction when a replica set is available. Falls back to a plain yield on standalone mongod so tests and single-node dev setups still work without errors.

Instance Method Summary collapse

Instance Method Details

#with_transactionObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_llm/mongoid/transaction.rb', line 9

def with_transaction(&)
  _run_transaction(&)
rescue ::Mongoid::Errors::TransactionsNotSupported, ::Mongo::Error::TransactionsNotSupported
  yield
rescue ::Mongo::Error::OperationFailure => e
  raise unless standalone_mongod_error?(e)

  yield
rescue NotImplementedError, NoMethodError # rubocop:disable Lint/DuplicateBranch
  yield
end