From 956df101e84891aeef9e7c49a22772e03ae33ef9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 24 Sep 2024 11:16:42 +0100 Subject: [PATCH] PR comments and type improvements. --- .../src/api/routes/tests/search.spec.ts | 8 +++++++- .../src/sdk/app/tables/external/index.ts | 6 ++++-- .../src/sdk/app/tables/external/utils.ts | 20 +++++++++++++------ .../server/src/tests/utilities/structures.ts | 10 ++++------ 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 4a695edc06..4125d7bf5b 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -77,6 +77,7 @@ describe.each([ table = await createTable( { name: { name: "name", type: FieldType.STRING }, + //@ts-ignore - API accepts this structure, will build out rest of definition productCat: { type: FieldType.LINK, relationshipType: type, @@ -2297,12 +2298,17 @@ describe.each([ } }) - it("can only pull 500 related rows", async () => { + it("can only pull 10 related rows", async () => { await withCoreEnv({ SQL_MAX_RELATED_ROWS: "10" }, async () => { const response = await expectQuery({}).toContain([{ name: "foo" }]) expect(response.rows[0].productCat).toBeArrayOfSize(10) }) }) + + it("can pull max rows when env not set (defaults to 500)", async () => { + const response = await expectQuery({}).toContain([{ name: "foo" }]) + expect(response.rows[0].productCat).toBeArrayOfSize(11) + }) }) ;(isSqs || isLucene) && describe("relations to same table", () => { diff --git a/packages/server/src/sdk/app/tables/external/index.ts b/packages/server/src/sdk/app/tables/external/index.ts index 913eae6d1f..e374e70c87 100644 --- a/packages/server/src/sdk/app/tables/external/index.ts +++ b/packages/server/src/sdk/app/tables/external/index.ts @@ -204,7 +204,9 @@ export async function save( // add in the new table for relationship purposes tables[tableToSave.name] = tableToSave - cleanupRelationships(tableToSave, tables, oldTable) + if (oldTable) { + cleanupRelationships(tableToSave, tables, { oldTable }) + } const operation = tableId ? Operation.UPDATE_TABLE : Operation.CREATE_TABLE await makeTableRequest( @@ -259,7 +261,7 @@ export async function destroy(datasourceId: string, table: Table) { const operation = Operation.DELETE_TABLE if (tables) { await makeTableRequest(datasource, operation, table, tables) - cleanupRelationships(table, tables) + cleanupRelationships(table, tables, { deleting: true }) delete tables[table.name] datasource.entities = tables } diff --git a/packages/server/src/sdk/app/tables/external/utils.ts b/packages/server/src/sdk/app/tables/external/utils.ts index f27c59dc5a..21ffa21053 100644 --- a/packages/server/src/sdk/app/tables/external/utils.ts +++ b/packages/server/src/sdk/app/tables/external/utils.ts @@ -20,17 +20,25 @@ import { cloneDeep } from "lodash/fp" export function cleanupRelationships( table: Table, tables: Record, - oldTable?: Table -) { - if (!oldTable) { - return - } + opts: { oldTable: Table } +): void +export function cleanupRelationships( + table: Table, + tables: Record, + opts: { deleting: boolean } +): void +export function cleanupRelationships( + table: Table, + tables: Record, + opts?: { oldTable?: Table; deleting?: boolean } +): void { + const oldTable = opts?.oldTable const tableToIterate = oldTable ? oldTable : table // clean up relationships in couch table schemas for (let [key, schema] of Object.entries(tableToIterate.schema)) { if ( schema.type === FieldType.LINK && - oldTable.schema[key] != null && + (opts?.deleting || oldTable?.schema[key] != null) && table.schema[key] == null ) { const schemaTableId = schema.tableId diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index 2e501932b8..72cd31e383 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -600,10 +600,10 @@ export function fullSchemaWithoutLinks({ allRequired, }: { allRequired?: boolean -}) { - const schema: { - [type in Exclude]: FieldSchema & { type: type } - } = { +}): { + [type in Exclude]: FieldSchema & { type: type } +} { + return { [FieldType.STRING]: { name: "string", type: FieldType.STRING, @@ -741,8 +741,6 @@ export function fullSchemaWithoutLinks({ }, }, } - - return schema } export function basicAttachment() { return {