From dd77779bc16fb1e7794afbec89497ac0d4745f2b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 5 Jan 2024 15:14:31 +0100 Subject: [PATCH] Move code to sdk --- .../server/src/api/controllers/datasource.ts | 80 +++---------------- .../server/src/sdk/app/datasources/index.ts | 2 + .../server/src/sdk/app/datasources/plus.ts | 71 ++++++++++++++++ 3 files changed, 86 insertions(+), 67 deletions(-) create mode 100644 packages/server/src/sdk/app/datasources/plus.ts diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index b794312fb6..364490e5f5 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -14,10 +14,7 @@ import { DatasourcePlus, FetchDatasourceInfoRequest, FetchDatasourceInfoResponse, - IntegrationBase, - Schema, SourceName, - Table, UpdateDatasourceResponse, UserCtx, VerifyDatasourceRequest, @@ -28,62 +25,6 @@ import { builderSocket } from "../../websockets" import { setupCreationAuth as googleSetupCreationAuth } from "../../integrations/googlesheets" import { isEqual } from "lodash" -async function getConnector( - datasource: Datasource -): Promise { - const Connector = await getIntegration(datasource.source) - // can't enrich if it doesn't have an ID yet - if (datasource._id) { - datasource = await sdk.datasources.enrich(datasource) - } - // Connect to the DB and build the schema - return new Connector(datasource.config) -} - -async function getAndMergeDatasource(datasource: Datasource) { - let existingDatasource: undefined | Datasource - if (datasource._id) { - existingDatasource = await sdk.datasources.get(datasource._id) - } - let enrichedDatasource = datasource - if (existingDatasource) { - enrichedDatasource = sdk.datasources.mergeConfigs( - datasource, - existingDatasource - ) - } - return await sdk.datasources.enrich(enrichedDatasource) -} - -async function buildSchemaHelper(datasource: Datasource): Promise { - const connector = (await getConnector(datasource)) as DatasourcePlus - return await connector.buildSchema(datasource._id!, datasource.entities!) -} - -async function buildFilteredSchema( - datasource: Datasource, - filter?: string[] -): Promise { - const schema = await buildSchemaHelper(datasource) - if (!filter) { - return schema - } - - let filteredSchema: Schema = { tables: {}, errors: {} } - for (let key in schema.tables) { - if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) { - filteredSchema.tables[key] = schema.tables[key] - } - } - - for (let key in schema.errors) { - if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) { - filteredSchema.errors[key] = schema.errors[key] - } - } - return filteredSchema -} - export async function fetch(ctx: UserCtx) { ctx.body = await sdk.datasources.fetch() } @@ -92,8 +33,7 @@ export async function verify( ctx: UserCtx ) { const { datasource } = ctx.request.body - const enrichedDatasource = await getAndMergeDatasource(datasource) - const connector = await getConnector(enrichedDatasource) + const connector = await sdk.datasources.getConnector(datasource) if (!connector.testConnection) { ctx.throw(400, "Connection information verification not supported") } @@ -109,8 +49,9 @@ export async function information( ctx: UserCtx ) { const { datasource } = ctx.request.body - const enrichedDatasource = await getAndMergeDatasource(datasource) - const connector = (await getConnector(enrichedDatasource)) as DatasourcePlus + const connector = (await sdk.datasources.getConnector( + datasource + )) as DatasourcePlus if (!connector.getTableNames) { ctx.throw(400, "Table name fetching not supported by datasource") } @@ -125,7 +66,10 @@ export async function buildSchemaFromDb(ctx: UserCtx) { const tablesFilter = ctx.request.body.tablesFilter const datasource = await sdk.datasources.get(ctx.params.datasourceId) - const { tables, errors } = await buildFilteredSchema(datasource, tablesFilter) + const { tables, errors } = await sdk.datasources.buildFilteredSchema( + datasource, + tablesFilter + ) datasource.entities = tables setDefaultDisplayColumns(datasource) @@ -277,7 +221,10 @@ export async function save( let errors: Record = {} if (fetchSchema) { - const schema = await buildFilteredSchema(datasource, tablesFilter) + const schema = await sdk.datasources.buildFilteredSchema( + datasource, + tablesFilter + ) datasource.entities = schema.tables setDefaultDisplayColumns(datasource) errors = schema.errors @@ -381,8 +328,7 @@ export async function query(ctx: UserCtx) { export async function getExternalSchema(ctx: UserCtx) { const datasource = await sdk.datasources.get(ctx.params.datasourceId) - const enrichedDatasource = await getAndMergeDatasource(datasource) - const connector = await getConnector(enrichedDatasource) + const connector = await sdk.datasources.getConnector(datasource) if (!connector.getExternalSchema) { ctx.throw(400, "Datasource does not support exporting external schema") diff --git a/packages/server/src/sdk/app/datasources/index.ts b/packages/server/src/sdk/app/datasources/index.ts index 1ce6b0e689..8f06e989d3 100644 --- a/packages/server/src/sdk/app/datasources/index.ts +++ b/packages/server/src/sdk/app/datasources/index.ts @@ -1,5 +1,7 @@ import * as datasources from "./datasources" +import * as plus from "./plus" export default { ...datasources, + ...plus, } diff --git a/packages/server/src/sdk/app/datasources/plus.ts b/packages/server/src/sdk/app/datasources/plus.ts new file mode 100644 index 0000000000..65b908526c --- /dev/null +++ b/packages/server/src/sdk/app/datasources/plus.ts @@ -0,0 +1,71 @@ +import { + Datasource, + DatasourcePlus, + IntegrationBase, + Schema, +} from "@budibase/types" +import * as datasources from "./datasources" +import { getIntegration } from "../../../integrations" + +export async function buildFilteredSchema( + datasource: Datasource, + filter?: string[] +): Promise { + const schema = await buildSchemaHelper(datasource) + if (!filter) { + return schema + } + + let filteredSchema: Schema = { tables: {}, errors: {} } + for (let key in schema.tables) { + if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) { + filteredSchema.tables[key] = schema.tables[key] + } + } + + for (let key in schema.errors) { + if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) { + filteredSchema.errors[key] = schema.errors[key] + } + } + return filteredSchema +} + +async function buildSchemaHelper(datasource: Datasource): Promise { + const connector = (await getConnector(datasource)) as DatasourcePlus + const externalSchema = await connector.buildSchema( + datasource._id!, + datasource.entities! + ) + return externalSchema +} + +export async function getConnector( + datasource: Datasource +): Promise { + const Connector = await getIntegration(datasource.source) + + datasource = await getAndMergeDatasource(datasource) + + // can't enrich if it doesn't have an ID yet + if (datasource._id) { + datasource = await datasources.enrich(datasource) + } + // Connect to the DB and build the schema + return new Connector(datasource.config) +} + +async function getAndMergeDatasource(datasource: Datasource) { + let existingDatasource: undefined | Datasource + if (datasource._id) { + existingDatasource = await datasources.get(datasource._id) + } + let enrichedDatasource = datasource + if (existingDatasource) { + enrichedDatasource = datasources.mergeConfigs( + datasource, + existingDatasource + ) + } + return await datasources.enrich(enrichedDatasource) +}