From f464a8048461c1952102950655f056409c2e5452 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 21 Nov 2024 18:09:21 +0000 Subject: [PATCH 1/2] Adding helper to create relationship to datasource API in tests. --- .../src/tests/utilities/api/datasource.ts | 66 +++++++++++++++++-- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/packages/server/src/tests/utilities/api/datasource.ts b/packages/server/src/tests/utilities/api/datasource.ts index 67484a688a..03ff6ac8d0 100644 --- a/packages/server/src/tests/utilities/api/datasource.ts +++ b/packages/server/src/tests/utilities/api/datasource.ts @@ -1,14 +1,17 @@ import { - Datasource, - VerifyDatasourceRequest, - CreateDatasourceResponse, - UpdateDatasourceResponse, - UpdateDatasourceRequest, - QueryJson, BuildSchemaFromSourceResponse, + CreateDatasourceResponse, + Datasource, FetchDatasourceInfoResponse, + FieldType, + QueryJson, + RelationshipType, + UpdateDatasourceRequest, + UpdateDatasourceResponse, + VerifyDatasourceRequest, } from "@budibase/types" import { Expectations, TestAPI } from "./base" +import { sql } from "@budibase/backend-core" export class DatasourceAPI extends TestAPI { create = async ( @@ -103,4 +106,55 @@ export class DatasourceAPI extends TestAPI { } ) } + + addExistingRelationship = async ( + tableId1: string, + tableId2: string, + relationshipNameInTable1: string, + relationshipNameInTable2: string, + primaryKey: string, + foreignKey: string, + expectations?: Expectations + ) => { + const tableInfo1 = sql.utils.breakExternalTableId(tableId1), + tableInfo2 = sql.utils.breakExternalTableId(tableId2) + if (tableInfo1.datasourceId !== tableInfo2.datasourceId) { + throw new Error( + "Tables are in different datasources, cannot create relationship." + ) + } + const datasource = await this.get(tableInfo1.datasourceId) + const table1 = datasource.entities?.[tableInfo1.tableName], + table2 = datasource.entities?.[tableInfo2.tableName] + if (!table1 || !table2) { + throw new Error( + "Both tables not found in datasource, cannot create relationship." + ) + } + + const table1HasPrimary = table1.primary!.includes(primaryKey) + table1.schema[relationshipNameInTable1] = { + type: FieldType.LINK, + name: relationshipNameInTable1, + tableId: tableId2, + relationshipType: table1HasPrimary + ? RelationshipType.MANY_TO_ONE + : RelationshipType.ONE_TO_MANY, + fieldName: table1HasPrimary ? foreignKey : primaryKey, + foreignKey: table1HasPrimary ? primaryKey : foreignKey, + main: table1HasPrimary, + } + table2.schema[relationshipNameInTable2] = { + type: FieldType.LINK, + name: relationshipNameInTable2, + tableId: tableId1, + relationshipType: table1HasPrimary + ? RelationshipType.ONE_TO_MANY + : RelationshipType.MANY_TO_ONE, + fieldName: table1HasPrimary ? primaryKey : foreignKey, + foreignKey: table1HasPrimary ? foreignKey : primaryKey, + main: !table1HasPrimary, + } + return await this.update(datasource, expectations) + } } From 23f29f9381e21e47f55cd073787c9f74d8686b98 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 21 Nov 2024 18:41:04 +0000 Subject: [PATCH 2/2] Adding relationship helper for defining existing relationships. --- .../server/src/api/routes/tests/row.spec.ts | 44 ++++++++------ .../src/tests/utilities/api/datasource.ts | 59 +++++++++---------- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index f0dc27708c..a7712a7137 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -3496,18 +3496,18 @@ if (descriptions.length) { }) }) - !isInternal && + if (!isInternal) { describe("bigint ids", () => { - let table: Table - let relatedTable: Table + let table: Table, relatedTable: Table + let tableName: string, relatedTableName: string beforeAll(async () => { - const tableName = generator.guid().substring(0, 10) + tableName = generator.guid().substring(0, 10) await client!.schema.createTable(tableName, table => { table.bigIncrements("id").primary() }) - const relatedTableName = generator.guid().substring(0, 10) + relatedTableName = generator.guid().substring(0, 10) await client!.schema.createTable(relatedTableName, table => { table.increments("id").primary() table @@ -3525,17 +3525,16 @@ if (descriptions.length) { table = tables.find(t => t.name === tableName)! relatedTable = tables.find(t => t.name === relatedTableName)! - await config.api.table.save({ - ...table, - schema: { - ...table.schema, - related: { - name: "related", - type: FieldType.LINK, - tableId: relatedTable._id!, - fieldName: "tableid", - relationshipType: RelationshipType.ONE_TO_MANY, - }, + await config.api.datasource.addExistingRelationship({ + one: { + tableId: relatedTable._id!, + relationshipName: "one", + foreignKey: "tableid", + }, + many: { + tableId: table._id!, + relationshipName: "many", + primaryKey: "id", }, }) }) @@ -3545,9 +3544,20 @@ if (descriptions.length) { await config.api.row.save(relatedTable._id!, { tableid: row.id }) const { rows } = await config.api.row.search(table._id!) - expect(rows).toEqual([]) + expect(rows.length).toEqual(1) + expect(rows[0]).toEqual( + expect.objectContaining({ + many: [ + { + _id: "%5B1%5D", + primaryDisplay: 1, + }, + ], + }) + ) }) }) + } } ) } diff --git a/packages/server/src/tests/utilities/api/datasource.ts b/packages/server/src/tests/utilities/api/datasource.ts index 03ff6ac8d0..87f03c8a6f 100644 --- a/packages/server/src/tests/utilities/api/datasource.ts +++ b/packages/server/src/tests/utilities/api/datasource.ts @@ -108,52 +108,47 @@ export class DatasourceAPI extends TestAPI { } addExistingRelationship = async ( - tableId1: string, - tableId2: string, - relationshipNameInTable1: string, - relationshipNameInTable2: string, - primaryKey: string, - foreignKey: string, + { + one, + many, + }: { + one: { tableId: string; relationshipName: string; foreignKey: string } + many: { tableId: string; relationshipName: string; primaryKey: string } + }, expectations?: Expectations ) => { - const tableInfo1 = sql.utils.breakExternalTableId(tableId1), - tableInfo2 = sql.utils.breakExternalTableId(tableId2) - if (tableInfo1.datasourceId !== tableInfo2.datasourceId) { + const oneTableInfo = sql.utils.breakExternalTableId(one.tableId), + manyTableInfo = sql.utils.breakExternalTableId(many.tableId) + if (oneTableInfo.datasourceId !== manyTableInfo.datasourceId) { throw new Error( "Tables are in different datasources, cannot create relationship." ) } - const datasource = await this.get(tableInfo1.datasourceId) - const table1 = datasource.entities?.[tableInfo1.tableName], - table2 = datasource.entities?.[tableInfo2.tableName] - if (!table1 || !table2) { + const datasource = await this.get(oneTableInfo.datasourceId) + const oneTable = datasource.entities?.[oneTableInfo.tableName], + manyTable = datasource.entities?.[manyTableInfo.tableName] + if (!oneTable || !manyTable) { throw new Error( "Both tables not found in datasource, cannot create relationship." ) } - const table1HasPrimary = table1.primary!.includes(primaryKey) - table1.schema[relationshipNameInTable1] = { + manyTable.schema[many.relationshipName] = { type: FieldType.LINK, - name: relationshipNameInTable1, - tableId: tableId2, - relationshipType: table1HasPrimary - ? RelationshipType.MANY_TO_ONE - : RelationshipType.ONE_TO_MANY, - fieldName: table1HasPrimary ? foreignKey : primaryKey, - foreignKey: table1HasPrimary ? primaryKey : foreignKey, - main: table1HasPrimary, + name: many.relationshipName, + tableId: oneTable._id!, + relationshipType: RelationshipType.MANY_TO_ONE, + fieldName: one.foreignKey, + foreignKey: many.primaryKey, + main: true, } - table2.schema[relationshipNameInTable2] = { + oneTable.schema[one.relationshipName] = { type: FieldType.LINK, - name: relationshipNameInTable2, - tableId: tableId1, - relationshipType: table1HasPrimary - ? RelationshipType.ONE_TO_MANY - : RelationshipType.MANY_TO_ONE, - fieldName: table1HasPrimary ? primaryKey : foreignKey, - foreignKey: table1HasPrimary ? foreignKey : primaryKey, - main: !table1HasPrimary, + name: one.relationshipName, + tableId: manyTable._id!, + relationshipType: RelationshipType.ONE_TO_MANY, + fieldName: many.primaryKey, + foreignKey: one.foreignKey, } return await this.update(datasource, expectations) }