Implement in memory filter

This commit is contained in:
Adria Navarro 2024-08-06 17:34:40 +02:00
parent 3b40db5db0
commit 5caf20635a
1 changed files with 31 additions and 8 deletions

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: any) => {
// 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: any) => {
// 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
} }
) )