Sequel ActiveRecord Adapter
This gem allows the Sequel library to reuse an existing ActiveRecord connection.
It supports postgresql
, mysql2
and sqlite3
adapters.
This can be useful if you're using a library that uses Sequel for database
interaction (e.g. Rodauth), but you want to avoid creating a separate
database connection. Or if you're transitioning from ActiveRecord to Sequel,
and want the database connection to be reused.
Note that this is a best-effort implementation, so some discrepancies are still
possible. However, it's worth mentioning that this gem passes Rodauth's test
suite for all adapters, which has some pretty advanced Sequel usage.
Installation
Add this line to your application's Gemfile:
gem "sequel-activerecord-adapter"
And then execute:
$ bundle install
Or install it yourself as:
$ gem install sequel-activerecord-adapter
Usage
Given you've configured your ActiveRecord connection, you can create a Sequel
database and start making queries:
require "sequel-activerecord-adapter"
DB = Sequel.activerecord
DB.create_table :posts do
primary_key :id
String :title, null: false
Stirng :body, null: false
end
DB[:posts].insert(
title: "Sequel ActiveRecord Adapter",
body: "Allows Sequel to reuse ActiveRecord's connection",
)
DB[:posts].all
DB[:posts].update(title: "Sequel Active Record Adapter")
Since Sequel is using ActiveRecord connection object to make queries, any SQL
queries will be logged to the ActiveRecord logger.
Configuration
By default, the connection configuration will be read from ActiveRecord::Base
.
If you want to use connection configuration from a different model, you can
pass the model class:
class MyModel < ActiveRecord::Base
connects_to database: { writing: :animals, reading: :animals_replica }
end
Sequel.activerecord(MyModel)
If the correct adapter cannot be inferred from ActiveRecord configuration at
the time of initialization, you can always specify it explicitly:
Sequel.activerecord(adapter: "postgresql")
Tests
The Rakefile has rake tasks for setting up and tearing down different
databases, which you need to run first.
Then you can run the tests:
$ rake test
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Sequel::Activerecord::Adapter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.