Skip to content

Commit faf1da9

Browse files
authored
Add new parameters to insert_many method (#212)
1 parent 0c51020 commit faf1da9

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

arango/collection.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,9 @@ def insert_many(
13221322
silent: bool = False,
13231323
overwrite: bool = False,
13241324
return_old: bool = False,
1325+
overwrite_mode: Optional[str] = None,
1326+
keep_none: Optional[bool] = None,
1327+
merge: Optional[bool] = None,
13251328
) -> Result[Union[bool, List[Union[Json, ArangoServerError]]]]:
13261329
"""Insert multiple documents.
13271330
@@ -1359,6 +1362,21 @@ def insert_many(
13591362
:param return_old: Include body of the old documents if replaced.
13601363
Applies only when value of **overwrite** is set to True.
13611364
:type return_old: bool
1365+
:param overwrite_mode: Overwrite behavior used when the document key
1366+
exists already. Allowed values are "replace" (replace-insert),
1367+
"update" (update-insert), "ignore" or "conflict".
1368+
Implicitly sets the value of parameter **overwrite**.
1369+
:type overwrite_mode: str | None
1370+
:param keep_none: If set to True, fields with value None are retained
1371+
in the document. Otherwise, they are removed completely. Applies
1372+
only when **overwrite_mode** is set to "update" (update-insert).
1373+
:type keep_none: bool | None
1374+
:param merge: If set to True (default), sub-dictionaries are merged
1375+
instead of the new one overwriting the old one. Applies only when
1376+
**overwrite_mode** is set to "update" (update-insert).
1377+
:type merge: bool | None
1378+
:return: Document metadata (e.g. document key, revision) or True if
1379+
parameter **silent** was set to True.
13621380
:return: List of document metadata (e.g. document keys, revisions) and
13631381
any exception, or True if parameter **silent** was set to True.
13641382
:rtype: [dict | ArangoServerError] | bool
@@ -1375,6 +1393,13 @@ def insert_many(
13751393
if sync is not None:
13761394
params["waitForSync"] = sync
13771395

1396+
if overwrite_mode is not None:
1397+
params["overwriteMode"] = overwrite_mode
1398+
if keep_none is not None:
1399+
params["keepNull"] = keep_none
1400+
if merge is not None:
1401+
params["mergeObjects"] = merge
1402+
13781403
request = Request(
13791404
method="post",
13801405
endpoint=f"/_api/document/{self.name}",

tests/test_document.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,63 @@ def test_document_insert_many(col, bad_col, docs):
234234
bad_col.insert_many(docs)
235235
assert err.value.error_code in {11, 1228}
236236

237+
# test overwrite_mode: replace
238+
extra_docs = [
239+
{"_key": docs[0]["_key"], "foo": {"qux": 2}},
240+
{"_key": "20", "foo": "t"},
241+
]
242+
results = col.insert_many(
243+
documents=extra_docs,
244+
return_old=True,
245+
return_new=True,
246+
overwrite_mode="replace",
247+
keep_none=False,
248+
merge=True,
249+
)
250+
251+
assert "old" in results[0]
252+
assert "new" in results[0]
253+
assert "old" not in results[1]
254+
assert "new" in results[1]
255+
256+
# test overwrite_mode: update
257+
extra_docs = [
258+
{"_key": docs[0]["_key"], "foo": {"t": 1}},
259+
{"_key": "20", "foo": None},
260+
]
261+
results = col.insert_many(
262+
documents=extra_docs,
263+
return_old=True,
264+
return_new=True,
265+
overwrite_mode="update",
266+
keep_none=False,
267+
merge=True,
268+
)
269+
assert "old" in results[0]
270+
assert "new" in results[0]
271+
assert results[0]["new"]["foo"] == {"qux": 2, "t": 1}
272+
assert "old" in results[1]
273+
assert "new" in results[1]
274+
assert "foo" not in results[1]["new"]
275+
276+
extra_docs = [
277+
{"_key": docs[0]["_key"], "foo": {"t": 1}},
278+
{"_key": "21", "foo": None},
279+
]
280+
281+
# Test insert conflict
282+
results = col.insert_many(
283+
documents=extra_docs,
284+
return_old=True,
285+
return_new=True,
286+
overwrite_mode="conflict",
287+
keep_none=True,
288+
merge=True,
289+
)
290+
assert isinstance(results[0], DocumentInsertError)
291+
assert results[0].error_code == 1210
292+
assert "new" in results[1]
293+
237294

238295
def test_document_update(col, docs):
239296
doc = docs[0]

0 commit comments

Comments
 (0)