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

View File

@ -8,8 +8,11 @@ import {
ViewV2, ViewV2,
AutoFieldSubType, AutoFieldSubType,
} from "@budibase/types" } from "@budibase/types"
import { context } from "@budibase/backend-core" import { context, HTTPError } from "@budibase/backend-core"
import { buildExternalTableId } from "../../../../integrations/utils" import {
breakExternalTableId,
buildExternalTableId,
} from "../../../../integrations/utils"
import { import {
foreignKeyStructure, foreignKeyStructure,
hasTypeChanged, 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( export async function save(
datasourceId: string, datasourceId: string,
update: Table, update: Table,

View File

@ -24,7 +24,11 @@ import * as viewsSdk from "../../views"
import { generateTableID, getRowParams } from "../../../../db/utils" import { generateTableID, getRowParams } from "../../../../db/utils"
import { quotas } from "@budibase/pro" 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() const tableId = generateTableID()
let tableToSave: Table = { let tableToSave: Table = {