Skip to main content

Python SDK for Azure Cosmos DB NoSQL

Project description

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(...)

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

cosmosdb-0.1.12.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

cosmosdb-0.1.12-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file cosmosdb-0.1.12.tar.gz.

File metadata

  • Download URL: cosmosdb-0.1.12.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for cosmosdb-0.1.12.tar.gz
Algorithm Hash digest
SHA256 92eb8e1f32fda27621bc628613f1cadec549e8ee6bde7dad0ae6f8dafc1f1ede
MD5 6af7e8d706794357d2c9581857e38d23
BLAKE2b-256 e8eaa6259cd550e81fc8bf7d0bf26af73d7f32ea2d69493deb888ea0955ecfc9

See more details on using hashes here.

File details

Details for the file cosmosdb-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: cosmosdb-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for cosmosdb-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 31f3f84c15af76db6857dd051179898c9b4b23e4bfb5852121c79deeae5ab479
MD5 8b5d3d583ab035bb39d322582173535c
BLAKE2b-256 6d71633b6b2a2740a6d7b97c4fd3461cd9ccac33cb67d9e07c305304400368fd

See more details on using hashes here.

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