From c9cfa2c5f90a279ea56f0fa11075114a92f5876f Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 5 Oct 2020 19:21:51 +0100 Subject: [PATCH] cleaner parsing --- packages/server/src/api/controllers/model.js | 6 +++- packages/server/src/utilities/csvParser.js | 29 +++++++------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/packages/server/src/api/controllers/model.js b/packages/server/src/api/controllers/model.js index 1b4c8d7304..c2e2c2e236 100644 --- a/packages/server/src/api/controllers/model.js +++ b/packages/server/src/api/controllers/model.js @@ -4,6 +4,7 @@ const { getRecordParams, getModelParams, generateModelID, + generateRecordID, } = require("../../db/utils") 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 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) } diff --git a/packages/server/src/utilities/csvParser.js b/packages/server/src/utilities/csvParser.js index 36e2cedf32..872ee5f8a4 100644 --- a/packages/server/src/utilities/csvParser.js +++ b/packages/server/src/utilities/csvParser.js @@ -25,27 +25,18 @@ function parse(path, parsers) { } }) result.fromFile(path).subscribe(row => { - // For each CSV row - // parse all the columns that need parsed + // For each CSV row parse all the columns that need parsed for (let key in parsers) { - // if the parsing has already failed for a column - // skip that column - if ( - !schema[key] || - !schema[key].success || - schema[key].type === "omit" - ) { - continue - } + if (!schema[key] || schema[key].success) { + // get the validator for the column type + const validator = VALIDATORS[parsers[key].type] - // get the validator for the column type - const validator = VALIDATORS[parsers[key].type] - - try { - // allow null/undefined values - schema[key].success = !row[key] || !!validator(row[key]) - } catch (err) { - schema[key].success = false + try { + // allow null/undefined values + schema[key].success = !row[key] || validator(row[key]) + } catch (err) { + schema[key].success = false + } } } })