Skip to content

Commit

Permalink
⬆️ nb-orm
Browse files Browse the repository at this point in the history
  • Loading branch information
j1g5awi committed Aug 4, 2024
1 parent cea8dcf commit 30c0547
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 254 deletions.
3 changes: 2 additions & 1 deletion nonebot_plugin_all4one/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from nonebot.exception import IgnoredException
from nonebot.message import run_preprocessor, event_preprocessor

require("nonebot_plugin_datastore")
require("nonebot_plugin_localstore")
require("nonebot_plugin_orm")

from .config import Config
from .onebotimpl import OneBotImplementation
Expand Down
16 changes: 7 additions & 9 deletions nonebot_plugin_all4one/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from httpx import AsyncClient
from sqlalchemy import JSON, Uuid, select
from sqlalchemy.orm import Mapped, mapped_column
from nonebot_plugin_orm import Model, get_session
from nonebot_plugin_localstore import get_plugin_data_dir
from nonebot.adapters.onebot.v12.exception import DatabaseError
from nonebot_plugin_datastore import create_session, get_plugin_data

plugin_data = get_plugin_data()
Model = plugin_data.Model


def get_sha256(data: bytes) -> str:
Expand All @@ -29,13 +27,13 @@ class File(Model):
sha256: Mapped[Optional[str]]


DATA_PATH = plugin_data.data_dir
DATA_PATH = get_plugin_data_dir()
FILE_PATH = DATA_PATH / "file"
FILE_PATH.mkdir(parents=True, exist_ok=True)


async def get_file(file_id: str, src: Optional[str] = None) -> File:
async with create_session() as session:
async with get_session() as session:
file = (
await session.scalars(select(File).where(File.id == UUID(file_id)))
).one_or_none()
Expand Down Expand Up @@ -75,15 +73,15 @@ async def upload_file(
sha256: Optional[str] = None,
) -> str:
if src and src_id:
async with create_session() as session:
async with get_session() as session:
if file := (
await session.scalars(
select(File).where(File.src == src).where(File.src_id == src_id)
)
).first():
return file.id.hex
if sha256:
async with create_session() as session:
async with get_session() as session:
if file := (
await session.scalars(select(File).where(File.sha256 == sha256))
).first():
Expand Down Expand Up @@ -126,7 +124,7 @@ async def upload_file(
path=path,
sha256=sha256,
)
async with create_session() as session:
async with get_session() as session:
session.add(file)
await session.commit()
await session.refresh(file)
Expand Down
Loading

0 comments on commit 30c0547

Please sign in to comment.