Method: Sequel::SequelMethods#connect
- Defined in:
- lib/sequel/core.rb
#connect(*args, &block) ⇒ Object
Creates a new database object based on the supplied connection string and optional arguments. The specified scheme determines the database class used, and the rest of the string specifies the connection options. For example:
DB = Sequel.connect('sqlite:/') # Memory database
DB = Sequel.connect('sqlite://blog.db') # ./blog.db
DB = Sequel.connect('sqlite:///blog.db') # /blog.db
DB = Sequel.connect('postgres://user:password@host:port/database_name')
DB = Sequel.connect('sqlite:///blog.db', max_connections: 10)
You can also pass a single options hash:
DB = Sequel.connect(adapter: 'sqlite', database: './blog.db')
If a block is given, it is passed the opened Database object, which is closed when the block exits. For example:
Sequel.connect('sqlite://blog.db'){|db| puts db[:users].count}
If a block is not given, a reference to this database will be held in Sequel::DATABASES until it is removed manually. This is by design, and used by Sequel::Model to pick the default database. It is recommended to pass a block if you do not want the resulting Database object to remain in memory until the process terminates, or use the keep_reference: false Database option.
For details, see the “Connecting to a Database” guide. To set up a primary/replica or sharded database connection, see the “Primary/Replica Database Configurations and Sharding” guide.
123 124 125 |
# File 'lib/sequel/core.rb', line 123 def connect(*args, &block) Database.connect(*args, &block) end |