From bd17770e4ec3cc9414ca97e4ce3de3dc923eb9b4 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 27 Nov 2024 12:21:27 +0000 Subject: [PATCH] Fix _some_ sqs problems. --- .../server/src/api/routes/tests/row.spec.ts | 6 +++--- .../src/sdk/app/rows/search/internal/sqs.ts | 19 +++++-------------- packages/server/src/sdk/app/rows/utils.ts | 16 ++++++++++++---- packages/server/src/sdk/app/tables/getters.ts | 1 + 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 1ab12feebb..6756d2e462 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -3268,7 +3268,7 @@ if (descriptions.length) { formula: { name: "formula", type: FieldType.FORMULA, - formula: formula, + formula, responseType: opts?.responseType, formulaType: opts?.formulaType || FormulaType.DYNAMIC, }, @@ -3276,7 +3276,7 @@ if (descriptions.length) { }) } - it("should be able to search for rows containing formulas", async () => { + it.only("should be able to search for rows containing formulas", async () => { const { rows } = await config.api.row.search(table._id!) expect(rows.length).toBe(1) expect(rows[0].links.length).toBe(1) @@ -3333,7 +3333,7 @@ if (descriptions.length) { }) isInternal && - it.only("should coerce a static handlebars formula", async () => { + it("should coerce a static handlebars formula", async () => { await updateFormulaColumn(encodeJS("return 1"), { responseType: FieldType.NUMBER, formulaType: FormulaType.STATIC, 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 aec934ae4e..1a55fdf99c 100644 --- a/packages/server/src/sdk/app/rows/search/internal/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/internal/sqs.ts @@ -180,19 +180,6 @@ function cleanupFilters(filters: SearchFilters, allTables: Table[]) { return filters } -function buildTableMap(tables: Table[]) { - const tableMap: Record = {} - for (let table of tables) { - // update the table name, should never query by name for SQLite - table.originalName = table.name - table.name = table._id! - // need a primary for sorting, lookups etc - table.primary = ["_id"] - tableMap[table._id!] = table - } - return tableMap -} - // table is only needed to handle relationships function reverseUserColumnMapping(rows: Row[], table?: Table) { const prefixLength = USER_COLUMN_PREFIX.length @@ -315,7 +302,11 @@ export async function search( } const allTables = await sdk.tables.getAllInternalTables() - const allTablesMap = buildTableMap(allTables) + const allTablesMap = allTables.reduce((acc, table) => { + acc[table._id!] = table + return acc + }, {} as Record) + // make sure we have the mapped/latest table if (table._id) { table = allTablesMap[table._id] diff --git a/packages/server/src/sdk/app/rows/utils.ts b/packages/server/src/sdk/app/rows/utils.ts index f7646a3a85..eb6859c58b 100644 --- a/packages/server/src/sdk/app/rows/utils.ts +++ b/packages/server/src/sdk/app/rows/utils.ts @@ -73,6 +73,17 @@ export function processRowCountResponse( } } +function processInternalTables(tables: Table[]) { + const tableMap: Record = {} + for (let table of tables) { + // update the table name, should never query by name for SQLite + table.originalName = table.name + table.name = table._id! + tableMap[table._id!] = table + } + return tableMap +} + export async function enrichQueryJson( json: QueryJson ): Promise { @@ -85,10 +96,7 @@ export async function enrichQueryJson( if (datasource) { tables = datasource.entities || {} } else { - tables = {} - for (const table of await sdk.tables.getAllInternalTables()) { - tables[table._id!] = table - } + tables = processInternalTables(await sdk.tables.getAllInternalTables()) } const table = tables[json.endpoint.entityId] diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 1ad82b8e42..79091ea660 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -46,6 +46,7 @@ export async function processTable(table: Table): Promise { const processed: Table = { ...table, type: "table", + primary: ["_id"], // internal tables must always use _id as primary key sourceId: table.sourceId || INTERNAL_TABLE_SOURCE_ID, sourceType: TableSourceType.INTERNAL, sql: true,