diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index f48b561ef0..3cdabf1ed5 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -1,4 +1,4 @@ -import { writable } from "svelte/store" +import { writable, get } from "svelte/store" import { cloneDeep } from "lodash/fp" import api from "../api" @@ -62,16 +62,30 @@ export const getBackendUiStore = () => { }), save: async table => { const updatedTable = cloneDeep(table) + const oldTable = get(store).tables.filter(t => t._id === table._id)[0] + const fieldNames = [] // update any renamed schema keys to reflect their names - for (let key in updatedTable.schema) { + for (let key of Object.keys(updatedTable.schema)) { + // if field name has been seen before remove it + if (fieldNames.indexOf(key.toLowerCase()) !== -1) { + delete updatedTable.schema[key] + continue + } const field = updatedTable.schema[key] + const oldField = oldTable?.schema[key] + // if the type has changed then revert back to the old field + if (oldField != null && oldField.type !== field.type) { + updatedTable.schema[key] = oldField + } // field has been renamed if (field.name && field.name !== key) { updatedTable.schema[field.name] = field updatedTable._rename = { old: key, updated: field.name } delete updatedTable.schema[key] } + // finally record this field has been used + fieldNames.push(key.toLowerCase()) } const SAVE_TABLE_URL = `/api/tables` diff --git a/packages/builder/src/components/backend/DataTable/Table.svelte b/packages/builder/src/components/backend/DataTable/Table.svelte index 4c0dc06d39..8354b464c0 100644 --- a/packages/builder/src/components/backend/DataTable/Table.svelte +++ b/packages/builder/src/components/backend/DataTable/Table.svelte @@ -132,7 +132,6 @@ font-size: 24px; font-weight: 600; text-rendering: optimizeLegibility; - text-transform: capitalize; margin-top: 0; display: flex; flex-direction: row; diff --git a/packages/builder/src/components/backend/DataTable/popovers/ColumnPopover.svelte b/packages/builder/src/components/backend/DataTable/popovers/ColumnPopover.svelte index 2857b651c3..f844cdefae 100644 --- a/packages/builder/src/components/backend/DataTable/popovers/ColumnPopover.svelte +++ b/packages/builder/src/components/backend/DataTable/popovers/ColumnPopover.svelte @@ -110,9 +110,6 @@ align-items: center; gap: var(--spacing-xs); } - .container span { - text-transform: capitalize; - } h5 { padding: var(--spacing-xl) 0 0 var(--spacing-xl); diff --git a/packages/server/src/api/controllers/table.js b/packages/server/src/api/controllers/table.js index 82f981e3e6..3030274016 100644 --- a/packages/server/src/api/controllers/table.js +++ b/packages/server/src/api/controllers/table.js @@ -41,6 +41,18 @@ exports.save = async function(ctx) { oldTable = await db.get(ctx.request.body._id) } + // make sure that types don't change of a column, have to remove + // the column if you want to change the type + if (oldTable && oldTable.schema) { + for (let propKey of Object.keys(tableToSave.schema)) { + let column = tableToSave.schema[propKey] + let oldColumn = oldTable.schema[propKey] + if (oldColumn && oldColumn.type !== column.type) { + ctx.throw(400, "Cannot change the type of a column") + } + } + } + // Don't rename if the name is the same let { _rename } = tableToSave if (_rename && _rename.old === _rename.updated) { @@ -50,9 +62,9 @@ exports.save = async function(ctx) { // rename row fields when table column is renamed if (_rename && tableToSave.schema[_rename.updated].type === "link") { - throw "Cannot rename a linked field." + ctx.throw(400, "Cannot rename a linked column.") } else if (_rename && tableToSave.primaryDisplay === _rename.old) { - throw "Cannot rename the display column." + ctx.throw(400, "Cannot rename the display column.") } else if (_rename) { const rows = await db.allDocs( getRowParams(tableToSave._id, null, {