-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdao.py
42 lines (29 loc) · 1.13 KB
/
dao.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import List, Optional
import model
import schema
async def create(obj_in: schema.Aerich) -> model.Aerich:
obj = model.Aerich(**obj_in.model_dump(exclude_unset=True))
await obj.save()
return obj
async def query_by_id(id: int) -> Optional[model.Aerich]:
return await model.Aerich.get_or_none(aerich_id=id)
async def update(id: int, obj_in: schema.Aerich) -> Optional[model.Aerich]:
obj = await query_by_id(id)
if obj:
for field, value in obj_in.model_dump(exclude_unset=True).items():
setattr(obj, field, value)
await obj.save()
return obj
async def delete_by_id(id: int) -> Optional[model.Aerich]:
obj = await query_by_id(id)
if obj:
await obj.delete()
return obj
async def count(**kwargs) -> int:
return await model.Aerich.filter(**kwargs).count()
async def query_all_by_limit(page_number: int, page_size: int,
**kwargs) -> List[model.Aerich]:
offset = (page_number - 1) * page_size
limit = page_size
return await model.Aerich.filter(**kwargs
).offset(offset).limit(limit).all()