Merge pull request #11897 from Budibase/fix/BUDI-7508
Relationships disappearing when adding new columns fix
This commit is contained in:
commit
812701b565
|
@ -1,7 +1,7 @@
|
|||
import { get, writable, derived } from "svelte/store"
|
||||
import { cloneDeep } from "lodash/fp"
|
||||
import { API } from "api"
|
||||
import { SWITCHABLE_TYPES } from "constants/backend"
|
||||
import { SWITCHABLE_TYPES, FIELDS } from "constants/backend"
|
||||
|
||||
export function createTablesStore() {
|
||||
const store = writable({
|
||||
|
@ -21,6 +21,23 @@ export function createTablesStore() {
|
|||
}))
|
||||
}
|
||||
|
||||
const singleFetch = async tableId => {
|
||||
const table = await API.getTable(tableId)
|
||||
store.update(state => {
|
||||
const list = []
|
||||
// update the list, keep order accurate
|
||||
for (let tbl of state.list) {
|
||||
if (table._id === tbl._id) {
|
||||
list.push(table)
|
||||
} else {
|
||||
list.push(tbl)
|
||||
}
|
||||
}
|
||||
state.list = list
|
||||
return state
|
||||
})
|
||||
}
|
||||
|
||||
const select = tableId => {
|
||||
store.update(state => ({
|
||||
...state,
|
||||
|
@ -63,6 +80,20 @@ export function createTablesStore() {
|
|||
const savedTable = await API.saveTable(updatedTable)
|
||||
replaceTable(savedTable._id, savedTable)
|
||||
select(savedTable._id)
|
||||
// make sure tables up to date (related)
|
||||
let tableIdsToFetch = []
|
||||
for (let column of Object.values(updatedTable?.schema || {})) {
|
||||
if (column.type === FIELDS.LINK.type) {
|
||||
tableIdsToFetch.push(column.tableId)
|
||||
}
|
||||
}
|
||||
tableIdsToFetch = [...new Set(tableIdsToFetch)]
|
||||
// too many tables to fetch, just get all
|
||||
if (tableIdsToFetch.length > 3) {
|
||||
await fetch()
|
||||
} else {
|
||||
await Promise.all(tableIdsToFetch.map(id => singleFetch(id)))
|
||||
}
|
||||
return savedTable
|
||||
}
|
||||
|
||||
|
|
|
@ -82,8 +82,9 @@ export const buildTableEndpoints = API => ({
|
|||
},
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets a list o tables.
|
||||
* Gets a list of tables.
|
||||
*/
|
||||
getTables: async () => {
|
||||
return await API.get({
|
||||
|
@ -91,6 +92,15 @@ export const buildTableEndpoints = API => ({
|
|||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Get a single table based on table ID.
|
||||
*/
|
||||
getTable: async tableId => {
|
||||
return await API.get({
|
||||
url: `/api/tables/${tableId}`,
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Saves a table.
|
||||
* @param table the table to save
|
||||
|
|
|
@ -14,10 +14,12 @@ import {
|
|||
Table,
|
||||
TableResponse,
|
||||
UserCtx,
|
||||
Datasource,
|
||||
} from "@budibase/types"
|
||||
import sdk from "../../../sdk"
|
||||
import { jsonFromCsvString } from "../../../utilities/csv"
|
||||
import { builderSocket } from "../../../websockets"
|
||||
import { cloneDeep } from "lodash"
|
||||
|
||||
function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
|
||||
if (table && !tableId) {
|
||||
|
@ -35,16 +37,16 @@ function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
|
|||
export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
|
||||
const internal = await sdk.tables.getAllInternalTables()
|
||||
|
||||
const externalTables = await sdk.datasources.getExternalDatasources()
|
||||
const datasources = await sdk.datasources.getExternalDatasources()
|
||||
|
||||
const external = externalTables.flatMap(table => {
|
||||
let entities = table.entities
|
||||
const external = datasources.flatMap(datasource => {
|
||||
let entities = datasource.entities
|
||||
if (entities) {
|
||||
return Object.values(entities).map<Table>((entity: Table) => ({
|
||||
...entity,
|
||||
type: "external",
|
||||
sourceId: table._id,
|
||||
sql: isSQL(table),
|
||||
sourceId: datasource._id,
|
||||
sql: isSQL(datasource),
|
||||
}))
|
||||
} else {
|
||||
return []
|
||||
|
@ -80,7 +82,7 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
|||
ctx.eventEmitter &&
|
||||
ctx.eventEmitter.emitTable(`table:save`, appId, { ...savedTable })
|
||||
ctx.body = savedTable
|
||||
builderSocket?.emitTableUpdate(ctx, { ...savedTable })
|
||||
builderSocket?.emitTableUpdate(ctx, cloneDeep(savedTable))
|
||||
}
|
||||
|
||||
export async function destroy(ctx: UserCtx) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { SqlQuery, Table, SearchFilters } from "@budibase/types"
|
||||
import { SqlQuery, Table, SearchFilters, Datasource } from "@budibase/types"
|
||||
import { DocumentType, SEPARATOR } from "../db/utils"
|
||||
import {
|
||||
FieldTypes,
|
||||
|
@ -184,7 +184,9 @@ export function getSqlQuery(query: SqlQuery | string): SqlQuery {
|
|||
}
|
||||
}
|
||||
|
||||
export const isSQL = helpers.isSQL
|
||||
export function isSQL(datasource: Datasource) {
|
||||
return helpers.isSQL(datasource)
|
||||
}
|
||||
|
||||
export function isIsoDateString(str: string) {
|
||||
const trimmedValue = str.trim()
|
||||
|
|
Loading…
Reference in New Issue