adding create_list to dbmongo
[osm/common.git] / osm_common / dbmongo.py
index 16da6e7..e40d0e4 100644 (file)
@@ -23,6 +23,7 @@ from http import HTTPStatus
 from time import time, sleep
 from copy import deepcopy
 from base64 import b64decode
+from uuid import uuid4
 
 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
 
@@ -343,6 +344,24 @@ class DbMongo(DbBase):
         except Exception as e:  # TODO refine
             raise DbException(e)
 
+    def create_list(self, table, indata_list):
+        """
+        Add several entries at once
+        :param table: collection or table
+        :param indata_list: content list to be added.
+        :return: the list of inserted '_id's. Exception on error
+        """
+        try:
+            for item in indata_list:
+                if item.get("_id") is None:
+                    item["_id"] = str(uuid4())
+            with self.lock:
+                collection = self.db[table]
+                data = collection.insert_many(indata_list)
+            return data.inserted_ids
+        except Exception as e:  # TODO refine
+            raise DbException(e)
+
     def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None):
         """
         Modifies an entry at database