From 9b1a7bd8541c4d887f48d3c549628da06cf14fa5 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 27 Oct 2023 11:43:01 +0100 Subject: [PATCH] PR comments - updating pickAPI to use a common function. --- .../src/api/controllers/public/utils.ts | 4 ++-- .../server/src/api/controllers/row/index.ts | 6 ++--- .../server/src/api/controllers/table/index.ts | 19 +++++++-------- packages/server/src/integrations/utils.ts | 23 ++++++++++++++++--- packages/server/src/sdk/app/rows/search.ts | 4 ++-- packages/server/src/sdk/app/tables/getters.ts | 12 ++++++---- .../server/src/sdk/app/tables/migration.ts | 4 ++-- packages/server/src/sdk/app/tables/update.ts | 4 ++-- packages/server/src/sdk/app/tables/utils.ts | 4 ++-- packages/server/src/sdk/app/views/index.ts | 4 ++-- .../src/utilities/rowProcessor/index.ts | 4 ++-- 11 files changed, 52 insertions(+), 36 deletions(-) diff --git a/packages/server/src/api/controllers/public/utils.ts b/packages/server/src/api/controllers/public/utils.ts index 1272fcb36a..1d67b49e0d 100644 --- a/packages/server/src/api/controllers/public/utils.ts +++ b/packages/server/src/api/controllers/public/utils.ts @@ -1,12 +1,12 @@ import { context } from "@budibase/backend-core" -import { isExternalTable } from "../../../integrations/utils" +import { isExternalTableID } from "../../../integrations/utils" import { APP_PREFIX, DocumentType } from "../../../db/utils" export async function addRev( body: { _id?: string; _rev?: string }, tableId?: string ) { - if (!body._id || (tableId && isExternalTable(tableId))) { + if (!body._id || (tableId && isExternalTableID(tableId))) { return body } let id = body._id diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 0ccbf5cacf..1a6747a085 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -1,7 +1,7 @@ import { quotas } from "@budibase/pro" import * as internal from "./internal" import * as external from "./external" -import { isExternalTable } from "../../../integrations/utils" +import { isExternalTableID } from "../../../integrations/utils" import { Ctx, UserCtx, @@ -30,7 +30,7 @@ import { Format } from "../view/exporters" export * as views from "./views" function pickApi(tableId: any) { - if (isExternalTable(tableId)) { + if (isExternalTableID(tableId)) { return external } return internal @@ -227,7 +227,7 @@ export async function search(ctx: Ctx) { export async function validate(ctx: Ctx) { const tableId = utils.getTableId(ctx) // external tables are hard to validate currently - if (isExternalTable(tableId)) { + if (isExternalTableID(tableId)) { ctx.body = { valid: true, errors: {} } } else { ctx.body = await sdk.rows.utils.validate({ diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index c68b6e9923..db2bd672d0 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -5,7 +5,11 @@ import { isSchema, validate as validateSchema, } from "../../../utilities/schema" -import { isExternalTable, isSQL } from "../../../integrations/utils" +import { + isExternalTable, + isExternalTableID, + isSQL, +} from "../../../integrations/utils" import { events } from "@budibase/backend-core" import { BulkImportRequest, @@ -29,17 +33,10 @@ import { builderSocket } from "../../../websockets" import { cloneDeep, isEqual } from "lodash" function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { - if (table && !tableId) { - tableId = table._id + if (table && isExternalTable(table)) { + return external } - if ( - table?.sourceId && - table.sourceId.includes(DocumentType.DATASOURCE + SEPARATOR) - ) { - return external - } else if (table?.sourceType === TableSourceType.EXTERNAL) { - return external - } else if (tableId && isExternalTable(tableId)) { + if (tableId && isExternalTableID(tableId)) { return external } return internal diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index 9e6c5ca3af..b4fff0737a 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -4,10 +4,13 @@ import { SearchFilters, Datasource, FieldType, + TableSourceType, } from "@budibase/types" import { DocumentType, SEPARATOR } from "../db/utils" import { InvalidColumns, NoEmptyFilterStrings } from "../constants" import { helpers } from "@budibase/shared-core" +import * as external from "../api/controllers/table/external" +import * as internal from "../api/controllers/table/internal" const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}` const ROW_ID_REGEX = /^\[.*]$/g @@ -82,12 +85,26 @@ export enum SqlClient { ORACLE = "oracledb", } -export function isExternalTable(tableId: string) { +export function isExternalTableID(tableId: string) { return tableId.includes(DocumentType.DATASOURCE) } -export function isInternalTable(tableId: string) { - return !isExternalTable(tableId) +export function isInternalTableID(tableId: string) { + return !isExternalTableID(tableId) +} + +export function isExternalTable(table: Table) { + if ( + table?.sourceId && + table.sourceId.includes(DocumentType.DATASOURCE + SEPARATOR) + ) { + return true + } else if (table?.sourceType === TableSourceType.EXTERNAL) { + return true + } else if (table?._id && isExternalTableID(table._id)) { + return true + } + return false } export function buildExternalTableId(datasourceId: string, tableName: string) { diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index caf9ffea02..31f7c74601 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -1,5 +1,5 @@ import { Row, SearchFilters, SearchParams } from "@budibase/types" -import { isExternalTable } from "../../../integrations/utils" +import { isExternalTableID } from "../../../integrations/utils" import * as internal from "./search/internal" import * as external from "./search/external" import { Format } from "../../../api/controllers/view/exporters" @@ -12,7 +12,7 @@ export interface ViewParams { } function pickApi(tableId: any) { - if (isExternalTable(tableId)) { + if (isExternalTableID(tableId)) { return external } return internal diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 176a20a59d..c0d5fe8da8 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -2,7 +2,7 @@ import { context } from "@budibase/backend-core" import { getMultiIDParams, getTableParams } from "../../../db/utils" import { breakExternalTableId, - isExternalTable, + isExternalTableID, isSQL, } from "../../../integrations/utils" import { @@ -17,7 +17,7 @@ import datasources from "../datasources" import sdk from "../../../sdk" export function processTable(table: Table): Table { - if (table._id && isExternalTable(table._id)) { + if (table._id && isExternalTableID(table._id)) { return { ...table, type: "table", @@ -79,7 +79,7 @@ export async function getExternalTable( export async function getTable(tableId: string): Promise { const db = context.getAppDB() let output: Table - if (isExternalTable(tableId)) { + if (isExternalTableID(tableId)) { let { datasourceId, tableName } = breakExternalTableId(tableId) const datasource = await datasources.get(datasourceId!) const table = await getExternalTable(datasourceId!, tableName!) @@ -109,8 +109,10 @@ export async function getExternalTablesInDatasource( } export async function getTables(tableIds: string[]): Promise { - const externalTableIds = tableIds.filter(tableId => isExternalTable(tableId)), - internalTableIds = tableIds.filter(tableId => !isExternalTable(tableId)) + const externalTableIds = tableIds.filter(tableId => + isExternalTableID(tableId) + ), + internalTableIds = tableIds.filter(tableId => !isExternalTableID(tableId)) let tables: Table[] = [] if (externalTableIds.length) { const externalTables = await getAllExternalTables() diff --git a/packages/server/src/sdk/app/tables/migration.ts b/packages/server/src/sdk/app/tables/migration.ts index 293f9184d6..7ab674bbb2 100644 --- a/packages/server/src/sdk/app/tables/migration.ts +++ b/packages/server/src/sdk/app/tables/migration.ts @@ -13,7 +13,7 @@ import { Table, } from "@budibase/types" import sdk from "../../../sdk" -import { isExternalTable } from "../../../integrations/utils" +import { isExternalTableID } from "../../../integrations/utils" import { EventType, updateLinks } from "../../../db/linkedRows" import { cloneDeep } from "lodash" @@ -58,7 +58,7 @@ function getColumnMigrator( // columns in internal tables. In the future, we may want to support other // migrations but for now return an error if we aren't migrating a user // relationship. - if (isExternalTable(table._id!)) { + if (isExternalTableID(table._id!)) { throw new BadRequestError("External tables cannot be migrated") } diff --git a/packages/server/src/sdk/app/tables/update.ts b/packages/server/src/sdk/app/tables/update.ts index c59f8a8a42..5c762e628b 100644 --- a/packages/server/src/sdk/app/tables/update.ts +++ b/packages/server/src/sdk/app/tables/update.ts @@ -1,5 +1,5 @@ import { Table, RenameColumn } from "@budibase/types" -import { isExternalTable } from "../../../integrations/utils" +import { isExternalTableID } from "../../../integrations/utils" import sdk from "../../index" import { context } from "@budibase/backend-core" import { isExternal } from "./utils" @@ -14,7 +14,7 @@ export * as internal from "./internal" export async function saveTable(table: Table): Promise
{ const db = context.getAppDB() let resp: DocumentInsertResponse - if (isExternalTable(table._id!)) { + if (isExternalTableID(table._id!)) { const datasource = await sdk.datasources.get(table.sourceId!) datasource.entities![table.name] = table resp = await db.put(datasource) diff --git a/packages/server/src/sdk/app/tables/utils.ts b/packages/server/src/sdk/app/tables/utils.ts index 68a054f6c1..b8e3d888af 100644 --- a/packages/server/src/sdk/app/tables/utils.ts +++ b/packages/server/src/sdk/app/tables/utils.ts @@ -1,10 +1,10 @@ import { Table, TableSourceType } from "@budibase/types" -import { isExternalTable } from "../../../integrations/utils" +import { isExternalTableID } from "../../../integrations/utils" export function isExternal(opts: { table?: Table; tableId?: string }): boolean { if (opts.table && opts.table.sourceType === TableSourceType.EXTERNAL) { return true - } else if (opts.tableId && isExternalTable(opts.tableId)) { + } else if (opts.tableId && isExternalTableID(opts.tableId)) { return true } return false diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 927f82cc68..67e7158f21 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -4,13 +4,13 @@ import { cloneDeep } from "lodash" import sdk from "../../../sdk" import * as utils from "../../../db/utils" -import { isExternalTable } from "../../../integrations/utils" +import { isExternalTableID } from "../../../integrations/utils" import * as internal from "./internal" import * as external from "./external" function pickApi(tableId: any) { - if (isExternalTable(tableId)) { + if (isExternalTableID(tableId)) { return external } return internal diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 0024e96d50..4a91f2c03d 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -17,7 +17,7 @@ import { processInputBBReferences, processOutputBBReferences, } from "./bbReferenceProcessor" -import { isExternalTable } from "../../integrations/utils" +import { isExternalTableID } from "../../integrations/utils" export * from "./utils" type AutoColumnProcessingOpts = { @@ -267,7 +267,7 @@ export async function outputProcessing( )) as Row[] } // remove null properties to match internal API - if (isExternalTable(table._id!)) { + if (isExternalTableID(table._id!)) { for (let row of enriched) { for (let key of Object.keys(row)) { if (row[key] === null) {