Parse values according to schema type and write to db
This commit is contained in:
+8
-11
@@ -10,9 +10,9 @@ import Share from 'react-native-share'
|
||||
import getDataAsCsvDataUri from '../lib/export-to-csv'
|
||||
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker'
|
||||
import rnfs from 'react-native-fs'
|
||||
import csvtojson from 'csvtojson'
|
||||
import styles from '../styles/index'
|
||||
import { settings as labels } from './labels'
|
||||
import { importCsv } from '../db'
|
||||
|
||||
export default class Settings extends Component {
|
||||
render() {
|
||||
@@ -51,7 +51,7 @@ export default class Settings extends Component {
|
||||
</View>
|
||||
<View style={styles.homeButton}>
|
||||
<Button
|
||||
onPress={ importData }
|
||||
onPress={ getFileContentAndImport }
|
||||
title="Import data">
|
||||
</Button>
|
||||
</View>
|
||||
@@ -61,7 +61,7 @@ export default class Settings extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
async function importData() {
|
||||
async function getFileContentAndImport() {
|
||||
let fileInfo
|
||||
try {
|
||||
fileInfo = await new Promise((resolve, reject) => {
|
||||
@@ -73,23 +73,20 @@ async function importData() {
|
||||
})
|
||||
})
|
||||
} catch (err) {
|
||||
Alert.alert('There was a problem opening the file picker')
|
||||
return Alert.alert('There was a problem opening the file picker')
|
||||
}
|
||||
|
||||
let fileContent
|
||||
try {
|
||||
fileContent = await rnfs.readFile(fileInfo.uri, 'utf8')
|
||||
} catch (err) {
|
||||
Alert.alert('Could not open file')
|
||||
console.log(err)
|
||||
return Alert.alert('Could not open file')
|
||||
}
|
||||
|
||||
let json
|
||||
try {
|
||||
json = await csvtojson().fromString(fileContent)
|
||||
importCsv(fileContent)
|
||||
} catch(err) {
|
||||
Alert.alert('Could not parse CSV file')
|
||||
//TODO
|
||||
}
|
||||
|
||||
console.log(json)
|
||||
}
|
||||
}
|
||||
|
||||
+110
-18
@@ -2,6 +2,8 @@ import Realm from 'realm'
|
||||
import { LocalDate } from 'js-joda'
|
||||
import { Base64 } from 'js-base64'
|
||||
import objectPath from 'object-path'
|
||||
import csvParser from 'csvtojson'
|
||||
import isObject from 'isobject'
|
||||
|
||||
import {
|
||||
cycleWithTempAndNoMucusShift,
|
||||
@@ -197,26 +199,115 @@ function getCycleDaysAsCsvDataUri() {
|
||||
rows.unshift(columnNames.join(','))
|
||||
return rows.join('\n')
|
||||
|
||||
function getColumnNamesForCsv() {
|
||||
return getPrefixedKeys('CycleDay')
|
||||
|
||||
function getPrefixedKeys(schemaName, prefix) {
|
||||
const schema = db.schema.find(x => x.name === schemaName).properties
|
||||
return Object.keys(schema).reduce((acc, key) => {
|
||||
const prefixedKey = prefix ? [prefix, key].join('.') : key
|
||||
const childSchemaName = schema[key].objectType
|
||||
if (!childSchemaName) {
|
||||
acc.push(prefixedKey)
|
||||
return acc
|
||||
}
|
||||
acc.push(...getPrefixedKeys(childSchemaName, prefixedKey))
|
||||
return acc
|
||||
}, [])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getColumnNamesForCsv() {
|
||||
return getPrefixedKeys('CycleDay')
|
||||
|
||||
function getPrefixedKeys(schemaName, prefix) {
|
||||
const schema = db.schema.find(x => x.name === schemaName).properties
|
||||
return Object.keys(schema).reduce((acc, key) => {
|
||||
const prefixedKey = prefix ? [prefix, key].join('.') : key
|
||||
const childSchemaName = schema[key].objectType
|
||||
if (!childSchemaName) {
|
||||
acc.push(prefixedKey)
|
||||
return acc
|
||||
}
|
||||
acc.push(...getPrefixedKeys(childSchemaName, prefixedKey))
|
||||
return acc
|
||||
}, [])
|
||||
}
|
||||
}
|
||||
|
||||
function getDbType(modelProperties, path) {
|
||||
if (path.length === 1) return modelProperties[path[0]].type
|
||||
const modelName = modelProperties[path[0]].objectType
|
||||
const model = db.schema.find(x => x.name === modelName)
|
||||
return getDbType(model.properties, path.slice(1))
|
||||
}
|
||||
|
||||
async function importCsv(csv, deleteFirst) {
|
||||
const cycleDayProperties = db.schema.find(x => x.name === 'CycleDay').properties
|
||||
const parseFuncs = {
|
||||
bool: val => val.toLowerCase() === 'false' ? false : true,
|
||||
int: parseNumberIfPossible,
|
||||
float: parseNumberIfPossible,
|
||||
double: parseNumberIfPossible,
|
||||
string: val => val
|
||||
}
|
||||
|
||||
function parseNumberIfPossible(val) {
|
||||
// Number and parseFloat catch different cases of weirdness,
|
||||
// so we test them both
|
||||
if (isNaN(Number(val)) || isNaN(parseFloat(val))) return val
|
||||
return Number(val)
|
||||
}
|
||||
|
||||
const config = {
|
||||
colParser: getColumnNamesForCsv().reduce((acc, colName) => {
|
||||
const path = colName.split('.')
|
||||
const dbType = getDbType(cycleDayProperties, path)
|
||||
acc[colName] = item => {
|
||||
if (item === '') return null
|
||||
return parseFuncs[dbType](item)
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
let cycleDays
|
||||
try {
|
||||
cycleDays = await csvParser(config)
|
||||
.fromString(csv)
|
||||
.on('header', validateHeaders)
|
||||
} catch(err) {
|
||||
// TODO
|
||||
console.log(err)
|
||||
}
|
||||
|
||||
//remove symptoms where all fields are null
|
||||
putNullForEmptySymptoms(cycleDays)
|
||||
|
||||
db.write(() => {
|
||||
db.delete(db.objects('CycleDay'))
|
||||
cycleDays.forEach((day, i) => {
|
||||
try {
|
||||
db.create('CycleDay', day)
|
||||
} catch (err) {
|
||||
const msg = `Error for line ${i + 1}(${day.date}): ${err.message}`
|
||||
throw new Error(msg)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function validateHeaders(headers) {
|
||||
const expectedHeaders = getColumnNamesForCsv()
|
||||
if (!headers.every(header => {
|
||||
return expectedHeaders.indexOf(header) > -1
|
||||
})) {
|
||||
const msg = `Expected CSV column titles to be ${expectedHeaders.join()}`
|
||||
throw new Error(msg)
|
||||
}
|
||||
}
|
||||
|
||||
function putNullForEmptySymptoms(data) {
|
||||
data.forEach(replaceWithNullIfAllPropertiesAreNull)
|
||||
|
||||
function replaceWithNullIfAllPropertiesAreNull(obj) {
|
||||
Object.keys(obj).forEach((key) => {
|
||||
if (!isObject(obj[key])) return
|
||||
if (Object.values(obj[key]).every(val => val === null)) {
|
||||
obj[key] = null
|
||||
return
|
||||
}
|
||||
replaceWithNullIfAllPropertiesAreNull(obj[key])
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export {
|
||||
saveSymptom,
|
||||
getOrCreateCycleDay,
|
||||
@@ -227,5 +318,6 @@ export {
|
||||
deleteAll,
|
||||
getPreviousTemperature,
|
||||
getCycleDay,
|
||||
getCycleDaysAsCsvDataUri
|
||||
getCycleDaysAsCsvDataUri,
|
||||
importCsv
|
||||
}
|
||||
|
||||
Generated
+1450
-1429
File diff suppressed because it is too large
Load Diff
@@ -18,6 +18,7 @@
|
||||
"assert": "^1.4.1",
|
||||
"csvtojson": "^2.0.8",
|
||||
"date-range": "0.0.2",
|
||||
"isobject": "^3.0.1",
|
||||
"js-base64": "^2.4.8",
|
||||
"js-joda": "^1.8.2",
|
||||
"moment": "^2.22.1",
|
||||
|
||||
Reference in New Issue
Block a user