Search.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import type { Query as PiniaOrmQuery } from 'pinia-orm'
  2. import type { ApiFilter } from '~/types/data'
  3. import ApiResource from '~/models/ApiResource'
  4. import { SEARCH_STRATEGY } from '~/types/enum/data'
  5. /**
  6. *
  7. */
  8. export default class Search implements ApiFilter {
  9. field: string
  10. value: Ref<string | number>
  11. private mode: string
  12. /**
  13. * @param field
  14. * @param value
  15. * @param mode The search strategy (exact [default], partial, start, end, word_start).
  16. * This strategy is defined API-side, but PiniaOrm needs to know how to handle this.
  17. * @see https://api-platform.com/docs/core/filters/
  18. */
  19. constructor(
  20. field: string,
  21. value: Ref<string | number>,
  22. mode: SEARCH_STRATEGY = SEARCH_STRATEGY.EXACT,
  23. ) {
  24. this.field = field
  25. this.value = value
  26. this.mode = mode
  27. }
  28. public applyToPiniaOrmQuery(
  29. query: PiniaOrmQuery<ApiResource>,
  30. ): PiniaOrmQuery<ApiResource> {
  31. return query.where(this.field, this.value.value)
  32. }
  33. public getApiQueryPart(): string {
  34. return `${this.field}[]=${this.value.value}`
  35. }
  36. }