Skip to main content

Async Google Cloud Datastore API

Project description

CI Release Version

Async Google Cloud Datastore API

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.



Installation

The most easy way is to install this package using PyPI:

pip install aiogcd

To install from source code:

python setup.py install

Connector

The connector is designed so that it can be used without using the ORM layer.

Quick usage:

import asyncio
from aiogcd.connector import GcdConnector

async def example():

    # create a Google Cloud Datastore connector
    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')

    # 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.
    await gcd.connect()

asyncio.run(example())

Entity

Create a new entity:

import asyncio
from aiogcd.connector import GcdConnector
from aiogcd.connector.entity import Entity

async def insert_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')

    await gcd.connect()

    alice = Entity({
        'properties': {
            'name': {'stringValue': 'Alice'},
            'age': {'integerValue': 26}
        },
        'key': {
            'partitionId': {'projectId': gcd.project_id},
            'path': [{'kind': 'User'}]
        }
    })

    await gcd.insert_entity(alice)

asyncio.run(insert_alice())

ORM Layer

import asyncio
from aiogcd.connector import GcdConnector
from aiogcd.connector.key import Key
from aiogcd.orm import GcdModel
from aiogcd.orm.properties import StringValue
from aiogcd.orm.properties import IntegerValue

# Create a GcdModel for kind 'User'
class User(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 insert
async def insert_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.
    assert alice.key.id is None

    # Insert the new entity.
    await gcd.insert_entity(alice)

    # The key now has an id assigned
    assert isinstance(alice.key.id, int)

    # return the key, we can use this later
    return alice.key

# example query
async def query_users():
    # query all user entities:
    users = await User.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 = await User.filter(key=key).get_entity(gcd)

    # get all user entities with name 'Bob' and age greater than 3
    users = await User.filter(
        User.name == 'Bob',
        User.age > 3).get_entities(gcd)

    # get all user entities with name 'Alice' and an ancestor key
    users = await User.filter(
        User.name == 'Alice',
        has_ancestor= Key('Foo', 123)).get_entities(gcd)

    # get all user entities with name 'Alice' and sort on age
    users = await User.filter(
        User.name == 'Alice'
        ).order_by(
            User.age.ascending
        ).get_entities(gcd)

    # get first 2 user entities with name 'Alice' and sort on age
    users = await User.filter(
        User.name == 'Alice'
        ).order_by(
            User.age.ascending
        ).limit(2).get_entities(gcd)

# example update
async def update_age(ks, new_age):
    # get the user by key string
    user = await User.filter(key=Key(ks=ks)).get_entity(gcd)

    # change the age
    user.age = new_age

    # save the changes
    await gcd.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())

Namespaces

As of version 0.11.4, aiogcd has support for namespaces. For using namespaces the Key and GcdModel

Key

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"
    }
    ...
}

GcdModel

If a GcdModel is used, it is possible to set the namespace on the model. For example:

class Example(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.

Emulator

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.

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

aiogcd-1.0.2.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiogcd-1.0.2-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file aiogcd-1.0.2.tar.gz.

File metadata

  • Download URL: aiogcd-1.0.2.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for aiogcd-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d15559fd97b22c56451d0418f278ba962b98b5f95ce846dcdf7ebe1878fa1f00
MD5 8a6f375da8a9fae32eefcd33b5e578ad
BLAKE2b-256 a1bb9c32a12f1104a26ec02c9e9ded36ff5653ba335da03ae809c305239f56f9

See more details on using hashes here.

File details

Details for the file aiogcd-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: aiogcd-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for aiogcd-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 84cd2ed07782c30ff76ec19c27b24487340f2d6f40ee27d38be91100c5b4b263
MD5 2ec0f7ffa31befb3b70529b6b8a5aa54
BLAKE2b-256 4d6f99d641c53b4b1c238b5d71a5808b07049ca7d6630450a09e76e38fdc7a73

See more details on using hashes here.

Supported by

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