Skip to main content

Low level and high performance asyncio client for Google Datastore REST API

Project description

Package version Supported Versions Test Code style: black

aiodatastore

aiodatastore is a low level and high performance asyncio client for Google Datastore REST API. Inspired by gcloud-aio library, 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 structures

Installation

pip install aiodatastore

How to create datastore client

from aiodatastore import Datastore

client = Datastore("project1", service_file="/path/to/file")

You can also set namespace if needed:

from aiodatastore import Datastore

client = Datastore("project1", service_file="/path/to/file", namespace="namespace1")

To use Datastore emulator (for tests or development), just define DATASTORE_EMULATOR_HOST environment variable (usually value is 127.0.0.1:8081).

How to work with keys and entities

from aiodatastore import Key, PartitionId, PathElement

key = Key(PartitionId("project1"), [PathElement("Kind1")])

You can also set namespace for key:

from aiodatastore import Key, PartitionId, PathElement

key = Key(PartitionId("project1", namespace_id="namespace1"), [PathElement("Kind1")])

And id or name for path element:

from aiodatastore import Key, PartitionId, PathElement

key1 = 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 each data type the library provides corresponding value class. Every value (except ArrayValue) can be indexed or not (indexed by default):

from aiodatastore import Entity, Key, PartitionId, PathElement
from aiodatastore import (
    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 .value attribute:

print(entity.properties["integer-prop"].value)
123

Use .value attribute 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, True
entity.properties["integer-prop"].value = 456
print(entity.properties["integer-prop"].value, entity.properties["integer-prop"].indexed)
456, True

entity.properties["integer-prop"] = IntegerValue(456, indexed=True)
print(entity.properties["integer-prop"].value, entity.properties["integer-prop"].indexed)
456, True

Use .indexed attribute to access or change index:

print(entity.properties["integer-prop"].indexed)
True

entity.properties["integer-prop"].indexed = False
print(entity.properties["integer-prop"].indexed)
False

To 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"),
})
await client.insert(entity)

To update an entity (the entity must already exist. Must have a complete key path):

entity.properties["string-prop"] = StringValue("new value")
await client.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"),
})
await client.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):

await client.delete(entity)

If you have entity's key or know how to build it:

await client.delete(key)

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

aiodatastore-0.1.5.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

aiodatastore-0.1.5-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file aiodatastore-0.1.5.tar.gz.

File metadata

  • Download URL: aiodatastore-0.1.5.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for aiodatastore-0.1.5.tar.gz
Algorithm Hash digest
SHA256 48d00392d2219a6ace1161bc52bb333834e768d3ef04a73eca407262fe98426a
MD5 18e09756601c02c5770b2840a817b23d
BLAKE2b-256 9140eac39d40221a104d470031b102d39c306132d18136bdf38e1918633a0a4a

See more details on using hashes here.

Provenance

File details

Details for the file aiodatastore-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: aiodatastore-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for aiodatastore-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b0b6a782ad990ae0f2c49df4b618d4a3c50b0e169ba5cd3ff05a07edf1107aaa
MD5 9a30083dbebde5ab70899c5fde88b7d2
BLAKE2b-256 d3a678b0f86c0efb8e8c4368a1c1b55e89ae3c1724c18aae5b4ae5b29f89b5c4

See more details on using hashes here.

Provenance

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