Ruby client for PostgREST
This gem is under development, any help are welcome 💪
Add this line to your application's Gemfile:
gem 'postgrest'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install postgrest
db = Postgrest::Client.new(url: url, headers: headers, schema: schema)
# Basic select
db.from('todos').select('*').execute
# or just db.from('todos').select
#<Postgrest::Responses::GetResponse GET OK data=[{"id"=>1, "title"=>"foo", "completed"=>false}, {"id"=>2, "title"=>"foo", "completed"=>false}]>
# Selecting just one or more fields
db.from('todos').select(:title).execute
#<Postgrest::Responses::GetResponse GET OK data=[{"title"=>"foo"}, {"title"=>"foo"}]>
You have the ability to alias the name of the column you want doing as follows:
db.from('todos').select('name:title').eq(id: 112).execute
#<Postgrest::Responses::GetResponse GET OK data=[{"name"=>"Go to the gym"}]>
db.from('todos').select('*').eq(id: 100).execute
#<Postgrest::Responses::GetResponse GET OK data=[{"id" => 100, "title"=>"foo", "completed" => true}}]>
db.from('todos').select('*').neq(id: 100).execute
#<Postgrest::Responses::GetResponse GET OK data=[{"id" => 101, "title"=>"foo", "completed" => true}}]>
TODO
TODO
db.from('todos').select(:id, :title).owners(:name, as: :owner).workers(:name, as: :worker).in(id: [112, 113]).order(id: :asc).execute
#<Postgrest::Responses::GetResponse GET OK data=[{"id"=>112, "title"=>"Eat something", "owner"=>{"name"=>"Marcelo"}, "worker"=>{"name"=>"Marcelo"}}, {"id"=>113, "title"=>"Go to the gym", "owner"=>{"name"=>"Marcelo"}, "worker"=>nil}]>
db.from('todos').insert(title: 'Go to the gym', completed: false).execute
#<Postgrest::Responses::PostResponse POST Created data=[{"id"=>1, "title"=>"Go to the gym", "completed"=>false}]>
db.from('todos').upsert(id: 1, title: 'Ok, I wont go to the gym', completed: true).execute
#<Postgrest::Responses::PostResponse POST Created data=[{"id"=>1, "title"=>"Ok, I wont go to the gym", "completed"=>true}]>
# Inserting multiple rows at once
db.from('todos').insert([
{ title: 'Go to the gym', completed: false },
{ title: 'Walk in the park', completed: true },
]).execute
#<Postgrest::Responses::PostResponse POST Created data=[{"id"=>110, "title"=>"Go to the gym", "completed"=>false}, {"id"=>111, "title"=>"Walk in the park", "completed"=>true}]>
# Query before update
db.from('todos').update(title: 'foobar').eq(id: 109).execute
#<Postgrest::Responses::PatchResponse PATCH OK data=[{"id"=>106, "title"=>"foo", "completed"=>false}]>
# Update all rows
db.from('todos').update(title: 'foobar').execute
#<Postgrest::Responses::PatchResponse PATCH OK data=[{"id"=>107, "title"=>"foobar", "completed"=>false}, {"id"=>1, "title"=>"foobar", "completed"=>true}, {"id"=>110, "title"=>"foobar", "completed"=>false}, {"id"=>111, "title"=>"foobar", "completed"=>true}, {"id"=>106, "title"=>"foobar", "completed"=>false}]>
# Querying before delete
db.from('todos').delete.eq(id: 109).execute
#<Postgrest::Responses::DeleteResponse DELETE OK data=[{"id"=>109, "title"=>"Go to the gym", "completed"=>false}]>
# OR deleting everything
db.from('todos').delete.execute
#<Postgrest::Responses::DeleteResponse DELETE OK data=[{"id"=>110, "title"=>"Go to the gym", "completed"=>false}, {"id"=>111, "title"=>"Go to the gym", "completed"=>false}]>
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/marcelobarreto/postgrest.
The gem is available as open source under the terms of the MIT License.