search.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* eslint-disable camelcase */
  2. import { SearchDelegatorName } from '~/interfaces/named-query';
  3. import { ISearchResult } from '~/interfaces/search';
  4. export type QueryTerms = {
  5. match: string[],
  6. not_match: string[],
  7. phrase: string[],
  8. not_phrase: string[],
  9. prefix: string[],
  10. not_prefix: string[],
  11. tag: string[],
  12. not_tag: string[],
  13. }
  14. export type ParsedQuery = { queryString: string, terms: QueryTerms, delegatorName?: string }
  15. export interface SearchQueryParser {
  16. parseSearchQuery(queryString: string, nqName: string | null): Promise<ParsedQuery>
  17. }
  18. export interface SearchResolver {
  19. resolve(parsedQuery: ParsedQuery): Promise<[SearchDelegator, SearchableData | null]>
  20. }
  21. export interface SearchDelegator<T = unknown, KEY extends AllTermsKey = AllTermsKey, QTERMS = unknown> {
  22. name?: SearchDelegatorName
  23. search(data: SearchableData | null, user, userGroups, option): Promise<ISearchResult<T>>
  24. isTermsNormalized(terms: Partial<QueryTerms>): terms is QTERMS,
  25. validateTerms(terms: QueryTerms): UnavailableTermsKey<KEY>[],
  26. }
  27. export type SearchableData = {
  28. queryString: string
  29. terms: QueryTerms
  30. }
  31. // Terms Key types
  32. export type AllTermsKey = keyof QueryTerms;
  33. export type UnavailableTermsKey<K extends AllTermsKey> = Exclude<AllTermsKey, K>;
  34. export type ESTermsKey = 'match' | 'not_match' | 'phrase' | 'not_phrase' | 'prefix' | 'not_prefix' | 'tag' | 'not_tag';
  35. export type MongoTermsKey = 'match' | 'not_match' | 'prefix' | 'not_prefix';
  36. // Query Terms types
  37. export type ESQueryTerms = Pick<QueryTerms, ESTermsKey>;
  38. export type MongoQueryTerms = Pick<QueryTerms, MongoTermsKey>;