Merge pull request #14331 from Budibase/BUDI-8506/in-memory-support-for-logical-operators

In-memory support for logical operators
This commit is contained in:
Adria Navarro 2024-08-06 18:26:09 +02:00 committed by GitHub
commit 99a026172b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 10 deletions

View File

@ -2697,7 +2697,7 @@ describe.each([
}) })
}) })
isSql && !isLucene &&
describe("$and", () => { describe("$and", () => {
beforeAll(async () => { beforeAll(async () => {
table = await createTable({ table = await createTable({
@ -2771,7 +2771,7 @@ describe.each([
}) })
}) })
isSql && !isLucene &&
describe("$or", () => { describe("$or", () => {
beforeAll(async () => { beforeAll(async () => {
table = await createTable({ table = await createTable({

View File

@ -464,14 +464,18 @@ export const runQuery = (docs: Record<string, any>[], query: SearchFilters) => {
) => ) =>
(doc: Record<string, any>) => { (doc: Record<string, any>) => {
for (const [key, testValue] of Object.entries(query[type] || {})) { 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) { if (query.allOr && result) {
return true return true
} else if (!query.allOr && !result) { } else if (!query.allOr && !result) {
return false return false
} }
} }
return true return !query.allOr
} }
const stringMatch = match( const stringMatch = match(
@ -674,15 +678,34 @@ export const runQuery = (docs: Record<string, any>[], query: SearchFilters) => {
const and = match( const and = match(
LogicalOperator.AND, LogicalOperator.AND,
(_docValue: Record<string, any>, _testValue: any) => { (docValue: Record<string, any>, conditions: SearchFilters[]) => {
// TODO if (!conditions.length) {
return false return false
} }
for (const condition of conditions) {
const matchesCondition = runQuery([docValue], condition)
if (!matchesCondition.length) {
return false
}
}
return true
}
) )
const or = match( const or = match(
LogicalOperator.AND, LogicalOperator.OR,
(_docValue: Record<string, any>, _testValue: any) => { (docValue: Record<string, any>, conditions: SearchFilters[]) => {
// TODO if (!conditions.length) {
return false
}
for (const condition of conditions) {
const matchesCondition = runQuery([docValue], {
...condition,
allOr: true,
})
if (matchesCondition.length) {
return true
}
}
return false return false
} }
) )