class Athena::ORM::EntityRepository(EntityType)
inherits Athena::ORM::RepositoryInterface
#
Finds entities of type EntityType by simple conditions.
Obtained via AORM::EntityManager#repository:
repository = em.repository User
repository.find 1 # => #<User:0x7f3a1c2b5e40 @id=1, @name="George">
repository.find_by name: "George" # => [#<User:0x7f3a1c2b5e40 @id=1, @name="George">]
repository.find_one_by name: "Nobody" # => nil
repository.count active: true # => 1
Entities found by a repository go through the identity map, so an entity that's already loaded is returned as is.
Criteria#
The finders filter on equality of every given field, combined with AND.
Criteria may be given as keyword arguments, which are checked at compile time against the entity's AORMA::Column instance variables and their types:
repository.find_by name: "George", active: true
repository.find_by nmae: "George" # Error: Unknown field 'nmae' for entity type User. # spellchecker:disable-line
Or as a Criteria hash keyed by field name, which also supports:
- An
Arrayvalue, matching any of its values viaIN - A
nilvalue, matching viaIS NULL - The owning side of a
AORMA::ManyToOneorAORMA::OneToOneassociation, given the identifier of the associated entity
alias Criteria = AORM::EntityRepository::Criteria
repository.find_by Criteria{"name" => ["George", "Jim"] of DB::Any}
repository.find_by Criteria{"deleted_at" => nil}
em.repository(Post).find_by Criteria{"author" => 1_i64}
Build the hash with the Criteria alias; a plain hash literal such as {"name" => "George"} is a Hash(String, String), which doesn't match it.
#find_by and #find_one_by additionally accept an order_by hash mapping field names to "ASC" or "DESC", and #find_by a limit and offset:
repository.find_by(order_by: {"name" => "ASC"}, limit: 10, offset: 20)
Anything more complex, such as joins or aggregates, goes through a native query, see AORM::NativeQuery.
Custom Repositories#
A subclass of this type can group the queries of an entity in one place.
Set it as the entity's repository_class on AORMA::Entity, and AORM::EntityManager#repository returns it in place of the default:
@[AORMA::Entity(repository_class: UserRepository)]
class User < AORM::Entity
# ...
end
class UserRepository < AORM::EntityRepository(User)
def find_by_username(username : String) : User?
self.find_one_by username: username
end
end
em.repository(User).find_by_username "George" # => #<User:0x7f3a1c2b5e40 @id=1, @name="George">
Methods#
#class_metadata : AORM::Mapping::ClassInterface#
Returns the mapping metadata of EntityType.
#count(criteria : Criteria) : Int#
Returns the number of entities matching criteria.
See Criteria.
#count : Int#
Returns the number of entities matching criteria, given as keyword arguments.
repository.count active: true # => 1
See Criteria.
#em : AORM::EntityManagerInterface#
Returns the entity manager this repository finds entities with.
#entity_class : AORM::Entity.class#
Returns the class of the entities this repository finds.
#find(id : Hash(String, Int | String) | Int | String, lock_mode : AORM::LockMode = :none, lock_version : Int32 | Nil = nil) : EntityType | Nil#
Returns the entity with the identifier id, or nil if there isn't one.
#find!(id : Hash(String, Int | String) | Int | String, lock_mode : AORM::LockMode = :none, lock_version : Int32 | Nil = nil) : EntityType#
Returns the entity with the identifier id.
Raises an AORM::Exceptions::NoResult if there isn't one.
#find_by(criteria : Criteria = Criteria.new, order_by : Hash(String, String) = Hash(String, String).new, limit : Int | Nil = nil, offset : Int | Nil = nil) : Array(EntityType)#
Returns the entities matching criteria, ordered by order_by, skipping the first offset entities, and returning at most limit.
repository.find_by AORM::EntityRepository::Criteria{"active" => true}, {"name" => "ASC"}, limit: 10, offset: 20
See Criteria.
#find_by : Array(EntityType)#
Returns the entities matching criteria, given as keyword arguments.
repository.find_by name: "George" # => [#<User:0x7f3a1c2b5e40 @id=1, @name="George">]
See Criteria.
#find_one_by(criteria : Criteria, order_by : Hash(String, String) = Hash(String, String).new) : EntityType | Nil#
Returns the first entity matching criteria, ordered by order_by, or nil if there isn't one.
See Criteria.
#find_one_by : EntityType | ::Nil#
Returns the first entity matching criteria, given as keyword arguments, or nil if there isn't one.
repository.find_one_by name: "George" # => #<User:0x7f3a1c2b5e40 @id=1, @name="George">
See Criteria.
#inspect(io : IO) : Nil#
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>