fix(search): search case insensitive across all dbs
BREAKING CHANGE: Use ILike operator instead of Like. This changes the search behavior from case sensitive to case insensitive on dbs like postgres.
This commit is contained in:
parent
959cfe608c
commit
e6354a4be7
@ -9,31 +9,29 @@ export interface PaginateQuery {
|
||||
path: string
|
||||
}
|
||||
|
||||
export const Paginate = createParamDecorator(
|
||||
(_data: unknown, ctx: ExecutionContext): PaginateQuery => {
|
||||
const request: Request = ctx.switchToHttp().getRequest()
|
||||
const { query } = request
|
||||
const path = request.protocol + '://' + request.get('host') + request.baseUrl + request.path
|
||||
export const Paginate = createParamDecorator((_data: unknown, ctx: ExecutionContext): PaginateQuery => {
|
||||
const request: Request = ctx.switchToHttp().getRequest()
|
||||
const { query } = request
|
||||
const path = request.protocol + '://' + request.get('host') + request.baseUrl + request.path
|
||||
|
||||
const sortBy: [string, string][] = []
|
||||
if (query.sortBy) {
|
||||
const params = !Array.isArray(query.sortBy) ? [query.sortBy] : query.sortBy
|
||||
for (const param of params as string[]) {
|
||||
if (typeof param === 'string') {
|
||||
const items = param.split(':')
|
||||
if (items.length === 2) {
|
||||
sortBy.push(items as [string, string])
|
||||
}
|
||||
const sortBy: [string, string][] = []
|
||||
if (query.sortBy) {
|
||||
const params = !Array.isArray(query.sortBy) ? [query.sortBy] : query.sortBy
|
||||
for (const param of params as string[]) {
|
||||
if (typeof param === 'string') {
|
||||
const items = param.split(':')
|
||||
if (items.length === 2) {
|
||||
sortBy.push(items as [string, string])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
page: query.page ? parseInt(query.page.toString(), 10) : undefined,
|
||||
limit: query.limit ? parseInt(query.limit.toString(), 10) : undefined,
|
||||
sortBy: sortBy.length ? sortBy : undefined,
|
||||
search: query.search ? query.search.toString() : undefined,
|
||||
path,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
page: query.page ? parseInt(query.page.toString(), 10) : undefined,
|
||||
limit: query.limit ? parseInt(query.limit.toString(), 10) : undefined,
|
||||
sortBy: sortBy.length ? sortBy : undefined,
|
||||
search: query.search ? query.search.toString() : undefined,
|
||||
path,
|
||||
}
|
||||
})
|
||||
|
@ -77,7 +77,7 @@ describe('paginate', () => {
|
||||
const config: PaginateConfig<CatEntity> = {
|
||||
sortableColumns: ['id'],
|
||||
defaultLimit: 5,
|
||||
maxLimit: 2
|
||||
maxLimit: 2,
|
||||
}
|
||||
const query: PaginateQuery = {
|
||||
path: '',
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Repository, FindConditions, SelectQueryBuilder, Like, ObjectLiteral } from 'typeorm'
|
||||
import { Repository, FindConditions, SelectQueryBuilder, ObjectLiteral, ILike } from 'typeorm'
|
||||
import { PaginateQuery } from './decorator'
|
||||
import { ServiceUnavailableException } from '@nestjs/common'
|
||||
|
||||
@ -41,7 +41,7 @@ export async function paginate<T>(
|
||||
config: PaginateConfig<T>
|
||||
): Promise<Paginated<T>> {
|
||||
let page = query.page || 1
|
||||
const limit = Math.min(query.limit || config.defaultLimit || 20, config.maxLimit || 100);
|
||||
const limit = Math.min(query.limit || config.defaultLimit || 20, config.maxLimit || 100)
|
||||
const sortBy = [] as SortBy<T>
|
||||
const search = query.search
|
||||
const path = query.path
|
||||
@ -90,7 +90,7 @@ export async function paginate<T>(
|
||||
const where: ObjectLiteral[] = []
|
||||
if (search && config.searchableColumns) {
|
||||
for (const column of config.searchableColumns) {
|
||||
where.push({ [column]: Like(`%${search}%`), ...config.where })
|
||||
where.push({ [column]: ILike(`%${search}%`), ...config.where })
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user