From a440e7631be1d91435ce771636dd86c04be90cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keviin=20=C3=85berg=20Kultalahti?= Date: Tue, 23 Mar 2021 11:54:03 +0100 Subject: [PATCH] move tables to separate store --- .../builder/src/builderStore/store/backend.js | 95 ---------------- .../src/builderStore/store/backend/index.js | 1 + .../src/builderStore/store/backend/tables.js | 107 ++++++++++++++++++ .../src/builderStore/store/frontend.js | 12 +- .../FlowChart/AutomationBlockTagline.svelte | 4 +- .../automation/SetupPanel/RowSelector.svelte | 6 +- .../SetupPanel/TableSelector.svelte | 4 +- .../DataTable/RelationshipDataTable.svelte | 6 +- .../backend/DataTable/ViewDataTable.svelte | 7 +- .../DataTable/modals/CreateEditColumn.svelte | 32 +++--- .../DataTable/modals/CreateEditRow.svelte | 5 +- .../DataTable/modals/CreateEditUser.svelte | 5 +- .../popovers/CalculatePopover.svelte | 3 +- .../popovers/CreateViewPopover.svelte | 8 +- .../DataTable/popovers/FilterPopover.svelte | 3 +- .../DataTable/popovers/GroupByPopover.svelte | 3 +- .../TableNavigator/TableNavigator.svelte | 7 +- .../modals/CreateTableModal.svelte | 7 +- .../common/LinkedRowSelector.svelte | 4 +- .../NavigationPanel/NewScreenModal.svelte | 5 +- .../PropertyControls/DatasourceSelect.svelte | 7 +- .../PropertyControls/TableSelect.svelte | 4 +- .../data/datasource/index.svelte | 5 +- .../data/table/[selectedTable]/_layout.svelte | 6 +- .../[application]/data/table/_layout.svelte | 8 +- .../[application]/data/table/index.svelte | 6 +- .../data/view/[selectedView]/_layout.svelte | 4 +- 27 files changed, 194 insertions(+), 170 deletions(-) create mode 100644 packages/builder/src/builderStore/store/backend/tables.js 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}
- {#each $backendUiStore.tables as table, idx} + {#each $tables.list as table, idx} 0} icon={`ri-${table._id === TableNames.USERS ? 'user' : 'table'}-line`} diff --git a/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte b/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte index 1bd1a29eb7..2259f6d5de 100644 --- a/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte +++ b/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte @@ -1,6 +1,7 @@ @@ -8,7 +8,7 @@
diff --git a/packages/builder/src/pages/[application]/data/datasource/index.svelte b/packages/builder/src/pages/[application]/data/datasource/index.svelte index 62bd7acd47..2d72709750 100644 --- a/packages/builder/src/pages/[application]/data/datasource/index.svelte +++ b/packages/builder/src/pages/[application]/data/datasource/index.svelte @@ -1,6 +1,5 @@ - {#if $backendUiStore.tables.length === 0} + {#if $tables.list.length === 0} Connect your first datasource to start building. {:else}Select a datasource to edit{/if} diff --git a/packages/builder/src/pages/[application]/data/table/[selectedTable]/_layout.svelte b/packages/builder/src/pages/[application]/data/table/[selectedTable]/_layout.svelte index 9892da00db..348cdbf67a 100644 --- a/packages/builder/src/pages/[application]/data/table/[selectedTable]/_layout.svelte +++ b/packages/builder/src/pages/[application]/data/table/[selectedTable]/_layout.svelte @@ -1,13 +1,13 @@ diff --git a/packages/builder/src/pages/[application]/data/table/_layout.svelte b/packages/builder/src/pages/[application]/data/table/_layout.svelte index 2eea0be4c2..f94954b840 100644 --- a/packages/builder/src/pages/[application]/data/table/_layout.svelte +++ b/packages/builder/src/pages/[application]/data/table/_layout.svelte @@ -1,5 +1,5 @@ diff --git a/packages/builder/src/pages/[application]/data/table/index.svelte b/packages/builder/src/pages/[application]/data/table/index.svelte index d1659257d4..a3c67cb0db 100644 --- a/packages/builder/src/pages/[application]/data/table/index.svelte +++ b/packages/builder/src/pages/[application]/data/table/index.svelte @@ -1,14 +1,14 @@ -{#if $backendUiStore.tables.length === 0} +{#if $tables.list.length === 0} Create your first table to start building {:else}Select a table to edit{/if} diff --git a/packages/builder/src/pages/[application]/data/view/[selectedView]/_layout.svelte b/packages/builder/src/pages/[application]/data/view/[selectedView]/_layout.svelte index e890c7e276..cfa3961dc5 100644 --- a/packages/builder/src/pages/[application]/data/view/[selectedView]/_layout.svelte +++ b/packages/builder/src/pages/[application]/data/view/[selectedView]/_layout.svelte @@ -1,11 +1,11 @@