Package: aiodatastore
aiodatastoreaiodatastoreis a low level and high performance asyncio client forGoogle Datastore REST API. Inspired bygcloud-aiolibrary, thanks!Key advantages:lazy properties loading (that's why it's fast, mostly)explicit value types for properties (no types guessing)strictly following Google Datastore REST API data structuresInstallationpip install aiodatastoreHow to create datastore clientfromaiodatastoreimportDatastoreclient=Datastore("project1",service_file="/path/to/file")You can also set namespace if needed:fromaiodatastoreimportDatastoreclient=Datastore("project1",service_file="/path/to/file",namespace="namespace1")To useDatastore emulator(for tests or development), just defineDATASTORE_EMULATOR_HOSTenvironment variable (usually value is127.0.0.1:8081).How to work withkeysandentitiesfromaiodatastoreimportKey,PartitionId,PathElementkey=Key(PartitionId("project1"),[PathElement("Kind1")])You can also setnamespacefor key:fromaiodatastoreimportKey,PartitionId,PathElementkey=Key(PartitionId("project1",namespace_id="namespace1"),[PathElement("Kind1")])Andidornamefor path element:fromaiodatastoreimportKey,PartitionId,PathElementkey1=Key(PartitionId("project1"),[PathElement("Kind1",id="12345")])key2=Key(PartitionId("project1"),[PathElement("Kind1",name="name1")])To create an entity object, you have to specify key and properties. Properties is a dict with string keys and typed values. For eachdata typethe library provides corresponding value class. Every value (except ArrayValue) can be indexed or not (indexed by default):fromaiodatastoreimportEntity,Key,PartitionId,PathElementfromaiodatastoreimport(ArrayValue,BoleanValue,BlobValue,DoubleValue,GeoPointValue,IntegerValue,LatLng,NullValue,StringValue,TimestampValue,)key=Key(PartitionId("project1"),[PathElement("Kind1")])entity=Entity(key,properties={"array-prop":ArrayValue([NullValue(),IntegerValue(123),StringValue("str1")]),"bool-prop":BooleanValue(True),"blob-prop":BlobValue("data to store as blob"),"double-prop":DoubleValue(1.23,indexed=False),"geo-prop":GeoPointValue(LatLng(1.23,4.56)),"integer-prop":IntegerValue(123),"null-prop":NullValue(),"string-prop":StringValue("str1"),"timestamp-prop":TimestampValue(datetime.datetime.utcnow()),})To access property value use.valueattribute:print(entity.properties["integer-prop"].value)123Use.valueattribute to change property value and keep index status. Or assign new value and set index:print(entity.properties["integer-prop"].value,entity.properties["integer-prop"].indexed)123,Trueentity.properties["integer-prop"].value=456print(entity.properties["integer-prop"].value,entity.properties["integer-prop"].indexed)456,Trueentity.properties["integer-prop"]=IntegerValue(456,indexed=True)print(entity.properties["integer-prop"].value,entity.properties["integer-prop"].indexed)456,TrueUse.indexedattribute to access or change index:print(entity.properties["integer-prop"].indexed)Trueentity.properties["integer-prop"].indexed=Falseprint(entity.properties["integer-prop"].indexed)FalseTo insert new entity (the entity key's final path element may be incomplete):key=Key(PartitionId("project1"),[PathElement("Kind1")])entity=Entity(key,properties={"string-prop":StringValue("some value"),})awaitclient.insert(entity)To update an entity (the entity must already exist. Must have a complete key path):entity.properties["string-prop"]=StringValue("new value")awaitclient.update(entity)To upsert an entity (the entity may or may not already exist. The entity key's final path element may be incomplete):key=Key(PartitionId("project1"),[PathElement("Kind1")])entity=Entity(key,properties={"string-prop":StringValue("some value"),})awaitclient.upsert(entity)To delete an entity (the entity may or may not already exist. Must have a complete key path and must not be reserved/read-only):awaitclient.delete(entity)If you have entity's key or know how to build it:awaitclient.delete(key)