SqlcachedClient
A Ruby client for Sqlcached.
Installation
Add this line to your application's Gemfile:
gem 'sqlcached_client'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sqlcached_client
Usage
Create your models as you would do with ActiveRecord
.
require 'sqlcached_client'
class Base < SqlcachedClient::Entity
# things shared amongst all models
server({ host: 'localhost', port: 8081 })
end
class User < Base
entity_name 'user'
query <<-SQL
SELECT * FROM users WHERE id IN ({{ ids }})
SQL
has_many :posts, where: { user_id: :id }
has_many :pictures, class_name: 'Image',
where: { imageable_id: :id, imageable_type: 'User' }
has_one :address, where: { user_id: :id }
end
class Post < Base
entity_name 'post'
query <<-SQL
SELECT * FROM posts WHERE author_id = {{ user_id }}
SQL
end
Run some queries:
users = User.where(ids: '1, 2, 3, 4')
users[0].pictures.each # you can navigate through the associations
Load in memory every associated set recursively:
users.build_associations # for each entity in the resultset, or...
users[0].build_associations # for a single entity
Contributing
- Fork it ( https://github.com/rmaestroni/sqlcached_client/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request