xbackend/crud/base.py
2021-05-06 13:04:58 +08:00

28 lines
871 B
Python

from typing import Union
from bson import ObjectId
class CRUDBase:
def __init__(self, coll_name):
self.coll_name = coll_name
async def get(self, db, id: Union[ObjectId, str]):
return (await db[self.coll_name].find_one({'_id': ObjectId(id)})) or dict()
async def read_have(self, db, user_id: str, **kwargs):
where = {'members': user_id}
where.update(kwargs)
cursor = db[self.coll_name].find(where)
return await cursor.to_list(length=999)
async def find_many(self, db, **kwargs):
cursor = db[self.coll_name].find(kwargs)
return await cursor.to_list(length=999)
async def delete(self, db, **kwargs):
return await db[self.coll_name].delete_many(kwargs)
async def update_one(self, db, id, **kwargs):
return await db[self.coll_name].update_one({'_id': id}, kwargs)