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,11 +1,26 @@
1
1
  import { getDocs, query } from "@react-native-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
25
  export const useInfiniteQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
11
26
  return useInfiniteReactQuery({
@@ -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,6 +1,17 @@
1
1
  /**
2
2
  * A custom hook that determines if the Firebase authentication state is ready.
3
3
  * It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
4
- * @returns {boolean} Indicates whether the authentication state is ready.
4
+ *
5
+ * @group Hook
6
+ *
7
+ * @returns {boolean}
8
+ *
9
+ * @example
10
+ * ```jsx
11
+ * export const MyComponent = () => {
12
+ * const isAuthStateReady = useAuthStateReady();
13
+ * console.log(isAuthStateReady);
14
+ * };
15
+ * ```
5
16
  */
6
17
  export declare const useAuthStateReady: () => boolean;
@@ -3,7 +3,18 @@ import { useCallback, useEffect, useState } from "react";
3
3
  /**
4
4
  * A custom hook that determines if the Firebase authentication state is ready.
5
5
  * It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
6
- * @returns {boolean} Indicates whether the authentication state is ready.
6
+ *
7
+ * @group Hook
8
+ *
9
+ * @returns {boolean}
10
+ *
11
+ * @example
12
+ * ```jsx
13
+ * export const MyComponent = () => {
14
+ * const isAuthStateReady = useAuthStateReady();
15
+ * console.log(isAuthStateReady);
16
+ * };
17
+ * ```
7
18
  */
8
19
  export const useAuthStateReady = () => {
9
20
  const firebaseAuth = useAuth();
@@ -4,7 +4,18 @@ import { useCallback, useEffect, useState } from "react";
4
4
  /**
5
5
  * A custom hook that determines if the Firebase authentication state is ready.
6
6
  * It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
7
- * @returns {boolean} Indicates whether the authentication state is ready.
7
+ *
8
+ * @group Hook
9
+ *
10
+ * @returns {boolean}
11
+ *
12
+ * @example
13
+ * ```jsx
14
+ * export const MyComponent = () => {
15
+ * const isAuthStateReady = useAuthStateReady();
16
+ * console.log(isAuthStateReady);
17
+ * };
18
+ * ```
8
19
  */
9
20
  export const useAuthStateReady = () => {
10
21
  const firebaseAuth = useAuth();
@@ -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(() => {