microsoft/skills

azure-cosmos-db-py

把 Cosmos 持久层接进 FastAPI,同时修正模板中的工程缺口

  • SKILL.md 已保存
  • 中文用法已整理
  • 安装与运行未实测
先判断是否解决你的问题

azure-cosmos-db-py 是什么?

azure-cosmos-db-py 为Python/FastAPI设计Cosmos DB持久层,拆分客户端、业务service和Pydantic模型,包含本地模拟器与Azure身份两种连接方案、分区访问及mock测试思路。它偏工程结构,区别于只列SDK CRUD的azure-cosmos-py。

中文说明由本站依据来源整理;英文原文保留在页面下方。

按客户端选择安装入口

怎么接入你的 Agent?

  1. 定位完整 Skill 目录

    打开本项目来源,核对 SKILL.md 及其引用的脚本和资料。

  2. 交给你正在用的 Agent

    在下面选择客户端,展开并复制安装指令,让 Agent 读取来源后选择安装方式。

  3. 确认识别,再做具体任务

    让 Agent 回报安装目录和识别到的名称 azure-cosmos-db-py,然后按本页使用示例提供材料。

展开并复制给 Codex 的安装指令
发送给 Codex

安装并检查 azure-cosmos-db-py

请在 Codex 的当前工作环境中安装并检查 azure-cosmos-db-py 这个 Skill。

项目身份:microsoft/skills / azure-cosmos-db-py
来源:https://github.com/microsoft/skills/blob/f03ec5392f7a0f3aca12af47477e92f1edf7b44c/.github/plugins/azure-sdk-python/skills/azure-cosmos-db-py/SKILL.md
已定位的源文件:.github/plugins/azure-sdk-python/skills/azure-cosmos-db-py/SKILL.md

1. 先阅读真实 SKILL.md 和它引用的必要文件,确认触发条件、安装方式、依赖与权限;尊重源文件要求的审批步骤。
2. 按当前客户端支持的方式安装完整 Skill 目录,保留已有配置,不只复制一段提示词。涉及缺失凭证、目标目录或权限时先列出缺项并向我确认。
3. 安装后回读实际目录、Skill 名称和识别结果,说明哪些步骤已完成,哪些只是文档检查,不得把安装成功当作任务已经实测。
4. 如来源提供了可安全验证的示例,在我提供所需输入后做一个最小任务;创建、修改、删除、部署或发送内容前,按源文件和我的授权执行。
准备输入,再按步骤执行

装好以后,具体怎么用?

准备哪些材料

  • 准备FastAPI/Pydantic工程、azure-cosmos与azure-identity、数据库/容器及实际分区路径;明确本地模拟器或真实Azure,提供workspace访问规则。

会得到什么

  • 客户端生命周期、service与模型转换方案,以及分区隔离、故障和并发的具体修订点。

实际使用顺序

  1. 先选原生aio,或明确同步SDK加run_in_threadpool的路径;在FastAPI lifespan中初始化并保留client/credential供关闭,避免混用SKILL.md里async get_container与同步_use_cosmos片段。
  2. 按Create/Update/Response/InDB职责映射camelCase文档,所有读取和删除携带授权后的workspace分区;source模板的upsert_document虽收partition_key却未验证它与文档一致,需要补校验。
  3. 把未找到与数据库不可用分开返回,不能把连接异常统一降为None/空列表。参数绑定只用于值,extra_filter必须来自受控程序;slug先查后写不是并发唯一性保证,需按业务补唯一性约束/冲突处理与测试。
使用示例

从一个具体任务开始

本站根据源文件编写的使用示例,未在本站实际运行。示例中的文件由你提供。

安装并识别 Skill 后发送

复制一个具体任务

教学任务:使用 azure-cosmos-db-py,为FastAPI的Project资源设计持久层,容器按/workspaceId分区。阅读附带client/service模板,修正get_container同步异步不一致、连接未关闭和不可用被当404的问题;给出授权workspace校验及mock测试设计,不连接Azure或写数据库。
预期结果

预期得到可供项目实现的分层设计与明确模板修订;本站未运行服务或测试。

使用前先看这些限制

使用条件与边界

  • 已审读附带两份模板及client/service参考,它们仍有TODO模型、初始化失败缓存不自动重试、缺少完整客户端关闭等缺口,不能照其自称推导为可直接生产使用。
  • 关闭TLS证书验证仅对应本地模拟器方案;不能靠endpoint包含localhost子串就把外部服务当模拟器。分区参数本身不替代用户授权。
针对这个 Skill 的疑问

常见问题

空列表就说明这个workspace没有项目吗?

源模板在数据库不可用时也会返回空列表,二者会混淆。工程接入应保留不可用状态和错误,让接口能区分真正无数据与后端故障。

需要核对时再展开原文

来源原文与资料状态

源文件
已保存 SKILL.md,抓取于 2026-11-16T12:36:35.145Z
中文使用说明
已针对下方源文件版本整理用途、用法和常见问题
安装与任务测试
本站未执行安装或任务测试
来源复核
本页中文用法已对照保存版本的完整 SKILL.md 阅读整理
展开 SKILL.md 原文、章节与历史收录资料

.github/plugins/azure-sdk-python/skills/azure-cosmos-db-py/SKILL.md · 抓取时间 2026-11-16T12:36:35.145Z

以下保留源文件的 description、章节及摘录,供核对中文说明;完整文件通过上方链接打开。

抓取成功不代表已完成安装或任务测试。

来源 description(原文)

Build Azure Cosmos DB NoSQL services with Python/FastAPI following production-grade patterns. Use when implementing database client setup with dual auth (DefaultAzureCredential + emulator), service layer classes with CRUD operations, partition key strategies, parameterized queries, or TDD patterns for Cosmos. Triggers on phrases like "Cosmos DB", "NoSQL database", "document store", "add persistence", "database service layer", or "Python Cosmos SDK".

查看源文件章节 / Source sections
  • 原文:Cosmos DB Service Implementation
  • 安装 / Installation
  • 原文:Environment Variables
  • 原文:Authentication & Lifecycle
  • 原文:Architecture Overview
  • 快速开始 / Quick Start
  • 原文:1. Client Module Setup
  • 原文:2. Pydantic Model Hierarchy
  • 原文:3. Service Layer Pattern
  • 原文:Core Principles
  • 原文:Security Requirements
  • 原文:Clean Code Conventions
  • 原文:TDD Requirements
  • 原文:Best Practices
  • 原文:Reference Files
  • 原文:Template Files
  • 原文:Quality Attributes (NFRs)
  • 原文:Reliability
  • 原文:Security
  • 原文:Maintainability
  • 原文:Testability
  • 原文:Performance
原文:Cosmos DB Service Implementation

Build production-grade Azure Cosmos DB NoSQL services following clean code, security best practices, and TDD principles.

安装 / Installation

```bash pip install azure-cosmos azure-identity ```

原文:Environment Variables

```bash COSMOS_ENDPOINT=https://<account>.documents.azure.com:443/ # Required for all auth methods COSMOS_DATABASE_NAME=<database-name> # Required for all auth methods COSMOS_CONTAINER_ID=<container-id> # Required for all auth methods # For emulator only (not production) COSMOS_KEY=<emulator-key> # Only required for key-based auth or emulator AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production ```

原文:Authentication & Lifecycle

> **🔑 Two rules apply to every code sample below:** > > 1. **Prefer `DefaultAzureCredential`.** It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation. > - Local dev: `DefaultAzureCredential` works as-is. > - Production: set `AZURE_TOKEN_CREDENTIALS=prod` (or `AZURE_TOKEN_CREDENTIALS=<specific_credential>`) to constrain the credential chain to production-safe credentials. > 2. **Wrap every client in a context manager** so HTTP transports, sockets, and token caches are released deterministically: > - Sync: `with <Client>(...) as client:` > - Async: `async with <Client>(...) as client:` **and** `async with DefaultAzureCredential() as credential:` (from `azure.identity.aio`) > > Snippets may abbreviate this setup, but production code should always follow both rules. **DefaultAzureCredential (preferred)**: ```python import os from azure.cosmos import CosmosClient from azure.identity import DefaultAzureCredential, ManagedIdentityCredential # Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential> credential = DefaultAzureCredential(require_envvar=True) # Or use a specific credential directly in production: # See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes # credential = ManagedIdentityCredential() with CosmosClient( url=os.environ["COSMOS_ENDPOINT"], credential=credential ) as client: # Use client here (see following sections for operations) ... ``` **Emulator (local development)**: ```python from azure.cosmos import CosmosClient with CosmosClient( url="https://localhost:8081", credential=os.environ["COSMOS_KEY"], connection_verify=False ) as client: # Use client here (see following sections for operations) ... ```

原文:Architecture Overview

``` ┌─────────────────────────────────────────────────────────────────┐ │ FastAPI Router │ │ - Auth dependencies (get_current_user, get_current_user_required) │ - HTTP error responses (HTTPException) │ └──────────────────────────────┬──────────────────────────────────┘ │ ┌──────────────────────────────▼──────────────────────────────────┐ │ Service Layer │ │ - Business logic and validation │ │ - Document ↔ Model conversion │ │ - Graceful degradation when Cosmos unavailable │ └──────────────────────────────┬──────────────────────────────────┘ │ ┌──────────────────────────────▼──────────────────────────────────┐ │ Cosmos DB Client Module │ │ - Singleton container initialization │ │ - Dual auth: DefaultAzureCredential (Azure) / Key (emulator) │ │ - Async wrapper via run_in_threadpool │ └─────────────────────────────────────────────────────────────────┘ ```

原文:1. Client Module Setup

Create a singleton Cosmos client with dual authentication: ```python # db/cosmos.py from azure.cosmos import CosmosClient from azure.identity import DefaultAzureCredential from starlette.concurrency import run_in_threadpool _cosmos_container = None def _is_emulator_endpoint(endpoint: str) -> bool: return "localhost" in endpoint or "127.0.0.1" in endpoint async def get_container(): global _cosmos_container if _cosmos_container is None: # Singleton: client lives for the FastAPI app lifetime; close in a lifespan shutdown handler. if _is_emulator_endpoint(settings.cosmos_endpoint): client = CosmosClient( url=settings.cosmos_endpoint, credential=settings.cosmos_key, connection_verify=False ) else: client = CosmosClient( url=settings.cosmos_endpoint, credential=DefaultAzureCredential() ) db = client.get_database_client(settings.cosmos_database_name) _cosmos_container = db.get_container_client(settings.cosmos_container_id) return _cosmos_container ``` **Full implementation**: See [references/client-setup.md](references/client-setup.md)

文件 SHA256:3dd01706390b4b1765ae1a7b3829878c4d0ba8485228f824e817287c4f8990d4

仓库许可证记录:MIT;skill-declaration-captured-not-interpreted,查看来源。

SKILL.md原文description · 2026-11-16T12:36:35.145Z

Build Azure Cosmos DB NoSQL services with Python/FastAPI following production-grade patterns. Use when implementing database client setup with dual auth (DefaultAzureCredential + emulator), service layer classes with CRUD operations, partition key strategies, parameterized queries, or TDD patterns for Cosmos. Triggers on phrases like "Cosmos DB", "NoSQL database", "document store", "add persistence", "database service layer", or "Python Cosmos SDK".

查看历史简介来源

历史收录数据

来源仓库microsoft/skills
Skill源路径.github/plugins/azure-sdk-python/skills/azure-cosmos-db-py/SKILL.md

安装前请让使用的Agent先读取这个SKILL.md及其所在目录,确认依赖和所需权限。此页不生成未经来源验证的安装命令。

返回 Skills 目录,按任务继续筛选 · MCP与Skills教程与资源