Code Monkey home page Code Monkey logo

postgrest-rb's Introduction

PostgREST

Status

Build Status Gem Version Code Climate Code Climate Ruby Style Guide RubyGems

Ruby client for PostgREST

This gem is under development, any help are welcome ๐Ÿ’ช

Installation

Add this line to your application's Gemfile:

gem 'postgrest'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install postgrest

Usage

Configuration

db = Postgrest::Client.new(url: url, headers: headers, schema: schema)

Selecting

# 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"}]>

Renaming a column name

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"}]>

Querying

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}}]>

Ordering

TODO

Relationships

TODO

Full query example

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}]>

Inserting

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}]>

Updating

# 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}]>

Deleting

# 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}]>

Development

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marcelobarreto/postgrest.

License

The gem is available as open source under the terms of the MIT License.

postgrest-rb's People

Contributors

marcelobarreto avatar michal-kazmierczak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.