Skip to main content

Datastore framework implementation for umongo

Project description

μDatastore: ODM for Google Cloud Datastore

Implemented as framework in the μMongo library (a python ODM for MongoDB)

While working Google Cloud Datastore, most of our code uses the google-cloud-datastore package, for creating, updating and querying entities. We often find ourselves writing code to export objects to datastore.Entity, and import from entities to objects. On AppEngine, there is ndb which avoids this, so the idea emerged to create an ODM for datastore ourselves.

Having worked with μMongo before, a prototype was created which implements an additional framework based on following conventions:

  • A datastore partition (project/namespace) corresponds to a mongo database
  • A datastore entity kind corresponds to a mongo collection
  • The datastore __key__ field corresponds to the mongo _id field

Install

pip install udatastore

Example

from datetime import datetime
from google.cloud import datastore
from udatastore import DataStoreInstance
from umongo import Document, fields, validate


db = datastore.Client(project="dummy", namespace='abcd')
instance = DataStoreInstance()
instance.init(db)


@instance.register
class User(Document):
    email = fields.EmailField(required=True, unique=True)
    birthday = fields.DateTimeField(validate=validate.Range(min=datetime(1900, 1, 1)))
    friends = fields.ListField(fields.ReferenceField("User"))

goku = User(email='goku@sayen.com', birthday=datetime(1984, 11, 20))
goku.commit()
vegeta = User(email='vegeta@over9000.com', friends=[goku])
vegeta.commit()

vegeta.friends
# <object umongo.data_objects.List([<object udatastore.reference.DataStoreReference(document=User, pk=<Key('User', 4476), project=dummy>)>])>
vegeta.dump()
# {'email': 'vegeta@over9000.com', 'id': '4477', 'friends': [<Key('User', 4476), project=dummy>]}
User.find_one({"email": 'goku@sayen.com'})
# <object Document __main__.User({'email': 'goku@sayen.com', 'id': 4474,
#                                 'friends': <object umongo.data_objects.List([])>,
#                                 'birthday': datetime.datetime(1984, 11, 20, 0, 0)})>

Limitations:

Not all features of μMongo are available or work exactly the same in μDatastore:

  • No indexes
  • We do not currently support all field types
  • Datastore converts any datetime to UTC timezone. For now, μDatastore overrides this behaviour and always works with unaware datetimes.
  • We bring our own ReferenceField implementation. You are free to use umongo.fields.ReferenceField, as the DataStoreBuilder will automatically replace these fields with our implementation. We also disable the io_validation on reference fields. Because datastore is eventually consistent this may report errors when creating references to previously created entities.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

udatastore-0.1.7.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

udatastore-0.1.7-py2.py3-none-any.whl (17.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file udatastore-0.1.7.tar.gz.

File metadata

  • Download URL: udatastore-0.1.7.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for udatastore-0.1.7.tar.gz
Algorithm Hash digest
SHA256 608eb2d49f504f309c0f3c07dd4a182c720c0526348d252fb35d374f6f130ddd
MD5 fd36b42ec6ec4f04ca4fd263367f1202
BLAKE2b-256 328872fa762661e10fab4fab607bae13d567ec8c1cb65d1c921c37412465efa6

See more details on using hashes here.

File details

Details for the file udatastore-0.1.7-py2.py3-none-any.whl.

File metadata

  • Download URL: udatastore-0.1.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for udatastore-0.1.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b409cbbf1c6ae793e2af5822b9a1a3c01b71f3a2ba6b34182c2e956e0527f7dd
MD5 16abbac02fba07e87379e820fdffc5af
BLAKE2b-256 a9a5c38414af6fe1b48025e916c2be278a35fe1e2f408a07e71829cf43829805

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page