cleaner parsing
This commit is contained in:
parent
ea950748e5
commit
5e6cbe3b6d
|
@ -4,6 +4,7 @@ const {
|
||||||
getRecordParams,
|
getRecordParams,
|
||||||
getModelParams,
|
getModelParams,
|
||||||
generateModelID,
|
generateModelID,
|
||||||
|
generateRecordID,
|
||||||
} = require("../../db/utils")
|
} = require("../../db/utils")
|
||||||
|
|
||||||
exports.fetch = async function(ctx) {
|
exports.fetch = async function(ctx) {
|
||||||
|
@ -83,7 +84,10 @@ exports.save = async function(ctx) {
|
||||||
// Populate the table with records imported from CSV in a bulk update
|
// Populate the table with records imported from CSV in a bulk update
|
||||||
const data = await csvParser.transform(dataImport)
|
const data = await csvParser.transform(dataImport)
|
||||||
|
|
||||||
for (let row of data) row.modelId = modelToSave._id
|
for (let row of data) {
|
||||||
|
row._id = generateRecordID(modelToSave._id)
|
||||||
|
row.modelId = modelToSave._id
|
||||||
|
}
|
||||||
|
|
||||||
await db.bulkDocs(data)
|
await db.bulkDocs(data)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,29 +25,20 @@ function parse(path, parsers) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
result.fromFile(path).subscribe(row => {
|
result.fromFile(path).subscribe(row => {
|
||||||
// For each CSV row
|
// For each CSV row parse all the columns that need parsed
|
||||||
// parse all the columns that need parsed
|
|
||||||
for (let key in parsers) {
|
for (let key in parsers) {
|
||||||
// if the parsing has already failed for a column
|
if (!schema[key] || schema[key].success) {
|
||||||
// skip that column
|
|
||||||
if (
|
|
||||||
!schema[key] ||
|
|
||||||
!schema[key].success ||
|
|
||||||
schema[key].type === "omit"
|
|
||||||
) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the validator for the column type
|
// get the validator for the column type
|
||||||
const validator = VALIDATORS[parsers[key].type]
|
const validator = VALIDATORS[parsers[key].type]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// allow null/undefined values
|
// allow null/undefined values
|
||||||
schema[key].success = !row[key] || !!validator(row[key])
|
schema[key].success = !row[key] || validator(row[key])
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
schema[key].success = false
|
schema[key].success = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
result.on("done", error => {
|
result.on("done", error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|
Loading…
Reference in New Issue