Ruby persistence framework with entities and repositories
Ruby persistence framework with entities and repositories
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
Hanami::Model supports Hanami 1.x only, and Ruby (MRI) 2.6 and 2.7.
Add this line to your application's Gemfile:
gem 'hanami-model'And then execute:
$ bundleOr install it yourself as:
$ gem install hanami-modelThis class provides a DSL to configure the connection.
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)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.
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!
endThis is not necessary when Hanami::Model is used within a Hanami application.
If an entity has the following accessors: :created_at and :updated_at, they will be automatically updated when the entity is persisted.
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"In order to log database operations, you can configure a logger:
Hanami::Model.configure do
# ...
logger "log/development.log", level: :debug
endIt 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 :defaultHanami::Model uses Semantic Versioning 2.0.0
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)Copyright © 2014-2021 Luca Guidi – Released under MIT License
This project was formerly known as Lotus (lotus-model).
No open issues yet, or sync has not completed.