summaryrefslogtreecommitdiff
path: root/ATRI/database
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2023-04-06 16:07:46 +0800
committerKyomotoi <[email protected]>2023-04-06 16:07:46 +0800
commit60be1eb2bd16fc89827a50ad18377642aae176be (patch)
tree657a08ad802b2b67c2f5d941d93b67c67160b315 /ATRI/database
parent170211bc72311fc4524e7c731303645eac2aa041 (diff)
downloadATRI-60be1eb2bd16fc89827a50ad18377642aae176be.tar.gz
ATRI-60be1eb2bd16fc89827a50ad18377642aae176be.tar.bz2
ATRI-60be1eb2bd16fc89827a50ad18377642aae176be.zip
🎨 优化数据库代码
Diffstat (limited to 'ATRI/database')
-rw-r--r--ATRI/database/__init__.py1
-rw-r--r--ATRI/database/wrapper.py31
2 files changed, 32 insertions, 0 deletions
diff --git a/ATRI/database/__init__.py b/ATRI/database/__init__.py
index 5c12c6a..f43b83b 100644
--- a/ATRI/database/__init__.py
+++ b/ATRI/database/__init__.py
@@ -1,2 +1,3 @@
from .db import init_database, close_database_connection
from .models import *
+from .wrapper import DatabaseWrapper
diff --git a/ATRI/database/wrapper.py b/ATRI/database/wrapper.py
new file mode 100644
index 0000000..7838bd6
--- /dev/null
+++ b/ATRI/database/wrapper.py
@@ -0,0 +1,31 @@
+from typing import Type
+
+from tortoise.models import Model
+
+
+class DatabaseWrapper:
+ def __init__(self, model: Type[Model]):
+ self.model = model
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc_val, exc_tb):
+ pass
+
+ async def add_sub(self, *args, **kwargs):
+ await self.model.create(*args, **kwargs)
+
+ async def update_sub(self, update_map: dict, **kwargs):
+ await self.model.filter(**kwargs).update(
+ **update_map
+ )
+
+ async def del_sub(self, query_map: dict):
+ await self.model.filter(**query_map).delete()
+
+ async def get_sub_list(self, query_map: dict) -> list:
+ return await self.model.filter(**query_map)
+
+ async def get_all_subs(self) -> list:
+ return await self.model.all()