PR comments - updating pickAPI to use a common function.

This commit is contained in:
mike12345567 2023-10-27 11:43:01 +01:00
parent 854ff47e1c
commit 9b1a7bd854
11 changed files with 52 additions and 36 deletions

View File

@ -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

View File

@ -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<SearchRowRequest, SearchRowResponse>) {
export async function validate(ctx: Ctx<Row, ValidateResponse>) {
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({

View File

@ -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

View File

@ -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) {

View File

@ -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

View File

@ -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<Table> {
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<Table[]> {
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()

View File

@ -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")
}

View File

@ -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<Table> {
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)

View File

@ -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

View File

@ -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

View File

@ -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<T extends Row[] | Row>(
)) 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) {