luckyframework / avram

A Crystal database wrapper for reading, writing, and migrating Postgres databases.
https://luckyframework.github.io/avram/
MIT License
165 stars 63 forks source link

Specify read/write databases #138

Open paulcsmith opened 4 years ago

paulcsmith commented 4 years ago

Extracted from #9

abstract class BaseModel < Avram::Model
  def database
     PrimaryDatabase
  end

  # Or set separate read/write database
  def read_database
    ReplicaDatabase
  end

  def write_database
    PrimaryDatabase
  end
end

This will be super easy to implement except for the specs. Need to think of a reliable way to do test it

matthewmcgarvey commented 3 years ago

There's going to have to be some way to specify to all of Avram which connection to use and not just in a particular model. If I write to the database and then later query it in the same request, I expect my changes to be there but they might not have been propagated to the read-replica yet.

Rails lays out some ways they handle it here https://guides.rubyonrails.org/active_record_multiple_databases.html