From efe66186f4037c3c519b3f1afc23d4119c74c553 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 9 Apr 2024 12:37:03 +0100 Subject: [PATCH] Last PR comment - fixing issue with lucene test case. --- .../backend-core/src/db/tests/lucene.spec.ts | 36 +++++++++++++------ .../server/src/sdk/app/tables/internal/sqs.ts | 3 ++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/backend-core/src/db/tests/lucene.spec.ts b/packages/backend-core/src/db/tests/lucene.spec.ts index 3158286da5..c41bdf88d1 100644 --- a/packages/backend-core/src/db/tests/lucene.spec.ts +++ b/packages/backend-core/src/db/tests/lucene.spec.ts @@ -5,25 +5,35 @@ import { EmptyFilterOption, SortOrder, SortType, + DocumentType, + SEPARATOR, } from "@budibase/types" import { fullSearch, paginatedSearch, QueryBuilder } from "../lucene" const INDEX_NAME = "main" -const TABLE_ID = "" +const TABLE_ID = DocumentType.TABLE + SEPARATOR + newid() const index = `function(doc) { - let props = ["property", "number", "array"] - for (let key of props) { - if (Array.isArray(doc[key])) { - for (let val of doc[key]) { + if (!doc._id.startsWith("ro_")) { + return + } + let keys = Object.keys(doc).filter(key => !key.startsWith("_")) + for (let key of keys) { + const value = doc[key] + if (Array.isArray(value)) { + for (let val of value) { index(key, val) } - } else if (doc[key]) { - index(key, doc[key]) + } else if (value) { + index(key, value) } } }` +function rowId(id?: string) { + return DocumentType.ROW + SEPARATOR + (id || newid()) +} + describe("lucene", () => { let db: Database, dbName: string @@ -32,17 +42,20 @@ describe("lucene", () => { // create the DB for testing db = getDB(dbName) await db.put({ - _id: newid(), + _id: rowId(), + tableId: TABLE_ID, property: "word", array: ["1", "4"], }) await db.put({ - _id: newid(), + _id: rowId(), + tableId: TABLE_ID, property: "word2", array: ["3", "1"], }) await db.put({ - _id: newid(), + _id: rowId(), + tableId: TABLE_ID, property: "word3", number: 1, array: ["1", "2"], @@ -254,7 +267,8 @@ describe("lucene", () => { docs = Array(QueryBuilder.maxLimit * 2.5) .fill(0) .map((_, i) => ({ - _id: i.toString().padStart(3, "0"), + _id: rowId(i.toString().padStart(3, "0")), + tableId: TABLE_ID, property: `value_${i.toString().padStart(3, "0")}`, array: [], })) diff --git a/packages/server/src/sdk/app/tables/internal/sqs.ts b/packages/server/src/sdk/app/tables/internal/sqs.ts index 1e8a60c5db..da947c62c2 100644 --- a/packages/server/src/sdk/app/tables/internal/sqs.ts +++ b/packages/server/src/sdk/app/tables/internal/sqs.ts @@ -38,6 +38,9 @@ const FieldTypeMap: Record = { function mapTable(table: Table): { [key: string]: SQLiteType } { const fields: Record = {} for (let [key, column] of Object.entries(table.schema)) { + if (!FieldTypeMap[column.type]) { + throw new Error(`Unable to map type "${column.type}" to SQLite type`) + } fields[key] = FieldTypeMap[column.type] } // there are some extra columns to map - add these in