mirror of
https://github.com/typicode/json-server.git
synced 2025-08-03 04:12:22 +08:00
fix: filtering with multiple conditions
This commit is contained in:
@ -95,202 +95,215 @@ 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
|
||||||
}[] = [
|
}[] = [
|
||||||
{
|
{
|
||||||
name: POSTS,
|
name: POSTS,
|
||||||
res: [post1, post2, post3],
|
res: [post1, post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { id: post1.id },
|
||||||
|
res: [post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { id: UNKNOWN_ID },
|
||||||
|
res: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views: post1.views.toString() },
|
||||||
|
res: [post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { 'author.name': post1.author.name },
|
||||||
|
res: [post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { 'tags[0]': 'foo' },
|
||||||
|
res: [post1, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { id: UNKNOWN_ID, views: post1.views.toString() },
|
||||||
|
res: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_ne: post1.views.toString() },
|
||||||
|
res: [post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_lt: (post1.views + 1).toString() },
|
||||||
|
res: [post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_lt: post1.views.toString() },
|
||||||
|
res: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_lte: post1.views.toString() },
|
||||||
|
res: [post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_gt: post1.views.toString() },
|
||||||
|
res: [post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_gt: (post1.views - 1).toString() },
|
||||||
|
res: [post1, post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_gte: post1.views.toString() },
|
||||||
|
res: [post1, post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: {
|
||||||
|
views_gt: post1.views.toString(),
|
||||||
|
views_lt: post3.views.toString(),
|
||||||
},
|
},
|
||||||
{
|
res: [post2],
|
||||||
name: POSTS,
|
},
|
||||||
params: { id: post1.id },
|
{
|
||||||
res: [post1],
|
data: { posts: [post3, post1, post2] },
|
||||||
|
name: POSTS,
|
||||||
|
params: { _sort: 'views' },
|
||||||
|
res: [post1, post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: { posts: [post3, post1, post2] },
|
||||||
|
name: POSTS,
|
||||||
|
params: { _sort: '-views' },
|
||||||
|
res: [post3, post2, post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: { posts: [post3, post1, post2] },
|
||||||
|
name: POSTS,
|
||||||
|
params: { _sort: '-views,id' },
|
||||||
|
res: [post3, post2, post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { published: 'true' },
|
||||||
|
res: [post1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { published: 'false' },
|
||||||
|
res: [post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { views_lt: post3.views.toString(), published: 'false' },
|
||||||
|
res: [post2],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { _start: 0, _end: 2 },
|
||||||
|
res: [post1, post2],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { _start: 1, _end: 3 },
|
||||||
|
res: [post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { _start: 0, _limit: 2 },
|
||||||
|
res: [post1, post2],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { _start: 1, _limit: 2 },
|
||||||
|
res: [post2, post3],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { _page: 1, _per_page: 2 },
|
||||||
|
res: {
|
||||||
|
first: 1,
|
||||||
|
last: 2,
|
||||||
|
prev: null,
|
||||||
|
next: 2,
|
||||||
|
pages: 2,
|
||||||
|
items,
|
||||||
|
data: [post1, post2],
|
||||||
},
|
},
|
||||||
{
|
},
|
||||||
name: POSTS,
|
{
|
||||||
params: { id: UNKNOWN_ID },
|
name: POSTS,
|
||||||
res: [],
|
params: { _page: 2, _per_page: 2 },
|
||||||
|
res: {
|
||||||
|
first: 1,
|
||||||
|
last: 2,
|
||||||
|
prev: 1,
|
||||||
|
next: null,
|
||||||
|
pages: 2,
|
||||||
|
items,
|
||||||
|
data: [post3],
|
||||||
},
|
},
|
||||||
{
|
},
|
||||||
name: POSTS,
|
{
|
||||||
params: { views: post1.views.toString() },
|
name: POSTS,
|
||||||
res: [post1],
|
params: { _page: 3, _per_page: 2 },
|
||||||
|
res: {
|
||||||
|
first: 1,
|
||||||
|
last: 2,
|
||||||
|
prev: 1,
|
||||||
|
next: null,
|
||||||
|
pages: 2,
|
||||||
|
items,
|
||||||
|
data: [post3],
|
||||||
},
|
},
|
||||||
{
|
},
|
||||||
name: POSTS,
|
{
|
||||||
params: { 'author.name': post1.author.name },
|
name: POSTS,
|
||||||
res: [post1],
|
params: { _page: 2, _per_page: 1 },
|
||||||
|
res: {
|
||||||
|
first: 1,
|
||||||
|
last: 3,
|
||||||
|
prev: 1,
|
||||||
|
next: 3,
|
||||||
|
pages: 3,
|
||||||
|
items,
|
||||||
|
data: [post2],
|
||||||
},
|
},
|
||||||
{
|
},
|
||||||
name: POSTS,
|
{
|
||||||
params: { 'tags[0]': 'foo' },
|
name: POSTS,
|
||||||
res: [post1, post3],
|
params: { _embed: ['comments'] },
|
||||||
},
|
res: [
|
||||||
{
|
{ ...post1, comments: [comment1] },
|
||||||
name: POSTS,
|
{ ...post2, comments: [] },
|
||||||
params: { id: UNKNOWN_ID, views: post1.views.toString() },
|
{ ...post3, comments: [] },
|
||||||
res: [],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: POSTS,
|
name: COMMENTS,
|
||||||
params: { views_ne: post1.views.toString() },
|
params: { _embed: ['post'] },
|
||||||
res: [post2, post3],
|
res: [{ ...comment1, post: post1 }],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: POSTS,
|
name: UNKNOWN_RESOURCE,
|
||||||
params: { views_lt: (post1.views + 1).toString() },
|
res: undefined,
|
||||||
res: [post1],
|
},
|
||||||
},
|
{
|
||||||
{
|
name: OBJECT,
|
||||||
name: POSTS,
|
res: obj,
|
||||||
params: { views_lt: post1.views.toString() },
|
},
|
||||||
res: [],
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { views_lte: post1.views.toString() },
|
|
||||||
res: [post1],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { views_gt: post1.views.toString() },
|
|
||||||
res: [post2, post3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { views_gt: (post1.views - 1).toString() },
|
|
||||||
res: [post1, post2, post3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { views_gte: post1.views.toString() },
|
|
||||||
res: [post1, post2, post3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: { posts: [post3, post1, post2] },
|
|
||||||
name: POSTS,
|
|
||||||
params: { _sort: 'views' },
|
|
||||||
res: [post1, post2, post3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: { posts: [post3, post1, post2] },
|
|
||||||
name: POSTS,
|
|
||||||
params: { _sort: '-views' },
|
|
||||||
res: [post3, post2, post1],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: { posts: [post3, post1, post2] },
|
|
||||||
name: POSTS,
|
|
||||||
params: { _sort: '-views,id' },
|
|
||||||
res: [post3, post2, post1],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { published: 'true' },
|
|
||||||
res: [post1],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { published: 'false' },
|
|
||||||
res: [post2, post3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _start: 0, _end: 2 },
|
|
||||||
res: [post1, post2],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _start: 1, _end: 3 },
|
|
||||||
res: [post2, post3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _start: 0, _limit: 2 },
|
|
||||||
res: [post1, post2],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _start: 1, _limit: 2 },
|
|
||||||
res: [post2, post3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _page: 1, _per_page: 2 },
|
|
||||||
res: {
|
|
||||||
first: 1,
|
|
||||||
last: 2,
|
|
||||||
prev: null,
|
|
||||||
next: 2,
|
|
||||||
pages: 2,
|
|
||||||
items,
|
|
||||||
data: [post1, post2],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _page: 2, _per_page: 2 },
|
|
||||||
res: {
|
|
||||||
first: 1,
|
|
||||||
last: 2,
|
|
||||||
prev: 1,
|
|
||||||
next: null,
|
|
||||||
pages: 2,
|
|
||||||
items,
|
|
||||||
data: [post3],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _page: 3, _per_page: 2 },
|
|
||||||
res: {
|
|
||||||
first: 1,
|
|
||||||
last: 2,
|
|
||||||
prev: 1,
|
|
||||||
next: null,
|
|
||||||
pages: 2,
|
|
||||||
items,
|
|
||||||
data: [post3],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _page: 2, _per_page: 1 },
|
|
||||||
res: {
|
|
||||||
first: 1,
|
|
||||||
last: 3,
|
|
||||||
prev: 1,
|
|
||||||
next: 3,
|
|
||||||
pages: 3,
|
|
||||||
items,
|
|
||||||
data: [post2],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: POSTS,
|
|
||||||
params: { _embed: ['comments'] },
|
|
||||||
res: [
|
|
||||||
{ ...post1, comments: [comment1] },
|
|
||||||
{ ...post2, comments: [] },
|
|
||||||
{ ...post3, comments: [] },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: COMMENTS,
|
|
||||||
params: { _embed: ['post'] },
|
|
||||||
res: [{ ...comment1, post: post1 }],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: UNKNOWN_RESOURCE,
|
|
||||||
res: undefined,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: OBJECT,
|
|
||||||
res: obj,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
for (const tc of arr) {
|
for (const tc of arr) {
|
||||||
await t.test(`${tc.name} ${JSON.stringify(tc.params)}`, () => {
|
await t.test(`${tc.name} ${JSON.stringify(tc.params)}`, () => {
|
||||||
if (tc.data) {
|
if (tc.data) {
|
||||||
|
@ -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
|
||||||
|
Reference in New Issue
Block a user