Typing table save

This commit is contained in:
Adria Navarro 2023-08-09 13:56:50 +03:00
parent dbbe9ff0ac
commit 082166d8f9
4 changed files with 36 additions and 10 deletions

View File

@ -22,6 +22,8 @@ import {
QueryJson,
RelationshipType,
RenameColumn,
SaveTableRequest,
SaveTableResponse,
Table,
TableRequest,
UserCtx,
@ -198,8 +200,8 @@ function isRelationshipSetup(column: FieldSchema) {
return column.foreignKey || column.through
}
export async function save(ctx: UserCtx) {
const inputs: TableRequest = ctx.request.body
export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
const inputs = ctx.request.body
const renamed = inputs?._rename
// can't do this right now
delete inputs.rows

View File

@ -9,6 +9,8 @@ import { isExternalTable, isSQL } from "../../../integrations/utils"
import { events } from "@budibase/backend-core"
import {
FetchTablesResponse,
SaveTableResponse,
SaveTableRequest,
Table,
TableResponse,
UserCtx,
@ -60,7 +62,7 @@ export async function find(ctx: UserCtx<void, TableResponse>) {
ctx.body = sdk.tables.enrichViewSchemas(table)
}
export async function save(ctx: UserCtx) {
export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
const appId = ctx.appId
const table = ctx.request.body
const isImport = table.rows

View File

@ -9,7 +9,15 @@ import {
fixAutoColumnSubType,
} from "../../../utilities/rowProcessor"
import { runStaticFormulaChecks } from "./bulkFormula"
import { Table, ViewV2 } from "@budibase/types"
import {
SaveTableRequest,
SaveTableResponse,
Table,
TableRequest,
UserCtx,
ViewStatisticsSchema,
ViewV2,
} from "@budibase/types"
import { quotas } from "@budibase/pro"
import isEqual from "lodash/isEqual"
import { cloneDeep } from "lodash/fp"
@ -33,10 +41,10 @@ function checkAutoColumns(table: Table, oldTable?: Table) {
return table
}
export async function save(ctx: any) {
export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
const db = context.getAppDB()
const { rows, ...rest } = ctx.request.body
let tableToSave = {
let tableToSave: TableRequest = {
type: "table",
_id: generateTableID(),
views: {},
@ -80,7 +88,7 @@ export async function save(ctx: any) {
let { _rename } = tableToSave
/* istanbul ignore next */
if (_rename && _rename.old === _rename.updated) {
_rename = null
_rename = undefined
delete tableToSave._rename
}
@ -105,7 +113,11 @@ export async function save(ctx: any) {
continue
}
if (tableView.schema.group || tableView.schema.field) continue
if (
(tableView.schema as ViewStatisticsSchema).group ||
tableView.schema.field
)
continue
tableView.schema = tableToSave.schema
}
@ -120,7 +132,7 @@ export async function save(ctx: any) {
tableToSave._rev = linkResp._rev
}
} catch (err) {
ctx.throw(400, err)
ctx.throw(400, err as string)
}
// don't perform any updates until relationships have been

View File

@ -1,4 +1,10 @@
import { Table, TableSchema, View, ViewV2 } from "../../../documents"
import {
Table,
TableRequest,
TableSchema,
View,
ViewV2,
} from "../../../documents"
interface ViewV2Response extends ViewV2 {
schema: TableSchema
@ -11,3 +17,7 @@ export interface TableResponse extends Table {
}
export type FetchTablesResponse = TableResponse[]
export interface SaveTableRequest extends TableRequest {}
export type SaveTableResponse = Table