fix: filtering with multiple conditions

This commit is contained in:
typicode
2024-08-19 15:02:36 +02:00
parent 6f32b96457
commit e6055e621d
2 changed files with 211 additions and 197 deletions

View File

@ -95,7 +95,7 @@ await test('find', async (t) => {
const arr: { const arr: {
data?: Data data?: Data
name: string name: string
params?: Parameters<Service["find"]>[1] params?: Parameters<Service['find']>[1]
res: Item | Item[] | PaginatedItems | undefined res: Item | Item[] | PaginatedItems | undefined
error?: Error error?: Error
}[] = [ }[] = [
@ -168,6 +168,14 @@ await test('find', async (t) => {
params: { views_gte: post1.views.toString() }, params: { views_gte: post1.views.toString() },
res: [post1, post2, post3], res: [post1, post2, post3],
}, },
{
name: POSTS,
params: {
views_gt: post1.views.toString(),
views_lt: post3.views.toString(),
},
res: [post2],
},
{ {
data: { posts: [post3, post1, post2] }, data: { posts: [post3, post1, post2] },
name: POSTS, name: POSTS,
@ -196,6 +204,11 @@ await test('find', async (t) => {
params: { published: 'false' }, params: { published: 'false' },
res: [post2, post3], res: [post2, post3],
}, },
{
name: POSTS,
params: { views_lt: post3.views.toString(), published: 'false' },
res: [post2],
},
{ {
name: POSTS, name: POSTS,
params: { _start: 0, _end: 2 }, params: { _start: 0, _end: 2 },

View File

@ -199,7 +199,7 @@ export class Service {
} }
// Convert query params to conditions // Convert query params to conditions
const conds: Record<string, [Condition, string | string[]]> = {} const conds: [string, Condition, string | string[]][] = []
for (const [key, value] of Object.entries(query)) { for (const [key, value] of Object.entries(query)) {
if (value === undefined || typeof value !== 'string') { if (value === undefined || typeof value !== 'string') {
continue continue
@ -209,7 +209,7 @@ export class Service {
const op = reArr?.at(1) const op = reArr?.at(1)
if (op && isCondition(op)) { if (op && isCondition(op)) {
const field = key.replace(re, '') const field = key.replace(re, '')
conds[field] = [op, value] conds.push([field, op, value])
continue continue
} }
if ( if (
@ -225,12 +225,13 @@ export class Service {
) { ) {
continue continue
} }
conds[key] = [Condition.default, value] conds.push([key, Condition.default, value])
} }
// Loop through conditions and filter items // Loop through conditions and filter items
const res = items.filter((item: Item) => { let filtered = items
for (const [key, [op, paramValue]] of Object.entries(conds)) { for (const [key, op, paramValue] of conds) {
filtered = filtered.filter((item: Item) => {
if (paramValue && !Array.isArray(paramValue)) { if (paramValue && !Array.isArray(paramValue)) {
// https://github.com/sindresorhus/dot-prop/issues/95 // https://github.com/sindresorhus/dot-prop/issues/95
const itemValue: unknown = getProperty(item, key) const itemValue: unknown = getProperty(item, key)
@ -308,13 +309,13 @@ export class Service {
} }
} }
} }
}
return true return true
}) })
}
// Sort // Sort
const sort = query._sort || '' const sort = query._sort || ''
const sorted = sortOn(res, sort.split(',')) const sorted = sortOn(filtered, sort.split(','))
// Slice // Slice
const start = query._start const start = query._start