django-socio-grpc icon indicating copy to clipboard operation
django-socio-grpc copied to clipboard

How-to: Mulitple Objects Serialisation - bulk creation of a list of objects ?

Open markdoerr opened this issue 1 year ago • 6 comments

I was lately trying to implement a multi / bulk create Service, similar as described at the DRF. Simple ideas failed, like adding many=True to the class Meta.

https://www.django-rest-framework.org/api-guide/serializers/#dealing-with-multiple-objects

Does DSG support the (bulk) generation of multiple objects, like providing a list of objects to the Create Method ? What is the best way to do that ? Using streams ? I would include it into the DSG Example, once I have an idea.

Merci :)

markdoerr avatar Jan 30 '24 20:01 markdoerr

Hi, there's no builtin way to do that with the generic services but it would be easy to implement. (largely the same as the CreateMixin)

class BulkCreateMixin:
    
    @grpc_action(
         request=SelfSerializer,
         use_request_list=True, 
         response=SelfSerializer,
         use_response_list=True
     )
    async def BulkCreate(self, request, context):
        serializer = await self.aget_serializer(message=request, many=True)
        await sync_to_async(serializer.is_valid)(raise_exception=True)
        await self.aperform_create(serializer)
        return await serializer.amessage

    async def perform_bulk_create(self, serializer):
        await serializer.asave()

legau avatar Jan 31 '24 17:01 legau

Merci, @legau, I will try to apply this to the book example in the next days - that we have your "recipe" documented for the public.

markdoerr avatar Jan 31 '24 21:01 markdoerr

btw. @legau, I guess there is a (gRPC) size limit of the length list. So for larger lists, a stream would be necessary ?

markdoerr avatar Jan 31 '24 21:01 markdoerr

Hello @markdoerr

You may want to look at this issue: https://github.com/socotecio/django-socio-grpc/issues/241

and this documentation: https://django-socio-grpc.readthedocs.io/en/stable/features/grpc-action.html#use-request-and-response-list

Also the gRPC size limits is configurable in https://django-socio-grpc.readthedocs.io/en/stable/settings.html#server-options

AMontagu avatar Feb 13 '24 13:02 AMontagu

Thanks, @AMontagu , I did not expect that you can set the size that big of 100MB - I had a size limit of 2-4 MB in my head: (https://github.com/grpc/grpc-web/issues/1182) Increasing this limit would also solve my file-upload issue.... Are there any caveats in increasing the limit (like more overhead...) ?

markdoerr avatar Feb 13 '24 16:02 markdoerr

If I remember correctly the main issue is that bigger message will have impact on your network flow and may increase delay on all your API. But not sure of me.

AMontagu avatar Feb 13 '24 17:02 AMontagu