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,8 +9,7 @@ export interface PaginateQuery {
|
|||||||
path: string
|
path: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Paginate = createParamDecorator(
|
export const Paginate = createParamDecorator((_data: unknown, ctx: ExecutionContext): PaginateQuery => {
|
||||||
(_data: unknown, ctx: ExecutionContext): PaginateQuery => {
|
|
||||||
const request: Request = ctx.switchToHttp().getRequest()
|
const request: Request = ctx.switchToHttp().getRequest()
|
||||||
const { query } = request
|
const { query } = request
|
||||||
const path = request.protocol + '://' + request.get('host') + request.baseUrl + request.path
|
const path = request.protocol + '://' + request.get('host') + request.baseUrl + request.path
|
||||||
@ -35,5 +34,4 @@ export const Paginate = createParamDecorator(
|
|||||||
search: query.search ? query.search.toString() : undefined,
|
search: query.search ? query.search.toString() : undefined,
|
||||||
path,
|
path,
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
)
|
|
||||||
|
@ -77,7 +77,7 @@ describe('paginate', () => {
|
|||||||
const config: PaginateConfig<CatEntity> = {
|
const config: PaginateConfig<CatEntity> = {
|
||||||
sortableColumns: ['id'],
|
sortableColumns: ['id'],
|
||||||
defaultLimit: 5,
|
defaultLimit: 5,
|
||||||
maxLimit: 2
|
maxLimit: 2,
|
||||||
}
|
}
|
||||||
const query: PaginateQuery = {
|
const query: PaginateQuery = {
|
||||||
path: '',
|
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 { PaginateQuery } from './decorator'
|
||||||
import { ServiceUnavailableException } from '@nestjs/common'
|
import { ServiceUnavailableException } from '@nestjs/common'
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ export async function paginate<T>(
|
|||||||
config: PaginateConfig<T>
|
config: PaginateConfig<T>
|
||||||
): Promise<Paginated<T>> {
|
): Promise<Paginated<T>> {
|
||||||
let page = query.page || 1
|
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 sortBy = [] as SortBy<T>
|
||||||
const search = query.search
|
const search = query.search
|
||||||
const path = query.path
|
const path = query.path
|
||||||
@ -90,7 +90,7 @@ export async function paginate<T>(
|
|||||||
const where: ObjectLiteral[] = []
|
const where: ObjectLiteral[] = []
|
||||||
if (search && config.searchableColumns) {
|
if (search && config.searchableColumns) {
|
||||||
for (const column of 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