4242from arango .response import Response
4343from arango .result import Result
4444from arango .typings import Fields , Headers , Json , Params
45- from arango .utils import get_doc_id , is_none_or_int , is_none_or_str
45+ from arango .utils import get_batches , get_doc_id , is_none_or_int , is_none_or_str
4646
4747
4848class Collection (ApiGroup ):
@@ -1934,7 +1934,8 @@ def import_bulk(
19341934 overwrite : Optional [bool ] = None ,
19351935 on_duplicate : Optional [str ] = None ,
19361936 sync : Optional [bool ] = None ,
1937- ) -> Result [Json ]:
1937+ batch_size : Optional [int ] = None ,
1938+ ) -> Union [Result [Json ], List [Result [Json ]]]:
19381939 """Insert multiple documents into the collection.
19391940
19401941 .. note::
@@ -1984,8 +1985,17 @@ def import_bulk(
19841985 :type on_duplicate: str
19851986 :param sync: Block until operation is synchronized to disk.
19861987 :type sync: bool | None
1988+ :param batch_size: Split up **documents** into batches of max length
1989+ **batch_size** and import them in a loop on the client side. If
1990+ **batch_size** is specified, the return type of this method
1991+ changes from a result object to a list of result objects.
1992+ IMPORTANT NOTE: this parameter may go through breaking changes
1993+ in the future where the return type may not be a list of result
1994+ objects anymore. Use it at your own risk, and avoid
1995+ depending on the return value if possible.
1996+ :type batch_size: int
19871997 :return: Result of the bulk import.
1988- :rtype: dict
1998+ :rtype: dict | list[dict]
19891999 :raise arango.exceptions.DocumentInsertError: If import fails.
19902000 """
19912001 documents = [self ._ensure_key_from_id (doc ) for doc in documents ]
@@ -2006,21 +2016,35 @@ def import_bulk(
20062016 if sync is not None :
20072017 params ["waitForSync" ] = sync
20082018
2009- request = Request (
2010- method = "post" ,
2011- endpoint = "/_api/import" ,
2012- data = documents ,
2013- params = params ,
2014- write = self .name ,
2015- )
2016-
20172019 def response_handler (resp : Response ) -> Json :
20182020 if resp .is_success :
20192021 result : Json = resp .body
20202022 return result
20212023 raise DocumentInsertError (resp , request )
20222024
2023- return self ._execute (request , response_handler )
2025+ if batch_size is None :
2026+ request = Request (
2027+ method = "post" ,
2028+ endpoint = "/_api/import" ,
2029+ data = documents ,
2030+ params = params ,
2031+ write = self .name ,
2032+ )
2033+
2034+ return self ._execute (request , response_handler )
2035+ else :
2036+ results = []
2037+ for batch in get_batches (documents , batch_size ):
2038+ request = Request (
2039+ method = "post" ,
2040+ endpoint = "/_api/import" ,
2041+ data = batch ,
2042+ params = params ,
2043+ write = self .name ,
2044+ )
2045+ results .append (self ._execute (request , response_handler ))
2046+
2047+ return results
20242048
20252049
20262050class StandardCollection (Collection ):
0 commit comments