Qs is a lightweight and DB-agnostic library for querying.
Add this line to your application's Gemfile:
gem 'qs'
And then execute:
$ bundle
Or install it yourself as:
$ gem install qs
Qs contains a number of entities that you will use for defining query set.
Resource
is an abstraction over DB drivers/adapters/interfaces/etc. You may think about resource as of lambda the execution of which returns connection to the particular DBMS.
postgresql = Qs.resource :postgresql, {user: "postgres", host: "localhost"}, ->(connection_params) do
PG.connect(connection_params)
end
redis = Qs.resource :redis, {user: "redis", host: "localhost", db: "tasks"}, ->(connection_params) do
Redis.new connection_params
end
Query
is a high-level abstraction. You may think about Query as of lambda with some additional features.
find_by_id = Qs.query :find_by_id, Qs.params_validator(id: {type: Fixnum}), ->(resources, params) do
resources[:redis].connection.get params[:id] ||
resources[:postgresql].connection.exec("SELECT * FROM tasks WHERE %{id} LIMIT 1;", params)
end
query.exec resources, id: 1
To describe query parameters you need to use another entity - ParamsValidator
(see above).
Domain
is an aggregate for queries and resources. You may store queries related to some particular domain together using ```Domain``.
tasks = Qs.domain :tasks
tasks.queries.add find_by_id.name, find_by_id
tasks.resources.add postgresql.name, postgresql
tasks.resources.add redis.name, redis
tasks.exec :find_by_id, id: 1
Querier
is a aggregate for a number of domains. Use one querier per application. For example, if you use SCS architecture you need to create a querier per each self-contained system.
querier = Qs.querier :main
querier.domains.add tasks.name, tasks
querier.exec :tasks, :find_by_id, id: 1
- Fork it ( https://github.com/[my-github-username]/qs/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