UNPKG

@jameshclrk/rxdb-hooks

Version:

React hooks for integrating with RxDB

87 lines (86 loc) 2.93 kB
import { RxQuery, RxDocument } from 'rxdb'; import { DeepReadonly } from 'rxdb/dist/types/types'; import { Override } from './type.helpers'; export type ResultMap<T> = Map<string, RxDocument<T, any>>; export type AnyQueryResult<T> = DeepReadonly<T>[] | RxDocument<T>[]; export type RxQueryResult<T> = { /** * Resulting documents. */ result: AnyQueryResult<T>; /** * Indicates that fetching is in progress. */ isFetching: boolean; /** * Indicates that all available results have been already fetched. * Relevant in "infinite scroll" pagination mode */ isExhausted: boolean; /** * Total number of pages, based on total number of results and page size. * Relevant in "traditional" pagination mode */ pageCount: number; /** * The number of the current page. * Relevant in "infinite scroll" pagination mode */ currentPage: number; /** * Allows consumer to request a specific page of results. * Relevant in "traditional" pagination mode */ fetchPage: (page: number) => void; /** * Allows consumer to incrementally request more results. * Relevant in "infinite scroll" pagination mode */ fetchMore: () => void; /** * Allows consumer to reset results. * Relevant in "infinite scroll" pagination mode */ resetList: () => void; }; export type RxQueryResultJSON<T> = Override<RxQueryResult<T>, { result: DeepReadonly<T>[]; }>; export type RxQueryResultDoc<T> = Override<RxQueryResult<T>, { result: RxDocument<T>[]; }>; /** * Traditional: * Results are split into pages, starts by rendering the first page and total * pageCount is returned, allowing for requesting results of any specific page. * Requires `pageSize` to be defined * * Infinite: * First page of results is rendered, allowing for gradually requesting more. * Requires `pageSize` to be defined */ export type PaginationMode = 'Traditional' | 'Infinite'; export interface UseRxQueryOptions { /** * Controls page size, in both "infinite scroll" & "traditional" pagination */ pageSize?: number; /** * Determines pagination mode */ pagination?: PaginationMode; /** * Converts resulting RxDocuments to plain objects */ json?: boolean; } export type ObservableReturningQuery<T> = RxQuery<T, Map<string, RxDocument<T, any>>> | RxQuery<T, RxDocument<T> | null> | RxQuery<T, RxDocument<T>[]>; export type AnyRxQuery<T> = ObservableReturningQuery<T>; declare function useRxQuery<T>(query?: AnyRxQuery<T>): RxQueryResultDoc<T>; declare function useRxQuery<T>(query: AnyRxQuery<T>, options?: Override<UseRxQueryOptions, { json?: false; }>): RxQueryResultDoc<T>; declare function useRxQuery<T>(query: AnyRxQuery<T>, options?: Override<UseRxQueryOptions, { json: true; }>): RxQueryResultJSON<T>; export default useRxQuery;