diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index 01d81425b6..82eadca312 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -23,7 +23,7 @@ import { Table, RowValue, DynamicVariable, - QueryJson, + QueryJsonRequest, } from "@budibase/types" import sdk from "../../sdk" import { builderSocket } from "../../websockets" @@ -299,7 +299,7 @@ export async function find(ctx: UserCtx) { } // dynamic query functionality -export async function query(ctx: UserCtx) { +export async function query(ctx: UserCtx) { const queryJson = ctx.request.body try { ctx.body = await makeExternalQuery(queryJson) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index baf1665cbe..422896cd52 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -137,7 +137,7 @@ function cleanupConfig(config: RunConfig, table: Table): RunConfig { function getEndpoint(tableId: string, operation: Operation) { const { datasourceId, tableName } = breakExternalTableId(tableId) - return { datasourceId, entityId: tableName, operation } + return { datasource: datasourceId, entityId: tableName, operation } } function isOneSide( @@ -706,7 +706,7 @@ export class ExternalRequest { let json: QueryJson = { endpoint: { - datasourceId: this.datasource._id!, + datasource: this.datasource, entityId: table.name, operation, }, diff --git a/packages/server/src/api/controllers/table/ExternalRequest.ts b/packages/server/src/api/controllers/table/ExternalRequest.ts index dc32c79c5a..ca4fcb1d16 100644 --- a/packages/server/src/api/controllers/table/ExternalRequest.ts +++ b/packages/server/src/api/controllers/table/ExternalRequest.ts @@ -15,7 +15,7 @@ export async function makeTableRequest( ) { const json: QueryJson = { endpoint: { - datasourceId: datasource._id!, + datasource, entityId: table._id!, operation, }, diff --git a/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts b/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts index 44b21e0350..9130034501 100644 --- a/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts +++ b/packages/server/src/api/routes/tests/queries/generic-sql.spec.ts @@ -837,7 +837,7 @@ if (descriptions.length) { const res = await config.api.datasource.query({ endpoint: { - datasourceId: datasource._id!, + datasource: datasource._id!, operation: Operation.READ, entityId, }, diff --git a/packages/server/src/integrations/base/query.ts b/packages/server/src/integrations/base/query.ts index 2185eace2c..6555a5de51 100644 --- a/packages/server/src/integrations/base/query.ts +++ b/packages/server/src/integrations/base/query.ts @@ -23,5 +23,5 @@ export async function makeExternalQuery( } const integration = new Integration(enrichedJson.datasource.config) - return integration.query(json) + return integration.query(enrichedJson) } diff --git a/packages/server/src/integrations/tests/sqlAlias.spec.ts b/packages/server/src/integrations/tests/sqlAlias.spec.ts index 66340bc794..73a9c9fe8d 100644 --- a/packages/server/src/integrations/tests/sqlAlias.spec.ts +++ b/packages/server/src/integrations/tests/sqlAlias.spec.ts @@ -4,8 +4,6 @@ import { QueryJson, SourceName, SqlQuery, - Table, - TableSourceType, SqlClient, EnrichedQueryJson, } from "@budibase/types" @@ -15,17 +13,6 @@ import { generator } from "@budibase/backend-core/tests" import sdk from "../../sdk" const Sql = sql.Sql - -// this doesn't exist strictly -const TABLE: Table = { - type: "table", - sourceType: TableSourceType.EXTERNAL, - sourceId: "SOURCE_ID", - schema: {}, - name: "tableName", - primary: ["id"], -} - const AliasTables = sdk.rows.AliasTables function multiline(sql: string) { @@ -249,7 +236,7 @@ describe("Captures of real examples", () => { function getQuery(op: Operation, fields: string[] = ["a"]): QueryJson { return { - endpoint: { datasourceId: "", entityId: "", operation: op }, + endpoint: { datasource: "", entityId: "", operation: op }, resource: { fields, }, diff --git a/packages/server/src/sdk/app/rows/search/internal/sqs.ts b/packages/server/src/sdk/app/rows/search/internal/sqs.ts index ba5614edca..d6ac2a5862 100644 --- a/packages/server/src/sdk/app/rows/search/internal/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/internal/sqs.ts @@ -358,7 +358,7 @@ export async function search( const request: QueryJson = { endpoint: { // not important, we query ourselves - datasourceId: SQS_DATASOURCE_INTERNAL, + datasource: SQS_DATASOURCE_INTERNAL, entityId: table._id!, operation: Operation.READ, }, diff --git a/packages/server/src/sdk/app/rows/sqlAlias.ts b/packages/server/src/sdk/app/rows/sqlAlias.ts index c14307360b..2f9bea3c91 100644 --- a/packages/server/src/sdk/app/rows/sqlAlias.ts +++ b/packages/server/src/sdk/app/rows/sqlAlias.ts @@ -9,11 +9,8 @@ import { SqlClient, Table, } from "@budibase/types" -import { SQS_DATASOURCE_INTERNAL } from "@budibase/backend-core" import { getSQLClient } from "./utils" import { cloneDeep } from "lodash" -import datasources from "../datasources" -import { BudibaseInternalDB } from "../../../db/utils" import { dataFilters } from "@budibase/shared-core" type PerformQueryFunction = ( @@ -185,14 +182,12 @@ export default class AliasTables { json: EnrichedQueryJson, queryFn: PerformQueryFunction ): Promise { - const datasourceId = json.endpoint.datasourceId - const isSqs = datasourceId === SQS_DATASOURCE_INTERNAL - let aliasingEnabled: boolean, datasource: Datasource + const datasource = json.datasource + const isSqs = datasource === undefined + let aliasingEnabled: boolean if (isSqs) { aliasingEnabled = this.isAliasingEnabled(json) - datasource = BudibaseInternalDB } else { - datasource = await datasources.get(datasourceId) aliasingEnabled = this.isAliasingEnabled(json, datasource) } diff --git a/packages/server/src/sdk/app/rows/utils.ts b/packages/server/src/sdk/app/rows/utils.ts index eb6859c58b..17bbebb251 100644 --- a/packages/server/src/sdk/app/rows/utils.ts +++ b/packages/server/src/sdk/app/rows/utils.ts @@ -23,6 +23,8 @@ import { isSQL } from "../../../integrations/utils" import { docIds, sql, SQS_DATASOURCE_INTERNAL } from "@budibase/backend-core" import { getTableFromSource } from "../../../api/controllers/row/utils" import env from "../../../environment" +import { breakExternalTableId } from "@budibase/backend-core/src/sql/utils" +import { isDatasourceId } from "@budibase/backend-core/src/docIds" const SQL_CLIENT_SOURCE_MAP: Record = { [SourceName.POSTGRES]: SqlClient.POSTGRES, @@ -88,8 +90,18 @@ export async function enrichQueryJson( json: QueryJson ): Promise { let datasource: Datasource | undefined = undefined - if (json.endpoint.datasourceId !== SQS_DATASOURCE_INTERNAL) { - datasource = await sdk.datasources.get(json.endpoint.datasourceId) + let entityId = json.endpoint.entityId + if (typeof json.endpoint.datasource === "string") { + if (json.endpoint.datasource !== SQS_DATASOURCE_INTERNAL) { + datasource = await sdk.datasources.get(json.endpoint.datasource, { + enriched: true, + }) + } + } else { + datasource = json.endpoint.datasource + if (isDatasourceId(entityId)) { + entityId = breakExternalTableId(entityId).tableName + } } let tables: Record @@ -99,7 +111,7 @@ export async function enrichQueryJson( tables = processInternalTables(await sdk.tables.getAllInternalTables()) } - const table = tables[json.endpoint.entityId] + const table = tables[entityId] return { table, diff --git a/packages/server/src/tests/utilities/api/datasource.ts b/packages/server/src/tests/utilities/api/datasource.ts index 87f03c8a6f..f4adaefa24 100644 --- a/packages/server/src/tests/utilities/api/datasource.ts +++ b/packages/server/src/tests/utilities/api/datasource.ts @@ -4,7 +4,7 @@ import { Datasource, FetchDatasourceInfoResponse, FieldType, - QueryJson, + QueryJsonRequest, RelationshipType, UpdateDatasourceRequest, UpdateDatasourceResponse, @@ -69,10 +69,7 @@ export class DatasourceAPI extends TestAPI { return await this._get(`/api/datasources`, { expectations }) } - query = async ( - query: Omit & Partial>, - expectations?: Expectations - ) => { + query = async (query: QueryJsonRequest, expectations?: Expectations) => { return await this._post(`/api/datasources/query`, { body: query, expectations, diff --git a/packages/types/src/sdk/search.ts b/packages/types/src/sdk/search.ts index d87472cca9..72f7de455a 100644 --- a/packages/types/src/sdk/search.ts +++ b/packages/types/src/sdk/search.ts @@ -158,7 +158,7 @@ export interface ManyToManyRelationshipJson { export interface QueryJson { endpoint: { - datasourceId: string + datasource: string | Datasource entityId: string operation: Operation schema?: string @@ -189,6 +189,10 @@ export interface EnrichedQueryJson extends QueryJson { datasource?: Datasource } +export interface QueryJsonRequest extends Omit { + endpoint: QueryJson["endpoint"] & { datasource: string } +} + export interface QueryOptions { disableReturning?: boolean disableBindings?: boolean