gaetk2.datastore module

gaetk2.datastore tries to codify a common set of expectations and usages for gaetk2.

Inherit from gaetk.datastore.gaetkModel instead of ndb.Model to get some added functionality. The rationale there is that e common interface and thus admin- and programmer-time is more important than savings on space and and processing time. To we add possible unneded database fields. You can remove them on a case by case basis in derivered classes.

  • query_iterator() - helps to iterate over big query results
  • get_or_insert_if_new() helps you to see if a new Entity was created.
  • copy_entity() - can write an entity with a different key to the datastore
  • write_on_change2() - basically implements conditional put()
  • reload_obj() - forces an object to be re-read from disk

Module contents

gaetk2.datastore.query_iterator(query, limit=50)[source]

Iterates over a datastore query while avoiding timeouts via a cursor.

Especially helpful for usage in backend-jobs.

gaetk2.datastore.copy_entity(e, **extra_args)[source]

Copy ndb entity but change values in kwargs.

Usage::
b = copy_entity(a, id=’new_id_here’) b.put()
gaetk2.datastore.get_or_insert_if_new(cls, id, **kwds)[source]

Like ndb.get_or_insert()` but returns (entity, new).

This allows you to see if something has been created or if there was an already existing entity:

>>> get_or_insert_if_new(Model, 'newid')
(<instance>, True)
>>> get_or_insert_if_new(Model, 'newid')
(<instance>, False)
gaetk2.datastore.write_on_change2(instance, data)[source]

Apply new data to an entity and write to datastore if anything changed.

This should save you money since reads are 3 times cheaper than writes. It also helps you do leave not given attributes unchanged.

Usage:

instance = ndb.Model...get()
dirty = write_on_change2(instance, ...,
  dict(id=123, amout_open=500, score=5, ...)
gaetk2.datastore.reload_obj(obj)[source]

Returns a reloaded Entity from disk.