# GdatastoreMapper [![CircleCI](https://circleci.com/gh/shinyaK14/gdatastore_mapper/tree/master.svg?style=svg)](https://circleci.com/gh/shinyaK14/gdatastore_mapper/tree/master) [![Gem Version](https://badge.fury.io/rb/gdatastore_mapper.svg)](https://badge.fury.io/rb/gdatastore_mapper) 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](#demo) - [Requirements](#requirements) - [Installation](#installation) - [Configuration](#configuration) - [Model Setting](#model-setting) - [Persistence Methods](#persistence-methods) - [Scoping Methods](#scoping-methods) - [Timestamp](#timestamp) - [Associations](#associations) - [One to Many](#one-to-many) - [Callbacks](#callbacks) - [Validations](#validations) - [Contact](#contact) - [Development](#development) ## Demo Here is [demo](https://gdatastore-mapper-sample.appspot.com/). The demo works with Google Cloud Datastore. Source code is [here](https://github.com/shinyaK14/gdatastore_mapper/tree/master/rails_example). ## 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: ```ruby 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. ```ruby 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.find(12) => # ``` ``` Book.find_by(title: 'Harry Potter') => # [# # # 100 ``` ``` Book.all => [# [# 2 ``` ``` harry_poter.author => [#