ActiveDatastore
With the release of the Google Cloud Datastore API, it's finally possible to leverage the datastore in pretty much any stack. Since Google seems to be supporting NodeJS, Python and Java right of the bat, it makes sense to bridge the Ruby gap.
ActiveDatastore is (eventually) meant to be comparable to ActiveRecord in terms of API, features and ease of use. It will rely heavily on ActiveModel, but will continue to be framework agnostic and usable with or without Rails.
Current Status
I've built a wrapper over the GCD API (ActiveDatastore::Dataset
) that's fully functional and can be used for pretty much all the examples given in the documentation. The wrapper's API is very similar to what you see in the NodeJS examples.
Right now, ActiveDatastore is very much a work in progress. Please evaluate it before production use, and do remember that the API may undergo breaking changes until we hit v1.0.
Installation
Add this line to your application's Gemfile:
gem 'active_datastore'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_datastore
Usage
Follow the instructions here to get your service account (AUTH_EMAIL
), private key (AUTH_KEY
) and dataset id (DATASET_ID
).
In your initializer (if you're on Rails you could create config/initializers/datastore.rb
):
require 'active_datastore'
AUTH_EMAIL = "something@developer.gserviceaccount.com"
AUTH_KEY = File.open("path/to/my/secret.key.p12").read
DATASET_ID = "my-dataset"
client = ActiveDatastore::Client.new AUTH_EMAIL, AUTH_KEY
$dataset = ActiveDatastore::Dataset.new DATASET_ID, client
The initialization of the client is performance / network intensive, so it might make sense to do it once and hold the $dataset
for the lifetime of the application. ActiveDatastore::Dataset
is stateless and will support this.
To make calls to the datastore, use the methods documented on the JSON API Reference, but remember to use the underscore equivalent names. For example, use begin_transaction
instead of beginTransaction
.
start_response = $dataset.begin_transaction({
isolationLevel: "snapshot"
})
start_response.data.kind # Should be "datastore#beginTransactionResponse"
transaction_id = start_response.data.transaction
rollback_response = $dataset.rollback({
transaction: transaction_id
})
rollback_response.data.kind # Should be "datastore#rollbackResponse"
Both the request body and the response.data
are hashes with camel case keys, as shown in the docs. Using camelCase for the request and response while underscore_case for the method names is a little inconsistent, but any effort to deeply walk through each hash and convert keys will probably be more trouble than it's worth.
Contributing
- Fork the repo
- Create your feature branch (
git checkout -b bug-fix
) - WRITE GOOD AND CLEAR TESTS. Do remember that this gem may be used in production to serve millions of requests. Bugs can be catastrophic. Also, I can't merge and maintain code I don't fully understand.
- Commit your changes (
git commit -am 'Fixed bug!'
) - Push to the branch (
git push origin bug-fix
) - Create new Pull Request
Please do the same for features you'd like to add, but send the pull request fast and early so we can talk about it and not do things twice. Pull requests are conversations.