-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbmanager.py
26 lines (21 loc) · 986 Bytes
/
dbmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import dataset
import logging
import uuid
# Set up logging for the module
logger = logging.getLogger(__name__)
def save_to_db(data, db_path):
try:
db = dataset.connect(f"sqlite:///{db_path}")
for entity in data:
schema = entity.get("schema")
if schema:
table_name = f"{schema}_entities"
primary_key = str(uuid.uuid4()) # Generate a UUID as the primary key
table = db.get_table(table_name) or db.create_table(table_name, primary_id="uuid")
entity["uuid"] = primary_key # Add the UUID to the entity dictionary
table.upsert(entity, keys=["uuid"]) # Use the UUID as the primary key
logger.info(f"Inserted '{schema}' into the database")
else:
logger.warning(f"Entity has no schema specified, skipping insertion: {entity}")
except Exception as e:
logger.error(f"Error while saving to the database: {e}")