From 3b40db5db02852e3838a3f6618746e4a7e27a725 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 6 Aug 2024 10:30:19 +0200 Subject: [PATCH 1/3] Run tests for in-memory --- packages/server/src/api/routes/tests/search.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 6ed9245ee6..e3f17c36cb 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -2697,7 +2697,7 @@ describe.each([ }) }) - isSql && + !isLucene && describe("$and", () => { beforeAll(async () => { table = await createTable({ @@ -2771,7 +2771,7 @@ describe.each([ }) }) - isSql && + !isLucene && describe("$or", () => { beforeAll(async () => { table = await createTable({ From 5caf20635aaedc40cd8afae7c3eaaddcce9c951d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 6 Aug 2024 17:34:40 +0200 Subject: [PATCH 2/3] Implement in memory filter --- packages/shared-core/src/filters.ts | 39 +++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index f4e40f50cb..bad6d36cba 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -464,14 +464,18 @@ export const runQuery = (docs: Record[], query: SearchFilters) => { ) => (doc: Record) => { for (const [key, testValue] of Object.entries(query[type] || {})) { - const result = test(deepGet(doc, removeKeyNumbering(key)), testValue) + const valueToCheck = + type === LogicalOperator.AND || type === LogicalOperator.OR + ? doc + : deepGet(doc, removeKeyNumbering(key)) + const result = test(valueToCheck, testValue) if (query.allOr && result) { return true } else if (!query.allOr && !result) { return false } } - return true + return !query.allOr } const stringMatch = match( @@ -674,15 +678,34 @@ export const runQuery = (docs: Record[], query: SearchFilters) => { const and = match( LogicalOperator.AND, - (_docValue: Record, _testValue: any) => { - // TODO - return false + (docValue: Record, conditions: any) => { + if (!conditions.length) { + return false + } + for (const condition of conditions) { + const matchesCondition = runQuery([docValue], condition) + if (!matchesCondition.length) { + return false + } + } + return true } ) const or = match( - LogicalOperator.AND, - (_docValue: Record, _testValue: any) => { - // TODO + LogicalOperator.OR, + (docValue: Record, conditions: any) => { + if (!conditions.length) { + return false + } + for (const condition of conditions) { + const matchesCondition = runQuery([docValue], { + ...condition, + allOr: true, + }) + if (matchesCondition.length) { + return true + } + } return false } ) From 0667449463161e37a81080d3a03596ffd40948ce Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 6 Aug 2024 18:25:35 +0200 Subject: [PATCH 3/3] Types --- packages/shared-core/src/filters.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index bad6d36cba..e16e27fd30 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -678,7 +678,7 @@ export const runQuery = (docs: Record[], query: SearchFilters) => { const and = match( LogicalOperator.AND, - (docValue: Record, conditions: any) => { + (docValue: Record, conditions: SearchFilters[]) => { if (!conditions.length) { return false } @@ -693,7 +693,7 @@ export const runQuery = (docs: Record[], query: SearchFilters) => { ) const or = match( LogicalOperator.OR, - (docValue: Record, conditions: any) => { + (docValue: Record, conditions: SearchFilters[]) => { if (!conditions.length) { return false }