GithubHelp home page GithubHelp logo

shinyak14 / gdatastore_mapper Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 93 KB

Google Cloud Datastore Mapper in Ruby / Ruby on Rails

License: MIT License

Ruby 82.90% Shell 0.19% JavaScript 1.73% CoffeeScript 0.30% CSS 1.32% HTML 13.55%
google-cloud-datastore ruby rails

gdatastore_mapper's Introduction

GdatastoreMapper

CircleCI Gem Version

GdatastoreMapper is a mapper framework for Google Cloud Datastore in Ruby / Ruby on Rails. Once you install GdatastoreMapper you can use Google Cloud Datastore like ActiveRecord.

Table of Contents

Demo

Here is demo. The demo works with Google Cloud Datastore.

Source code is here.

Requirements

GdatastoreMapper requires Rails version >= 5

Installation

Execute rails new with --skip-active-record

$ rails new your_project --skip-active-record

Add this line to your application's Gemfile:

gem 'gdatastore_mapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install gdatastore_mapper

Configuration

GdatastoreMapper configuration can be done through a database.yml. The simplest configuration is as follows, which sets the emulator_host to "localhost:8444" and dataset_id.

# config/database.yml
production:
  dataset_id: your-google-cloud-platform-project-id

staging:
  dataset_id: your-google-cloud-platform-project-id

development:
  dataset_id: your-google-cloud-platform-project-id
  emulator_host: localhost:8444

test:
  dataset_id: your-google-cloud-platform-project-id
  emulator_host: localhost:8444

Model Setting

Only 2 things you need to do.

  1. To include GdatastoreMapper
  2. To set attr_accessor as column

That's it! No need to db:migrate.

class Book
  include GdatastoreMapper::Base

  attr_accessor :title, :author
end

Persistence Methods

new record

book = Book.new
book.title = 'Harry Potter'
book.save
book = Book.new(title: 'Harry Potter')
book.save
Book.create(title: 'Harry Potter')

update

book.update(title: 'Harry Potter 2')

delete

book.delete
Book.delete_all

Scoping Methods

Book.where(title: 'Harry Potter')
=> [#<Book:0x00 @created_at=2017-04-08 21:22:31 +0200, @title="Harry Potter",
    @id=70, @updated_at=2017-04-08 21:22:31 +0200>]
Book.find(12)
=> #<Book:0x00 @created_at=2017-04-07 10:03:54 +0200, @title="Harry Potter",
    @id=12, @updated_at=2017-04-07 22:57:57 +0200>
Book.find_by(title: 'Harry Potter')
=> #<Book:0x00 @title="Harry Potter" ....
Book.order(title: :asc)
=> [#<Book:0x00 @title="Harry Potter" .... ]
Book.first
=> #<Book:0x00 @title="Harry Potter" ....
Book.last
=> #<Book:0x00 @title="Harry Potter" ....
Book.count
=> 100
Book.all
=> [#<Book:0x00 @title="Harry Potter" .... ]

Timestamp

All records have created_at and updated_at. They will be updated automatically.

Associations

One to Many

Associations can be set the same as Active Record.

example of one to many relationship

class Book
  include GdatastoreMapper::Base

  attr_accessor :title

  belongs_to :author
end
class Author
  include GdatastoreMapper::Base

  attr_accessor :name

  has_many :books
end

books.create

rowling = Author.create(name: 'J. K. Rowling')
harry_poter = rolling.books.create(title: 'Harry Poter')
harry_poter2 = rolling.books.create(title: 'Harry Poter 2')

books

rowling.books
=> [#<Book:0x00 @title="Harry Potter" .... ]

books.count

rowling.books.count
=> 2
harry_poter.author
=> [#<Author:0x00 @name="J. K. Rowling" .... ]

Method Chaining

rowling.books.order(created_at: :desc).limit(3)
=> [#<Book:0x00 @title="Harry Potter" .... ]
rowling.books.where(series: 'Harry Potter').order(published_at: :asc).limit(3)
=> [#<Book:0x00 @title="Harry Potter" .... ]

Callbacks

class Book
  include GdatastoreMapper::Base

  before_save :something_before_save

  private
  def something_before_save
    something that you want
  end

end

Validations

class Author
  include GdatastoreMapper::Base

  attr_accessor :email

  validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
  validates :email, length: { in: 3..20 }
  validates_uniqueness_of :email
end

Contact

Please shoot me an e-mail if you find any issues, ideas and comments. [email protected] Thanks!

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/shinyaK14/gdatastore_mapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

gdatastore_mapper's People

Contributors

shinyak14 avatar

Watchers

James Cloos 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.