예제 #1
0
 async def insert(
     self,
     model: MongoDBModel,
     session: ClientSession = None,
     include=None,
     exclude=None,
 ) -> InsertOneResult:
     data = model.dict(include=include, exclude=exclude)
     data["_id"] = data.pop("id")
     collection_name = model.get_db_collection()
     collection = self.get_collection(collection_name)
     return await collection.insert_one(data, session=session)
예제 #2
0
    async def delete(self,
                     model: MongoDBModel,
                     session: ClientSession = None,
                     **kwargs) -> DeleteResult:
        _id = kwargs.pop("id", None)
        if _id is not None:
            kwargs["_id"] = _id

        collection_name = model.get_db_collection()
        collection = self.get_collection(collection_name)
        res = await collection.delete_many(kwargs, session=session)
        return res
예제 #3
0
    async def count(self,
                    model: MongoDBModel,
                    session: ClientSession = None,
                    **kwargs) -> int:
        _id = kwargs.pop("id", None)
        if _id is not None:
            kwargs["_id"] = _id

        collection_name = model.get_db_collection()
        collection = self.get_collection(collection_name)
        res = await collection.count_documents(kwargs, session=session)
        return res
예제 #4
0
    async def get(self,
                  model: MongoDBModel,
                  session: ClientSession = None,
                  **kwargs) -> dict:
        _id = kwargs.pop("id", notset)
        if _id != notset:
            kwargs["_id"] = _id

        collection_name = model.get_db_collection()
        collection = self.get_collection(collection_name)
        res = await collection.find_one(kwargs, session=session)
        return res
예제 #5
0
    async def update_many(self,
                          model: MongoDBModel,
                          filter_kwargs: dict,
                          session: ClientSession = None,
                          **kwargs) -> UpdateResult:
        _id = filter_kwargs.pop("id", None)
        if _id is not None:
            filter_kwargs["_id"] = _id

        collection_name = model.get_db_collection()
        collection = self.get_collection(collection_name)
        res = await collection.update_many(filter_kwargs,
                                           kwargs,
                                           session=session)
        return res
예제 #6
0
    def list(self,
             model: MongoDBModel,
             session: ClientSession = None,
             _offset: int = 0,
             _limit: int = 0,
             **kwargs) -> Cursor:
        _id = kwargs.pop("id", None)
        if _id is not None:
            kwargs["_id"] = _id

        collection_name = model.get_db_collection()
        collection = self.get_collection(collection_name)
        return collection.find(kwargs,
                               session=session,
                               skip=_offset,
                               limit=_limit)