diff --git a/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte b/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte index e6d6026f7c..845a1b8549 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte @@ -33,7 +33,7 @@ ...datasource, name, } - await datasources.update({ + await datasources.save({ datasource: updatedDatasource, integration: integrationForDatasource(get(integrations), datasource), }) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte index d2682597d4..247c1b8041 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte @@ -41,7 +41,7 @@ get(integrations), datasource ) - await datasources.update({ datasource, integration }) + await datasources.save({ datasource, integration }) await afterSave({ datasource, action }) } catch (err) { diff --git a/packages/builder/src/components/integration/RestQueryViewer.svelte b/packages/builder/src/components/integration/RestQueryViewer.svelte index 3b1356efe2..16d73e8863 100644 --- a/packages/builder/src/components/integration/RestQueryViewer.svelte +++ b/packages/builder/src/components/integration/RestQueryViewer.svelte @@ -176,7 +176,7 @@ notifications.success(`Request saved successfully`) if (dynamicVariables) { datasource.config.dynamicVariables = rebuildVariables(saveId) - datasource = await datasources.update({ + datasource = await datasources.save({ integration: integrationInfo, datasource, }) diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte index c4f0861f45..b472a18514 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte @@ -13,7 +13,7 @@ async function saveDatasource({ config, name }) { try { - await datasources.update({ + await datasources.save({ integration, datasource: { ...datasource, config, name }, }) diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte index 83a8e13c9e..b0f8b86a89 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte @@ -16,7 +16,7 @@ get(integrations), updatedDatasource ) - await datasources.update({ datasource: updatedDatasource, integration }) + await datasources.save({ datasource: updatedDatasource, integration }) notifications.success( `Datasource ${updatedDatasource.name} updated successfully` ) diff --git a/packages/builder/src/stores/builder/datasources.ts b/packages/builder/src/stores/builder/datasources.ts index e167b10c2c..6eeaeceaaf 100644 --- a/packages/builder/src/stores/builder/datasources.ts +++ b/packages/builder/src/stores/builder/datasources.ts @@ -1,4 +1,4 @@ -import { writable, derived, get } from "svelte/store" +import { derived, get } from "svelte/store" import { IntegrationTypes, DEFAULT_BB_DATASOURCE_ID, @@ -17,6 +17,7 @@ import { } from "@budibase/types" // @ts-ignore import { TableNames } from "constants" +import BudiStore from "stores/BudiStore" // when building the internal DS - seems to represent it slightly differently to the backend typing of a DS interface InternalDatasource extends Omit { @@ -41,102 +42,131 @@ class TableImportError extends Error { } } -interface DatasourceStore { +interface BuilderDatasourceStore { list: Datasource[] selectedDatasourceId: null | string } -export function createDatasourcesStore() { - const store = writable({ - list: [], - selectedDatasourceId: null, - }) +interface DerivedDatasourceStore extends Omit { + list: (Datasource | InternalDatasource)[] + selected?: Datasource | InternalDatasource + hasDefaultData: boolean + hasData: boolean +} - const derivedStore = derived([store, tables], ([$store, $tables]) => { - // Set the internal datasource entities from the table list, which we're - // able to keep updated unlike the egress generated definition of the - // internal datasource - let internalDS: Datasource | InternalDatasource | undefined = - $store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) - let otherDS = $store.list?.filter(ds => ds._id !== BUDIBASE_INTERNAL_DB_ID) - if (internalDS) { - const tables: Table[] = $tables.list?.filter((table: Table) => { - return ( - table.sourceId === BUDIBASE_INTERNAL_DB_ID && - table._id !== TableNames.USERS - ) - }) - internalDS = { - ...internalDS, - entities: tables, +export class DatasourceStore extends BudiStore { + constructor() { + super({ + list: [], + selectedDatasourceId: null, + hasDefaultData: false, + hasData: false, + }) + + const derivedStore = derived< + [DatasourceStore, BudiStore], + DerivedDatasourceStore + >([this, tables as any], ([$store, $tables]) => { + // Set the internal datasource entities from the table list, which we're + // able to keep updated unlike the egress generated definition of the + // internal datasource + let internalDS: Datasource | InternalDatasource | undefined = + $store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) + let otherDS = $store.list?.filter( + ds => ds._id !== BUDIBASE_INTERNAL_DB_ID + ) + if (internalDS) { + const tables: Table[] = $tables.list?.filter((table: Table) => { + return ( + table.sourceId === BUDIBASE_INTERNAL_DB_ID && + table._id !== TableNames.USERS + ) + }) + internalDS = { + ...internalDS, + entities: tables, + } } - } - // Build up enriched DS list - // Only add the internal DS if we have at least one non-users table - let list: (InternalDatasource | Datasource)[] = [] - if (internalDS?.entities?.length) { - list.push(internalDS) - } - list = list.concat(otherDS || []) + // Build up enriched DS list + // Only add the internal DS if we have at least one non-users table + let list: (InternalDatasource | Datasource)[] = [] + if (internalDS?.entities?.length) { + list.push(internalDS) + } + list = list.concat(otherDS || []) - return { - ...$store, - list, - selected: list?.find(ds => ds._id === $store.selectedDatasourceId), - hasDefaultData: list?.some(ds => ds._id === DEFAULT_BB_DATASOURCE_ID), - hasData: list?.length > 0, - } - }) + return { + ...$store, + list, + selected: list?.find(ds => ds._id === $store.selectedDatasourceId), + hasDefaultData: list?.some(ds => ds._id === DEFAULT_BB_DATASOURCE_ID), + hasData: list?.length > 0, + } + }) - const fetch = async () => { + this.fetch = this.fetch.bind(this) + this.init = this.fetch.bind(this) + this.select = this.select.bind(this) + this.updateSchema = this.updateSchema.bind(this) + this.create = this.create.bind(this) + this.delete = this.deleteDatasource.bind(this) + this.save = this.save.bind(this) + this.replaceDatasource = this.replaceDatasource.bind(this) + this.getTableNames = this.getTableNames.bind(this) + this.subscribe = derivedStore.subscribe + } + + async fetch() { const datasources = await API.getDatasources() - store.update(state => ({ + this.store.update(state => ({ ...state, list: datasources, })) } - const select = (id: string) => { - store.update(state => ({ + async init() { + return this.fetch() + } + + select(id: string) { + this.store.update(state => ({ ...state, selectedDatasourceId: id, })) } - const updateDatasource = ( + private updateDatasourceInStore( response: { datasource: Datasource; errors?: Record }, { ignoreErrors }: { ignoreErrors?: boolean } = {} - ) => { + ) { const { datasource, errors } = response if (!ignoreErrors && errors && Object.keys(errors).length > 0) { throw new TableImportError(errors) } - replaceDatasource(datasource._id!, datasource) - select(datasource._id!) + this.replaceDatasource(datasource._id!, datasource) + this.select(datasource._id!) return datasource } - const updateSchema = async ( - datasource: Datasource, - tablesFilter: string[] - ) => { + async updateSchema(datasource: Datasource, tablesFilter: string[]) { const response = await API.buildDatasourceSchema( datasource?._id!, tablesFilter ) - updateDatasource(response) + this.updateDatasourceInStore(response) } - const sourceCount = (source: string) => { - return get(store).list.filter(datasource => datasource.source === source) - .length + sourceCount(source: string) { + return get(this.store).list.filter( + datasource => datasource.source === source + ).length } - const checkDatasourceValidity = async ( + async checkDatasourceValidity( integration: Integration, datasource: Datasource - ): Promise<{ valid: boolean; error?: string }> => { + ): Promise<{ valid: boolean; error?: string }> { if (integration.features?.[DatasourceFeature.CONNECTION_CHECKING]) { const { connected, error } = await API.validateDatasource(datasource) if (connected) { @@ -148,14 +178,14 @@ export function createDatasourcesStore() { return { valid: true } } - const create = async ({ + async create({ integration, config, }: { integration: UIIntegration config: Record - }) => { - const count = sourceCount(integration.name) + }) { + const count = this.sourceCount(integration.name) const nameModifier = count === 0 ? "" : ` ${count + 1}` const datasource: Datasource = { @@ -167,7 +197,7 @@ export function createDatasourcesStore() { isSQL: integration.isSQL, } - const { valid, error } = await checkDatasourceValidity( + const { valid, error } = await this.checkDatasourceValidity( integration, datasource ) @@ -180,41 +210,45 @@ export function createDatasourcesStore() { fetchSchema: integration.plus, }) - return updateDatasource(response, { ignoreErrors: true }) + return this.updateDatasourceInStore(response, { ignoreErrors: true }) } - const update = async ({ + async save({ integration, datasource, }: { integration: Integration datasource: Datasource - }) => { - if (await checkDatasourceValidity(integration, datasource)) { + }) { + if (await this.checkDatasourceValidity(integration, datasource)) { throw new Error("Unable to connect") } const response = await API.updateDatasource(datasource) - return updateDatasource(response) + return this.updateDatasourceInStore(response) } - const deleteDatasource = async (datasource: Datasource) => { + async deleteDatasource(datasource: Datasource) { if (!datasource?._id || !datasource?._rev) { return } await API.deleteDatasource(datasource._id, datasource._rev) - replaceDatasource(datasource._id) + this.replaceDatasource(datasource._id) } - const replaceDatasource = (datasourceId: string, datasource?: Datasource) => { + async delete(datasource: Datasource) { + return this.deleteDatasource(datasource) + } + + replaceDatasource(datasourceId: string, datasource?: Datasource) { if (!datasourceId) { return } // Handle deletion if (!datasource) { - store.update(state => ({ + this.store.update(state => ({ ...state, list: state.list.filter(x => x._id !== datasourceId), })) @@ -224,9 +258,9 @@ export function createDatasourcesStore() { } // Add new datasource - const index = get(store).list.findIndex(x => x._id === datasource._id) + const index = get(this.store).list.findIndex(x => x._id === datasource._id) if (index === -1) { - store.update(state => ({ + this.store.update(state => ({ ...state, list: [...state.list, datasource], })) @@ -238,30 +272,21 @@ export function createDatasourcesStore() { // Update existing datasource else if (datasource) { - store.update(state => { + this.store.update(state => { state.list[index] = datasource return state }) } } - const getTableNames = async (datasource: Datasource) => { + async getTableNames(datasource: Datasource) { const info = await API.fetchInfoForDatasource(datasource) return info.tableNames || [] } - return { - subscribe: derivedStore.subscribe, - fetch, - init: fetch, - select, - updateSchema, - create, - update, - delete: deleteDatasource, - replaceDatasource, - getTableNames, - } + // subscribe() { + // return this.derivedStore.subscribe() + // } } -export const datasources = createDatasourcesStore() +export const datasources = new DatasourceStore() diff --git a/packages/builder/src/stores/builder/sortedIntegrations.ts b/packages/builder/src/stores/builder/sortedIntegrations.ts index bd8bb8154f..7872d74d7f 100644 --- a/packages/builder/src/stores/builder/sortedIntegrations.ts +++ b/packages/builder/src/stores/builder/sortedIntegrations.ts @@ -3,6 +3,7 @@ import { derived } from "svelte/store" import { DatasourceTypes } from "constants/backend" import { UIIntegration, Integration } from "@budibase/types" +import BudiStore from "stores/BudiStore" const getIntegrationOrder = (type: string | undefined) => { // if type is not known, sort to end @@ -18,29 +19,35 @@ const getIntegrationOrder = (type: string | undefined) => { return type.charCodeAt(0) + 4 } -export const createSortedIntegrationsStore = () => { - return derived( - integrations, - $integrations => { - const entries: [string, Integration][] = Object.entries($integrations) - const integrationsAsArray = entries.map(([name, integration]) => ({ - name, - ...integration, - })) +class SortedIntegrationStore extends BudiStore { + constructor() { + super([]) - return integrationsAsArray.sort((integrationA, integrationB) => { - const integrationASortOrder = getIntegrationOrder(integrationA.type) - const integrationBSortOrder = getIntegrationOrder(integrationB.type) - if (integrationASortOrder === integrationBSortOrder) { - return integrationA.friendlyName.localeCompare( - integrationB.friendlyName - ) - } + const derivedStore = derived( + integrations, + $integrations => { + const entries: [string, Integration][] = Object.entries($integrations) + const integrationsAsArray = entries.map(([name, integration]) => ({ + name, + ...integration, + })) - return integrationASortOrder < integrationBSortOrder ? -1 : 1 - }) - } - ) + return integrationsAsArray.sort((integrationA, integrationB) => { + const integrationASortOrder = getIntegrationOrder(integrationA.type) + const integrationBSortOrder = getIntegrationOrder(integrationB.type) + if (integrationASortOrder === integrationBSortOrder) { + return integrationA.friendlyName.localeCompare( + integrationB.friendlyName + ) + } + + return integrationASortOrder < integrationBSortOrder ? -1 : 1 + }) + } + ) + + this.subscribe = derivedStore.subscribe + } } -export const sortedIntegrations = createSortedIntegrationsStore() +export const sortedIntegrations = new SortedIntegrationStore()