Package aiogcd includes both a Connector and ORM layer for communicating with Google Cloud Datastore without using App Engine.
By design the connector has no dependencies to the ORM layer. This makes it possible to use the connector without the orm layer if this is preferred.
The most easy way is to install this package using PyPI:
pip install aiogcd
To install from source code:
python setup.py install
The connector is designed so that it can be used without using the ORM layer.
importasynciofromaiogcd.connectorimportGcdConnectorasyncdefexample():
# create a Google Cloud Datastore connectorgcd=GcdConnector(
project_id='my_project_id_or_app_id',
client_id='my_client_id',
client_secret='my_client_secret',
token_file='token_file.json')
# make at least one call to gcd using the connect() function. this function# creates the token file in case it does not exists and might prompt the# user for a code.awaitgcd.connect()
asyncio.run(example())Create a new entity:
importasynciofromaiogcd.connectorimportGcdConnectorfromaiogcd.connector.entityimportEntityasyncdefinsert_alice():
gcd=GcdConnector(
project_id='my_project_id_or_app_id',
client_id='my_client_id',
client_secret='my_client_secret',
token_file='token_file.json')
awaitgcd.connect()
alice=Entity({
'properties': {
'name': {'stringValue': 'Alice'},
'age': {'integerValue': 26}
},
'key': {
'partitionId': {'projectId': gcd.project_id},
'path': [{'kind': 'User'}]
}
})
awaitgcd.insert_entity(alice)
asyncio.run(insert_alice())importasynciofromaiogcd.connectorimportGcdConnectorfromaiogcd.connector.keyimportKeyfromaiogcd.ormimportGcdModelfromaiogcd.orm.propertiesimportStringValuefromaiogcd.orm.propertiesimportIntegerValue# Create a GcdModel for kind 'User'classUser(GcdModel):
name=StringValue()
age=IntegerValue()
# If you want a model for a specific kind and use a different class name# you can set __kind__ to the required kind name. For example:# class UserModel(GcdModel):# __kind__ = 'User'# ...# example insertasyncdefinsert_alice():
# Create a key. As name/id we are allowed to use None since this is# a new key. Gcd will assign a new id to the key.key=Key('User', None, project_id=gcd.project_id)
# Create a new User entity.alice=User(name='Alice', age=26, key=key)
# note that the key has no id assigned yet.assertalice.key.idisNone# Insert the new entity.awaitgcd.insert_entity(alice)
# The key now has an id assignedassertisinstance(alice.key.id, int)
# return the key, we can use this laterreturnalice.key# example queryasyncdefquery_users():
# query all user entities:users=awaitUser.filter().get_entities(gcd)
# create a Key from a key string:key=Key(ks='<key_string>')
# use the Key to get the User entity:user=awaitUser.filter(key=key).get_entity(gcd)
# get all user entities with name 'Bob' and age greater than 3users=awaitUser.filter(
User.name=='Bob',
User.age>3).get_entities(gcd)
# get all user entities with name 'Alice' and an ancestor keyusers=awaitUser.filter(
User.name=='Alice',
has_ancestor=Key('Foo', 123)).get_entities(gcd)
# get all user entities with name 'Alice' and sort on ageusers=awaitUser.filter(
User.name=='Alice'
).order_by(
User.age.ascending
).get_entities(gcd)
# get first 2 user entities with name 'Alice' and sort on ageusers=awaitUser.filter(
User.name=='Alice'
).order_by(
User.age.ascending
).limit(2).get_entities(gcd)
# example updateasyncdefupdate_age(ks, new_age):
# get the user by key stringuser=awaitUser.filter(key=Key(ks=ks)).get_entity(gcd)
# change the ageuser.age=new_age# save the changesawaitgcd.update_entity(user)
gcd=GcdConnector(
project_id='my_project_id_or_app_id',
client_id='my_client_id',
client_secret='my_client_secret',
token_file='token_file.json')
asyncio.run(gcd.connect())
asyncio.run(insert_alice())
asyncio.run(query_users())As of version 0.11.4, aiogcd has support for namespaces. For using namespaces the Key and GcdModel
When initializing a key from a key-string, the namespace_id is automatically unpacked.
If instead a path is used, the namespace can be given using the namespace_id=<my_namespace> keyword argument.
It is also possible to set the namespace using a dictionary, like in the example below:
{
"partitionId": {
"projectId": "my-project-id",
"namespaceId": "my-namespace"
}
...
}If a GcdModel is used, it is possible to set the namespace on the model. For example:
classExample(GcdModel):
__namespace__='my-namespace'With the above solution, queries like Example.get_entities(gcd) will automatically query the correct namespace.
As an alternative, the GcdConnector can be initialized using the namespace_id=... keyword argument.
If the namespace_id argument is used, all queries will use the given namespace unless explicitly overwritten by a GcdModel.
The Google Datastore emulator is supported by aiogcd and thus reads the DATASTORE_EMULATOR_HOST environment variable.
Note that the DATASTORE_PROJECT_ID will be ignored and still needs to be set using the initialization of the GcdConnector.
See https://cloud.google.com/datastore/docs/tools/datastore-emulator for documentation on how to start the emulator.