-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Hi guys,
I was reviewing the "state of the world" of the datastore API, and found a few things that seem (to me at least) a bit more complicated than they should be.
A bit of this comes from the authentication stuff (which we have another open bug for) but I wanted to bring up a few things for discussion... If this issue gets out of control, we can move the discussion elsewhere, but I wanted to kick things off:
Create a new Person entity in the Datastore
What I have to write
from gcloud import datastore
datastore.set_defaults()
entity = datastore.Entity(key=datastore.Key('Person'))
entity.update({'name': 'Jimmy'})
datastore.put([entity])What I'd like to write
from gcloud import datastore
entity = datastore.Entity('Person')
entity.update({'name': 'Jimmy'})
entity.put()Key differences
- no
set_defaults()(there's another issue open for this I believe) - Create an entity of kind Person without lots more typing (or maybe we get the entity with
datastore.Kind('Person').get_entity()?) .putnot requiring a list for a single entity
Get an existing Person (123) from the Datastore
What I have to write
from gcloud import datastore
datastore.set_defaults()
entity = datastore.get(datastore.Key('Person', 123))What I'd like to write
from gcloud import datastore
datastore.set_defaults()
entity = datastore.Kind('Person').get_by_id(123)Key differences
- No
set_defaults - Equivalent of "drilling down" into a "table" (kind), and then a "row" (id)
(Not hugely concerned about this use case, but wanted to toss it out)
Create a new Person using a specific set of credentials (/home/creds.json) to talk to a specific dataset ('my-dataset') in the Datastore
(Being looked into in PR #641)
What I have to write
(This is a best guess... I don't know if this code is even right... And even so, this code won't do the same thing if you ran it on App Engine because of the order of evaluation here: http://googlecloudplatform.github.io/gcloud-python/latest/gcloud-api.html#gcloud.credentials.get_credentials )
import sys
sys.env['GOOGLE_APPLICATION_CREDENTIALS'] = '/home/creds.json'
from gcloud import datastore
datastore.set_defaults()
entity = datastore.Entity(key=datastore.Key('Person', dataset_id='my-dataset'))
entity.update{'name': 'Jimmy'})
datastore.put(entity)What I'd like to write
from gcloud import datastore
datastore.set_credentials('/home/creds.json')
dataset = datastore.Dataset('my-dataset')
entity = dataset.Entity('Person')
entity.update({'name': 'Jimmy'})
entity.put()Key differences
- Simple in-code way to set which credentials file to use
- High-level concept of a dataset
- Datasets can provide Entities just like
datastorewould