Possible to use a sequel plugin in hanami?


#1

Something which is pretty key to me is the ability to have optimistic locking available. As far as I can tell, this is a feature which exists in AR and sequel, but not in rom, or in hanami.

I am wondering if there is some way I can use this https://raw.githubusercontent.com/jeremyevans/sequel/master/lib/sequel/plugins/optimistic_locking.rb in my repositories, given that sequel is what is in play at the bottom of the stack.

Thanks!


#2

Lack of ability to use underlying Sequel and sophisticated ROM features in Hanami Repos made us switch to plain Sequel completely and get rid of ROM in Hanami app :frowning:


#3

Hey, hello. You have access for sequel connection object. It’s mean that you can add extentions in hanami-model and ROM. For this, you need to open config/aenvironment.rb file and update model configuration block:

  model do
    adapter :sql, ENV.fetch('DATABASE_URL')

    # ...

    # add your plugins here:
    gateway do |g|
      g.connection.extension(:connection_validator)
      g.connection.pool.connection_validation_timeout = ENV['DATABASE_CONNECTION_VALIDATION_TIMEOUT'] || 30 # seconds
    end
  end

About plugins: if I remember right (and check in google) sequel uses plugins for Sequel::Model only. It’s mean that you can’t use it with hanami model but, if you can rewrite it as an extension, you can add it to sequel connection.


#4

Why didn’t you switch to rom standalone without hanami-model?


#5

Being honest, I find ROM quite complicated and verbose. Still there was a huge cognitive issues trying to understand what is different in ROM 3.x (where hanami-model stuck) and ROM 4.x we had a lot of confusion in documentation of ROM, while trying to find out “how to make this ROM feature working in Hanami”.

Also it still not very clear how to use pure ROM, while having migrations and what is even more important - correct code load sequence (in dev env with Shotgun reloads). Also we had troubles while composing complicated JOINs in ROM or making “soft delete” functionality (and making it not verbose).

In general at the moment of making a decision to switch to Sequel our team was already a little biased against ROM. Even more - we use Sequel-Model. In general ActiveRecord as a wrapper to make SQL queries is very fluent and clear.

So we have a mixed architecture now - there are still hanami-model in the app, it serves “native” hanami migrations and there are still 5-7 entities / repositories that are using “legacy” ROM inside, but they are too large (but independent in terms of relations) to rewrite them now (or there duplicated of the same Entity and Sequel Model - to serve automatic relations).

All other functionality is made with Sequel Model wrapped by custom repositories to hide queries details. And it output array of Sequel Models (minus here is that these models could be modified and saved outside of the Repo, but we handle it via code review and convention “all find/save/update inside a Repo only”). And there is still a minor issue due to code [re]load sequences, because there is no clear docs how to inject own step to load Models in the chain of hanami-container initializations.

Yes, it’s a bit awkward… but it works fine, with a bit of team discipline :slight_smile: