Skip to main content

Python helpers for Azure Cosmos DB SQL and MongoDB APIs.

Project description

appfx-cosmosdb

appfx-cosmosdb provides type-safe, async repository helpers for Azure Cosmos DB using either the SQL/Core API or the MongoDB API. The package keeps the two API surfaces explicit so applications import only the implementation they use.

Package identity

Purpose Name
Distribution appfx-cosmosdb
SQL/Core API namespace appfx.cosmosdb.sql
MongoDB API namespace appfx.cosmosdb.mongo

The top-level appfx.cosmosdb package is not the public home for repository or entity classes. Import from appfx.cosmosdb.sql or appfx.cosmosdb.mongo directly.

Installation

Install only the dependencies for the Cosmos DB API you use:

python -m pip install "appfx-cosmosdb[sql]"
python -m pip install "appfx-cosmosdb[mongo]"

Install both SQL and MongoDB dependencies when one application uses both APIs:

python -m pip install "appfx-cosmosdb[all]"

For local development:

python -m pip install --upgrade pip
python -m pip install -e ".[dev,all]"

SQL/Core API quick start

from appfx.cosmosdb.sql import RepositoryBase, RootEntityBase, SortDirection, SortField


class Customer(RootEntityBase["Customer", str]):
    name: str
    email: str
    is_active: bool = True


class CustomerRepository(RepositoryBase[Customer, str]):
    def __init__(self, connection_string: str, database_name: str):
        super().__init__(
            connection_string=connection_string,
            database_name=database_name,
            container_name="customers",
        )


async with CustomerRepository(connection_string, "app") as repo:
    await repo.add_async(Customer(id="customer-1", name="Ada", email="ada@example.com"))

    active_customers = await repo.find_async(
        {"is_active": True},
        sort_fields=[SortField("name", SortDirection.ASCENDING)],
    )

SQL entities automatically include a computed /_partitionKey value derived from the entity id. Use raw SQL methods for projections, aggregations, and other queries that are clearer in Cosmos DB SQL syntax.

MongoDB API quick start

from appfx.cosmosdb.mongo import RepositoryBase, RootEntityBase, SortDirection, SortField
from pydantic import Field


class Customer(RootEntityBase["Customer", str]):
    name: str
    email: str
    tags: list[str] = Field(default_factory=list)


class CustomerRepository(RepositoryBase[Customer, str]):
    def __init__(self, connection_string: str, database_name: str):
        super().__init__(
            connection_string=connection_string,
            database_name=database_name,
            collection_name="customers",
        )


async with CustomerRepository(connection_string, "app") as repo:
    await repo.add_async(Customer(id="customer-1", name="Ada", email="ada@example.com"))

    premium_customers = await repo.find_async(
        {"tags": {"$in": ["premium"]}},
        sort_fields=[SortField("name", SortDirection.ASCENDING)],
    )

Mongo repositories pass predicate dictionaries through to MongoDB-style query operators, including array and regular-expression operators supported by the driver and Cosmos DB MongoDB API.

Core concepts

  • Entity models inherit from RootEntityBase["EntityName", KeyType] for independently stored documents and from EntityBase for nested models.
  • Repositories inherit from the API-specific RepositoryBase[TEntity, TKey] and centralize CRUD, query, count, pagination, and bulk-delete operations.
  • Sort helpers are public from both API namespaces: SortField and SortDirection.
  • Resource cleanup should use async with repo: or await repo.close().

Documentation

Testing

The default test suite is unit-test focused:

python -m pytest

Live Cosmos DB tests require explicit integration setup, including a Cosmos DB account, API-specific connection settings, and any required Azure RBAC or network access. Do not put credentials in source-controlled files.

Development validation

python -m pytest
python -m pytest --cov
python -m ruff check .
python -m ruff format --check .
python -m mypy
python -m build
python -m twine check dist/*

License and provenance

This package is licensed under the MIT License. Portions of the documentation were adapted from earlier Cosmos DB helper documentation that was also MIT licensed; obsolete package names, repository URLs, and legacy branding were removed during the appfx-cosmosdb migration.

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

appfx_cosmosdb-0.1.0.tar.gz (44.3 kB view details)

Uploaded Source

Built Distribution

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

appfx_cosmosdb-0.1.0-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file appfx_cosmosdb-0.1.0.tar.gz.

File metadata

  • Download URL: appfx_cosmosdb-0.1.0.tar.gz
  • Upload date:
  • Size: 44.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for appfx_cosmosdb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d810e125f3a688e59b0937363bb652ab5973ba3238bb685f25e0baf88efbf27f
MD5 315dc5ce32eb72c34a122d3a20411823
BLAKE2b-256 39b6799c989a4a9e4c8669bcd9920ce5f650796cee2253ceb3614f1e26558b59

See more details on using hashes here.

Provenance

The following attestation bundles were made for appfx_cosmosdb-0.1.0.tar.gz:

Publisher: publish.yml on Dongbumlee/appfx-cosmosdb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file appfx_cosmosdb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: appfx_cosmosdb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for appfx_cosmosdb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d8a00907819b8e3457430b856cdcde69fdae27f5ed9c010613de15658d5bc8b
MD5 dc2750a60906238699e562e572e2818b
BLAKE2b-256 94703d6a448b8d53dd4d4c34d9bfc968e2d1e4edeb810260712bdbd4778ecc7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for appfx_cosmosdb-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Dongbumlee/appfx-cosmosdb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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