mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-08 23:06:03 +00:00
feat: 持久化和 web 接口基础架构
This commit is contained in:
0
pkg/persistence/__init__.py
Normal file
0
pkg/persistence/__init__.py
Normal file
40
pkg/persistence/database.py
Normal file
40
pkg/persistence/database.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
|
||||
import sqlalchemy.ext.asyncio as sqlalchemy_asyncio
|
||||
|
||||
from ..core import app
|
||||
|
||||
|
||||
preregistered_managers: list[type[BaseDatabaseManager]] = []
|
||||
|
||||
def manager_class(name: str) -> None:
|
||||
"""注册一个数据库管理类"""
|
||||
|
||||
def decorator(cls: type[BaseDatabaseManager]) -> type[BaseDatabaseManager]:
|
||||
cls.name = name
|
||||
preregistered_managers.append(cls)
|
||||
return cls
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
class BaseDatabaseManager(abc.ABC):
|
||||
"""基础数据库管理类"""
|
||||
|
||||
name: str
|
||||
|
||||
ap: app.Application
|
||||
|
||||
engine: sqlalchemy_asyncio.AsyncEngine
|
||||
|
||||
def __init__(self, ap: app.Application) -> None:
|
||||
self.ap = ap
|
||||
|
||||
@abc.abstractmethod
|
||||
async def initialize(self) -> None:
|
||||
pass
|
||||
|
||||
def get_engine(self) -> sqlalchemy_asyncio.AsyncEngine:
|
||||
return self.engine
|
||||
0
pkg/persistence/databases/__init__.py
Normal file
0
pkg/persistence/databases/__init__.py
Normal file
13
pkg/persistence/databases/sqlite.py
Normal file
13
pkg/persistence/databases/sqlite.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy.ext.asyncio as sqlalchemy_asyncio
|
||||
|
||||
from .. import database
|
||||
|
||||
|
||||
@database.manager_class("sqlite")
|
||||
class SQLiteDatabaseManager(database.BaseDatabaseManager):
|
||||
"""SQLite 数据库管理类"""
|
||||
|
||||
async def initialize(self) -> None:
|
||||
self.engine = sqlalchemy_asyncio.create_async_engine(f"sqlite+aiosqlite:///{self.ap.system_cfg.data['persistence']['sqlite']['path']}")
|
||||
55
pkg/persistence/mgr.py
Normal file
55
pkg/persistence/mgr.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import datetime
|
||||
|
||||
import sqlalchemy.ext.asyncio as sqlalchemy_asyncio
|
||||
import sqlalchemy
|
||||
|
||||
from . import database
|
||||
from ..core import app
|
||||
from .databases import sqlite
|
||||
|
||||
|
||||
class PersistenceManager:
|
||||
"""持久化模块管理器"""
|
||||
|
||||
ap: app.Application
|
||||
|
||||
db: database.BaseDatabaseManager
|
||||
"""数据库管理器"""
|
||||
|
||||
meta: sqlalchemy.MetaData
|
||||
|
||||
def __init__(self, ap: app.Application):
|
||||
self.ap = ap
|
||||
self.meta = sqlalchemy.MetaData()
|
||||
|
||||
async def initialize(self):
|
||||
|
||||
for manager in database.preregistered_managers:
|
||||
self.db = manager(self.ap)
|
||||
await self.db.initialize()
|
||||
|
||||
await self.create_tables()
|
||||
|
||||
async def create_tables(self):
|
||||
# TODO: 对扩展友好
|
||||
|
||||
# 日志
|
||||
async with self.get_db_engine().connect() as conn:
|
||||
await conn.run_sync(self.meta.create_all)
|
||||
|
||||
await conn.commit()
|
||||
|
||||
async def execute_async(
|
||||
self,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
async with self.get_db_engine().connect() as conn:
|
||||
await conn.execute(*args, **kwargs)
|
||||
await conn.commit()
|
||||
|
||||
def get_db_engine(self) -> sqlalchemy_asyncio.AsyncEngine:
|
||||
return self.db.get_engine()
|
||||
Reference in New Issue
Block a user