Merge pull request #764 from Budibase/qol-updates

QOL updates
This commit is contained in:
Andrew Kingston 2020-10-21 10:45:52 +01:00 committed by GitHub
commit 2bfb72da2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 118 additions and 70 deletions

View File

@ -86,10 +86,12 @@ const contextToBindables = (tables, walkResult) => context => {
} }
const newBindable = ([key, fieldSchema]) => { const newBindable = ([key, fieldSchema]) => {
// Replace link bindings with a new property representing the count // Replace certain bindings with a new property to help display components
let runtimeBoundKey = key let runtimeBoundKey = key
if (fieldSchema.type === "link") { if (fieldSchema.type === "link") {
runtimeBoundKey = `${key}_count` runtimeBoundKey = `${key}_count`
} else if (fieldSchema.type === "attachment") {
runtimeBoundKey = `${key}_first`
} }
return { return {
type: "context", type: "context",

View File

@ -233,7 +233,7 @@ const createLink = store => async (url, title) => {
// Save page and regenerate all CSS because otherwise weird things happen // Save page and regenerate all CSS because otherwise weird things happen
nav._children = [...nav._children, newLink] nav._children = [...nav._children, newLink]
setCurrentPage("main") state.currentPageName = "main"
regenerateCssForScreen(state.pages.main) regenerateCssForScreen(state.pages.main)
for (let screen of state.pages.main._screens) { for (let screen of state.pages.main._screens) {
regenerateCssForScreen(screen) regenerateCssForScreen(screen)

View File

@ -1,5 +1,5 @@
<script> <script>
import { Heading, Body, Button, Select } from "@budibase/bbui" import { Heading, Body, Button, Select, Label } from "@budibase/bbui"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { FIELDS } from "constants/backend" import { FIELDS } from "constants/backend"
import api from "builderStore/api" import api from "builderStore/api"
@ -14,15 +14,17 @@
schema: {}, schema: {},
} }
let parseResult let csvString
let primaryDisplay
let schema = {}
let fields = []
$: schema = parseResult && parseResult.schema $: valid = !schema || fields.every(column => schema[column].success)
$: valid =
!schema || Object.keys(schema).every(column => schema[column].success)
$: dataImport = { $: dataImport = {
valid, valid,
schema: buildTableSchema(schema), schema: buildTableSchema(schema),
path: files[0] && files[0].path, csvString,
primaryDisplay,
} }
function buildTableSchema(schema) { function buildTableSchema(schema) {
@ -43,11 +45,20 @@
async function validateCSV() { async function validateCSV() {
const response = await api.post("/api/tables/csv/validate", { const response = await api.post("/api/tables/csv/validate", {
file: files[0], csvString,
schema: schema || {}, schema: schema || {},
}) })
parseResult = await response.json() const parseResult = await response.json()
schema = parseResult && parseResult.schema
fields = Object.keys(schema || {}).filter(
key => schema[key].type !== "omit"
)
// Check primary display is valid
if (!primaryDisplay || fields.indexOf(primaryDisplay) === -1) {
primaryDisplay = fields[0]
}
if (response.status !== 200) { if (response.status !== 200) {
notifier.danger("CSV Invalid, please try another CSV file") notifier.danger("CSV Invalid, please try another CSV file")
@ -57,13 +68,7 @@
async function handleFile(evt) { async function handleFile(evt) {
const fileArray = Array.from(evt.target.files) const fileArray = Array.from(evt.target.files)
const filesToProcess = fileArray.map(({ name, path, size }) => ({ if (fileArray.some(file => file.size >= FILE_SIZE_LIMIT)) {
name,
path,
size,
}))
if (filesToProcess.some(file => file.size >= FILE_SIZE_LIMIT)) {
notifier.danger( notifier.danger(
`Files cannot exceed ${FILE_SIZE_LIMIT / `Files cannot exceed ${FILE_SIZE_LIMIT /
BYTES_IN_MB}MB. Please try again with smaller files.` BYTES_IN_MB}MB. Please try again with smaller files.`
@ -71,9 +76,14 @@
return return
} }
files = filesToProcess // Read CSV as plain text to upload alongside schema
let reader = new FileReader()
await validateCSV() reader.addEventListener("load", function(e) {
csvString = e.target.result
files = fileArray
validateCSV()
})
reader.readAsBinaryString(fileArray[0])
} }
async function omitColumn(columnName) { async function omitColumn(columnName) {
@ -94,8 +104,8 @@
</label> </label>
</div> </div>
<div class="schema-fields"> <div class="schema-fields">
{#if schema} {#if fields.length}
{#each Object.keys(schema).filter(key => schema[key].type !== 'omit') as columnName} {#each fields as columnName}
<div class="field"> <div class="field">
<span>{columnName}</span> <span>{columnName}</span>
<Select <Select
@ -117,6 +127,16 @@
{/each} {/each}
{/if} {/if}
</div> </div>
{#if fields.length}
<div class="display-column">
<Label extraSmall grey>Display Column</Label>
<Select thin secondary bind:value={primaryDisplay}>
{#each fields as field}
<option value={field}>{field}</option>
{/each}
</Select>
</div>
{/if}
<style> <style>
.dropzone { .dropzone {
@ -188,4 +208,8 @@
grid-gap: var(--spacing-m); grid-gap: var(--spacing-m);
font-size: var(--font-size-xs); font-size: var(--font-size-xs);
} }
.display-column {
margin-top: var(--spacing-xl);
}
</style> </style>

View File

@ -38,12 +38,19 @@
} }
async function saveTable() { async function saveTable() {
// Create table let newTable = {
const table = await backendUiStore.actions.tables.save({
name, name,
schema: dataImport.schema || {}, schema: dataImport.schema || {},
dataImport, dataImport,
}) }
// Only set primary display if defined
if (dataImport.primaryDisplay && dataImport.primaryDisplay.length) {
newTable.primaryDisplay = dataImport.primaryDisplay
}
// Create table
const table = await backendUiStore.actions.tables.save(newTable)
notifier.success(`Table ${name} created successfully.`) notifier.success(`Table ${name} created successfully.`)
analytics.captureEvent("Table Created", { name }) analytics.captureEvent("Table Created", { name })

View File

@ -135,6 +135,9 @@
.toprightnav { .toprightnav {
display: flex; display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
} }
.topleftnav { .topleftnav {

View File

@ -109,7 +109,7 @@ exports.save = async function(ctx) {
ctx.eventEmitter && ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:save`, instanceId, tableToSave) ctx.eventEmitter.emitTable(`table:save`, instanceId, tableToSave)
if (dataImport && dataImport.path) { if (dataImport && dataImport.csvString) {
// Populate the table with rows imported from CSV in a bulk update // Populate the table with rows imported from CSV in a bulk update
const data = await csvParser.transform(dataImport) const data = await csvParser.transform(dataImport)
@ -156,10 +156,7 @@ exports.destroy = async function(ctx) {
} }
exports.validateCSVSchema = async function(ctx) { exports.validateCSVSchema = async function(ctx) {
const { file, schema = {} } = ctx.request.body const { csvString, schema = {} } = ctx.request.body
const result = await csvParser.parse(file.path, schema) const result = await csvParser.parse(csvString, schema)
ctx.body = { ctx.body = { schema: result }
schema: result,
path: file.path,
}
} }

View File

@ -11,8 +11,8 @@ const PARSERS = {
datetime: attribute => new Date(attribute).toISOString(), datetime: attribute => new Date(attribute).toISOString(),
} }
function parse(path, parsers) { function parse(csvString, parsers) {
const result = csv().fromFile(path) const result = csv().fromString(csvString)
const schema = {} const schema = {}
@ -52,7 +52,7 @@ function parse(path, parsers) {
}) })
} }
async function transform({ schema, path }) { async function transform({ schema, csvString }) {
const colParser = {} const colParser = {}
for (let key in schema) { for (let key in schema) {
@ -60,7 +60,7 @@ async function transform({ schema, path }) {
} }
try { try {
const json = await csv({ colParser }).fromFile(path) const json = await csv({ colParser }).fromString(csvString)
return json return json
} catch (err) { } catch (err) {
console.error(`Error transforming CSV to JSON for data import`, err) console.error(`Error transforming CSV to JSON for data import`, err)

View File

@ -1,6 +1,7 @@
const csvParser = require("../csvParser"); const fs = require("fs")
const csvParser = require("../csvParser")
const CSV_PATH = __dirname + "/test.csv"; const CSV_PATH = __dirname + "/test.csv"
const SCHEMAS = { const SCHEMAS = {
VALID: { VALID: {
@ -27,16 +28,16 @@ const SCHEMAS = {
BROKEN: { BROKEN: {
Address: { Address: {
type: "datetime", type: "datetime",
} },
}, },
}; }
describe("CSV Parser", () => { describe("CSV Parser", () => {
const csvString = fs.readFileSync(CSV_PATH, "utf8")
describe("parsing", () => { describe("parsing", () => {
it("returns status and types for a valid CSV transformation", async () => { it("returns status and types for a valid CSV transformation", async () => {
expect( expect(await csvParser.parse(csvString, SCHEMAS.VALID)).toEqual({
await csvParser.parse(CSV_PATH, SCHEMAS.VALID)
).toEqual({
Address: { Address: {
success: true, success: true,
type: "string", type: "string",
@ -49,13 +50,11 @@ describe("CSV Parser", () => {
success: true, success: true,
type: "string", type: "string",
}, },
}); })
}); })
it("returns status and types for an invalid CSV transformation", async () => { it("returns status and types for an invalid CSV transformation", async () => {
expect( expect(await csvParser.parse(csvString, SCHEMAS.INVALID)).toEqual({
await csvParser.parse(CSV_PATH, SCHEMAS.INVALID)
).toEqual({
Address: { Address: {
success: false, success: false,
type: "number", type: "number",
@ -68,41 +67,43 @@ describe("CSV Parser", () => {
success: true, success: true,
type: "string", type: "string",
}, },
}); })
}); })
}); })
describe("transformation", () => { describe("transformation", () => {
it("transforms a CSV file into JSON", async () => { it("transforms a CSV file into JSON", async () => {
expect( expect(
await csvParser.transform({ await csvParser.transform({
schema: SCHEMAS.VALID, schema: SCHEMAS.VALID,
path: CSV_PATH, csvString,
}) })
).toMatchSnapshot(); ).toMatchSnapshot()
}); })
it("transforms a CSV file into JSON ignoring certain fields", async () => { it("transforms a CSV file into JSON ignoring certain fields", async () => {
expect( expect(
await csvParser.transform({ await csvParser.transform({
schema: SCHEMAS.IGNORE, schema: SCHEMAS.IGNORE,
path: CSV_PATH, csvString,
}) })
).toEqual([ ).toEqual([
{ {
Name: "Bert" Name: "Bert",
}, },
{ {
Name: "Ernie" Name: "Ernie",
}, },
{ {
Name: "Big Bird" Name: "Big Bird",
} },
]); ])
}); })
it("throws an error on invalid schema", async () => { it("throws an error on invalid schema", async () => {
await expect(csvParser.transform({ schema: SCHEMAS.BROKEN, path: CSV_PATH })).rejects.toThrow() await expect(
}); csvParser.transform({ schema: SCHEMAS.BROKEN, csvString })
}); ).rejects.toThrow()
}); })
})
})

View File

@ -51,8 +51,15 @@
// Fetch table schema so we can check for linked rows // Fetch table schema so we can check for linked rows
const tableObj = await fetchTable(row.tableId) const tableObj = await fetchTable(row.tableId)
for (let key of Object.keys(tableObj.schema)) { for (let key of Object.keys(tableObj.schema)) {
if (tableObj.schema[key].type === "link") { const type = tableObj.schema[key].type
if (type === "link") {
row[`${key}_count`] = Array.isArray(row[key]) ? row[key].length : 0 row[`${key}_count`] = Array.isArray(row[key]) ? row[key].length : 0
} else if (type === "attachment") {
let url = null
if (Array.isArray(row[key]) && row[key][0] != null) {
url = row[key][0].url
}
row[`${key}_first`] = url
} }
} }

View File

@ -6,11 +6,11 @@ export default async function fetchData(datasource, store) {
if (name) { if (name) {
let rows = [] let rows = []
if (type === "table") { if (type === "table") {
rows = fetchTableData() rows = await fetchTableData()
} else if (type === "view") { } else if (type === "view") {
rows = fetchViewData() rows = await fetchViewData()
} else if (type === "link") { } else if (type === "link") {
rows = fetchLinkedRowsData() rows = await fetchLinkedRowsData()
} }
// Fetch table schema so we can check for linked rows // Fetch table schema so we can check for linked rows
@ -19,8 +19,15 @@ export default async function fetchData(datasource, store) {
const keys = Object.keys(table.schema) const keys = Object.keys(table.schema)
rows.forEach(row => { rows.forEach(row => {
for (let key of keys) { for (let key of keys) {
if (table.schema[key].type === "link") { const type = table.schema[key].type
if (type === "link") {
row[`${key}_count`] = Array.isArray(row[key]) ? row[key].length : 0 row[`${key}_count`] = Array.isArray(row[key]) ? row[key].length : 0
} else if (type === "attachment") {
let url = null
if (Array.isArray(row[key]) && row[key][0] != null) {
url = row[key][0].url
}
row[`${key}_first`] = url
} }
} }
}) })