diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index 42d657cf6c..97cf8db299 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -23,13 +23,11 @@ import { Table, RowValue, DynamicVariable, - QueryJsonRequest, } from "@budibase/types" import sdk from "../../sdk" import { builderSocket } from "../../websockets" import { isEqual } from "lodash" import { processTable } from "../../sdk/app/tables/getters" -import { makeExternalQuery } from "../../integrations/base/query" export async function fetch(ctx: UserCtx) { ctx.body = await sdk.datasources.fetch() diff --git a/packages/server/src/integrations/base/query.ts b/packages/server/src/integrations/base/query.ts index 6555a5de51..2cbbbecc8d 100644 --- a/packages/server/src/integrations/base/query.ts +++ b/packages/server/src/integrations/base/query.ts @@ -1,27 +1,39 @@ -import { DatasourcePlusQueryResponse, QueryJson } from "@budibase/types" +import { + DatasourcePlusQueryResponse, + EnrichedQueryJson, + QueryJson, +} from "@budibase/types" import { getIntegration } from "../index" import sdk from "../../sdk" import { enrichQueryJson } from "../../sdk/app/rows/utils" +function isEnriched( + json: QueryJson | EnrichedQueryJson +): json is EnrichedQueryJson { + return "datasource" in json +} + export async function makeExternalQuery( - json: QueryJson + json: QueryJson | EnrichedQueryJson ): Promise { - const enrichedJson = await enrichQueryJson(json) - if (!enrichedJson.datasource) { + if (!isEnriched(json)) { + json = await enrichQueryJson(json) + if (json.datasource) { + json.datasource = await sdk.datasources.enrich(json.datasource) + } + } + + if (!json.datasource) { throw new Error("No datasource provided for external query") } - enrichedJson.datasource = await sdk.datasources.enrich( - enrichedJson.datasource - ) - - const Integration = await getIntegration(enrichedJson.datasource.source) + const Integration = await getIntegration(json.datasource.source) // query is the opinionated function if (!Integration.prototype.query) { throw "Datasource does not support query." } - const integration = new Integration(enrichedJson.datasource.config) - return integration.query(enrichedJson) + const integration = new Integration(json.datasource.config) + return integration.query(json) } diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index a7a0b53312..2fac306590 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -509,8 +509,8 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { async query(json: EnrichedQueryJson): Promise { const schema = this.config.schema await this.connect() - if (schema && schema !== DEFAULT_SCHEMA && json?.endpoint) { - json.endpoint.schema = schema + if (schema && schema !== DEFAULT_SCHEMA) { + json.schema = schema } const operation = this._operation(json) const queryFn = (query: any, op: string) => this.internalQuery(query, op) diff --git a/packages/server/src/integrations/oracle.ts b/packages/server/src/integrations/oracle.ts index ded21487f6..d8708d9053 100644 --- a/packages/server/src/integrations/oracle.ts +++ b/packages/server/src/integrations/oracle.ts @@ -572,11 +572,7 @@ class OracleIntegration extends Sql implements DatasourcePlus { return response.rows as Row[] } else { // get the last row that was updated - if ( - response.lastRowid && - json.endpoint?.entityId && - operation !== Operation.DELETE - ) { + if (response.lastRowid && operation !== Operation.DELETE) { const lastRow = await this.internalQuery({ sql: `SELECT * FROM "${json.table.name}" WHERE ROWID = '${response.lastRowid}'`, }) diff --git a/packages/server/src/integrations/tests/sqlAlias.spec.ts b/packages/server/src/integrations/tests/sqlAlias.spec.ts index 4de84f689e..0f43e41755 100644 --- a/packages/server/src/integrations/tests/sqlAlias.spec.ts +++ b/packages/server/src/integrations/tests/sqlAlias.spec.ts @@ -269,7 +269,7 @@ describe("Captures of real examples", () => { fields: string[] = ["a"] ): EnrichedQueryJson { return { - endpoint: { datasourceId: "", entityId: "", operation: op }, + operation: op, resource: { fields, }, diff --git a/packages/server/src/tests/utilities/api/datasource.ts b/packages/server/src/tests/utilities/api/datasource.ts index ebd1b9f7c7..0bc365661e 100644 --- a/packages/server/src/tests/utilities/api/datasource.ts +++ b/packages/server/src/tests/utilities/api/datasource.ts @@ -4,7 +4,6 @@ import { Datasource, FetchDatasourceInfoResponse, FieldType, - QueryJsonRequest, RelationshipType, UpdateDatasourceRequest, UpdateDatasourceResponse,