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 { context } from "@budibase/backend-core"
import { isExternalTable } from "../../../integrations/utils" import { isExternalTableID } from "../../../integrations/utils"
import { APP_PREFIX, DocumentType } from "../../../db/utils" import { APP_PREFIX, DocumentType } from "../../../db/utils"
export async function addRev( export async function addRev(
body: { _id?: string; _rev?: string }, body: { _id?: string; _rev?: string },
tableId?: string tableId?: string
) { ) {
if (!body._id || (tableId && isExternalTable(tableId))) { if (!body._id || (tableId && isExternalTableID(tableId))) {
return body return body
} }
let id = body._id let id = body._id

View File

@ -1,7 +1,7 @@
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import * as internal from "./internal" import * as internal from "./internal"
import * as external from "./external" import * as external from "./external"
import { isExternalTable } from "../../../integrations/utils" import { isExternalTableID } from "../../../integrations/utils"
import { import {
Ctx, Ctx,
UserCtx, UserCtx,
@ -30,7 +30,7 @@ import { Format } from "../view/exporters"
export * as views from "./views" export * as views from "./views"
function pickApi(tableId: any) { function pickApi(tableId: any) {
if (isExternalTable(tableId)) { if (isExternalTableID(tableId)) {
return external return external
} }
return internal return internal
@ -227,7 +227,7 @@ export async function search(ctx: Ctx<SearchRowRequest, SearchRowResponse>) {
export async function validate(ctx: Ctx<Row, ValidateResponse>) { export async function validate(ctx: Ctx<Row, ValidateResponse>) {
const tableId = utils.getTableId(ctx) const tableId = utils.getTableId(ctx)
// external tables are hard to validate currently // external tables are hard to validate currently
if (isExternalTable(tableId)) { if (isExternalTableID(tableId)) {
ctx.body = { valid: true, errors: {} } ctx.body = { valid: true, errors: {} }
} else { } else {
ctx.body = await sdk.rows.utils.validate({ ctx.body = await sdk.rows.utils.validate({

View File

@ -5,7 +5,11 @@ import {
isSchema, isSchema,
validate as validateSchema, validate as validateSchema,
} from "../../../utilities/schema" } from "../../../utilities/schema"
import { isExternalTable, isSQL } from "../../../integrations/utils" import {
isExternalTable,
isExternalTableID,
isSQL,
} from "../../../integrations/utils"
import { events } from "@budibase/backend-core" import { events } from "@budibase/backend-core"
import { import {
BulkImportRequest, BulkImportRequest,
@ -29,17 +33,10 @@ import { builderSocket } from "../../../websockets"
import { cloneDeep, isEqual } from "lodash" import { cloneDeep, isEqual } from "lodash"
function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
if (table && !tableId) { if (table && isExternalTable(table)) {
tableId = table._id return external
} }
if ( if (tableId && isExternalTableID(tableId)) {
table?.sourceId &&
table.sourceId.includes(DocumentType.DATASOURCE + SEPARATOR)
) {
return external
} else if (table?.sourceType === TableSourceType.EXTERNAL) {
return external
} else if (tableId && isExternalTable(tableId)) {
return external return external
} }
return internal return internal

View File

@ -4,10 +4,13 @@ import {
SearchFilters, SearchFilters,
Datasource, Datasource,
FieldType, FieldType,
TableSourceType,
} from "@budibase/types" } from "@budibase/types"
import { DocumentType, SEPARATOR } from "../db/utils" import { DocumentType, SEPARATOR } from "../db/utils"
import { InvalidColumns, NoEmptyFilterStrings } from "../constants" import { InvalidColumns, NoEmptyFilterStrings } from "../constants"
import { helpers } from "@budibase/shared-core" 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 DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
const ROW_ID_REGEX = /^\[.*]$/g const ROW_ID_REGEX = /^\[.*]$/g
@ -82,12 +85,26 @@ export enum SqlClient {
ORACLE = "oracledb", ORACLE = "oracledb",
} }
export function isExternalTable(tableId: string) { export function isExternalTableID(tableId: string) {
return tableId.includes(DocumentType.DATASOURCE) return tableId.includes(DocumentType.DATASOURCE)
} }
export function isInternalTable(tableId: string) { export function isInternalTableID(tableId: string) {
return !isExternalTable(tableId) 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) { export function buildExternalTableId(datasourceId: string, tableName: string) {

View File

@ -1,5 +1,5 @@
import { Row, SearchFilters, SearchParams } from "@budibase/types" 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 internal from "./search/internal"
import * as external from "./search/external" import * as external from "./search/external"
import { Format } from "../../../api/controllers/view/exporters" import { Format } from "../../../api/controllers/view/exporters"
@ -12,7 +12,7 @@ export interface ViewParams {
} }
function pickApi(tableId: any) { function pickApi(tableId: any) {
if (isExternalTable(tableId)) { if (isExternalTableID(tableId)) {
return external return external
} }
return internal return internal

View File

@ -2,7 +2,7 @@ import { context } from "@budibase/backend-core"
import { getMultiIDParams, getTableParams } from "../../../db/utils" import { getMultiIDParams, getTableParams } from "../../../db/utils"
import { import {
breakExternalTableId, breakExternalTableId,
isExternalTable, isExternalTableID,
isSQL, isSQL,
} from "../../../integrations/utils" } from "../../../integrations/utils"
import { import {
@ -17,7 +17,7 @@ import datasources from "../datasources"
import sdk from "../../../sdk" import sdk from "../../../sdk"
export function processTable(table: Table): Table { export function processTable(table: Table): Table {
if (table._id && isExternalTable(table._id)) { if (table._id && isExternalTableID(table._id)) {
return { return {
...table, ...table,
type: "table", type: "table",
@ -79,7 +79,7 @@ export async function getExternalTable(
export async function getTable(tableId: string): Promise<Table> { export async function getTable(tableId: string): Promise<Table> {
const db = context.getAppDB() const db = context.getAppDB()
let output: Table let output: Table
if (isExternalTable(tableId)) { if (isExternalTableID(tableId)) {
let { datasourceId, tableName } = breakExternalTableId(tableId) let { datasourceId, tableName } = breakExternalTableId(tableId)
const datasource = await datasources.get(datasourceId!) const datasource = await datasources.get(datasourceId!)
const table = await getExternalTable(datasourceId!, tableName!) const table = await getExternalTable(datasourceId!, tableName!)
@ -109,8 +109,10 @@ export async function getExternalTablesInDatasource(
} }
export async function getTables(tableIds: string[]): Promise<Table[]> { export async function getTables(tableIds: string[]): Promise<Table[]> {
const externalTableIds = tableIds.filter(tableId => isExternalTable(tableId)), const externalTableIds = tableIds.filter(tableId =>
internalTableIds = tableIds.filter(tableId => !isExternalTable(tableId)) isExternalTableID(tableId)
),
internalTableIds = tableIds.filter(tableId => !isExternalTableID(tableId))
let tables: Table[] = [] let tables: Table[] = []
if (externalTableIds.length) { if (externalTableIds.length) {
const externalTables = await getAllExternalTables() const externalTables = await getAllExternalTables()

View File

@ -13,7 +13,7 @@ import {
Table, Table,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { isExternalTable } from "../../../integrations/utils" import { isExternalTableID } from "../../../integrations/utils"
import { EventType, updateLinks } from "../../../db/linkedRows" import { EventType, updateLinks } from "../../../db/linkedRows"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
@ -58,7 +58,7 @@ function getColumnMigrator(
// columns in internal tables. In the future, we may want to support other // 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 // migrations but for now return an error if we aren't migrating a user
// relationship. // relationship.
if (isExternalTable(table._id!)) { if (isExternalTableID(table._id!)) {
throw new BadRequestError("External tables cannot be migrated") throw new BadRequestError("External tables cannot be migrated")
} }

View File

@ -1,5 +1,5 @@
import { Table, RenameColumn } from "@budibase/types" import { Table, RenameColumn } from "@budibase/types"
import { isExternalTable } from "../../../integrations/utils" import { isExternalTableID } from "../../../integrations/utils"
import sdk from "../../index" import sdk from "../../index"
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
import { isExternal } from "./utils" import { isExternal } from "./utils"
@ -14,7 +14,7 @@ export * as internal from "./internal"
export async function saveTable(table: Table): Promise<Table> { export async function saveTable(table: Table): Promise<Table> {
const db = context.getAppDB() const db = context.getAppDB()
let resp: DocumentInsertResponse let resp: DocumentInsertResponse
if (isExternalTable(table._id!)) { if (isExternalTableID(table._id!)) {
const datasource = await sdk.datasources.get(table.sourceId!) const datasource = await sdk.datasources.get(table.sourceId!)
datasource.entities![table.name] = table datasource.entities![table.name] = table
resp = await db.put(datasource) resp = await db.put(datasource)

View File

@ -1,10 +1,10 @@
import { Table, TableSourceType } from "@budibase/types" 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 { export function isExternal(opts: { table?: Table; tableId?: string }): boolean {
if (opts.table && opts.table.sourceType === TableSourceType.EXTERNAL) { if (opts.table && opts.table.sourceType === TableSourceType.EXTERNAL) {
return true return true
} else if (opts.tableId && isExternalTable(opts.tableId)) { } else if (opts.tableId && isExternalTableID(opts.tableId)) {
return true return true
} }
return false return false

View File

@ -4,13 +4,13 @@ import { cloneDeep } from "lodash"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "../../../db/utils" import * as utils from "../../../db/utils"
import { isExternalTable } from "../../../integrations/utils" import { isExternalTableID } from "../../../integrations/utils"
import * as internal from "./internal" import * as internal from "./internal"
import * as external from "./external" import * as external from "./external"
function pickApi(tableId: any) { function pickApi(tableId: any) {
if (isExternalTable(tableId)) { if (isExternalTableID(tableId)) {
return external return external
} }
return internal return internal

View File

@ -17,7 +17,7 @@ import {
processInputBBReferences, processInputBBReferences,
processOutputBBReferences, processOutputBBReferences,
} from "./bbReferenceProcessor" } from "./bbReferenceProcessor"
import { isExternalTable } from "../../integrations/utils" import { isExternalTableID } from "../../integrations/utils"
export * from "./utils" export * from "./utils"
type AutoColumnProcessingOpts = { type AutoColumnProcessingOpts = {
@ -267,7 +267,7 @@ export async function outputProcessing<T extends Row[] | Row>(
)) as Row[] )) as Row[]
} }
// remove null properties to match internal API // remove null properties to match internal API
if (isExternalTable(table._id!)) { if (isExternalTableID(table._id!)) {
for (let row of enriched) { for (let row of enriched) {
for (let key of Object.keys(row)) { for (let key of Object.keys(row)) {
if (row[key] === null) { if (row[key] === null) {