From 7d2861718a3511963a02bbb9737eafb504bd4f7f Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Tue, 16 Apr 2024 16:41:39 +0100 Subject: [PATCH 1/4] Making meta required in query JSON. --- .../api/controllers/row/ExternalRequest.ts | 16 ++++++++++ .../routes/tests/queries/generic-sql.spec.ts | 3 ++ .../server/src/integrations/tests/sql.spec.ts | 30 ++++++++++++++----- .../src/integrations/tests/sqlAlias.spec.ts | 15 ++++++++++ packages/types/src/sdk/search.ts | 2 +- 5 files changed, 57 insertions(+), 9 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 4adbb72c7a..da620a92e6 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -119,6 +119,9 @@ async function removeManyToManyRelationships( endpoint: getEndpoint(tableId, Operation.DELETE), body: { [colName]: null }, filters, + meta: { + table, + } }) } else { return [] @@ -133,6 +136,9 @@ async function removeOneToManyRelationships(rowId: string, table: Table) { return getDatasourceAndQuery({ endpoint: getEndpoint(tableId, Operation.UPDATE), filters, + meta: { + table, + } }) } else { return [] @@ -248,6 +254,9 @@ export class ExternalRequest { const response = await getDatasourceAndQuery({ endpoint: getEndpoint(table._id!, Operation.READ), filters: buildFilters(rowId, {}, table), + meta: { + table, + } }) if (Array.isArray(response) && response.length > 0) { return response[0] @@ -395,6 +404,9 @@ export class ExternalRequest { [fieldName]: row[lookupField], }, }, + meta: { + table, + } }) // this is the response from knex if no rows found const rows: Row[] = @@ -425,6 +437,7 @@ export class ExternalRequest { // if we're creating (in a through table) need to wipe the existing ones first const promises = [] const related = await this.lookupRelations(mainTableId, row) + const table = this.getTable(mainTableId) for (let relationship of relationships) { const { key, tableId, isUpdate, id, ...rest } = relationship const body: { [key: string]: any } = processObjectSync(rest, row, {}) @@ -470,6 +483,9 @@ export class ExternalRequest { // if we're doing many relationships then we're writing, only one response body, filters: buildFilters(id, {}, linkTable), + meta: { + table, + } }) ) } else { 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 7790f909e7..1f83adc663 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 @@ -755,6 +755,9 @@ describe.each( name: "two", }, }, + meta: { + table: config.table, + }, }) expect(res).toHaveLength(1) expect(res[0]).toEqual({ diff --git a/packages/server/src/integrations/tests/sql.spec.ts b/packages/server/src/integrations/tests/sql.spec.ts index dc2a06446b..d056159d7d 100644 --- a/packages/server/src/integrations/tests/sql.spec.ts +++ b/packages/server/src/integrations/tests/sql.spec.ts @@ -9,6 +9,14 @@ import { } from "@budibase/types" const TABLE_NAME = "test" +const TABLE: Table = { + type: "table", + sourceType: TableSourceType.EXTERNAL, + sourceId: "SOURCE_ID", + schema: {}, + name: TABLE_NAME, + primary: ["id"], +} function endpoint(table: any, operation: any) { return { @@ -25,6 +33,10 @@ function generateReadJson({ sort, paginate, }: any = {}): QueryJson { + const tableObj = { ...TABLE } + if (table) { + tableObj.name = table + } return { endpoint: endpoint(table || TABLE_NAME, "READ"), resource: { @@ -34,14 +46,7 @@ function generateReadJson({ sort: sort || {}, paginate: paginate || {}, meta: { - table: { - type: "table", - sourceType: TableSourceType.EXTERNAL, - sourceId: "SOURCE_ID", - schema: {}, - name: table || TABLE_NAME, - primary: ["id"], - } as any, + table: tableObj, }, } } @@ -49,6 +54,9 @@ function generateReadJson({ function generateCreateJson(table = TABLE_NAME, body = {}): QueryJson { return { endpoint: endpoint(table, "CREATE"), + meta: { + table: TABLE, + }, body, } } @@ -70,6 +78,9 @@ function generateUpdateJson({ function generateDeleteJson(table = TABLE_NAME, filters = {}): QueryJson { return { endpoint: endpoint(table, "DELETE"), + meta: { + table: TABLE, + }, filters, } } @@ -102,6 +113,9 @@ function generateRelationshipJson(config: { schema?: string } = {}): QueryJson { }, ], extra: { idFilter: {} }, + meta: { + table: TABLE, + }, } } diff --git a/packages/server/src/integrations/tests/sqlAlias.spec.ts b/packages/server/src/integrations/tests/sqlAlias.spec.ts index 58c3a05245..f4edab8dad 100644 --- a/packages/server/src/integrations/tests/sqlAlias.spec.ts +++ b/packages/server/src/integrations/tests/sqlAlias.spec.ts @@ -4,6 +4,8 @@ import { QueryJson, SourceName, SqlQuery, + Table, + TableSourceType, } from "@budibase/types" import { join } from "path" import Sql from "../base/sql" @@ -11,6 +13,16 @@ import { SqlClient } from "../utils" import { generator } from "@budibase/backend-core/tests" import sdk from "../../sdk" +// 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) { @@ -222,6 +234,9 @@ describe("Captures of real examples", () => { resource: { fields, }, + meta: { + table: TABLE, + }, } } diff --git a/packages/types/src/sdk/search.ts b/packages/types/src/sdk/search.ts index 9325f09eed..6cac76e01d 100644 --- a/packages/types/src/sdk/search.ts +++ b/packages/types/src/sdk/search.ts @@ -90,7 +90,7 @@ export interface QueryJson { paginate?: PaginationJson body?: Row | Row[] table?: Table - meta?: { + meta: { table?: Table tables?: Record renamed?: RenameColumn From ccb56c8788391d046243d059ce48898275b073ca Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Tue, 16 Apr 2024 17:05:09 +0100 Subject: [PATCH 2/4] Linting. --- .../server/src/api/controllers/row/ExternalRequest.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index da620a92e6..835e5bb20d 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -121,7 +121,7 @@ async function removeManyToManyRelationships( filters, meta: { table, - } + }, }) } else { return [] @@ -138,7 +138,7 @@ async function removeOneToManyRelationships(rowId: string, table: Table) { filters, meta: { table, - } + }, }) } else { return [] @@ -256,7 +256,7 @@ export class ExternalRequest { filters: buildFilters(rowId, {}, table), meta: { table, - } + }, }) if (Array.isArray(response) && response.length > 0) { return response[0] @@ -406,7 +406,7 @@ export class ExternalRequest { }, meta: { table, - } + }, }) // this is the response from knex if no rows found const rows: Row[] = @@ -485,7 +485,7 @@ export class ExternalRequest { filters: buildFilters(id, {}, linkTable), meta: { table, - } + }, }) ) } else { From 8b0fc5ed5d1b640a15adcf7fd1932ca71909cc1c Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Tue, 16 Apr 2024 17:22:17 +0100 Subject: [PATCH 3/4] Making table required. --- .../server/src/api/controllers/row/ExternalRequest.ts | 2 +- .../server/src/api/controllers/table/ExternalRequest.ts | 1 + .../src/api/routes/tests/queries/generic-sql.spec.ts | 2 +- packages/server/src/integrations/tests/sql.spec.ts | 8 ++++++++ packages/types/src/sdk/search.ts | 2 +- 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 835e5bb20d..3dd3f9b8e7 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -437,7 +437,7 @@ export class ExternalRequest { // if we're creating (in a through table) need to wipe the existing ones first const promises = [] const related = await this.lookupRelations(mainTableId, row) - const table = this.getTable(mainTableId) + const table = this.getTable(mainTableId)! for (let relationship of relationships) { const { key, tableId, isUpdate, id, ...rest } = relationship const body: { [key: string]: any } = processObjectSync(rest, row, {}) diff --git a/packages/server/src/api/controllers/table/ExternalRequest.ts b/packages/server/src/api/controllers/table/ExternalRequest.ts index 65cead3a1d..1e57ea3294 100644 --- a/packages/server/src/api/controllers/table/ExternalRequest.ts +++ b/packages/server/src/api/controllers/table/ExternalRequest.ts @@ -22,6 +22,7 @@ export async function makeTableRequest( operation, }, meta: { + table, tables, }, table, 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 1f83adc663..718f18203c 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 @@ -756,7 +756,7 @@ describe.each( }, }, meta: { - table: config.table, + table: config.table!, }, }) expect(res).toHaveLength(1) diff --git a/packages/server/src/integrations/tests/sql.spec.ts b/packages/server/src/integrations/tests/sql.spec.ts index d056159d7d..4ee544cc5e 100644 --- a/packages/server/src/integrations/tests/sql.spec.ts +++ b/packages/server/src/integrations/tests/sql.spec.ts @@ -66,7 +66,15 @@ function generateUpdateJson({ body = {}, filters = {}, meta = {}, +}: { + table: string + body?: any + filters?: any + meta?: any }): QueryJson { + if (!meta.table) { + meta.table = table + } return { endpoint: endpoint(table, "UPDATE"), filters, diff --git a/packages/types/src/sdk/search.ts b/packages/types/src/sdk/search.ts index 6cac76e01d..0b93fb9215 100644 --- a/packages/types/src/sdk/search.ts +++ b/packages/types/src/sdk/search.ts @@ -91,7 +91,7 @@ export interface QueryJson { body?: Row | Row[] table?: Table meta: { - table?: Table + table: Table tables?: Record renamed?: RenameColumn } From 2efbd6726c5878fc2e99793427ca6079e4ae8265 Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Tue, 16 Apr 2024 17:28:13 +0100 Subject: [PATCH 4/4] Removing meta from test API, it cannot be supplied, it is an internal property. --- .../server/src/api/routes/tests/queries/generic-sql.spec.ts | 3 --- packages/server/src/tests/utilities/api/datasource.ts | 5 ++++- 2 files changed, 4 insertions(+), 4 deletions(-) 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 718f18203c..7790f909e7 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 @@ -755,9 +755,6 @@ describe.each( name: "two", }, }, - meta: { - table: config.table!, - }, }) expect(res).toHaveLength(1) expect(res[0]).toEqual({ diff --git a/packages/server/src/tests/utilities/api/datasource.ts b/packages/server/src/tests/utilities/api/datasource.ts index 0296f58f7d..0362a25940 100644 --- a/packages/server/src/tests/utilities/api/datasource.ts +++ b/packages/server/src/tests/utilities/api/datasource.ts @@ -60,7 +60,10 @@ export class DatasourceAPI extends TestAPI { }) } - query = async (query: QueryJson, expectations?: Expectations) => { + query = async ( + query: Omit, + expectations?: Expectations + ) => { return await this._post(`/api/datasources/query`, { body: query, expectations,