Files
Verificacion_EFOS_Backend/app/helpers/db_adapter.py

42 lines
1.2 KiB
Python

from typing import List, Dict
from database import sessionLocal
from app.modules.edos.models import EDOS
class DBAdapter:
""" Adapter: insert cleen data on a db"""
def __inti__(self):
self.db = sessionLocal()
def insert_filtered_data(self, mapped_rows: List[Dict]) -> Dict:
""" Insert only data filtered and mapped"""
results = {
'total_processed': len(mapped_rows),
'inserted': 0,
'errors': [],
'duplicates_skiped': 0
}
for row in mapped_rows:
try:
#verified dupled data
existing = self.db.query(EDOS).filter_by(
unique_field=row.get('unique_field')
).first()
if existing:
results['duplicates_skiped'] += 1
continue
instance = EDOS(**row)
self.db.add(instance)
self.db.commit()
results['inserted'] += 1
except Exception as e:
self.db.rollback()
results['errors'].append({
'row': row,
'error': str(e)
})
self.db.close()
return results