react-query-firebase 2.1.4 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/package.json +1 -1
  2. package/react-native/auth/index.d.ts +1 -0
  3. package/react-native/auth/index.js +1 -0
  4. package/react-native/auth/index.ts +1 -0
  5. package/react-native/auth/useAuthStateReady.d.ts +17 -0
  6. package/react-native/auth/useAuthStateReady.js +33 -0
  7. package/react-native/auth/useAuthStateReady.ts +38 -0
  8. package/react-native/firestore/useCompositeFilter.d.ts +62 -20
  9. package/react-native/firestore/useCompositeFilter.js +51 -11
  10. package/react-native/firestore/useCompositeFilter.ts +70 -25
  11. package/react-native/firestore/useCountQuery.d.ts +35 -9
  12. package/react-native/firestore/useCountQuery.js +15 -6
  13. package/react-native/firestore/useCountQuery.ts +38 -14
  14. package/react-native/firestore/useInfiniteQuery.d.ts +39 -7
  15. package/react-native/firestore/useInfiniteQuery.js +19 -4
  16. package/react-native/firestore/useInfiniteQuery.ts +47 -8
  17. package/react-native/firestore/useQuery.d.ts +37 -12
  18. package/react-native/firestore/useQuery.js +17 -9
  19. package/react-native/firestore/useQuery.ts +40 -14
  20. package/types/index.d.ts +2 -0
  21. package/types/index.js +2 -0
  22. package/web/auth/useAuthStateReady.d.ts +12 -1
  23. package/web/auth/useAuthStateReady.js +12 -1
  24. package/web/auth/useAuthStateReady.ts +12 -1
  25. package/web/firestore/useCompositeFilter.d.ts +63 -21
  26. package/web/firestore/useCompositeFilter.js +48 -9
  27. package/web/firestore/useCompositeFilter.ts +73 -23
  28. package/web/firestore/useCountQuery.d.ts +37 -11
  29. package/web/firestore/useCountQuery.js +16 -7
  30. package/web/firestore/useCountQuery.ts +44 -19
  31. package/web/firestore/useInfiniteQuery.d.ts +41 -10
  32. package/web/firestore/useInfiniteQuery.js +21 -6
  33. package/web/firestore/useInfiniteQuery.ts +51 -23
  34. package/web/firestore/useQuery.d.ts +39 -15
  35. package/web/firestore/useQuery.js +19 -11
  36. package/web/firestore/useQuery.ts +49 -27
@@ -1,35 +1,67 @@
1
- import { DocumentData, QueryFilterConstraint, WhereFilterOp, documentId, and, or, where } from "firebase/firestore";
1
+ import {
2
+ QueryFilterConstraint as FBQueryFilterConstraint,
3
+ QueryCompositeFilterConstraint as FBQueryCompositeFilterConstraint,
4
+ WhereFilterOp,
5
+ documentId,
6
+ and,
7
+ or,
8
+ where
9
+ } from "firebase/firestore";
2
10
  import { useMemo } from "react";
11
+ import { AppModel } from "../../types";
3
12
 
4
- type CompositeFilterDocumentData = DocumentData;
13
+ export type QueryFilterConstraint = FBQueryFilterConstraint | FBQueryCompositeFilterConstraint;
5
14
 
6
- export type QueryElement<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
15
+ export type QueryElement<AppModelType extends AppModel = AppModel> = {
7
16
  operator?: "OR" | "AND";
8
17
  children?: QueryElement[];
9
- field?: keyof (DbModelType & { documentId?: string[] });
10
- value?: DbModelType[keyof DbModelType];
18
+ field?: keyof (AppModelType & { documentId?: string[] });
19
+ value?: AppModelType[keyof AppModelType];
11
20
  op?: WhereFilterOp;
12
21
  };
13
22
 
14
- export type CompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
23
+ export type CompositeFilter<AppModelType extends AppModel = AppModel> = {
15
24
  operator: "OR" | "AND";
16
- children: QueryElement<DbModelType & { documentId?: string[] }>[];
25
+ children: QueryElement<AppModelType & { documentId?: string[] }>[];
17
26
  };
18
27
 
19
- export type UseCompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
20
- query?: CompositeFilter<DbModelType>;
28
+ /**
29
+ * @inline
30
+ */
31
+ export type UseCompositeFilter<AppModelType extends AppModel = AppModel> = {
32
+ query?: CompositeFilter<AppModelType>;
21
33
  };
22
34
 
23
35
  /**
24
- * Constructs a composite query filter based on the provided query structure.
36
+ * Constructs a composite or where query filter based on the provided query structure.
25
37
  * It recursively builds query constraints using logical "or" or "and" operators.
26
38
  *
27
- * @param {QueryElement<DbModelType>} query - The query element or structure to be evaluated and transformed into filter constraints.
28
- * @returns {QueryFieldFilterConstraint | null} A constructed query filter constraint based on the input query, or null if no valid constraints can be derived.
39
+ * @group Utility
40
+ *
41
+ * @param {QueryElement<AppModelType>} query
42
+ *
43
+ * @returns {QueryFilterConstraint | null}
44
+ *
45
+ * @example
46
+ * ```jsx
47
+ * export const MyComponent = () => {
48
+ * const filter = buildCompositeFilter({
49
+ * operator: "AND",
50
+ * children: [
51
+ * {
52
+ * field: "field",
53
+ * value: "value",
54
+ * op: "=="
55
+ * },
56
+ * ...(query ? [query] : [])
57
+ * ]
58
+ * });
59
+ * console.log(filter);
60
+ * };
61
+ * ```
29
62
  */
30
-
31
- export const buildCompositeFilter = <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>(
32
- query: QueryElement<DbModelType>
63
+ export const buildCompositeFilter = <AppModelType extends AppModel = AppModel>(
64
+ query: QueryElement<AppModelType>
33
65
  ): QueryFilterConstraint | null => {
34
66
  if (query.children) {
35
67
  const queryConstraints = query.children.map(buildCompositeFilter).filter((constraint) => !!constraint);
@@ -49,18 +81,36 @@ export const buildCompositeFilter = <DbModelType extends CompositeFilterDocument
49
81
  };
50
82
 
51
83
  /**
52
- * A custom hook that generates a composite filter for database queries, using the provided query configuration.
53
- * It applies either an 'OR' or 'AND' logical operation based on the type specified in the query.
84
+ * A custom hook that constructs a composite or where query filter based on the provided query structure.
85
+ * It recursively builds query constraints using logical "or" or "and" operators.
86
+ *
87
+ * @group Hook
88
+ *
89
+ * @param {QueryElement<AppModelType>} query
54
90
  *
55
- * @param {Object} query - The query configuration object that contains subqueries and a type for logical combination.
56
- * @param {string} query.type - The type of composite operation ('or'/'and').
57
- * @param {Array} query.children - An array of subqueries that will be processed to form the composite filter.
91
+ * @returns {QueryFilterConstraint | null}
58
92
  *
59
- * @returns {(Function|undefined)} A composite query filter constraint function formed by combining subqueries or undefined if there are no valid constraints.
93
+ * @example
94
+ * ```jsx
95
+ * export const MyComponent = () => {
96
+ * const filter = useCompositeFilter({
97
+ * operator: "AND",
98
+ * children: [
99
+ * {
100
+ * field: "field",
101
+ * value: "value",
102
+ * op: "=="
103
+ * },
104
+ * ...(query ? [query] : [])
105
+ * ]
106
+ * });
107
+ * console.log(filter);
108
+ * };
109
+ * ```
60
110
  */
61
- export const useCompositeFilter = <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>({
111
+ export const useCompositeFilter = <AppModelType extends AppModel = AppModel>({
62
112
  query
63
- }: UseCompositeFilter<DbModelType>) => {
113
+ }: UseCompositeFilter<AppModelType>) => {
64
114
  return useMemo(() => {
65
115
  const queryConstraints =
66
116
  query?.children?.map?.((subQuery) => buildCompositeFilter(subQuery))?.filter<QueryFilterConstraint>?.(
@@ -1,23 +1,49 @@
1
- import { DocumentData, CollectionReference, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
1
+ import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
2
2
  import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
3
- type UseCountQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
3
+ import { QueryFilterConstraint } from "./useCompositeFilter";
4
+ import { AppModel } from "../../types";
5
+ /**
6
+ * @inline
7
+ */
8
+ type UseCountQueryOptions<AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel> = {
9
+ /**
10
+ * Reqct-query options that must include queryKey and shall not define queryFn
11
+ */
4
12
  options: Omit<UseReactQueryOptions<number, Error, number>, "queryFn"> & Required<Pick<UseReactQueryOptions<number, Error, number>, "queryKey">>;
13
+ /**
14
+ * Reference to a Firestore collection
15
+ */
5
16
  collectionReference: CollectionReference<AppModelType, DbModelType>;
17
+ /**
18
+ * Non composite filter constraints such as limit, order, where
19
+ */
6
20
  queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
7
- compositeFilter?: QueryCompositeFilterConstraint;
21
+ /**
22
+ * Composite filter
23
+ */
24
+ compositeFilter?: QueryFilterConstraint;
8
25
  };
9
26
  /**
10
27
  * Executes a query with specified constraints and returns the count of matched documents.
11
28
  *
12
- * This function utilizes React Query to asynchronously fetch the count of documents from a server database
13
- * that match the provided query constraints and an optional composite filter.
29
+ * @group Hook
30
+ *
31
+ * @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
14
32
  *
15
- * @param {UseCountQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
16
- * @param {AppModelType extends DocumentData = DocumentData} [options.options] - Additional options for the React Query.
17
- * @param {unknown} [options.query] - Reference to the query object to be executed.
18
- * @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
19
- * @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
20
33
  * @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
34
+ *
35
+ * @example
36
+ * ```jsx
37
+ * export const MyComponent = () => {
38
+ * const count = useCountQuery({
39
+ * options: {
40
+ * queryKey: ['key']
41
+ * },
42
+ * collectionReference: collection(),
43
+ * });
44
+ * console.log(count);
45
+ * };
46
+ * ```
21
47
  */
22
- export declare const useCountQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, collectionReference, queryConstraints, compositeFilter }: UseCountQueryOptions<AppModelType, DbModelType>) => UseQueryResult<number>;
48
+ export declare const useCountQuery: <AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel>({ options, collectionReference, queryConstraints, compositeFilter }: UseCountQueryOptions<AppModelType, DbModelType>) => UseQueryResult<number>;
23
49
  export {};
@@ -3,15 +3,24 @@ import { useQuery as useReactQuery } from "@tanstack/react-query";
3
3
  /**
4
4
  * Executes a query with specified constraints and returns the count of matched documents.
5
5
  *
6
- * This function utilizes React Query to asynchronously fetch the count of documents from a server database
7
- * that match the provided query constraints and an optional composite filter.
6
+ * @group Hook
7
+ *
8
+ * @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
8
9
  *
9
- * @param {UseCountQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
10
- * @param {AppModelType extends DocumentData = DocumentData} [options.options] - Additional options for the React Query.
11
- * @param {unknown} [options.query] - Reference to the query object to be executed.
12
- * @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
13
- * @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
14
10
  * @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
11
+ *
12
+ * @example
13
+ * ```jsx
14
+ * export const MyComponent = () => {
15
+ * const count = useCountQuery({
16
+ * options: {
17
+ * queryKey: ['key']
18
+ * },
19
+ * collectionReference: collection(),
20
+ * });
21
+ * console.log(count);
22
+ * };
23
+ * ```
15
24
  */
16
25
  export const useCountQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
17
26
  return useReactQuery({
@@ -1,5 +1,4 @@
1
1
  import {
2
- DocumentData,
3
2
  getCountFromServer,
4
3
  CollectionReference,
5
4
  query,
@@ -13,36 +12,58 @@ import {
13
12
  useQuery as useReactQuery,
14
13
  UseQueryOptions as UseReactQueryOptions
15
14
  } from "@tanstack/react-query";
15
+ import { QueryFilterConstraint } from "./useCompositeFilter";
16
+ import { AppModel } from "../../types";
16
17
 
17
- type UseCountQueryOptions<
18
- AppModelType extends DocumentData = DocumentData,
19
- DbModelType extends DocumentData = DocumentData
20
- > = {
18
+ /**
19
+ * @inline
20
+ */
21
+ type UseCountQueryOptions<AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel> = {
22
+ /**
23
+ * Reqct-query options that must include queryKey and shall not define queryFn
24
+ */
21
25
  options: Omit<UseReactQueryOptions<number, Error, number>, "queryFn"> &
22
26
  Required<Pick<UseReactQueryOptions<number, Error, number>, "queryKey">>;
27
+
28
+ /**
29
+ * Reference to a Firestore collection
30
+ */
23
31
  collectionReference: CollectionReference<AppModelType, DbModelType>;
32
+
33
+ /**
34
+ * Non composite filter constraints such as limit, order, where
35
+ */
24
36
  queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
25
- compositeFilter?: QueryCompositeFilterConstraint;
37
+
38
+ /**
39
+ * Composite filter
40
+ */
41
+ compositeFilter?: QueryFilterConstraint;
26
42
  };
27
43
 
28
44
  /**
29
45
  * Executes a query with specified constraints and returns the count of matched documents.
30
46
  *
31
- * This function utilizes React Query to asynchronously fetch the count of documents from a server database
32
- * that match the provided query constraints and an optional composite filter.
47
+ * @group Hook
48
+ *
49
+ * @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
33
50
  *
34
- * @param {UseCountQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
35
- * @param {AppModelType extends DocumentData = DocumentData} [options.options] - Additional options for the React Query.
36
- * @param {unknown} [options.query] - Reference to the query object to be executed.
37
- * @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
38
- * @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
39
51
  * @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
52
+ *
53
+ * @example
54
+ * ```jsx
55
+ * export const MyComponent = () => {
56
+ * const count = useCountQuery({
57
+ * options: {
58
+ * queryKey: ['key']
59
+ * },
60
+ * collectionReference: collection(),
61
+ * });
62
+ * console.log(count);
63
+ * };
64
+ * ```
40
65
  */
41
-
42
- export const useCountQuery = <
43
- AppModelType extends DocumentData = DocumentData,
44
- DbModelType extends DocumentData = DocumentData
45
- >({
66
+ export const useCountQuery = <AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel>({
46
67
  options,
47
68
  collectionReference,
48
69
  queryConstraints = [],
@@ -52,7 +73,11 @@ export const useCountQuery = <
52
73
  ...options,
53
74
  queryFn: async () => {
54
75
  const queryToExecute = compositeFilter
55
- ? query(collectionReference, compositeFilter, ...(queryConstraints as QueryNonFilterConstraint[]))
76
+ ? query(
77
+ collectionReference,
78
+ compositeFilter as QueryCompositeFilterConstraint,
79
+ ...(queryConstraints as QueryNonFilterConstraint[])
80
+ )
56
81
  : query(collectionReference, ...queryConstraints);
57
82
 
58
83
  const querySnapshot = await getCountFromServer(queryToExecute);
@@ -1,18 +1,49 @@
1
- import { CollectionReference, DocumentData, FirestoreDataConverter, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
1
+ import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
2
2
  import { UseInfiniteQueryOptions as UseReactInfiniteQueryOptions, QueryKey, UseInfiniteQueryResult, InfiniteData } from "@tanstack/react-query";
3
- type UseInfiniteQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TQueryKey extends QueryKey = QueryKey> = {
3
+ import { QueryFilterConstraint } from "./useCompositeFilter";
4
+ import { AppModel } from "../../types";
5
+ /**
6
+ * @inline
7
+ */
8
+ type UseInfiniteQueryOptions<AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey> = {
9
+ /**
10
+ * Reqct-query options that must include queryKey and shall not define queryFn
11
+ */
4
12
  options: Omit<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryFn"> & Required<Pick<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryKey">>;
5
- collectionReference: CollectionReference<AppModelType, DbModelType>;
13
+ /**
14
+ * Reference to a Firestore collection
15
+ */
16
+ collectionReference: CollectionReference<AppModelType, AppModelType>;
17
+ /**
18
+ * Non composite filter constraints such as limit, order, where
19
+ */
6
20
  queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
7
- compositeFilter?: QueryCompositeFilterConstraint;
8
- converter?: FirestoreDataConverter<AppModelType, DbModelType>;
21
+ /**
22
+ * Composite filter
23
+ */
24
+ compositeFilter?: QueryFilterConstraint;
9
25
  };
10
26
  /**
11
- * Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
12
- * It fetches data in pages and can load more as required.
27
+ * Executes an infinite query on a Firestore data source and returns the resulting documents as an array.
28
+ *
29
+ * @group Hook
30
+ *
31
+ * @param {UseInfiniteQueryOptions<AppModelType, TQueryKey>} options - Configuration options for the query.
32
+ *
33
+ * @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} An object containing documents that match the query.
13
34
  *
14
- * @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
15
- * @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
35
+ * @example
36
+ * ```jsx
37
+ * export const MyComponent = () => {
38
+ * const docs = useInfiniteQuery({
39
+ * options: {
40
+ * queryKey: ['key']
41
+ * },
42
+ * collectionReference: collection(),
43
+ * });
44
+ * console.log(docs);
45
+ * };
46
+ * ```
16
47
  */
17
- export declare const useInfiniteQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, collectionReference, queryConstraints, compositeFilter, converter }: UseInfiniteQueryOptions<AppModelType, DbModelType>) => UseInfiniteQueryResult<InfiniteData<AppModelType[]>>;
48
+ export declare const useInfiniteQuery: <AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey>({ options, collectionReference, queryConstraints, compositeFilter }: UseInfiniteQueryOptions<AppModelType, TQueryKey>) => UseInfiniteQueryResult<InfiniteData<AppModelType[]>>;
18
49
  export {};
@@ -1,13 +1,28 @@
1
1
  import { getDocs, query } from "firebase/firestore";
2
2
  import { useInfiniteQuery as useInfiniteReactQuery } from "@tanstack/react-query";
3
3
  /**
4
- * Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
5
- * It fetches data in pages and can load more as required.
4
+ * Executes an infinite query on a Firestore data source and returns the resulting documents as an array.
6
5
  *
7
- * @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
8
- * @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
6
+ * @group Hook
7
+ *
8
+ * @param {UseInfiniteQueryOptions<AppModelType, TQueryKey>} options - Configuration options for the query.
9
+ *
10
+ * @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} An object containing documents that match the query.
11
+ *
12
+ * @example
13
+ * ```jsx
14
+ * export const MyComponent = () => {
15
+ * const docs = useInfiniteQuery({
16
+ * options: {
17
+ * queryKey: ['key']
18
+ * },
19
+ * collectionReference: collection(),
20
+ * });
21
+ * console.log(docs);
22
+ * };
23
+ * ```
9
24
  */
10
- export const useInfiniteQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter, converter }) => {
25
+ export const useInfiniteQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
11
26
  return useInfiniteReactQuery({
12
27
  ...options,
13
28
  queryFn: async ({ pageParam }) => {
@@ -15,7 +30,7 @@ export const useInfiniteQuery = ({ options, collectionReference, queryConstraint
15
30
  const queryToExecute = compositeFilter
16
31
  ? query(collectionReference, compositeFilter, ...allQueryConstraints)
17
32
  : query(collectionReference, ...allQueryConstraints);
18
- const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
33
+ const querySnapshot = await getDocs(queryToExecute);
19
34
  const docs = [];
20
35
  if (querySnapshot) {
21
36
  querySnapshot.forEach((doc) => {
@@ -1,7 +1,5 @@
1
1
  import {
2
2
  CollectionReference,
3
- DocumentData,
4
- FirestoreDataConverter,
5
3
  getDocs,
6
4
  query,
7
5
  QueryCompositeFilterConstraint,
@@ -16,12 +14,16 @@ import {
16
14
  UseInfiniteQueryResult,
17
15
  InfiniteData
18
16
  } from "@tanstack/react-query";
17
+ import { QueryFilterConstraint } from "./useCompositeFilter";
18
+ import { AppModel } from "../../types";
19
19
 
20
- type UseInfiniteQueryOptions<
21
- AppModelType extends DocumentData = DocumentData,
22
- DbModelType extends DocumentData = DocumentData,
23
- TQueryKey extends QueryKey = QueryKey
24
- > = {
20
+ /**
21
+ * @inline
22
+ */
23
+ type UseInfiniteQueryOptions<AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey> = {
24
+ /**
25
+ * Reqct-query options that must include queryKey and shall not define queryFn
26
+ */
25
27
  options: Omit<
26
28
  UseReactInfiniteQueryOptions<
27
29
  AppModelType[],
@@ -46,38 +48,64 @@ type UseInfiniteQueryOptions<
46
48
  "queryKey"
47
49
  >
48
50
  >;
49
- collectionReference: CollectionReference<AppModelType, DbModelType>;
51
+
52
+ /**
53
+ * Reference to a Firestore collection
54
+ */
55
+ collectionReference: CollectionReference<AppModelType, AppModelType>;
56
+
57
+ /**
58
+ * Non composite filter constraints such as limit, order, where
59
+ */
50
60
  queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
51
- compositeFilter?: QueryCompositeFilterConstraint;
52
- converter?: FirestoreDataConverter<AppModelType, DbModelType>;
61
+
62
+ /**
63
+ * Composite filter
64
+ */
65
+ compositeFilter?: QueryFilterConstraint;
53
66
  };
54
67
 
55
68
  /**
56
- * Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
57
- * It fetches data in pages and can load more as required.
69
+ * Executes an infinite query on a Firestore data source and returns the resulting documents as an array.
70
+ *
71
+ * @group Hook
72
+ *
73
+ * @param {UseInfiniteQueryOptions<AppModelType, TQueryKey>} options - Configuration options for the query.
74
+ *
75
+ * @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} An object containing documents that match the query.
58
76
  *
59
- * @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
60
- * @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
77
+ * @example
78
+ * ```jsx
79
+ * export const MyComponent = () => {
80
+ * const docs = useInfiniteQuery({
81
+ * options: {
82
+ * queryKey: ['key']
83
+ * },
84
+ * collectionReference: collection(),
85
+ * });
86
+ * console.log(docs);
87
+ * };
88
+ * ```
61
89
  */
62
- export const useInfiniteQuery = <
63
- AppModelType extends DocumentData = DocumentData,
64
- DbModelType extends DocumentData = DocumentData
65
- >({
90
+ export const useInfiniteQuery = <AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey>({
66
91
  options,
67
92
  collectionReference,
68
93
  queryConstraints = [],
69
- compositeFilter,
70
- converter
71
- }: UseInfiniteQueryOptions<AppModelType, DbModelType>): UseInfiniteQueryResult<InfiniteData<AppModelType[]>> => {
94
+ compositeFilter
95
+ }: UseInfiniteQueryOptions<AppModelType, TQueryKey>): UseInfiniteQueryResult<InfiniteData<AppModelType[]>> => {
72
96
  return useInfiniteReactQuery({
73
97
  ...options,
74
98
  queryFn: async ({ pageParam }) => {
75
99
  const allQueryConstraints = [...queryConstraints, ...(pageParam ? [pageParam] : [])];
76
100
  const queryToExecute = compositeFilter
77
- ? query(collectionReference, compositeFilter, ...(allQueryConstraints as QueryNonFilterConstraint[]))
101
+ ? query(
102
+ collectionReference,
103
+ compositeFilter as QueryCompositeFilterConstraint,
104
+ ...(allQueryConstraints as QueryNonFilterConstraint[])
105
+ )
78
106
  : query(collectionReference, ...allQueryConstraints);
79
107
 
80
- const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
108
+ const querySnapshot = await getDocs(queryToExecute);
81
109
  const docs: AppModelType[] = [];
82
110
 
83
111
  if (querySnapshot) {
@@ -1,25 +1,49 @@
1
- import { DocumentData, FirestoreDataConverter, CollectionReference, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
1
+ import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
2
2
  import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
3
- type UseQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
3
+ import { QueryFilterConstraint } from "./useCompositeFilter";
4
+ import { AppModel } from "../../types";
5
+ /**
6
+ * @inline
7
+ */
8
+ type UseQueryOptions<AppModelType extends AppModel = AppModel> = {
9
+ /**
10
+ * Reqct-query options that must include queryKey and shall not define queryFn
11
+ */
4
12
  options: Omit<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryFn"> & Required<Pick<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryKey">>;
5
- collectionReference: CollectionReference<AppModelType, DbModelType>;
13
+ /**
14
+ * Reference to a Firestore collection
15
+ */
16
+ collectionReference: CollectionReference<AppModelType, AppModelType>;
17
+ /**
18
+ * Non composite filter constraints such as limit, order, where
19
+ */
6
20
  queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
7
- compositeFilter?: QueryCompositeFilterConstraint;
8
- converter?: FirestoreDataConverter<AppModelType, DbModelType>;
21
+ /**
22
+ * Composite filter
23
+ */
24
+ compositeFilter?: QueryFilterConstraint;
9
25
  };
10
26
  /**
11
- * Executes a query on a Firestore-like data source and returns the resulting documents as an array.
27
+ * Executes a query on a Firestore data source and returns the resulting documents as an array.
28
+ *
29
+ * @group Hook
12
30
  *
13
- * This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
14
- * reference and constraints. It supports optional filtering, conversion, and additional query constraints.
31
+ * @param {UseQueryOptions<AppModelType>} options - Configuration options for the query.
15
32
  *
16
- * @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
17
- * @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
18
- * @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
19
- * @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
20
- * @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
33
+ * @returns {UseQueryResult<AppModelType[]>} An object containing documents that match the query.
21
34
  *
22
- * @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
35
+ * @example
36
+ * ```jsx
37
+ * export const MyComponent = () => {
38
+ * const docs = useQuery({
39
+ * options: {
40
+ * queryKey: ['key']
41
+ * },
42
+ * collectionReference: collection(),
43
+ * });
44
+ * console.log(docs);
45
+ * };
46
+ * ```
23
47
  */
24
- export declare const useQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, collectionReference, queryConstraints, compositeFilter, converter }: UseQueryOptions<AppModelType, DbModelType>) => UseQueryResult<AppModelType[]>;
48
+ export declare const useQuery: <AppModelType extends AppModel = AppModel>({ options, collectionReference, queryConstraints, compositeFilter }: UseQueryOptions<AppModelType>) => UseQueryResult<AppModelType[]>;
25
49
  export {};
@@ -1,27 +1,35 @@
1
1
  import { getDocs, query } from "firebase/firestore";
2
2
  import { useQuery as useReactQuery } from "@tanstack/react-query";
3
3
  /**
4
- * Executes a query on a Firestore-like data source and returns the resulting documents as an array.
4
+ * Executes a query on a Firestore data source and returns the resulting documents as an array.
5
5
  *
6
- * This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
7
- * reference and constraints. It supports optional filtering, conversion, and additional query constraints.
6
+ * @group Hook
8
7
  *
9
- * @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
10
- * @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
11
- * @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
12
- * @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
13
- * @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
8
+ * @param {UseQueryOptions<AppModelType>} options - Configuration options for the query.
14
9
  *
15
- * @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
10
+ * @returns {UseQueryResult<AppModelType[]>} An object containing documents that match the query.
11
+ *
12
+ * @example
13
+ * ```jsx
14
+ * export const MyComponent = () => {
15
+ * const docs = useQuery({
16
+ * options: {
17
+ * queryKey: ['key']
18
+ * },
19
+ * collectionReference: collection(),
20
+ * });
21
+ * console.log(docs);
22
+ * };
23
+ * ```
16
24
  */
17
- export const useQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter, converter }) => {
25
+ export const useQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
18
26
  return useReactQuery({
19
27
  ...options,
20
28
  queryFn: async () => {
21
29
  const queryToExecute = compositeFilter
22
30
  ? query(collectionReference, compositeFilter, ...queryConstraints)
23
31
  : query(collectionReference, ...queryConstraints);
24
- const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
32
+ const querySnapshot = await getDocs(queryToExecute);
25
33
  const docs = [];
26
34
  if (querySnapshot) {
27
35
  querySnapshot.forEach((doc) => {