Class: Hanami::Repository
- Inherits:
-
ROM::Repository::Root
- Object
- ROM::Repository::Root
- Hanami::Repository
- Defined in:
- lib/hanami/repository.rb
Overview
Mediates between the entities and the persistence layer, by offering an API to query and execute commands on a database.
By default, a repository is named after an entity, by appending the ‘Repository` suffix to the entity class name.
A repository is storage independent. All the queries and commands are delegated to the current adapter.
This architecture has several advantages:
* Applications depend on an abstract API, instead of low level details
(Dependency Inversion principle)
* Applications depend on a stable API, that doesn't change if the
storage changes
* Developers can postpone storage decisions
* Isolates the persistence logic at a low level
Hanami::Model is shipped with one adapter:
* SqlAdapter
All the queries and commands are private. This decision forces developers to define intention revealing API, instead of leaking storage API details outside of a repository.
Defined Under Namespace
Modules: Commands
Constant Summary collapse
- COMMAND_PLUGINS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Plugins for database commands
%i[schema mapping timestamps].freeze
Class Method Summary collapse
-
.associations(&blk) ⇒ Object
private
Declare associations for the repository.
-
.configuration ⇒ Object
private
Configuration.
-
.container ⇒ Object
private
Container.
-
.define_associations ⇒ Object
private
It defines associations, by adding relations to the repository.
-
.define_mapping ⇒ Object
private
Defines the mapping between a database table and an entity.
-
.define_relation ⇒ Object
private
Define a database relation, which describes how data is fetched from the database.
-
.inherited(klass) ⇒ Object
private
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
-
.load! ⇒ Object
private
Define relations, mapping and associations.
-
.mapping(&blk) ⇒ Object
Declare mapping between database columns and entity’s attributes.
-
.schema(&blk) ⇒ Object
Declare database schema.
Instance Method Summary collapse
-
#all ⇒ Array<Hanami::Entity>
Return all the records for the relation.
-
#clear ⇒ Object
Deletes all the records from the relation.
-
#command(*args, **opts, &block) ⇒ ROM::Command
Define a new ROM::Command while preserving the defaults used by Hanami itself.
-
#find(id) ⇒ Hanami::Entity, NilClass
Find by primary key.
-
#first ⇒ Hanami::Entity, NilClass
Returns the first record for the relation.
-
#initialize ⇒ Hanami::Repository
constructor
Initialize a new instance.
-
#last ⇒ Hanami::Entity, NilClass
Returns the last record for the relation.
Constructor Details
#initialize ⇒ Hanami::Repository
Initialize a new instance
419 420 421 |
# File 'lib/hanami/repository.rb', line 419 def initialize super(self.class.container) end |
Class Method Details
.associations(&blk) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Declare associations for the repository
NOTE: This is an experimental feature
248 249 250 |
# File 'lib/hanami/repository.rb', line 248 def self.associations(&blk) @associations = blk end |
.configuration ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Configuration
123 124 125 |
# File 'lib/hanami/repository.rb', line 123 def self.configuration Hanami::Model.configuration end |
.container ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Container
131 132 133 |
# File 'lib/hanami/repository.rb', line 131 def self.container Hanami::Model.container end |
.define_associations ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
It defines associations, by adding relations to the repository
231 232 233 |
# File 'lib/hanami/repository.rb', line 231 def self.define_associations Model::Associations::Dsl.new(self, &@associations) unless @associations.nil? end |
.define_mapping ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines the mapping between a database table and an entity.
It’s also responsible to associate table columns to entity attributes.
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/hanami/repository.rb', line 206 def self.define_mapping self.entity = Utils::Class.load!(entity_name) e = entity m = @mapping blk = lambda do |_| model e register_as Model::MappedRelation.mapper_name instance_exec(&m) unless m.nil? end root = self.root configuration.mappers { define(root, &blk) } configuration.define_mappings(root, &blk) configuration.register_entity(relation, entity_name.underscore, e) end |
.define_relation ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Define a database relation, which describes how data is fetched from the database.
It auto-infers the underlying database table.
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/hanami/repository.rb', line 172 def self.define_relation a = @associations s = @schema configuration.relation(relation) do if s.nil? schema(infer: true) do associations(&a) unless a.nil? end else schema(&s) end end relations(relation) root(relation) class_eval %{ def #{relation} Hanami::Model::MappedRelation.new(@#{relation}) end }, __FILE__, __LINE__ - 4 end |
.inherited(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/hanami/repository.rb', line 307 def self.inherited(klass) klass.class_eval do include Utils::ClassAttribute auto_struct true @associations = nil @mapping = nil @schema = nil class_attribute :entity class_attribute :entity_name class_attribute :relation Hanami::Utils::IO.silence_warnings do def self.relation=(name) @relation = name.to_sym end end self.entity_name = Model::EntityName.new(name) self.relation = Model::RelationName.new(name) commands :create, update: :by_pk, delete: :by_pk, mapper: Model::MappedRelation.mapper_name, use: COMMAND_PLUGINS prepend Commands end Hanami::Model.repositories << klass end |
.load! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Define relations, mapping and associations
296 297 298 299 300 |
# File 'lib/hanami/repository.rb', line 296 def self.load! define_relation define_mapping define_associations end |
.mapping(&blk) ⇒ Object
Declare mapping between database columns and entity’s attributes
NOTE: This should be used only when there is a name mismatch (eg. in legacy databases).
288 289 290 |
# File 'lib/hanami/repository.rb', line 288 def self.mapping(&blk) @mapping = blk end |
.schema(&blk) ⇒ Object
Declare database schema
NOTE: This should be used only when Hanami can’t find a corresponding Ruby type for your column.
269 270 271 |
# File 'lib/hanami/repository.rb', line 269 def self.schema(&blk) @schema = blk end |
Instance Method Details
#all ⇒ Array<Hanami::Entity>
Return all the records for the relation
451 452 453 |
# File 'lib/hanami/repository.rb', line 451 def all root.as(:entity).to_a end |
#clear ⇒ Object
Deletes all the records from the relation
485 486 487 |
# File 'lib/hanami/repository.rb', line 485 def clear root.delete end |
#command(*args, **opts, &block) ⇒ ROM::Command
Define a new ROM::Command while preserving the defaults used by Hanami itself.
It allows the user to define a new command to, for example, create many records at the same time and still get entities back.
The first argument is the command and relation it will operate on.
155 156 157 158 159 160 |
# File 'lib/hanami/repository.rb', line 155 def command(*args, **opts, &block) opts[:use] = COMMAND_PLUGINS | Array(opts[:use]) opts[:mapper] = opts.fetch(:mapper, Model::MappedRelation.mapper_name) super(*args, **opts, &block) end |
#find(id) ⇒ Hanami::Entity, NilClass
Find by primary key
437 438 439 440 441 |
# File 'lib/hanami/repository.rb', line 437 def find(id) root.by_pk(id).as(:entity).one rescue => e raise Hanami::Model::Error.for(e) end |
#first ⇒ Hanami::Entity, NilClass
Returns the first record for the relation
463 464 465 |
# File 'lib/hanami/repository.rb', line 463 def first root.as(:entity).limit(1).one end |
#last ⇒ Hanami::Entity, NilClass
Returns the last record for the relation
475 476 477 |
# File 'lib/hanami/repository.rb', line 475 def last root.as(:entity).limit(1).reverse.one end |