diff --git a/packages/builder/src/builderStore/dataBinding.js b/packages/builder/src/builderStore/dataBinding.js index fa6f10c98c..f53fc66a01 100644 --- a/packages/builder/src/builderStore/dataBinding.js +++ b/packages/builder/src/builderStore/dataBinding.js @@ -1,7 +1,10 @@ import { cloneDeep } from "lodash/fp" import { get } from "svelte/store" import { store } from "builderStore" -import { tables as tablesStore, queries as queriesStores } from 'builderStore/store/backend/' +import { + tables as tablesStore, + queries as queriesStores, +} from "builderStore/store/backend/" import { findComponentPath } from "./storeUtils" import { makePropSafe } from "@budibase/string-templates" import { TableNames } from "../constants" diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 3295b82006..bd41964492 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -1,7 +1,6 @@ import { writable } from "svelte/store" -const INITIAL_BACKEND_UI_STATE = { -} +const INITIAL_BACKEND_UI_STATE = {} export const getBackendUiStore = () => { const store = writable({ ...INITIAL_BACKEND_UI_STATE }) diff --git a/packages/builder/src/builderStore/store/backend/database.js b/packages/builder/src/builderStore/store/backend/database.js index c619ca200d..a2946c2488 100644 --- a/packages/builder/src/builderStore/store/backend/database.js +++ b/packages/builder/src/builderStore/store/backend/database.js @@ -1,3 +1,3 @@ import { writable } from "svelte/store" -export const database = writable({}) \ No newline at end of file +export const database = writable({}) diff --git a/packages/builder/src/builderStore/store/backend/datasources.js b/packages/builder/src/builderStore/store/backend/datasources.js index d84e66d344..3fb54dd624 100644 --- a/packages/builder/src/builderStore/store/backend/datasources.js +++ b/packages/builder/src/builderStore/store/backend/datasources.js @@ -20,7 +20,7 @@ function createDatasourcesStore() { }, select: async datasourceId => { update(state => ({ ...state, selected: datasourceId })) - queries.update(state => ({...state, selected: null})) + queries.update(state => ({ ...state, selected: null })) }, save: async datasource => { const response = await api.post("/api/datasources", datasource) diff --git a/packages/builder/src/builderStore/store/backend/queries.js b/packages/builder/src/builderStore/store/backend/queries.js index 9e4490a7f0..ff2eda67ff 100644 --- a/packages/builder/src/builderStore/store/backend/queries.js +++ b/packages/builder/src/builderStore/store/backend/queries.js @@ -1,9 +1,9 @@ -import { writable } from "svelte/store" +import { writable, get } from "svelte/store" import { datasources, integrations } from "./" import api from "../../api" function createQueriesStore() { - const { subscribe, set, update } = writable({list: [], selected: null}) + const { subscribe, set, update } = writable({ list: [], selected: null }) return { subscribe, @@ -12,7 +12,7 @@ function createQueriesStore() { fetch: async () => { const response = await api.get(`/api/queries`) const json = await response.json() - update(state => ({...state, list: json})) + update(state => ({ ...state, list: json })) return json }, save: async (datasourceId, query) => { @@ -44,7 +44,7 @@ function createQueriesStore() { } else { queries.push(json) } - return { list: queries, selected: json._id} + return { list: queries, selected: json._id } }) return json }, @@ -55,9 +55,7 @@ function createQueriesStore() { delete: async query => { await api.delete(`/api/queries/${query._id}/${query._rev}`) update(state => { - state.list = state.list.filter( - existing => existing._id !== query._id - ) + state.list = state.list.filter(existing => existing._id !== query._id) if (state.selected === query._id) { state.selected = null } diff --git a/packages/builder/src/builderStore/store/backend/roles.js b/packages/builder/src/builderStore/store/backend/roles.js index fc8bf57b23..1d353e433f 100644 --- a/packages/builder/src/builderStore/store/backend/roles.js +++ b/packages/builder/src/builderStore/store/backend/roles.js @@ -23,8 +23,8 @@ function createRolesStore() { } async function getRoles() { - const response = await api.get("/api/roles") - return await response.json() + const response = await api.get("/api/roles") + return await response.json() } export const roles = createRolesStore() diff --git a/packages/builder/src/builderStore/store/backend/rows.js b/packages/builder/src/builderStore/store/backend/rows.js index 0d63bbb64f..c16941925c 100644 --- a/packages/builder/src/builderStore/store/backend/rows.js +++ b/packages/builder/src/builderStore/store/backend/rows.js @@ -1,5 +1,5 @@ -import { writable,get } from "svelte/store" -import { views } from './' +import { writable, get } from "svelte/store" +import { views } from "./" function createRowsStore() { const { subscribe } = writable([]) @@ -12,4 +12,3 @@ function createRowsStore() { } export const rows = createRowsStore() - diff --git a/packages/builder/src/builderStore/store/backend/tables.js b/packages/builder/src/builderStore/store/backend/tables.js index c78cdbb1a3..00ace52f18 100644 --- a/packages/builder/src/builderStore/store/backend/tables.js +++ b/packages/builder/src/builderStore/store/backend/tables.js @@ -1,121 +1,121 @@ import { writable, get } from "svelte/store" -import { views } from './' +import { views } from "./" import { cloneDeep } from "lodash/fp" import api from "../../api" function createTablesStore() { const store = writable({ - list: [], - selected: {}, - draft: {}, + list: [], + selected: {}, + draft: {}, }) - const { subscribe, update, set } = store + const { subscribe, update, set } = store - async function fetch() { - const tablesResponse = await api.get(`/api/tables`) - const tables = await tablesResponse.json() - update(state => ({...state, list: tables})) + async function fetch() { + const tablesResponse = await api.get(`/api/tables`) + const tables = await tablesResponse.json() + update(state => ({ ...state, list: tables })) + } + + async function select(table) { + if (!table) { + console.log("Setting selected to null") + update(state => ({ + ...state, + selected: {}, + })) + } else { + console.log("Setting selected to null") + update(state => ({ + ...state, + selected: table, + draft: cloneDeep(table), + })) + views.select({ name: `all_${table._id}` }) + } + } + + async function save(table) { + const updatedTable = cloneDeep(table) + const oldTable = get(store).list.filter(t => t._id === table._id)[0] + + const fieldNames = [] + // update any renamed schema keys to reflect their names + 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()) } - async function select(table) { - if (!table) { - console.log('Setting selected to null') - update(state => ({ - ...state, - selected: {} - })) - } else { - console.log('Setting selected to null') - update(state => ({ - ...state, - selected: table, - draft: cloneDeep(table), - })) - views.select({ name: `all_${table._id}` }) - } - } - - async function save(table) { - const updatedTable = cloneDeep(table) - const oldTable = get(store).list.filter(t => t._id === table._id)[0] - - const fieldNames = [] - // update any renamed schema keys to reflect their names - 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 response = await api.post(`/api/tables`, updatedTable) - const savedTable = await response.json() - await fetch() - await select(savedTable) - return savedTable - } + const response = await api.post(`/api/tables`, updatedTable) + const savedTable = await response.json() + await fetch() + await select(savedTable) + return savedTable + } return { subscribe, set, fetch, select, - save, + save, delete: async table => { - await api.delete(`/api/tables/${table._id}/${table._rev}`) - update(state => ({ - ...state, - list: state.list.filter(existing => existing._id !== table._id), - selected: {} - })) + await api.delete(`/api/tables/${table._id}/${table._rev}`) + update(state => ({ + ...state, + list: state.list.filter(existing => existing._id !== table._id), + selected: {}, + })) }, saveField: ({ originalName, field, primaryDisplay = false, indexes }) => { - update(state => { + update(state => { // delete the original if renaming // need to handle if the column had no name, empty string if (originalName || originalName === "") { - delete state.draft.schema[originalName] - state.draft._rename = { + delete state.draft.schema[originalName] + state.draft._rename = { old: originalName, updated: field.name, - } + } } // Optionally set display column if (primaryDisplay) { - state.draft.primaryDisplay = field.name + state.draft.primaryDisplay = field.name } if (indexes) { - state.draft.indexes = indexes + state.draft.indexes = indexes } state.draft.schema[field.name] = cloneDeep(field) save(state.draft) return state - }) + }) }, deleteField: field => { - update(state => { - delete state.draft.schema[field.name] - save(state.draft) - return state - }) + update(state => { + delete state.draft.schema[field.name] + save(state.draft) + return state + }) }, } } diff --git a/packages/builder/src/builderStore/store/backend/views.js b/packages/builder/src/builderStore/store/backend/views.js index 9c32c3da9f..7a97a9d325 100644 --- a/packages/builder/src/builderStore/store/backend/views.js +++ b/packages/builder/src/builderStore/store/backend/views.js @@ -4,43 +4,43 @@ import api from "../../api" function createViewsStore() { const { subscribe, update } = writable({ - list: [], - selected: null + list: [], + selected: null, }) return { subscribe, select: view => { - update(state => ({ - ...state, - selected: view, - })) + update(state => ({ + ...state, + selected: view, + })) }, delete: async view => { - await api.delete(`/api/views/${view}`) - await tables.fetch() + await api.delete(`/api/views/${view}`) + await tables.fetch() }, save: async view => { - const response = await api.post(`/api/views`, view) - const json = await response.json() + const response = await api.post(`/api/views`, view) + const json = await response.json() - const viewMeta = { - name: view.name, - ...json, - } + const viewMeta = { + name: view.name, + ...json, + } - update(state => { - const viewTable = get(tables).list.find( - table => table._id === view.tableId - ) + update(state => { + const viewTable = get(tables).list.find( + table => table._id === view.tableId + ) - if (view.originalName) delete viewTable.views[view.originalName] - viewTable.views[view.name] = viewMeta + if (view.originalName) delete viewTable.views[view.originalName] + viewTable.views[view.name] = viewMeta - state.tables = state.tables - state.selectedView = viewMeta - return state - }) + state.tables = state.tables + state.selectedView = viewMeta + return state + }) }, } } diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 8c890cc5f5..58717050dc 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -9,7 +9,13 @@ import { selectedAccessRole, } from "builderStore" // Backendstores -import { datasources, integrations, queries, database, tables } from 'builderStore/store/backend/' +import { + datasources, + integrations, + queries, + database, + tables, +} from "builderStore/store/backend/" import { fetchComponentLibDefinitions } from "../loadComponentLibraries" import api from "../api" @@ -61,7 +67,12 @@ export const getFrontendStore = () => { await hostingStore.actions.fetch() // Initialise backend stores - const [_datasources, _integrations, _queries, _tables] = await Promise.all([ + const [ + _datasources, + _integrations, + _queries, + _tables, + ] = await Promise.all([ api.get(`/api/datasources`).then(r => r.json()), api.get("/api/integrations").then(r => r.json()), api.get(`/api/queries`).then(r => r.json()), @@ -74,9 +85,8 @@ export const getFrontendStore = () => { tables.set({ list: _tables, selected: {}, - draft: {} + draft: {}, }) - }, routing: { fetch: async () => {