diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 8f12661284..a7947f3e5e 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -40,101 +40,6 @@ export const getBackendUiStore = () => { return state }), }, - tables: { - fetch: async () => { - const tablesResponse = await api.get(`/api/tables`) - const tables = await tablesResponse.json() - store.update(state => { - state.tables = tables - return state - }) - }, - select: table => - store.update(state => { - state.selectedTable = table - state.draftTable = cloneDeep(table) - state.selectedView = { name: `all_${table._id}` } - return state - }), - 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 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` - const response = await api.post(SAVE_TABLE_URL, updatedTable) - const savedTable = await response.json() - await store.actions.tables.fetch() - store.actions.tables.select(savedTable) - return savedTable - }, - delete: async table => { - await api.delete(`/api/tables/${table._id}/${table._rev}`) - store.update(state => { - state.tables = state.tables.filter( - existing => existing._id !== table._id - ) - state.selectedTable = {} - return state - }) - }, - saveField: ({ originalName, field, primaryDisplay = false, indexes }) => { - store.update(state => { - // delete the original if renaming - // need to handle if the column had no name, empty string - if (originalName || originalName === "") { - delete state.draftTable.schema[originalName] - state.draftTable._rename = { - old: originalName, - updated: field.name, - } - } - - // Optionally set display column - if (primaryDisplay) { - state.draftTable.primaryDisplay = field.name - } - - if (indexes) { - state.draftTable.indexes = indexes - } - - state.draftTable.schema[field.name] = cloneDeep(field) - store.actions.tables.save(state.draftTable) - return state - }) - }, - deleteField: field => { - store.update(state => { - delete state.draftTable.schema[field.name] - store.actions.tables.save(state.draftTable) - return state - }) - }, - }, views: { select: view => store.update(state => { diff --git a/packages/builder/src/builderStore/store/backend/index.js b/packages/builder/src/builderStore/store/backend/index.js index 11a5613459..303891e33b 100644 --- a/packages/builder/src/builderStore/store/backend/index.js +++ b/packages/builder/src/builderStore/store/backend/index.js @@ -1,4 +1,5 @@ export { database } from "./database" +export { tables } from "./tables" export { permissions } from "./permissions" export { roles } from "./roles" export { datasources } from "./datasources" diff --git a/packages/builder/src/builderStore/store/backend/tables.js b/packages/builder/src/builderStore/store/backend/tables.js new file mode 100644 index 0000000000..29c09839dc --- /dev/null +++ b/packages/builder/src/builderStore/store/backend/tables.js @@ -0,0 +1,107 @@ +import { writable } from "svelte/store" +import { cloneDeep } from "lodash/fp" +import api from "../../api" + +function createTablesStore() { + const store = writable({ + list: [], + selected: {}, + draft: {}, + view: {} + }) + const { subscribe, update, set } = store + + return { + subscribe, + set, + fetch: async () => { + const tablesResponse = await api.get(`/api/tables`) + const tables = await tablesResponse.json() + update(state => ({...state, list: tables})) + }, + select: table => + update(state => ({ + ...state, + selected: table, + draft: cloneDeep(table), + view: { name: `all_${table._id}` } + })), + save: async 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 store.fetch() + await store.select(savedTable) + return savedTable + }, + 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: {} + })) + }, + saveField: ({ originalName, field, primaryDisplay = false, indexes }) => { + 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 = { + old: originalName, + updated: field.name, + } + } + + // Optionally set display column + if (primaryDisplay) { + state.draft.primaryDisplay = field.name + } + + if (indexes) { + state.draft.indexes = indexes + } + + state.draft.schema[field.name] = cloneDeep(field) + store.save(state.draft) + return state + }) + }, + deleteField: field => { + update(state => { + delete state.draft.schema[field.name] + store.save(state.draft) + return state + }) + }, + } +} + +export const tables = createTablesStore() diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index e506ecb4f1..2b450eb49f 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -10,7 +10,7 @@ import { selectedAccessRole, } from "builderStore" // Backendstores -import { datasources, integrations, queries, database } from 'builderStore/store/backend/' +import { datasources, integrations, queries, database, tables } from 'builderStore/store/backend/' import { fetchComponentLibDefinitions } from "../loadComponentLibraries" import api from "../api" @@ -62,15 +62,21 @@ export const getFrontendStore = () => { await hostingStore.actions.fetch() // Initialise backend stores - const [_datasources, _integrations, _queries] = 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()) + api.get(`/api/queries`).then(r => r.json()), + api.get(`/api/tables`).then(r => r.json()), ]) datasources.set({ list: _datasources, selected: null }) integrations.set(_integrations) queries.set({ list: _queries, selected: null }) database.set(application.instance) + tables.set({ + list: _tables, + selected: {}, + draft: {} + }) }, routing: { diff --git a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/AutomationBlockTagline.svelte b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/AutomationBlockTagline.svelte index 2afe7dbbfa..318d54943f 100644 --- a/packages/builder/src/components/automation/AutomationBuilder/FlowChart/AutomationBlockTagline.svelte +++ b/packages/builder/src/components/automation/AutomationBuilder/FlowChart/AutomationBlockTagline.svelte @@ -1,7 +1,7 @@ -{#if $backendUiStore.selectedDatabase && $backendUiStore.selectedDatabase._id} +{#if $database?.selected?._id}