Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

model

> 数据库
Open source

Ruby persistence framework with entities and repositories

447 stars0 likes2 views
WebsiteGitHub

About

Ruby persistence framework with entities and repositories

Hanami::Model (deprecated)

Important notice

NOTE: Hanami::Model was the persistence layer for Hanami 1.x. This library will not receive any updates.

For the persistence layer for Hanami 2.x, please see hanami/db

Contact

  • Home page: http://hanamirb.org
  • Mailing List: http://hanamirb.org/mailing-list
  • API Doc: http://rubydoc.info/gems/hanami-model
  • Chat: https://chat.hanamirb.org

Rubies

Hanami::Model supports Hanami 1.x only, and Ruby (MRI) 2.6 and 2.7.

Installation

Add this line to your application's Gemfile:

ruby
gem 'hanami-model'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hanami-model

Usage

This class provides a DSL to configure the connection.

ruby
require 'hanami/model'
require 'hanami/model/sql'

class User  1

found = repository.find(user.id)
found == user # => true

updated = repository.update(user.id, age: 34)
updated.age # => 34

repository.delete(user.id)

Concepts

Entities

A model domain object that is defined by its identity. See "Domain Driven Design" by Eric Evans.

An entity is the core of an application, where the part of the domain logic is implemented. It's a small, cohesive object that expresses coherent and meaningful behaviors.

It deals with one and only one responsibility that is pertinent to the domain of the application, without caring about details such as persistence or validations.

This simplicity of design allows developers to focus on behaviors, or message passing if you will, which is the quintessence of Object Oriented Programming.

ruby
require 'hanami/model'

class Person  `ArticleRepository`).

### Thread safety

**Hanami::Model**'s is thread safe during the runtime, but it isn't during the loading process.
The mapper compiles some code internally, so be sure to safely load it before your application starts.

```ruby
Mutex.new.synchronize do
  Hanami::Model.load!
end

This is not necessary when Hanami::Model is used within a Hanami application.

Features

Timestamps

If an entity has the following accessors: :created_at and :updated_at, they will be automatically updated when the entity is persisted.

ruby
require 'hanami/model'
require 'hanami/model/sql'

class User  "2016-09-19 13:40:13 UTC"
puts user.updated_at.to_s # => "2016-09-19 13:40:13 UTC"

sleep 3
user = repository.update(user.id, age: 34)
puts user.created_at.to_s # => "2016-09-19 13:40:13 UTC"
puts user.updated_at.to_s # => "2016-09-19 13:40:16 UTC"

Configuration

Logging

In order to log database operations, you can configure a logger:

ruby
Hanami::Model.configure do
  # ...
  logger "log/development.log", level: :debug
end

It accepts the following arguments:

  • stream: a Ruby StringIO object - it can be $stdout or a path to file (eg. "log/development.log") - Defaults to $stdout
  • :level: logging level - it can be: :debug, :info, :warn, :error, :fatal, :unknown - Defaults to :debug
  • :formatter: logging formatter - it can be: :default or :json - Defaults to :default

Versioning

Hanami::Model uses Semantic Versioning 2.0.0

Contributing

  1. Fork it ( https://github.com/hanami/model/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Copyright

Copyright © 2014-2021 Luca Guidi – Released under MIT License

This project was formerly known as Lotus (lotus-model).

Issues· 19 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubydatabaseentityentity-frameworkhanami

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库