Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare applications for Azure deployment by generating infrastructure code, Dockerfiles, and config files.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/cosmos-db/sdk.md
1# Cosmos DB SDK Connection Patterns23## Node.js45```javascript6const { CosmosClient } = require("@azure/cosmos");78const client = new CosmosClient(process.env.COSMOS_CONNECTION_STRING);9const database = client.database("appdb");10const container = database.container("items");1112// Query example13const { resources } = await container.items14.query("SELECT * FROM c WHERE c.userId = @userId", {15parameters: [{ name: "@userId", value: userId }]16})17.fetchAll();18```1920## Python2122```python23from azure.cosmos import CosmosClient24import os2526client = CosmosClient.from_connection_string(os.environ["COSMOS_CONNECTION_STRING"])27database = client.get_database_client("appdb")28container = database.get_container_client("items")2930# Query example31items = container.query_items(32query="SELECT * FROM c WHERE c.userId = @userId",33parameters=[{"name": "@userId", "value": user_id}]34)35```3637## .NET3839```csharp40using Microsoft.Azure.Cosmos;4142var client = new CosmosClient(Environment.GetEnvironmentVariable("COSMOS_CONNECTION_STRING"));43var database = client.GetDatabase("appdb");44var container = database.GetContainer("items");4546// Query example47var query = new QueryDefinition("SELECT * FROM c WHERE c.userId = @userId")48.WithParameter("@userId", userId);49var iterator = container.GetItemQueryIterator<dynamic>(query);50```5152## Best Practices5354| Practice | Reason |55|----------|--------|56| Reuse client instances | Connection pooling |57| Use parameterized queries | SQL injection prevention |58| Set appropriate timeouts | Handle transient failures |59| Enable diagnostics in dev | Debug RU consumption |60