Code Monkey home page Code Monkey logo

Comments (6)

AlfonsoUceda avatar AlfonsoUceda commented on May 23, 2024

something like that using sequel like the link provided:

def transaction(options = {})
   DB.transaction(options) do
      yield # if a exception is raised here, then the rollback is fired.
   end
end

from model.

jodosha avatar jodosha commented on May 23, 2024

@AlfonsoUceda yes, but we don't have a DB constant. SqlAdapter holds an instance variable called @connection, which is the same Sequel type of DB.

from model.

jodosha avatar jodosha commented on May 23, 2024

Because the only high level object that we expose to developers is a repository, we should consider to add that method there. Example BookRepository.transaction { ... }.

from model.

AlfonsoUceda avatar AlfonsoUceda commented on May 23, 2024

@jodosha can you assing to me please? I'm going to do a PR later or in this weekend

from model.

AlfonsoUceda avatar AlfonsoUceda commented on May 23, 2024

@jodosha I have a dude with the implementation, If you use the Repository.transaction I think you will use the repository inside, isn't it? I give you 2 examples but I think it should be explicitly done.

what do you think?

UserRepository.transaction { UserRepository.first }
# or
UserRepository.transaction { first }

from model.

jodosha avatar jodosha commented on May 23, 2024

@AlfonsoUceda It should be a public method and context free (first option in your example).

A bit of explanation, this can be useful for complex scenarios where several entities needs to be persisted atomically, and eventually rolled back in case of error.

class Signup
  def initialize(params)
    @user = User.new(params[:user])
    @account = Account.new(params[:account])
  end

  def call
    AccountRepository.transaction do
      AccountRepository.create(@account)
      UserRepository.create(@user)
    end
  end
end

from model.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.