From 4ab3aef02058f63f2eff700cd6822f472959bf12 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 10 Jul 2024 11:05:06 +0100 Subject: [PATCH] PR comments. --- .../src/api/controllers/row/utils/sqlUtils.ts | 9 +- .../src/api/routes/tests/search.spec.ts | 119 ++++++++++++------ .../server/src/sdk/app/tables/internal/sqs.ts | 4 +- 3 files changed, 91 insertions(+), 41 deletions(-) diff --git a/packages/server/src/api/controllers/row/utils/sqlUtils.ts b/packages/server/src/api/controllers/row/utils/sqlUtils.ts index 0d64113788..32124fa79d 100644 --- a/packages/server/src/api/controllers/row/utils/sqlUtils.ts +++ b/packages/server/src/api/controllers/row/utils/sqlUtils.ts @@ -24,9 +24,11 @@ export function isManyToMany( function isCorrectRelationship( relationship: RelationshipsJson, + table1: Table, + table2: Table, row: Row ): boolean { - const junctionTableId = relationship.through! + const junctionTableId = generateJunctionTableID(table1._id!, table2._id!) const possibleColumns = [ `${junctionTableId}.doc1.fieldName`, `${junctionTableId}.doc2.fieldName`, @@ -76,7 +78,10 @@ export async function updateRelationshipColumns( if (!linked._id) { continue } - if (!opts?.sqs || isCorrectRelationship(relationship, row)) { + if ( + !opts?.sqs || + isCorrectRelationship(relationship, table, linkedTable, row) + ) { columns[relationship.column] = linked } } diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 9c76d90e24..d27d0e677c 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -2085,57 +2085,104 @@ describe.each([ }) }) - isInternal && - describe("relations to same table", () => { - let relatedTable: Table, relatedRows: Row[] + describe("relations to same table", () => { + let relatedTable: Table, relatedRows: Row[] - beforeAll(async () => { - relatedTable = await createTable( - { - name: { name: "name", type: FieldType.STRING }, - }, - "productCategory" - ) - table = await createTable({ + beforeAll(async () => { + relatedTable = await createTable( + { name: { name: "name", type: FieldType.STRING }, - related1: { - type: FieldType.LINK, - name: "related1", - fieldName: "main1", - tableId: relatedTable._id!, - relationshipType: RelationshipType.MANY_TO_MANY, - }, - related2: { - type: FieldType.LINK, - name: "related2", - fieldName: "main2", - tableId: relatedTable._id!, - relationshipType: RelationshipType.MANY_TO_MANY, - }, - }) - relatedRows = await Promise.all([ - config.api.row.save(relatedTable._id!, { name: "foo" }), - config.api.row.save(relatedTable._id!, { name: "bar" }), - ]) - await config.api.row.save(table._id!, { + }, + "productCategory" + ) + table = await createTable({ + name: { name: "name", type: FieldType.STRING }, + related1: { + type: FieldType.LINK, + name: "related1", + fieldName: "main1", + tableId: relatedTable._id!, + relationshipType: RelationshipType.MANY_TO_MANY, + }, + related2: { + type: FieldType.LINK, + name: "related2", + fieldName: "main2", + tableId: relatedTable._id!, + relationshipType: RelationshipType.MANY_TO_MANY, + }, + }) + relatedRows = await Promise.all([ + config.api.row.save(relatedTable._id!, { name: "foo" }), + config.api.row.save(relatedTable._id!, { name: "bar" }), + config.api.row.save(relatedTable._id!, { name: "baz" }), + config.api.row.save(relatedTable._id!, { name: "boo" }), + ]) + await Promise.all([ + config.api.row.save(table._id!, { name: "test", related1: [relatedRows[0]._id!], related2: [relatedRows[1]._id!], - }) + }), + config.api.row.save(table._id!, { + name: "test2", + related1: [relatedRows[2]._id!], + related2: [relatedRows[3]._id!], + }), + ]) + }) + + it("should be able to relate to same table", async () => { + await expectSearch({ + query: {}, + }).toContainExactly([ + { + name: "test", + related1: [{ _id: relatedRows[0]._id }], + related2: [{ _id: relatedRows[1]._id }], + }, + { + name: "test2", + related1: [{ _id: relatedRows[2]._id }], + related2: [{ _id: relatedRows[3]._id }], + }, + ]) + }) + + isSqs && + it("should be able to filter down to second row with equal", async () => { + await expectSearch({ + query: { + equal: { + ["related1.name"]: "baz", + }, + }, + }).toContainExactly([ + { + name: "test2", + related1: [{ _id: relatedRows[2]._id }], + }, + ]) }) - it("should be able to relate to same table", async () => { + isSqs && + it("should be able to filter down to first row with not equal", async () => { await expectSearch({ - query: {}, + query: { + notEqual: { + ["1:related2.name"]: "bar", + ["2:related2.name"]: "baz", + ["3:related2.name"]: "boo", + }, + }, }).toContainExactly([ { name: "test", related1: [{ _id: relatedRows[0]._id }], - related2: [{ _id: relatedRows[1]._id }], }, ]) }) - }) + }) isInternal && describe("no column error backwards compat", () => { diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index 706e0ca167..fc0ee8fc0b 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -176,12 +176,10 @@ export async function addTable(table: Table) { export async function removeTable(table: Table) { const db = context.getAppDB() try { - let response = await Promise.all([ + const [tables, definition] = await Promise.all([ tablesSdk.getAllInternalTables(), db.get(SQLITE_DESIGN_DOC_ID), ]) - const tables: Table[] = response[0], - definition: SQLiteDefinition = response[1] const tableIds = tables .map(tbl => tbl._id!) .filter(id => !id.includes(table._id!))