# nutricare-data-packages

Nutricare 的数据访问基础包，封装 MongoDB 仓储与本地存储抽象基类。

## 安装

```bash
pip install nutricare-data-packages
```

## 快速使用

```python
from pathlib import Path
import os

from nutricare_data_packages import StorageBase


class ReportStorage(StorageBase):
    @property
    def storage_subdir(self) -> str:
        return "reports"

    def _write_impl(self, relative_path: str, data: bytes) -> Path:
        target = self.resolve_path(relative_path)
        target.parent.mkdir(parents=True, exist_ok=True)
        target.write_bytes(data)
        return target


os.environ["mongodb_url"] = "127.0.0.1:27017"
os.environ["NUTRICARE_TICKET_ID"] = "ticket-001"
os.environ["NUTRICARE_TASK_ID"] = "task-001"

storage = ReportStorage()
storage.write_metadata(
    relative_path=relative_path,
    size=len(content),
    md5="...",
    status="pending",
)
path = storage.write(relative_path, content)
```

说明：
- MongoDB 连接串只从环境变量 `mongodb_url` 读取。
- 允许传入 `127.0.0.1:27017`、`mongodb:127.0.0.1:27017` 或完整 `mongodb://...`。
- 文件写入会按 `relative_path + md5` 做幂等校验，已存在且一致时直接返回。

## 本地构建与发布

```bash
python -m pip install --upgrade build twine
python -m build
python -m twine check dist/*
python -m twine upload dist/*
```
