summaryrefslogtreecommitdiff
path: root/ATRI/database/wrapper.py
blob: df6057ff250dbd9648df1853c710cd52aab0ea93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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()