Module: Sequelizer

Defined in:
lib/sequelizer.rb,
lib/sequelizer/cli.rb,
lib/sequelizer/options.rb,
lib/sequelizer/version.rb,
lib/sequelizer/env_config.rb,
lib/sequelizer/yaml_config.rb,
lib/sequelizer/options_hash.rb,
lib/sequelizer/connection_maker.rb,
lib/sequelizer/gemfile_modifier.rb

Overview

Sequelizer

Sequelizer is a Ruby gem that simplifies database connections using Sequel. It allows users to configure database connections via config/database.yml or .env files, providing an easy-to-use interface for establishing database connections without hardcoding sensitive information.

Usage

Include this module in any class where you’d like to quickly establish a Sequel connection to a database:

class MyClass
  include Sequelizer

  def some_method
    db[:users].all  # Uses cached connection
  end
end

Configuration Sources

Configuration is loaded from multiple sources in order of precedence:

  1. Passed options

  2. .env file

  3. Environment variables

  4. config/database.yml

  5. ~/.config/sequelizer/database.yml

Examples

# Use cached connection
db[:users].all

# Create new connection with custom options
new_db(adapter: 'postgres', host: 'localhost')

Defined Under Namespace

Classes: CLI, ConnectionMaker, EnvConfig, GemfileModifier, Options, OptionsHash, YamlConfig

Constant Summary collapse

VERSION =

Version for the gem

'0.2.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optionsHash

Returns the default options hash for database connections.

Returns:

  • (Hash)

    the default connection options



49
50
51
# File 'lib/sequelizer.rb', line 49

def self.options
  Options.new.to_hash
end

Instance Method Details

#db(options = {}) ⇒ Sequel::Database

Instantiates and memoizes a database connection. The db method instantiates the connection on the first call and then memoizes itself so only a single connection is used on repeated calls.

Examples:

db[:users].all  # Uses cached connection
db(adapter: 'postgres')[:products].count

Parameters:

  • options (Hash) (defaults to: {})

    an optional set of database connection options. If no options are provided, options are read from config/sequelizer.yml or from .env or from environment variables.

Returns:

  • (Sequel::Database)

    the memoized database connection



65
66
67
# File 'lib/sequelizer.rb', line 65

def db(options = {})
  @_sequelizer_db ||= new_db(options)
end

#find_cached(options) ⇒ Sequel::Database?

Finds a cached connection for the given options.

Parameters:

  • options (Hash)

    the connection options to look up

Returns:

  • (Sequel::Database, nil)

    the cached connection or nil if not found



91
92
93
94
# File 'lib/sequelizer.rb', line 91

def find_cached(options)
  @_sequelizer_cache ||= {}
  @_sequelizer_cache[options]
end

#new_db(options = {}) ⇒ Sequel::Database

Instantiates and returns a new database connection on each call.

Examples:

conn1 = new_db
conn2 = new_db  # Different connection instance
new_db(force_new: true)  # Bypasses cache entirely

Parameters:

  • options (Hash) (defaults to: {})

    an optional set of database connection options. If no options are provided, options are read from config/sequelizer.yml or from .env or from environment variables.

Returns:

  • (Sequel::Database)

    a new database connection



80
81
82
83
84
85
# File 'lib/sequelizer.rb', line 80

def new_db(options = {})
  cached = find_cached(options)
  return cached if cached && !options[:force_new]

  @_sequelizer_cache[options] = ConnectionMaker.new(options).connection
end