Guide
Filtering
데이터 필터링 및 쿼리 작성
필터링
01.software SDK는 강력한 쿼리 빌더를 제공합니다.
기본 쿼리
페이지네이션
const response = await client.from('products').find({
page: 1, // 페이지 번호 (기본: 1)
limit: 20 // 페이지당 항목 수 (기본: 20)
})
// 응답에서 페이지 정보 확인
if (response.success) {
const { pagination } = response
console.log(pagination?.totalDocs) // 전체 문서 수
console.log(pagination?.totalPages) // 전체 페이지 수
console.log(pagination?.hasNextPage) // 다음 페이지 여부
console.log(pagination?.hasPrevPage) // 이전 페이지 여부
}정렬
// 내림차순 (최신순)
const response = await client.from('products').find({
sort: '-createdAt'
})
// 오름차순
const response = await client.from('products').find({
sort: 'price'
})
// 여러 필드로 정렬
const response = await client.from('products').find({
sort: '-status,price'
})Where 조건
equals (같음)
const response = await client.from('products').find({
where: {
status: { equals: 'active' }
}
})not_equals (같지 않음)
const response = await client.from('products').find({
where: {
status: { not_equals: 'archived' }
}
})greater_than / less_than
const response = await client.from('products').find({
where: {
price: { greater_than: 10000 },
stock: { less_than: 100 }
}
})greater_than_equal / less_than_equal
const response = await client.from('products').find({
where: {
price: {
greater_than_equal: 10000,
less_than_equal: 50000
}
}
})in (포함)
const response = await client.from('products').find({
where: {
status: { in: ['active', 'draft'] }
}
})not_in (포함하지 않음)
const response = await client.from('products').find({
where: {
status: { not_in: ['archived', 'deleted'] }
}
})like (부분 일치)
const response = await client.from('products').find({
where: {
title: { like: '셔츠' } // '셔츠'가 포함된 상품
}
})contains (배열에 포함)
const response = await client.from('products').find({
where: {
tags: { contains: 'featured' }
}
})exists (필드 존재 여부)
// thumbnail이 있는 상품
const response = await client.from('products').find({
where: {
thumbnail: { exists: true }
}
})
// thumbnail이 없는 상품
const response = await client.from('products').find({
where: {
thumbnail: { exists: false }
}
})논리 연산자
AND 조건
여러 조건을 모두 만족해야 합니다.
const response = await client.from('products').find({
where: {
and: [
{ status: { equals: 'active' } },
{ price: { greater_than: 10000 } },
{ stock: { greater_than: 0 } }
]
}
})OR 조건
조건 중 하나라도 만족하면 됩니다.
const response = await client.from('products').find({
where: {
or: [
{ status: { equals: 'active' } },
{ status: { equals: 'featured' } }
]
}
})복합 조건
AND와 OR를 조합하여 복잡한 쿼리를 작성할 수 있습니다.
const response = await client.from('products').find({
where: {
and: [
{ status: { equals: 'active' } },
{
or: [
{ price: { less_than: 10000 } },
{ tags: { contains: 'sale' } }
]
}
]
}
})관계 필터링
관계 필드로 필터링
// 특정 카테고리의 상품
const response = await client.from('products').find({
where: {
categories: { equals: 'category-id' }
}
})
// 특정 브랜드의 상품
const response = await client.from('products').find({
where: {
brand: { equals: 'brand-id' }
}
})여러 관계 값으로 필터링
const response = await client.from('products').find({
where: {
categories: { in: ['category-1', 'category-2'] }
}
})예제
활성 상품 최신순
const response = await client.from('products').find({
where: { status: { equals: 'active' } },
sort: '-createdAt',
limit: 20
})가격 범위 검색
const response = await client.from('products').find({
where: {
price: {
greater_than_equal: 10000,
less_than_equal: 50000
},
status: { equals: 'active' }
}
})게시된 포스트
const response = await client.from('posts').find({
where: {
status: { equals: 'published' },
publishedAt: { less_than_equal: new Date().toISOString() }
},
sort: '-publishedAt',
limit: 10
})재고 있는 변형
const response = await client.from('product-variants').find({
where: {
product: { equals: 'product-id' },
stock: { greater_than: 0 }
}
})모든 where 조건은 Payload CMS의 쿼리 문법을 따릅니다.
다음 단계
- Webhooks - 이벤트 수신
- React Query - React에서 사용하기
- 에러 처리 - 에러 핸들링