Module: Hanami::Model

Defined in:
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/error.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/types.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/mapping.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/plugins.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/version.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migrator.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migration.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/types.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/association.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/entity_name.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/console.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/configurator.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/configuration.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/relation_name.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/plugins/schema.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/mapped_relation.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migrator/logger.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/plugins/mapping.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/associations/dsl.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migrator/adapter.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/entity/schema.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/plugins/timestamps.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/consoles/mysql.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migrator/connection.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/consoles/sqlite.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/associations/has_one.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/associations/has_many.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/consoles/abstract.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migrator/mysql_adapter.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/associations/belongs_to.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migrator/sqlite_adapter.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/consoles/postgresql.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/associations/many_to_many.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/migrator/postgres_adapter.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model/sql/types/schema/coercions.rb,
gems/gems/hanami-model-1.3.2/lib/hanami/model.rb

Overview

Hanami persistence

Since:

  • 0.1.0

Defined Under Namespace

Modules: Associations, Sql, Types Classes: CheckConstraintViolationError, Configuration, ConstraintViolationError, DatabaseError, Error, ForeignKeyConstraintViolationError, InvalidCommandError, MigrationError, Migrator, MissingPrimaryKeyError, NotNullConstraintViolationError, UniqueConstraintViolationError, UnknownAttributeError, UnknownDatabaseAdapterError, UnknownDatabaseTypeError

Constant Summary

VERSION =

Defines the version

Since:

  • 0.1.0

'1.3.2'.freeze

Class Method Summary collapse

Class Method Details

.configurationObject

Current configuration

Since:

  • 0.1.0

def self.configuration
  @configuration ||= Configuration.new(config)
end

.configure(&block) ⇒ Object

Configure the framework

Examples:

require 'hanami/model'

Hanami::Model.configure do
  adapter :sql, ENV['DATABASE_URL']

  migrations 'db/migrations'
  schema     'db/schema.sql'
end

Since:

  • 0.1.0

def self.configure(&block)
  @config = Configurator.build(&block)
  self
end

.disconnectObject

Disconnect from the database

This is useful for rebooting applications in production and to ensure that the framework prunes stale connections.

Examples:

With Full Stack Hanami Project

# config/puma.rb
# ...
on_worker_boot do
  Hanami.boot
end

With Standalone Hanami::Model

# config/puma.rb
# ...
on_worker_boot do
  Hanami::Model.disconnect
  Hanami::Model.load!
end

Since:

  • 1.0.0

def self.disconnect
  configuration.connection&.disconnect
end

.load!(&blk) ⇒ Object

Since:

  • 0.1.0

def self.load!(&blk)
  @container = configuration.load!(repositories, &blk)
  @loaded    = true
end

.migration(&blk) ⇒ Object

Define a migration

It must define an up/down strategy to write schema changes (up) and to rollback them (down).

We can use up and down blocks for custom strategies, or only one change block that automatically implements “down” strategy.

Examples:

Use up/down blocks

Hanami::Model.migration do
  up do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  down do
    drop_table :books
  end
end

Use change block

Hanami::Model.migration do
  change do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  # DOWN strategy is automatically generated
end

Parameters:

  • blk (Proc)

    a block that defines up/down or change database migration

Since:

  • 0.4.0

def self.migration(&blk)
  Migration.new(configuration.gateways[:default], &blk)
end