@@ -169,14 +169,18 @@ def _prep_from_doc(
169
169
else :
170
170
return doc_id , doc_id , {"If-Match" : rev }
171
171
172
- def _ensure_key_in_body (self , body : Json ) -> Json :
173
- """Return the document body with "_key" field populated.
172
+ def _ensure_key_in_body (self , body : Json , index : Optional [int ] = None ) -> Json :
173
+ """Return the document body with "_key" field populated. If
174
+ neither "_key" or "_id" exist, set "_key" value to **index**,
175
+ where **index** is the document's position in the sequence.
174
176
175
177
:param body: Document body.
176
178
:type body: dict
179
+ :param index: Document index value in the original list of documents.
180
+ :param index: int | None
177
181
:return: Document body with "_key" field.
178
182
:rtype: dict
179
- :raise arango.exceptions.DocumentParseError: On missing ID and key .
183
+ :raise arango.exceptions.DocumentParseError: On missing _key, _id, & index .
180
184
"""
181
185
if "_key" in body :
182
186
return body
@@ -185,29 +189,24 @@ def _ensure_key_in_body(self, body: Json) -> Json:
185
189
body = body .copy ()
186
190
body ["_key" ] = doc_id [len (self ._id_prefix ) :]
187
191
return body
192
+ elif index :
193
+ body = body .copy ()
194
+ body ["_key" ] = str (index )
195
+ return body
196
+
188
197
raise DocumentParseError ('field "_key" or "_id" required' )
189
198
190
- def _ensure_key_from_id (self , body : Json , index : Optional [ int ] = None ) -> Json :
199
+ def _ensure_key_from_id (self , body : Json ) -> Json :
191
200
"""Return the body with "_key" field if it has "_id" field.
192
- If it has neither, set the "_key" value to i, where i
193
- is the document's index position in the sequence.
194
-
195
201
:param body: Document body.
196
202
:type body: dict
197
- :param index: Document index value in the original list of documents.
198
- :param index: int | None
199
203
:return: Document body with "_key" field if it has "_id" field.
200
204
:rtype: dict
201
205
"""
202
206
if "_id" in body and "_key" not in body :
203
207
doc_id = self ._validate_id (body ["_id" ])
204
208
body = body .copy ()
205
209
body ["_key" ] = doc_id [len (self ._id_prefix ) :]
206
-
207
- if "_id" not in body and "_key" not in body :
208
- body = body .copy ()
209
- body ["_key" ] = str (index )
210
-
211
210
return body
212
211
213
212
@property
@@ -2002,7 +2001,7 @@ def import_bulk(
2002
2001
:raise arango.exceptions.DocumentInsertError: If import fails.
2003
2002
"""
2004
2003
documents = [
2005
- self ._ensure_key_from_id (doc , i ) for i , doc in enumerate (documents , 1 )
2004
+ self ._ensure_key_in_body (doc , i ) for i , doc in enumerate (documents , 1 )
2006
2005
]
2007
2006
2008
2007
params : Params = {"type" : "array" , "collection" : self .name }
0 commit comments