适用于 Nonebot2 的数据存储插件
Project description
NoneBot Plugin DataStore
✨ NoneBot 数据存储插件 ✨
使用方式
先在插件代码最前面声明依赖
from nonebot import require
require("nonebot_plugin_datastore")
插件数据相关功能
from nonebot_plugin_datastore import get_plugin_data
plugin_data = get_plugin_data()
# 获取插件缓存目录
plugin_data.cache_dir
# 获取插件配置目录
plugin_data.config_dir
# 获取插件数据目录
plugin_data.data_dir
# 读取配置
plugin_data.config.get(key)
# 存储配置
plugin_data.config.set(key, value)
数据库相关功能,详细用法见 SQLModel
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
命令行支持(需安装 nb-cli 1.0+)
数据存储路径
# 获取当前数据存储路径
nb datastore dir
# 获取指定插件的数据存储路径
nb datastore dir --name plugin_name
数据库管理
生成迁移文件
# 生成项目内所有启用数据库插件的迁移文件(不包括 site-packages 中的插件)
nb datastore migrate
# 生成指定插件的迁移文件
nb datastore migrate --name plugin_name -m example
升级插件数据库
# 升级所有启用数据库插件的数据库
nb datastore upgrade
# 升级指定插件的数据库
nb datastore upgrade --name plugin_name
# 升级至指定版本
nb datastore upgrade --name plugin_name revision
降级插件数据库
# 降级所有启用数据库插件的数据库
nb datastore downgrade
# 降级指定插件的数据库
nb datastore downgrade --name plugin_name
# 降级至指定版本
nb datastore downgrade --name plugin_name revision
配置项
配置方式:直接在 NoneBot
全局配置文件中添加以下配置项即可。
datastore_cache_dir
- 类型:
str
- 默认:
- macOS: ~/Library/Caches/nonebot2
- Unix: ~/.cache/nonebot2 (XDG default)
- Windows: C:\Users<username>\AppData\Local\nonebot2\Cache
- 说明: 缓存目录
datastore_config_dir
- 类型:
str
- 默认:
- macOS: same as user_data_dir
- Unix: ~/.config/nonebot2
- Win XP (roaming): C:\Documents and Settings<username>\Local Settings\Application Data\nonebot2
- Win 7 (roaming): C:\Users<username>\AppData\Roaming\nonebot2
- 说明: 配置目录
datastore_data_dir
- 类型:
str
- 默认:
- macOS: ~/Library/Application Support/nonebot2
- Unix: ~/.local/share/nonebot2 or in $XDG_DATA_HOME, if defined
- Win XP (not roaming): C:\Documents and Settings<username>\Application Data\nonebot2
- Win 7 (not roaming): C:\Users<username>\AppData\Local\nonebot2
- 说明: 数据目录
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.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8f017edd5442864e9effb82bb1ca95843a401a199912aa6a0b571a6f6a2693c |
|
MD5 | 9a676c0b45e387dc8293a947f195e221 |
|
BLAKE2b-256 | f36e94d4775b0f4b5fd6fe7ee8ed5d5f9fc34371ca4e13633e77f1d7fb4a5214 |
Close
Hashes for nonebot_plugin_datastore-0.5.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aece705ed846ef10f8f60554fd06cc299939179a10d0bb8770399f08a6974f8d |
|
MD5 | 52e314e2e743b842200b8ad33bf5dd8b |
|
BLAKE2b-256 | bb375a25708b0f8a172b53d72de2523e597924d90b57c153fd7b4d120f86f7e1 |