Implement external
This commit is contained in:
parent
7e2ecc1b3a
commit
6f2aa32878
|
@ -4,13 +4,18 @@ import { context, HTTPError } from "@budibase/backend-core"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
import * as utils from "../../../db/utils"
|
import * as utils from "../../../db/utils"
|
||||||
import { enrichSchema, isV2 } from "."
|
import { enrichSchema, isV2 } from "."
|
||||||
|
import { breakExternalTableId } from "../../../integrations/utils"
|
||||||
|
|
||||||
export async function get(
|
export async function get(
|
||||||
viewId: string,
|
viewId: string,
|
||||||
opts?: { enriched: boolean }
|
opts?: { enriched: boolean }
|
||||||
): Promise<ViewV2> {
|
): Promise<ViewV2> {
|
||||||
const { tableId } = utils.extractViewInfoFromID(viewId)
|
const { tableId } = utils.extractViewInfoFromID(viewId)
|
||||||
const table = await sdk.tables.getTable(tableId)
|
|
||||||
|
const { datasourceId, tableName } = breakExternalTableId(tableId)
|
||||||
|
const ds = await sdk.datasources.get(datasourceId!)
|
||||||
|
|
||||||
|
const table = ds.entities![tableName!]
|
||||||
const views = Object.values(table.views!)
|
const views = Object.values(table.views!)
|
||||||
const found = views.find(v => isV2(v) && v.id === viewId)
|
const found = views.find(v => isV2(v) && v.id === viewId)
|
||||||
if (!found) {
|
if (!found) {
|
||||||
|
@ -35,20 +40,23 @@ export async function create(
|
||||||
|
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
|
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const { datasourceId, tableName } = breakExternalTableId(tableId)
|
||||||
table.views ??= {}
|
const ds = await sdk.datasources.get(datasourceId!)
|
||||||
|
ds.entities![tableName!].views ??= {}
|
||||||
table.views[view.name] = view
|
ds.entities![tableName!].views![view.name] = view
|
||||||
await db.put(table)
|
await db.put(ds)
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
|
export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const table = await sdk.tables.getTable(tableId)
|
|
||||||
table.views ??= {}
|
|
||||||
|
|
||||||
const existingView = Object.values(table.views).find(
|
const { datasourceId, tableName } = breakExternalTableId(tableId)
|
||||||
|
const ds = await sdk.datasources.get(datasourceId!)
|
||||||
|
ds.entities![tableName!].views ??= {}
|
||||||
|
const views = ds.entities![tableName!].views!
|
||||||
|
|
||||||
|
const existingView = Object.values(views).find(
|
||||||
v => isV2(v) && v.id === view.id
|
v => isV2(v) && v.id === view.id
|
||||||
)
|
)
|
||||||
if (!existingView) {
|
if (!existingView) {
|
||||||
|
@ -56,9 +64,9 @@ export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("set to", view)
|
console.log("set to", view)
|
||||||
delete table.views[existingView.name]
|
delete views[existingView.name]
|
||||||
table.views[view.name] = view
|
views[view.name] = view
|
||||||
await db.put(table)
|
await db.put(ds)
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,12 +74,15 @@ export async function remove(viewId: string): Promise<ViewV2> {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
|
|
||||||
const view = await get(viewId)
|
const view = await get(viewId)
|
||||||
const table = await sdk.tables.getTable(view?.tableId)
|
|
||||||
if (!view) {
|
if (!view) {
|
||||||
throw new HTTPError(`View ${viewId} not found`, 404)
|
throw new HTTPError(`View ${viewId} not found`, 404)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete table.views![view?.name]
|
const { datasourceId, tableName } = breakExternalTableId(view.tableId)
|
||||||
await db.put(table)
|
const ds = await sdk.datasources.get(datasourceId!)
|
||||||
|
|
||||||
|
delete ds.entities![tableName!].views![view?.name]
|
||||||
|
await db.put(ds)
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue