Move datasource.buildSchemaFromDb to sdk

This commit is contained in:
Adria Navarro 2024-01-08 19:07:47 +01:00
parent 8e038e61b0
commit 3f6e1bbaa2
4 changed files with 29 additions and 32 deletions

View File

@ -63,24 +63,16 @@ export async function information(
} }
} }
export async function buildSchemaFromDb( export async function buildSchemaFromSource(
ctx: UserCtx<BuildSchemaFromSourceRequest, BuildSchemaFromSourceResponse> ctx: UserCtx<BuildSchemaFromSourceRequest, BuildSchemaFromSourceResponse>
) { ) {
const db = context.getAppDB() const datasourceId = ctx.params.datasourceId
const tablesFilter = ctx.request.body.tablesFilter const tablesFilter = ctx.request.body.tablesFilter
const datasource = await sdk.datasources.get(ctx.params.datasourceId)
const { tables, errors } = await sdk.datasources.buildFilteredSchema( const { datasource, errors } = await sdk.datasources.buildSchemaFromSource(
datasource, datasourceId,
tablesFilter tablesFilter
) )
datasource.entities = tables
setDefaultDisplayColumns(datasource)
const dbResp = await db.put(
sdk.tables.populateExternalTableSchemas(datasource)
)
datasource._rev = dbResp.rev
ctx.body = { ctx.body = {
datasource: await sdk.datasources.removeSecretSingle(datasource), datasource: await sdk.datasources.removeSecretSingle(datasource),
@ -88,24 +80,6 @@ export async function buildSchemaFromDb(
} }
} }
/**
* Make sure all datasource entities have a display name selected
*/
function setDefaultDisplayColumns(datasource: Datasource) {
//
for (let entity of Object.values(datasource.entities || {})) {
if (entity.primaryDisplay) {
continue
}
const notAutoColumn = Object.values(entity.schema).find(
schema => !schema.autocolumn
)
if (notAutoColumn) {
entity.primaryDisplay = notAutoColumn.name
}
}
}
/** /**
* Check for variables that have been updated or removed and invalidate them. * Check for variables that have been updated or removed and invalidate them.
*/ */

View File

@ -53,7 +53,7 @@ router
.post( .post(
"/api/datasources/:datasourceId/schema", "/api/datasources/:datasourceId/schema",
authorized(permissions.BUILDER), authorized(permissions.BUILDER),
datasourceController.buildSchemaFromDb datasourceController.buildSchemaFromSource
) )
.post( .post(
"/api/datasources", "/api/datasources",

View File

@ -337,7 +337,7 @@ const preSaveAction: Partial<Record<SourceName, any>> = {
/** /**
* Make sure all datasource entities have a display name selected * Make sure all datasource entities have a display name selected
*/ */
function setDefaultDisplayColumns(datasource: Datasource) { export function setDefaultDisplayColumns(datasource: Datasource) {
// //
for (let entity of Object.values(datasource.entities || {})) { for (let entity of Object.values(datasource.entities || {})) {
if (entity.primaryDisplay) { if (entity.primaryDisplay) {

View File

@ -5,7 +5,9 @@ import {
Schema, Schema,
} from "@budibase/types" } from "@budibase/types"
import * as datasources from "./datasources" import * as datasources from "./datasources"
import tableSdk from "../tables"
import { getIntegration } from "../../../integrations" import { getIntegration } from "../../../integrations"
import { context } from "@budibase/backend-core"
export async function buildFilteredSchema( export async function buildFilteredSchema(
datasource: Datasource, datasource: Datasource,
@ -60,3 +62,24 @@ export async function getAndMergeDatasource(datasource: Datasource) {
} }
return await datasources.enrich(datasource) return await datasources.enrich(datasource)
} }
export async function buildSchemaFromSource(
datasourceId: string,
tablesFilter?: string[]
) {
const db = context.getAppDB()
const datasource = await datasources.get(datasourceId)
const { tables, errors } = await buildFilteredSchema(datasource, tablesFilter)
datasource.entities = tables
datasources.setDefaultDisplayColumns(datasource)
const dbResp = await db.put(tableSdk.populateExternalTableSchemas(datasource))
datasource._rev = dbResp.rev
return {
datasource,
errors,
}
}