diff --git a/packages/builder/src/components/nav/ModelNavigator/TableDataImport.svelte b/packages/builder/src/components/nav/ModelNavigator/TableDataImport.svelte index 9bdaf7e77d..8e6b184755 100644 --- a/packages/builder/src/components/nav/ModelNavigator/TableDataImport.svelte +++ b/packages/builder/src/components/nav/ModelNavigator/TableDataImport.svelte @@ -2,6 +2,7 @@ import { Heading, Body, Button, Select } from "@budibase/bbui" import { notifier } from "builderStore/store/notifications" import { FIELDS } from "constants/backend" + import api from "builderStore/api" const BYTES_IN_KB = 1000 const BYTES_IN_MB = 1000000 @@ -28,6 +29,9 @@ const modelSchema = {} for (let key in schema) { const type = schema[key].type + + if (type === "omit") continue + modelSchema[key] = { name: key, type, @@ -38,16 +42,9 @@ } async function validateCSV() { - const response = await fetch("/api/models/csv/validate", { - method: "POST", - body: JSON.stringify({ - file: files[0], - schema: schema || {}, - }), - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, + const response = await api.post("/api/models/csv/validate", { + file: files[0], + schema: schema || {}, }) parseResult = await response.json() @@ -79,8 +76,9 @@ await validateCSV() } - function omitColumn(columnName) { - parsers[columnName] = PARSERS.omit + async function omitColumn(columnName) { + schema[columnName].type = "omit" + await validateCSV() } const handleTypeChange = column => evt => { @@ -97,7 +95,7 @@
{#if schema} - {#each Object.keys(schema) as columnName} + {#each Object.keys(schema).filter(key => schema[key].type !== 'omit') as columnName}
{columnName} - + {schema[columnName].success ? 'Success' : 'Failure'} attribute.toString(), - number: attribute => Number(attribute), datetime: attribute => new Date(attribute).toISOString(), } function parse(path, parsers) { - const result = csv().fromFile(path) + const result = csv({ parsers }).fromFile(path) const schema = {} @@ -30,10 +28,17 @@ function parse(path, parsers) { // For each CSV row // parse all the columns that need parsed for (let key in parsers) { - // if the schema has already borked for a parser, skip this column - if (!schema[key] || !schema[key].success) continue + // if the parsing has already failed for a column + // skip that column + if ( + !schema[key] || + !schema[key].success || + schema[key].type === "omit" + ) { + continue + } - // get the validator + // get the validator for the column type const validator = VALIDATORS[parsers[key].type] try { @@ -54,12 +59,11 @@ function parse(path, parsers) { }) } -// TODO: significant refactor async function transform({ schema, path }) { const colParser = {} for (let key in schema) { - colParser[key] = PARSERS[schema[key].type] + colParser[key] = PARSERS[schema[key].type] || schema[key].type } try {