From 082166d8f9455683acd5402a6dc4edba3f2122e0 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 9 Aug 2023 13:56:50 +0300 Subject: [PATCH] Typing table save --- .../src/api/controllers/table/external.ts | 6 +++-- .../server/src/api/controllers/table/index.ts | 4 +++- .../src/api/controllers/table/internal.ts | 24 ++++++++++++++----- packages/types/src/api/web/app/table.ts | 12 +++++++++- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index eb1eeb7256..a8f1c1ccbb 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -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) { + const inputs = ctx.request.body const renamed = inputs?._rename // can't do this right now delete inputs.rows diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 53202d6878..e44ac94881 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -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) { ctx.body = sdk.tables.enrichViewSchemas(table) } -export async function save(ctx: UserCtx) { +export async function save(ctx: UserCtx) { const appId = ctx.appId const table = ctx.request.body const isImport = table.rows diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index e80c11eed5..da4e5351d7 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -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) { 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 diff --git a/packages/types/src/api/web/app/table.ts b/packages/types/src/api/web/app/table.ts index 178f758254..ff288811c9 100644 --- a/packages/types/src/api/web/app/table.ts +++ b/packages/types/src/api/web/app/table.ts @@ -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