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 { get, writable, derived } from "svelte/store"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { API } from "api"
|
import { API } from "api"
|
||||||
import { SWITCHABLE_TYPES } from "constants/backend"
|
import { SWITCHABLE_TYPES, FIELDS } from "constants/backend"
|
||||||
|
|
||||||
export function createTablesStore() {
|
export function createTablesStore() {
|
||||||
const store = writable({
|
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 => {
|
const select = tableId => {
|
||||||
store.update(state => ({
|
store.update(state => ({
|
||||||
...state,
|
...state,
|
||||||
|
@ -63,6 +80,20 @@ export function createTablesStore() {
|
||||||
const savedTable = await API.saveTable(updatedTable)
|
const savedTable = await API.saveTable(updatedTable)
|
||||||
replaceTable(savedTable._id, savedTable)
|
replaceTable(savedTable._id, savedTable)
|
||||||
select(savedTable._id)
|
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
|
return savedTable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -82,8 +82,9 @@ export const buildTableEndpoints = API => ({
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list o tables.
|
* Gets a list of tables.
|
||||||
*/
|
*/
|
||||||
getTables: async () => {
|
getTables: async () => {
|
||||||
return await API.get({
|
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.
|
* Saves a table.
|
||||||
* @param table the table to save
|
* @param table the table to save
|
||||||
|
|
|
@ -14,10 +14,12 @@ import {
|
||||||
Table,
|
Table,
|
||||||
TableResponse,
|
TableResponse,
|
||||||
UserCtx,
|
UserCtx,
|
||||||
|
Datasource,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
import { jsonFromCsvString } from "../../../utilities/csv"
|
import { jsonFromCsvString } from "../../../utilities/csv"
|
||||||
import { builderSocket } from "../../../websockets"
|
import { builderSocket } from "../../../websockets"
|
||||||
|
import { cloneDeep } from "lodash"
|
||||||
|
|
||||||
function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
|
function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
|
||||||
if (table && !tableId) {
|
if (table && !tableId) {
|
||||||
|
@ -35,16 +37,16 @@ function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
|
||||||
export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
|
export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
|
||||||
const internal = await sdk.tables.getAllInternalTables()
|
const internal = await sdk.tables.getAllInternalTables()
|
||||||
|
|
||||||
const externalTables = await sdk.datasources.getExternalDatasources()
|
const datasources = await sdk.datasources.getExternalDatasources()
|
||||||
|
|
||||||
const external = externalTables.flatMap(table => {
|
const external = datasources.flatMap(datasource => {
|
||||||
let entities = table.entities
|
let entities = datasource.entities
|
||||||
if (entities) {
|
if (entities) {
|
||||||
return Object.values(entities).map<Table>((entity: Table) => ({
|
return Object.values(entities).map<Table>((entity: Table) => ({
|
||||||
...entity,
|
...entity,
|
||||||
type: "external",
|
type: "external",
|
||||||
sourceId: table._id,
|
sourceId: datasource._id,
|
||||||
sql: isSQL(table),
|
sql: isSQL(datasource),
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
return []
|
return []
|
||||||
|
@ -80,7 +82,7 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
||||||
ctx.eventEmitter &&
|
ctx.eventEmitter &&
|
||||||
ctx.eventEmitter.emitTable(`table:save`, appId, { ...savedTable })
|
ctx.eventEmitter.emitTable(`table:save`, appId, { ...savedTable })
|
||||||
ctx.body = savedTable
|
ctx.body = savedTable
|
||||||
builderSocket?.emitTableUpdate(ctx, { ...savedTable })
|
builderSocket?.emitTableUpdate(ctx, cloneDeep(savedTable))
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function destroy(ctx: UserCtx) {
|
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 { DocumentType, SEPARATOR } from "../db/utils"
|
||||||
import {
|
import {
|
||||||
FieldTypes,
|
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) {
|
export function isIsoDateString(str: string) {
|
||||||
const trimmedValue = str.trim()
|
const trimmedValue = str.trim()
|
||||||
|
|
Loading…
Reference in New Issue