Sequel Database Toolkit

repository·master·Indexed 26 days ago

https://github.com/jeremyevans/sequel

A flexible and powerful SQL database toolkit for Ruby featuring a concise query DSL, a comprehensive ORM, and support for advanced features like connection pooling and sharding. It includes a command-line tool for database interaction, migrations, and schema dumping, as well as various plugins such as column_conflicts, input_transformer, and insert_conflict.

Tokens
2.8K
Snippets
8
Records
31
Agent score
90%

What's inside Sequel

  1. Use the hook_class_methods plugin for backwards compatibility

    master

    The hook_class_methods plugin allows you to define model hooks using class methods with blocks or tags.

    Note: This plugin is intended for backwards compatibility and its use is not encouraged. The recommended way to add hooks in Sequel is to override the instance method and call super.

    def before_save
      super
      self.created_at = Time.now
    end

    Plugin Usage

    To enable this plugin for all model subclasses:

    Sequel::Model.plugin :hook_class_methods

    To enable it for a specific class:

    Album.plugin :hook_class_methods
  2. Use the column_conflicts plugin to handle name collisions

    master

    The column_conflicts plugin prevents errors when database column names conflict with existing Ruby or Sequel method names (e.g., a column named class or id). It overrides Model#get_column_value and #set_column_value to access the values hash directly instead of using send when a conflict is detected.

    Note: Enabling this plugin incurs a performance hit because it must check for column conflicts. Sequel does not enable this by default.

  3. Use the insert_conflict plugin to handle unique constraint conflicts

    master

    The insert_conflict plugin enables handling unique constraint conflicts during the saving of a new model instance using INSERT ON CONFLICT (supported by PostgreSQL 9.5+ and SQLite 3.24.0+).

    This plugin is designed to turn insert conflicts into updates rather than ignoring them. You can apply it to a specific model class or to all Sequel::Model subclasses.

    Important Notes:

    • Do not use this plugin to ignore conflicts; it is intended for turning conflicts into updates.
    • This plugin disables prepared insert statements as they are incompatible with this functionality.
    # Enable for a specific model
    class Album < Sequel::Model
      plugin :insert_conflict
    end
    
    # Or enable for all models
    Sequel::Model.plugin :insert_conflict
    
    # Usage on a new instance
    Album.new(name: 'Foo', copies_sold: 1000).
      insert_conflict(
        target: :name,
        update: {copies_sold: Sequel[:excluded][:copies_sold]}
      ).
      save
  4. Use the InputTransformer plugin

    master

    The input_transformer plugin allows you to define generic transformations for input values in model column setters. Transformations are applied when using the []= setter method (e.g., model.column = value).

    To enable this plugin for all model subclasses, call it on Sequel::Model. To enable it for a specific model class, call it on that class.

  5. Use the insert_returning_select plugin

    master

    The insert_returning_select plugin optimizes row insertion by using the INSERT ... RETURNING syntax when the database supports it.

    If a model's dataset selects explicit columns (or table.*), this plugin automatically sets the RETURNING clause on the dataset used to insert rows. This allows Sequel to perform the insert and the subsequent data refresh in a single query, rather than two separate queries. This behavior is the default in Sequel when the model does not select explicit columns, but this plugin enables it for models that do use explicit selection.

    You can apply this plugin globally to all Sequel::Model subclasses or specifically to a single model class.

    # Make all model subclasses automatically setup insert returning clauses
    Sequel::Model.plugin :insert_returning_select
    
    # OR: Make a specific class (e.g., Album) automatically setup insert returning clauses
    class Album < Sequel::Model
    end
    Album.plugin :insert_returning_select
  6. Use the Sequel CLI to connect to a database

    master

    The sequel command-line tool allows you to interact with databases via a URI, a path to a YAML configuration file, or by piping code. If no arguments are provided and the input is an interactive terminal, it starts an IRB session with the database connected as DB.

    Usage: sequel [options] <uri|path> [file]

    Examples:

    • Connect to SQLite: sequel sqlite://blog.db
    • Connect to PostgreSQL: sequel postgres://localhost/my_blog
    • Use a YAML config file: sequel config/database.yml
    sequel sqlite://blog.db
    sequel postgres://localhost/my_blog
    sequel config/database.yml
  7. Define hooks using hook_class_methods

    master

    When using the hook_class_methods plugin, you can define hooks in three ways:

    1. Block only: Can cause duplicate hooks if code is reloaded. before_save { self.created_at = Time.now }

    2. Block with tag: Safe for reloading. before_save(:set_created_at) { self.created_at = Time.now }

    3. Tag only: Safe for reloading; calls the specified instance method. before_save(:set_created_at)

    # Block only
    before_save{self.created_at = Time.now}
    
    # Block with tag, safe for reloading
    before_save(:set_created_at){self.created_at = Time.now}
    
    # Tag only, safe for reloading, calls instance method
    before_save(:set_created_at)