适用于 Nonebot2 的数据存储插件
Project description
NoneBot Plugin DataStore
✨ NoneBot 数据存储插件 ✨
使用方式
先在插件代码最前面声明依赖
from nonebot import require
require("nonebot_plugin_datastore")
插件数据相关功能
from nonebot_plugin_datastore import get_plugin_data
DATA = get_plugin_data()
# 缓存目录
DATA.cache_dir
# 配置目录
DATA.config_dir
# 数据目录
DATA.data_dir
数据库相关功能
from nonebot.params import Depends
from nonebot_plugin_datastore import get_plugin_data, get_session
from sqlmodel.ext.asyncio.session import AsyncSession
# 定义模型
Model = get_plugin_data().Model
class Example(Model, table=True):
"""示例模型"""
__table_args__ = {"extend_existing": True}
id: Optional[int] = Field(default=None, primary_key=True)
message: str
# 数据库相关操作
@matcher.handle()
def handle(session: AsyncSession = Depends(get_session)):
example = Example(message="matcher")
session.add(example)
await session.commit()
# 因为 driver.on_startup 无法保证函数运行顺序
# 如需在 NoneBot 启动时且数据库初始化后运行的函数
# 请使用 post_db_init 而不是 Nonebot 的 on_startup
from nonebot_plugin_datastore.db import post_db_init
@post_db_init
async def do_something():
pass
CLI 功能(需要安装 nb-cli)
# 自动生成迁移文件
nb datastore revision --autogenerate --name plugin_name -m example
# 升级数据库
nb datastore upgrade --name plugin_name
# 降级数据库
nb datastore downgrade --name plugin_name
配置项
配置方式:直接在 NoneBot
全局配置文件中添加以下配置项即可。
datastore_cache_dir
- 类型:
str
- 默认: nonebot_plugin_localstore 插件提供的缓存目录
- 说明: 缓存目录
datastore_config_dir
- 类型:
str
- 默认: nonebot_plugin_localstore 插件提供的配置目录
- 说明: 配置目录
datastore_data_dir
- 类型:
str
- 默认: nonebot_plugin_localstore 插件提供的数据目录
- 说明: 数据目录
datastore_enable_database
- 类型:
bool
- 默认:
True
- 说明: 是否启动数据库
datastore_database_url
- 类型:
str
- 默认:
sqlite+aiosqlite:///data_dir/data.db
- 说明: 数据库连接字符串,默认使用 SQLite 数据库
datastore_database_echo
- 类型:
bool
- 默认:
False
- 说明: 是否显示数据库执行的语句与其参数列表
计划
- 调整配置为 K-V 存储
- 调整配置存放位置至专门的配置目录
- 数据库为可选项
- 支持将配置存放至数据库中
- 支持 Alembic
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for nonebot_plugin_datastore-0.5.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b430cf23812329c69a200f77bf258ac61f0cf8a06b1888558a3f7d31f3f009c |
|
MD5 | 28735ed17d14230d513f5aad93c8a74a |
|
BLAKE2b-256 | ab3a4d08ad7974999d5c031ee086f4baed032394cb683d09874b49c5910b1dab |
Close
Hashes for nonebot_plugin_datastore-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a93f6d3f5fc495b1ab4ba3e1edb073d2abfc64a7d10f93e1e2790970add76ba |
|
MD5 | 89f424d90bf61043767e77f5788b35ba |
|
BLAKE2b-256 | da06374973c9471066d4ab9f48da9f03f48070fca15a3c71a15643c94502e5be |