thomaxxl / safrs

SqlAlchemy Flask-Restful Swagger Json:API OpenAPI
GNU General Public License v3.0
405 stars 73 forks source link

Bulk create to endpoints #50

Closed wuzechen closed 4 years ago

wuzechen commented 4 years ago

Hi, this is a very good tool to build the API quickly and I am happy to use it. Is there a way to bulk create rows?

thomaxxl commented 4 years ago

Hi,

I guess you want something like this: http://springbot.github.io/json-api/extensions/bulk/ ? I just commited a change that implements this but it needs improvements: the POST works with {data : [{..},{..}] } but the swagger isn't created and PATCH doesn't work.

wuzechen commented 4 years ago

Thanks for your reply. There is another issue about bulk insert. Follow the http://springbot.github.io/json-api/extensions/bulk/ Content-type of headers should be application/vnd.api+json; ext=bulk If I try to use this header, it gives me a header error. I fixed in safrs/request.py and safrs/jsonapi.py like this safrs/request.py

class SAFRSRequest(Request):
-     jsonapi_content_types = ["application/json", "application/vnd.api+json"]
+     jsonapi_content_types = ["application/json", "application/vnd.api+json", "application/vnd.api+json; ext=bulk"]
+     jsonapi_content_bulk_types = ["application/vnd.api+json; ext=bulk"]
      is_jsonapi = False
+     is_bulk = False

    def __init__(self, *args, **kwargs):
        """
            constructor
        """
        super().__init__(*args, **kwargs)
        if self.content_type in self.jsonapi_content_types:
            self.is_jsonapi = True
            self.parameter_storage_class = TypeConversionDict
+       if self.content_type in self.jsonapi_content_bulk_types:
+          self.is_bulk = True
        self.parse_jsonapi_args()

safrs/jsonapi.py

         safrs.log.debug("method {} args {}".format(self.method_name, args))
-         if request.is_jsonapi:
+         if request.is_jsonapi and not request.is_bulk:
             args["self"] = instance
         result = method(**args)
thomaxxl commented 4 years ago

ok, thanks I'll implement it like that by next week.

thomaxxl commented 4 years ago

I've committed support for bulk extension.

This still requires some improvement though.

thomaxxl commented 4 years ago

v2.6.0 contains the bulk create functionality, it's undocumented for now. Also, bulk patch doesn't work either.