Move db operations back to db module

This commit is contained in:
Julia Friesel
2018-08-09 11:48:28 +02:00
parent 53328f608b
commit b07a2c8a53
3 changed files with 43 additions and 33 deletions
+34 -2
View File
@@ -176,9 +176,38 @@ function getPreviousTemperature(cycleDay) {
return winner.temperature.value
}
const schema = db.schema.reduce((acc, curr) => {
acc[curr.name] = curr.properties
return acc
}, {})
function tryToCreateCycleDay(day, i) {
try {
db.create('CycleDay', day)
} catch (err) {
const msg = `Line ${i + 1}(${day.date}): ${err.message}`
throw new Error(msg)
}
}
function tryToImportWithDelete(cycleDays) {
db.write(() => {
db.delete(db.objects('CycleDay'))
cycleDays.forEach(tryToCreateCycleDay)
})
}
function tryToImportWithoutDelete(cycleDays) {
db.write(() => {
cycleDays.forEach((day, i) => {
const existing = getCycleDay(day.date)
if (existing) db.delete(existing)
tryToCreateCycleDay(day, i)
})
})
}
export {
db,
saveSymptom,
getOrCreateCycleDay,
bleedingDaysSortedByDate,
@@ -187,5 +216,8 @@ export {
fillWithDummyData,
deleteAll,
getPreviousTemperature,
getCycleDay
getCycleDay,
schema,
tryToImportWithDelete,
tryToImportWithoutDelete
}