diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index bbbcf96538..1d7643dd7b 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -26,6 +26,7 @@ import { IntegrationBase, DatasourcePlus, SourceName, + Ctx, } from "@budibase/types" import sdk from "../../sdk" import { builderSocket } from "../../websockets" @@ -441,3 +442,18 @@ export async function query(ctx: UserCtx) { ctx.throw(400, err) } } + +export async function getExternalSchema(ctx: Ctx) { + const { datasource } = ctx.request.body + const enrichedDatasource = await getAndMergeDatasource(datasource) + const connector = await getConnector(enrichedDatasource) + + if (!connector.getExternalSchema) { + ctx.throw(400, "Datasource does not support exporting external schema") + } + const response = await connector.getExternalSchema() + + ctx.body = { + schema: response, + } +} diff --git a/packages/server/src/api/routes/datasource.ts b/packages/server/src/api/routes/datasource.ts index f874d3c4aa..c05d1753a2 100644 --- a/packages/server/src/api/routes/datasource.ts +++ b/packages/server/src/api/routes/datasource.ts @@ -66,5 +66,10 @@ router authorized(permissions.BUILDER), datasourceController.destroy ) + .get( + "/api/datasources/:datasourceId/external-schema", + authorized(permissions.BUILDER), + datasourceController.getExternalSchema + ) export default router diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 451da5fa8f..54870cf0ec 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -181,6 +181,7 @@ class PostgresIntegration extends Sql implements DatasourcePlus { const response: ConnectionInfo = { connected: false, } + try { await this.openConnection() response.connected = true @@ -385,7 +386,7 @@ class PostgresIntegration extends Sql implements DatasourcePlus { } } - async getSchema() { + async getExternalSchema() { const dumpCommandParts = [ `user=${this.config.user}`, `host=${this.config.host}`, diff --git a/packages/types/src/sdk/datasources.ts b/packages/types/src/sdk/datasources.ts index 50ea063ca3..bccb7c04d9 100644 --- a/packages/types/src/sdk/datasources.ts +++ b/packages/types/src/sdk/datasources.ts @@ -140,6 +140,7 @@ export interface IntegrationBase { update?(query: any): Promise delete?(query: any): Promise testConnection?(): Promise + getExternalSchema?(): Promise } export interface DatasourcePlus extends IntegrationBase {