Merge pull request #15213 from Budibase/fix-ds-validity-checking

Fix datasource validity checking
This commit is contained in:
Michael Drury 2024-12-18 16:58:29 +00:00 committed by GitHub
commit b9c6f27b3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 98 additions and 101 deletions

View File

@ -1,40 +1,22 @@
import { writable, Writable } from "svelte/store" import { writable, Writable, Readable } from "svelte/store"
interface BudiStoreOpts { interface BudiStoreOpts {
debug?: boolean debug?: boolean
} }
export default class BudiStore<T> implements Writable<T> { export class BudiStore<T> {
store: Writable<T> store: Writable<T>
subscribe: Writable<T>["subscribe"] subscribe: Writable<T>["subscribe"]
update: Writable<T>["update"] update: Writable<T>["update"]
set: Writable<T>["set"] set: Writable<T>["set"]
constructor(init: T, opts?: BudiStoreOpts) { constructor(init: T, opts?: BudiStoreOpts) {
const store = writable<T>(init) this.store = writable<T>(init)
/**
* Internal Svelte store
*/
this.store = store
/**
* Exposes the svelte subscribe fn to allow $ notation access
* @example
* $navigation.selectedScreenId
*/
this.subscribe = this.store.subscribe this.subscribe = this.store.subscribe
/**
* Exposes the svelte update fn.
* *Store modification should be kept to a minimum
*/
this.update = this.store.update this.update = this.store.update
this.set = this.store.set this.set = this.store.set
/** // Optional debug mode to output the store updates to console
* Optional debug mode to output the store updates to console
*/
if (opts?.debug) { if (opts?.debug) {
this.subscribe(state => { this.subscribe(state => {
console.warn(`${this.constructor.name}`, state) console.warn(`${this.constructor.name}`, state)
@ -42,3 +24,18 @@ export default class BudiStore<T> implements Writable<T> {
} }
} }
} }
export class DerivedBudiStore<T, DerivedT extends T> extends BudiStore<T> {
derivedStore: Readable<DerivedT>
subscribe: Readable<DerivedT>["subscribe"]
constructor(
init: T,
makeDerivedStore: (store: Writable<T>) => Readable<DerivedT>,
opts?: BudiStoreOpts
) {
super(init, opts)
this.derivedStore = makeDerivedStore(this.store)
this.subscribe = this.derivedStore.subscribe
}
}

View File

@ -1,5 +1,5 @@
import { API } from "api" import { API } from "api"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
export const INITIAL_APP_META_STATE = { export const INITIAL_APP_META_STATE = {
appId: "", appId: "",

View File

@ -1,7 +1,7 @@
import { get } from "svelte/store" import { get } from "svelte/store"
import { createBuilderWebsocket } from "./websocket.js" import { createBuilderWebsocket } from "./websocket.js"
import { BuilderSocketEvent } from "@budibase/shared-core" import { BuilderSocketEvent } from "@budibase/shared-core"
import BudiStore from "../BudiStore.js" import { BudiStore } from "../BudiStore.js"
import { TOUR_KEYS } from "components/portal/onboarding/tours.js" import { TOUR_KEYS } from "components/portal/onboarding/tours.js"
export const INITIAL_BUILDER_STATE = { export const INITIAL_BUILDER_STATE = {

View File

@ -28,7 +28,7 @@ import {
DB_TYPE_INTERNAL, DB_TYPE_INTERNAL,
DB_TYPE_EXTERNAL, DB_TYPE_EXTERNAL,
} from "constants/backend" } from "constants/backend"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
import { Utils } from "@budibase/frontend-core" import { Utils } from "@budibase/frontend-core"
import { FieldType } from "@budibase/types" import { FieldType } from "@budibase/types"
import { utils } from "@budibase/shared-core" import { utils } from "@budibase/shared-core"

View File

@ -1,4 +1,4 @@
import { derived, get } from "svelte/store" import { derived, get, Writable } from "svelte/store"
import { import {
IntegrationTypes, IntegrationTypes,
DEFAULT_BB_DATASOURCE_ID, DEFAULT_BB_DATASOURCE_ID,
@ -17,12 +17,7 @@ import {
} from "@budibase/types" } from "@budibase/types"
// @ts-ignore // @ts-ignore
import { TableNames } from "constants" import { TableNames } from "constants"
import BudiStore from "stores/BudiStore" import { DerivedBudiStore } 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<Datasource, "entities"> {
entities: Table[]
}
class TableImportError extends Error { class TableImportError extends Error {
errors: Record<string, string> errors: Record<string, string>
@ -42,68 +37,76 @@ class TableImportError extends Error {
} }
} }
// when building the internal DS - seems to represent it slightly differently to the backend typing of a DS
interface InternalDatasource extends Omit<Datasource, "entities"> {
entities: Table[]
}
interface BuilderDatasourceStore { interface BuilderDatasourceStore {
list: Datasource[] rawList: Datasource[]
selectedDatasourceId: null | string selectedDatasourceId: null | string
} }
interface DerivedDatasourceStore extends Omit<BuilderDatasourceStore, "list"> { interface DerivedDatasourceStore extends BuilderDatasourceStore {
list: (Datasource | InternalDatasource)[] list: (Datasource | InternalDatasource)[]
selected?: Datasource | InternalDatasource selected?: Datasource | InternalDatasource
hasDefaultData: boolean hasDefaultData: boolean
hasData: boolean hasData: boolean
} }
export class DatasourceStore extends BudiStore<DerivedDatasourceStore> { export class DatasourceStore extends DerivedBudiStore<
BuilderDatasourceStore,
DerivedDatasourceStore
> {
constructor() { constructor() {
super({ const makeDerivedStore = (store: Writable<BuilderDatasourceStore>) => {
list: [], return derived([store, tables], ([$store, $tables]) => {
selectedDatasourceId: null, // Set the internal datasource entities from the table list, which we're
hasDefaultData: false, // able to keep updated unlike the egress generated definition of the
hasData: false, // internal datasource
}) let internalDS: Datasource | InternalDatasource | undefined =
$store.rawList?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID)
const derivedStore = derived< let otherDS = $store.rawList?.filter(
[DatasourceStore, BudiStore<any>], ds => ds._id !== BUDIBASE_INTERNAL_DB_ID
DerivedDatasourceStore )
>([this, tables as any], ([$store, $tables]) => { if (internalDS) {
// Set the internal datasource entities from the table list, which we're const tables: Table[] = $tables.list?.filter((table: Table) => {
// able to keep updated unlike the egress generated definition of the return (
// internal datasource table.sourceId === BUDIBASE_INTERNAL_DB_ID &&
let internalDS: Datasource | InternalDatasource | undefined = table._id !== TableNames.USERS
$store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) )
let otherDS = $store.list?.filter( })
ds => ds._id !== BUDIBASE_INTERNAL_DB_ID internalDS = {
) ...internalDS,
if (internalDS) { entities: tables,
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 // Build up enriched DS list
// Only add the internal DS if we have at least one non-users table // Only add the internal DS if we have at least one non-users table
let list: (InternalDatasource | Datasource)[] = [] let list: (InternalDatasource | Datasource)[] = []
if (internalDS?.entities?.length) { if (internalDS?.entities?.length) {
list.push(internalDS) list.push(internalDS)
} }
list = list.concat(otherDS || []) list = list.concat(otherDS || [])
return { return {
...$store, ...$store,
list, list,
selected: list?.find(ds => ds._id === $store.selectedDatasourceId), selected: list?.find(ds => ds._id === $store.selectedDatasourceId),
hasDefaultData: list?.some(ds => ds._id === DEFAULT_BB_DATASOURCE_ID), hasDefaultData: list?.some(ds => ds._id === DEFAULT_BB_DATASOURCE_ID),
hasData: list?.length > 0, hasData: list?.length > 0,
} }
}) })
}
super(
{
rawList: [],
selectedDatasourceId: null,
},
makeDerivedStore
)
this.fetch = this.fetch.bind(this) this.fetch = this.fetch.bind(this)
this.init = this.fetch.bind(this) this.init = this.fetch.bind(this)
@ -114,14 +117,13 @@ export class DatasourceStore extends BudiStore<DerivedDatasourceStore> {
this.save = this.save.bind(this) this.save = this.save.bind(this)
this.replaceDatasource = this.replaceDatasource.bind(this) this.replaceDatasource = this.replaceDatasource.bind(this)
this.getTableNames = this.getTableNames.bind(this) this.getTableNames = this.getTableNames.bind(this)
this.subscribe = derivedStore.subscribe
} }
async fetch() { async fetch() {
const datasources = await API.getDatasources() const datasources = await API.getDatasources()
this.store.update(state => ({ this.store.update(state => ({
...state, ...state,
list: datasources, rawList: datasources,
})) }))
} }
@ -158,7 +160,7 @@ export class DatasourceStore extends BudiStore<DerivedDatasourceStore> {
} }
sourceCount(source: string) { sourceCount(source: string) {
return get(this.store).list.filter( return get(this.store).rawList.filter(
datasource => datasource.source === source datasource => datasource.source === source
).length ).length
} }
@ -220,7 +222,7 @@ export class DatasourceStore extends BudiStore<DerivedDatasourceStore> {
integration: Integration integration: Integration
datasource: Datasource datasource: Datasource
}) { }) {
if (await this.checkDatasourceValidity(integration, datasource)) { if (!(await this.checkDatasourceValidity(integration, datasource)).valid) {
throw new Error("Unable to connect") throw new Error("Unable to connect")
} }
@ -250,7 +252,7 @@ export class DatasourceStore extends BudiStore<DerivedDatasourceStore> {
if (!datasource) { if (!datasource) {
this.store.update(state => ({ this.store.update(state => ({
...state, ...state,
list: state.list.filter(x => x._id !== datasourceId), rawList: state.rawList.filter(x => x._id !== datasourceId),
})) }))
tables.removeDatasourceTables(datasourceId) tables.removeDatasourceTables(datasourceId)
queries.removeDatasourceQueries(datasourceId) queries.removeDatasourceQueries(datasourceId)
@ -258,11 +260,13 @@ export class DatasourceStore extends BudiStore<DerivedDatasourceStore> {
} }
// Add new datasource // Add new datasource
const index = get(this.store).list.findIndex(x => x._id === datasource._id) const index = get(this.store).rawList.findIndex(
x => x._id === datasource._id
)
if (index === -1) { if (index === -1) {
this.store.update(state => ({ this.store.update(state => ({
...state, ...state,
list: [...state.list, datasource], rawList: [...state.rawList, datasource],
})) }))
// If this is a new datasource then we should refresh the tables list, // If this is a new datasource then we should refresh the tables list,
@ -273,7 +277,7 @@ export class DatasourceStore extends BudiStore<DerivedDatasourceStore> {
// Update existing datasource // Update existing datasource
else if (datasource) { else if (datasource) {
this.store.update(state => { this.store.update(state => {
state.list[index] = datasource state.rawList[index] = datasource
return state return state
}) })
} }
@ -283,10 +287,6 @@ export class DatasourceStore extends BudiStore<DerivedDatasourceStore> {
const info = await API.fetchInfoForDatasource(datasource) const info = await API.fetchInfoForDatasource(datasource)
return info.tableNames || [] return info.tableNames || []
} }
// subscribe() {
// return this.derivedStore.subscribe()
// }
} }
export const datasources = new DatasourceStore() export const datasources = new DatasourceStore()

View File

@ -1,6 +1,6 @@
import { get } from "svelte/store" import { get } from "svelte/store"
import { previewStore } from "stores/builder" import { previewStore } from "stores/builder"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
export const INITIAL_HOVER_STATE = { export const INITIAL_HOVER_STATE = {
componentId: null, componentId: null,

View File

@ -1,6 +1,6 @@
import { derived, get } from "svelte/store" import { derived, get } from "svelte/store"
import { componentStore } from "stores/builder" import { componentStore } from "stores/builder"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
import { API } from "api" import { API } from "api"
export const INITIAL_LAYOUT_STATE = { export const INITIAL_LAYOUT_STATE = {

View File

@ -1,7 +1,7 @@
import { get } from "svelte/store" import { get } from "svelte/store"
import { API } from "api" import { API } from "api"
import { appStore } from "stores/builder" import { appStore } from "stores/builder"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
export const INITIAL_NAVIGATION_STATE = { export const INITIAL_NAVIGATION_STATE = {
navigation: "Top", navigation: "Top",

View File

@ -1,5 +1,5 @@
import { get, derived } from "svelte/store" import { get, derived } from "svelte/store"
import BudiStore from "stores/BudiStore" import { BudiStore } from "stores/BudiStore"
import { tables } from "./tables" import { tables } from "./tables"
import { viewsV2 } from "./viewsV2" import { viewsV2 } from "./viewsV2"
import { automationStore } from "./automations" import { automationStore } from "./automations"

View File

@ -12,7 +12,7 @@ import {
} from "stores/builder" } from "stores/builder"
import { createHistoryStore } from "stores/builder/history" import { createHistoryStore } from "stores/builder/history"
import { API } from "api" import { API } from "api"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
export const INITIAL_SCREENS_STATE = { export const INITIAL_SCREENS_STATE = {
screens: [], screens: [],

View File

@ -3,7 +3,7 @@ import { derived } from "svelte/store"
import { DatasourceTypes } from "constants/backend" import { DatasourceTypes } from "constants/backend"
import { UIIntegration, Integration } from "@budibase/types" import { UIIntegration, Integration } from "@budibase/types"
import BudiStore from "stores/BudiStore" import { BudiStore } from "stores/BudiStore"
const getIntegrationOrder = (type: string | undefined) => { const getIntegrationOrder = (type: string | undefined) => {
// if type is not known, sort to end // if type is not known, sort to end

View File

@ -3,7 +3,7 @@ import { derived } from "svelte/store"
import { AppStatus } from "constants" import { AppStatus } from "constants"
import { API } from "api" import { API } from "api"
import { auth } from "./auth" import { auth } from "./auth"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
import { App, UpdateAppRequest } from "@budibase/types" import { App, UpdateAppRequest } from "@budibase/types"
interface AppIdentifierMetadata { interface AppIdentifierMetadata {

View File

@ -1,7 +1,7 @@
import { get } from "svelte/store" import { get } from "svelte/store"
import { API } from "api" import { API } from "api"
import { licensing } from "./licensing" import { licensing } from "./licensing"
import BudiStore from "../BudiStore" import { BudiStore } from "../BudiStore"
import { import {
DownloadAuditLogsRequest, DownloadAuditLogsRequest,
SearchAuditLogsRequest, SearchAuditLogsRequest,

View File

@ -2,7 +2,7 @@ import { get } from "svelte/store"
import { API } from "api" import { API } from "api"
import { admin } from "stores/portal" import { admin } from "stores/portal"
import analytics from "analytics" import analytics from "analytics"
import BudiStore from "stores/BudiStore" import { BudiStore } from "stores/BudiStore"
import { import {
isSSOUser, isSSOUser,
SetInitInfoRequest, SetInitInfoRequest,