Add this line to your application's Gemfile:
gem 'dao-repository'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dao-repository
Repositories in dao-rb are objects that allow you map your data into entities and save them through data gateway.
Basic methods of each repository:
Repository.all
should return all data.Repository.find
should find entity with given id. You can return entity with its relations by adding a list of them into the second argument (Repository.find(1, with: [:author, :comments])
), but your gateway should support eager loading. If entity was not found then method will raise an exception.Repository.find_by_id
also should find entity with given id, but if it was not found then method returnnil
.Repository.last
should return last entity. You can return entity with its relations by adding a list of them into the first argument (Repository.last(with: [:author, :comments])
), but your gateway should support eager loading.Repository.count
should return count of entities.Repository.build
should build entity from data object.Repository.save
should save changes in entity.Repository.save_all
should save collection of entities.Repository.delete
should delete entity.Repository.delete_by_id
should delete entity by id.Repository.with_transaction(&block)
execute block in transaction if gateway support it.Repository.with_lock(id, *args, &block)
in transaction: locks (with given arguments) db record with given id, execute block if gateway support it.Repository.by_criteria
should return entities by given criteria.Repository.by_criteria_count
should return count of elements by given criteria.
require 'dao/entity'
require 'dao/repository'
require 'dao/gateway/active_record'
class Comment < ApplicationRecord
end
class Post < ApplicationRecord
has_many :comments
end
class CommentEntity < Dao::Entity::Base
attribute :body, String
end
class PostEntity < Dao::Entity::Base
attribute :id, Integer
attribute :body, String
attribute :comments, Array[CommentEntity]
end
class PostRepository < Dao::Repository::Base
entity PostEntity
gateway Dao::Gateway::ActiveRecord::Base, Post, Dao::Gateway::ActiveRecord::BaseTransformer
end
post = PostRepository.last(with: :comments)
post.id # => 1
post.body # => "Post body"
post.comments # => [#<CommentEntity:0x007ffdcb923a30>]
You can define custom methods in your repository:
class Post < ApplicationRecord
has_many :comments
scope :deleted, -> { where(deleted: true) }
end
class PostRepository < Dao::Repository::Base
entity PostEntity
gateway Dao::Gateway::ActiveRecord::Base, Post, Dao::Gateway::ActiveRecord::BaseTransformer
def self.deleted
scope.deleted(with: :comments).apply # it will return deleted posts with loaded comments.
end
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/dao-rb/dao-repository.
The gem is available as open source under the terms of the MIT License.