What ORM to use in one process multiple db connections sinatra application?
DataMapper is designed for multi-database use.
You can set up multiple repositories just by saying something like DataMapper.setup(:repository_one, "mysql://localhost/my_db_name")
.
DataMapper then tracks all the repositories that have been setup in a hash that you can reference and use for scoping:
DataMapper.repository(:repository_one){ MyModel.all }
(The default scope just being DataMapper.repository, which you can set up by saying DataMapper.setup(:default, "postgres://localhost/my_primary_db")
or the like)
Adding an ORM to a Sinatra app; Is there an ideal one with less issues and good performance?
Sequel is fast enough but feature less while ActiveRecord has many cool features what lead to some performance problems.
But, in most cases, performance of the ORM shouldn't become a problem with wise usage. Usually you should think about database performance and application design but not about ORM performance.
If you need something easy and straight you should use Sequel, but for big applications with many nesting forms I prefer ActiveRecord.
One Rails model with two database choices, chosen on instantiation
As the documentation for ActiveRecord::Base discusses, it is easy to have different Rails model objects connecting to different databases using the establish_connection
method.
However, if you want the same class to connect to multiple databases based on configuration, that will be kind of a pain. Do you need to use ActiveRecord here or could you use DataMapper? That would work better in this scenario I think. Check out What ORM to use in one process multiple db connections sinatra application? for an example
Ruby on Rails - multiple database connections
After researching for a bit, turns out there's a much simpler approach using ActiveRecord Connection pool.
- Setup a relationship between users and the database connections, in this case user 1 .. n connections.
Make sure the model record can individually connect using
obj = ActiveRecord::Base.establish_connection(...spec...)
obj.connection.exec_query("Select * from users")
# the response is in the form of ActiveResult, which allows flexible operations around the result.Close the connection once done with the database.
References:
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html
Use existing database and tables with Sinatra and Datamapper?
Sinatra has no provision for talking to databases. You can use the mysql2 gem to talk to the database, but I'd highly recommend looking at Sequel, which is a very flexible and powerful ORM, which makes it very easy to talk to legacy databases.
Look through the README, and Cheat Sheet and you'll get a good idea of how easy it is to connect to an existing database, without needing to worry about modifying it.
This is untested of course, but it's the basic steps needed to make a connection and retrieve a record from an existing database:
require 'sequel'
DB = Sequel.connect('mysql2://your_DB:credentials@host/table')
foo = DB[:serverinfo].select(:DB, :SERVERNM).where(:STATUS => 'A').first
Wrap the code above inside a get
or post
handler in Sinatra and it'll retrieve the record. Something like this would get you close:
require 'sequel'
require 'json'
DB = Sequel.connect('mysql2://your_DB:credentials@host/table')
get '/' do
content_type :json
foo = DB[:serverinfo].select(:DB, :SERVERNM).where(:STATUS => 'A').first
foo.to_json # would return the hash to the browser
end
As far as using Datamapper, read the quote at the top of Sequel's main page.
database connection pooling in ruby
To persist a connection, you need only create an instance variable (Sinatra Applications are just objects anyway) or a global variable. Or a class that manages connections for you. Most Ruby database libraries I've seen are Database Adapters or just clients.
@db = Mysql2::Client.new #...
Or a global variable:
$db = Mysql2::Client.new #...
Connection pooling is just a way to share a small number of connections across multiple threads/fibers for the lifespan of the application. Java, the JVM, as far as I know doesn't share connections between processes.
However, there is a general purpose Connection Pool library for Ruby.
Connecting to existing Heroku Postgres DB for Sinatra app
Turns out Heroku has a great tutorial that not only connects to the replicated database, but is also written for Sinatra.
https://devcenter.heroku.com/articles/getting-started-with-heroku-and-connect-without-local-dev#introduction
Right place for Sequel DB connection while working on Phusion Passenger with nginx
This issue is documented in Appendix C.3 of the Phusion Passenger manual. The usual method is to hook into the post-fork callback and re-establish the connection there.
Ruby + Sequel + Sinatra + MySQL connection issues
If you are using preload_app in your Unicorn configuration, but aren't disconnecting Sequel's database connections before the fork, that could possibly cause the errors you are seeing. I think both Sequel's and Unicorn's documentation explain the issue:
https://bogomips.org/unicorn/Unicorn/Configurator.html
http://sequel.jeremyevans.net/rdoc/files/doc/code_order_rdoc.html#label-Disconnect+If+Using+Forking+Webserver+with+Code+Preloading
Sinatra not understanding settings in config.ru?
Looks like it might be an ordering problem. If that DataMapper.setup(...)
line is really at the top level of app.rb
, it will be called as soon as you require './app.rb'
, before configure
has run.
It's best not to do any work upon loading a file. Use some form of explicit or lazy initialization instead.
Related Topics
Typeerror: Object Doesn't Support This Property or Method
How to Convert PDF to Excel or CSV in Rails 4
Scraping an Angularjs Application
How to Upload a JSON File with Secret Keys to Heroku
Ruby Convert Single Quotes to Double Quotes in Xml
How to Install Rmagick Gem on Windows
What Regex How to Use to Get the Domain Name from a Url in Ruby
Error Installing "Kgio-2.9.2" Gem on Windows
Building Gem, Executable Not Found
Ruby Private Attr_Accessor and Unexpected Nil
Error Creating Rails Db Using Rake Db:Create
Show Markers on Google Maps Dynamically -Rails 3.2
Ruby - Naming Convention - Letter Case for Acronyms in Class/Module Names
How to Define a Simple Global Variable in an Rspec Test That Can Be Accesed by Helper Functions