Skip to main content

Cosmos DB

Python SDK for Azure Cosmos DB NoSQL

About Cosmos DB

Structure (DBs, containers, items)

  • A CosmosDB resource can have multiple DBs
  • A DB can have multiple containers
  • A container can have multiple items

Identifying

  • Every item has a compulsory id field
  • Every container defines a partition key, compulsory for all of it's items
    • The container's partition key field can be set to any string, including /id
  • If the container's partition key is, e.g., username, a new item would be:
    {
        "id": "<uuid>",
        "username": "<username>",
        // other optional fields
    }
    

Querying

  • Points reads can be done by giving db, container, id, partition_key
  • Containers can be queried with SQL
    • Specifying a partition_key will limit the query to a partition

Authorization

Both required fields can be obtained from the Azure Portal, within the Cosmos DB resource, Keys tab:

  • COSMOS_ENDPOINT (URI in the portal): https://<resource-name>.documents.azure.com:443/
  • COSMOS_KEY (PRIMARY KEY in the portal): <random base64 string>

Environment (.env)

For convenience, you can set os.environ["COSMOS_ENDPOINT"] and os.environ["COSMOS_KEY"]. If you do, you can skip specifying them on every call.

E.g., create a .env file:

COSMOS_ENDPOINT="<COSMOS_ENDPOINT>"
COSMOS_KEY="<COSMOS_KEY>"

Then load it before importing

from dotenv import load_dotenv
load_dotenv()
import cosmosdb as db

db.client() # just works!

API

  • Asynchronous (everything is a coroutine)
  • Single calls or multiple using a same client:
    # single call
    await db.list.dbs(url=COSMOS_ENDPOINT, key=COSMOS_KEY) # these aren't necessary with a .env file
    
    # multiple calls
    async with db.client(url=COSMOS_ENDPOINT, key=COSMOS_KEY) as client: # idem.
        await db.create.db("<db name>", client=client)
        dbs = await db.list.dbs(client=client)
    

Functions

cosmosdb
  create
    async def db(...)
    async def container(...)
    async def item(...)
  delete
    async def db(...)
    async def container(...)
    async def item(...)
  list
    async def dbs(...)
    async def containers(...)
    async def items(...)
  query
    async def read(...)
    async def sql(...)

Metadata

Release files for cosmosdb 0.1.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cosmosdb 0.1.12
File Size Uploaded
cosmosdb-0.1.12.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cosmosdb 0.1.12
File Interpreter ABI Platform
cosmosdb-0.1.12-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / cosmosdb-0.1.12.tar.gz

Download URL cosmosdb-0.1.12.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
92eb8e1f32fda27621bc628613f1cadec549e8ee6bde7dad0ae6f8dafc1f1ede
BLAKE2b-256 checksum
How to use checksums
e8eaa6259cd550e81fc8bf7d0bf26af73d7f32ea2d69493deb888ea0955ecfc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.6

Release files / cosmosdb-0.1.12-py3-none-any.whl

Download URL cosmosdb-0.1.12-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31f3f84c15af76db6857dd051179898c9b4b23e4bfb5852121c79deeae5ab479
BLAKE2b-256 checksum
How to use checksums
6d71633b6b2a2740a6d7b97c4fd3461cd9ccac33cb67d9e07c305304400368fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.6

Release history Release notifications | RSS feed

This release

0.1.12 This release

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page