Create external table in sdk

This commit is contained in:
Adria Navarro 2024-10-09 13:20:02 +02:00
parent 15d124bfaf
commit 4efe335b65
3 changed files with 45 additions and 8 deletions

View File

@ -1,6 +1,6 @@
import { Row, Table } from "@budibase/types"
// import * as external from "./external"
import * as external from "./external"
import * as internal from "./internal"
import { isExternal } from "./utils"
@ -9,10 +9,11 @@ export async function create(
rows?: Row[],
userId?: string
): Promise<Table> {
let createdTable: Table
if (isExternal({ table })) {
// const datasourceId = table.sourceId!
throw "not implemented"
// return await external.create(table, rows, userId)
createdTable = await external.create(table)
} else {
createdTable = await internal.create(table, rows, userId)
}
return await internal.create(table, rows, userId)
return createdTable
}

View File

@ -8,8 +8,11 @@ import {
ViewV2,
AutoFieldSubType,
} from "@budibase/types"
import { context } from "@budibase/backend-core"
import { buildExternalTableId } from "../../../../integrations/utils"
import { context, HTTPError } from "@budibase/backend-core"
import {
breakExternalTableId,
buildExternalTableId,
} from "../../../../integrations/utils"
import {
foreignKeyStructure,
hasTypeChanged,
@ -86,6 +89,35 @@ function validate(table: Table, oldTable?: Table) {
}
}
function getDatasourceId(table: Table) {
if (!table) {
throw new Error("No table supplied")
}
if (table.sourceId) {
return table.sourceId
}
if (!table._id) {
throw new Error("No table ID supplied")
}
return breakExternalTableId(table._id).datasourceId
}
export async function create(table: Omit<Table, "_id" | "_rev">) {
const datasourceId = getDatasourceId(table)
const tableToCreate = { ...table, created: true }
try {
const result = await save(datasourceId!, tableToCreate)
return result.table
} catch (err: any) {
if (err instanceof Error) {
throw new HTTPError(err.message, 400)
} else {
throw new HTTPError(err?.message || err, err.status || 500)
}
}
}
export async function save(
datasourceId: string,
update: Table,

View File

@ -24,7 +24,11 @@ import * as viewsSdk from "../../views"
import { generateTableID, getRowParams } from "../../../../db/utils"
import { quotas } from "@budibase/pro"
export async function create(table: Table, rows?: Row[], userId?: string) {
export async function create(
table: Omit<Table, "_id" | "_rev">,
rows?: Row[],
userId?: string
) {
const tableId = generateTableID()
let tableToSave: Table = {