Skip to content

Commit

Permalink
Documented 'Configuring Reader and Writer Multi-DB Roles'
Browse files Browse the repository at this point in the history
  • Loading branch information
hughkelsey authored Mar 1, 2024
1 parent 9ac476c commit 7fd628e
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,28 @@ If you want more things to happen when a user accesses one of your short urls, y

You can store a `shortened_urls` table in another database and connecting to it by creating a initializer with the following:

```ruby
ActiveSupport.on_load(:shortener_record) do
connects_to(database: { writing: :dbname, reading: :dbname_replica })
end
```
ActiveSupport.on_load(:shortener_record) do
connects_to(database: { writing: :dbname, reading: :dbname_replica })
end

**Note:** Please, replace `dbname` and `dbname_replica` to match your database configuration.

=== Configuring Reader and Writer Multi-DB Roles

Shortener has one write operation that happens on a GET request. To allow this you can override this method in an initializer.

module ShortenerWriterMonkeyPatch
def increment_usage_count
ActiveRecord::Base.connected_to(role: :writing) do
self.class.increment_counter(:use_count, id)
end
end
end

ActiveSupport.on_load(:shortener_shortened_url) do
Shortener::ShortenedUrl.prepend(ShortenerWriterMonkeyPatch)
end

== Contributing

We welcome new contributors. Because we're all busy people, and because Shortener
Expand Down

0 comments on commit 7fd628e

Please sign in to comment.