react-query-firebase 2.2.0 → 2.3.1

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