react-query-firebase 2.3.2 → 2.4.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 (133) hide show
  1. package/package.json +1 -1
  2. package/react-native/firestore/index.d.ts +5 -0
  3. package/react-native/firestore/index.js +5 -0
  4. package/react-native/firestore/index.ts +5 -0
  5. package/react-native/firestore/useAddDocMutation.d.ts +38 -9
  6. package/react-native/firestore/useAddDocMutation.js +25 -11
  7. package/react-native/firestore/useAddDocMutation.ts +43 -13
  8. package/react-native/firestore/useBatchWrite.d.ts +31 -7
  9. package/react-native/firestore/useBatchWrite.js +22 -4
  10. package/react-native/firestore/useBatchWrite.ts +31 -7
  11. package/react-native/firestore/useCollectionReference.d.ts +30 -9
  12. package/react-native/firestore/useCollectionReference.js +15 -7
  13. package/react-native/firestore/useCollectionReference.ts +34 -25
  14. package/react-native/firestore/useCompositeFilter.d.ts +1 -46
  15. package/react-native/firestore/useCompositeFilter.js +2 -45
  16. package/react-native/firestore/useCompositeFilter.ts +2 -74
  17. package/react-native/firestore/useCountQuery.d.ts +1 -1
  18. package/react-native/firestore/useCountQuery.ts +1 -1
  19. package/react-native/firestore/useDeleteDocMutation.d.ts +39 -10
  20. package/react-native/firestore/useDeleteDocMutation.js +22 -5
  21. package/react-native/firestore/useDeleteDocMutation.ts +38 -15
  22. package/react-native/firestore/useDocReference.d.ts +22 -14
  23. package/react-native/firestore/useDocReference.js +20 -26
  24. package/react-native/firestore/useDocReference.ts +34 -47
  25. package/react-native/firestore/useDocReferences.d.ts +23 -18
  26. package/react-native/firestore/useDocReferences.js +18 -19
  27. package/react-native/firestore/useDocReferences.ts +26 -34
  28. package/react-native/firestore/useFirestore.d.ts +4 -3
  29. package/react-native/firestore/useFirestore.js +4 -3
  30. package/react-native/firestore/useFirestore.ts +4 -3
  31. package/react-native/firestore/useGetDocData.d.ts +33 -14
  32. package/react-native/firestore/useGetDocData.js +21 -6
  33. package/react-native/firestore/useGetDocData.ts +34 -22
  34. package/react-native/firestore/useInfiniteQuery.d.ts +2 -2
  35. package/react-native/firestore/useInfiniteQuery.ts +3 -3
  36. package/react-native/firestore/useQuery.d.ts +1 -1
  37. package/react-native/firestore/useQuery.ts +1 -1
  38. package/react-native/firestore/useQueryConstraints.d.ts +2 -22
  39. package/react-native/firestore/useQueryConstraints.js +1 -39
  40. package/react-native/firestore/useQueryConstraints.ts +3 -43
  41. package/react-native/firestore/useRunTransaction.d.ts +33 -9
  42. package/react-native/firestore/useRunTransaction.js +20 -4
  43. package/react-native/firestore/useRunTransaction.ts +35 -16
  44. package/react-native/firestore/useSetDocMutation.d.ts +43 -11
  45. package/react-native/firestore/useSetDocMutation.js +28 -11
  46. package/react-native/firestore/useSetDocMutation.ts +49 -28
  47. package/react-native/firestore/useUpdateDocMutation.d.ts +40 -18
  48. package/react-native/firestore/useUpdateDocMutation.js +18 -13
  49. package/react-native/firestore/useUpdateDocMutation.ts +40 -31
  50. package/react-native/firestore/utils/buildCompositeFilter.d.ts +47 -0
  51. package/react-native/firestore/utils/buildCompositeFilter.js +45 -0
  52. package/react-native/firestore/utils/buildCompositeFilter.ts +75 -0
  53. package/react-native/firestore/utils/buildQueryConstraint.d.ts +23 -0
  54. package/react-native/firestore/utils/buildQueryConstraint.js +41 -0
  55. package/react-native/firestore/utils/buildQueryConstraint.ts +54 -0
  56. package/react-native/firestore/utils/getDocData.d.ts +11 -11
  57. package/react-native/firestore/utils/getDocData.js +5 -8
  58. package/react-native/firestore/utils/getDocData.ts +12 -16
  59. package/react-native/firestore/utils/getDocRef.d.ts +12 -9
  60. package/react-native/firestore/utils/getDocRef.js +7 -8
  61. package/react-native/firestore/utils/getDocRef.ts +12 -13
  62. package/react-native/firestore/utils/getDocSnap.d.ts +11 -13
  63. package/react-native/firestore/utils/getDocSnap.js +5 -10
  64. package/react-native/firestore/utils/getDocSnap.ts +12 -17
  65. package/types/AppModel.d.ts +4 -1
  66. package/types/AppModel.ts +4 -1
  67. package/web/firestore/index.d.ts +5 -0
  68. package/web/firestore/index.js +5 -0
  69. package/web/firestore/index.ts +5 -0
  70. package/web/firestore/useAddDocMutation.d.ts +41 -13
  71. package/web/firestore/useAddDocMutation.js +27 -14
  72. package/web/firestore/useAddDocMutation.ts +49 -37
  73. package/web/firestore/useBatchWrite.d.ts +34 -7
  74. package/web/firestore/useBatchWrite.js +22 -4
  75. package/web/firestore/useBatchWrite.ts +34 -7
  76. package/web/firestore/useCollectionReference.d.ts +32 -11
  77. package/web/firestore/useCollectionReference.js +15 -7
  78. package/web/firestore/useCollectionReference.ts +34 -13
  79. package/web/firestore/useCompositeFilter.d.ts +2 -47
  80. package/web/firestore/useCompositeFilter.js +2 -42
  81. package/web/firestore/useCompositeFilter.ts +2 -72
  82. package/web/firestore/useCountQuery.d.ts +4 -4
  83. package/web/firestore/useCountQuery.ts +5 -5
  84. package/web/firestore/useDeleteDocMutation.d.ts +41 -11
  85. package/web/firestore/useDeleteDocMutation.js +24 -7
  86. package/web/firestore/useDeleteDocMutation.ts +45 -23
  87. package/web/firestore/useDocReference.d.ts +23 -15
  88. package/web/firestore/useDocReference.js +16 -31
  89. package/web/firestore/useDocReference.ts +28 -46
  90. package/web/firestore/useDocReferences.d.ts +23 -18
  91. package/web/firestore/useDocReferences.js +18 -21
  92. package/web/firestore/useDocReferences.ts +26 -30
  93. package/web/firestore/useFirestore.d.ts +4 -3
  94. package/web/firestore/useFirestore.js +4 -3
  95. package/web/firestore/useFirestore.ts +4 -3
  96. package/web/firestore/useGetDocData.d.ts +32 -13
  97. package/web/firestore/useGetDocData.js +21 -6
  98. package/web/firestore/useGetDocData.ts +34 -23
  99. package/web/firestore/useGetRealtimeDocData.js +2 -2
  100. package/web/firestore/useGetRealtimeDocData.ts +2 -2
  101. package/web/firestore/useInfiniteQuery.d.ts +2 -2
  102. package/web/firestore/useInfiniteQuery.js +1 -1
  103. package/web/firestore/useInfiniteQuery.ts +4 -4
  104. package/web/firestore/useQuery.d.ts +1 -1
  105. package/web/firestore/useQuery.js +1 -1
  106. package/web/firestore/useQuery.ts +2 -2
  107. package/web/firestore/useQueryConstraints.d.ts +0 -21
  108. package/web/firestore/useQueryConstraints.js +1 -39
  109. package/web/firestore/useQueryConstraints.ts +2 -51
  110. package/web/firestore/useRunTransaction.d.ts +33 -8
  111. package/web/firestore/useRunTransaction.js +20 -4
  112. package/web/firestore/useRunTransaction.ts +35 -10
  113. package/web/firestore/useSetDocMutation.d.ts +45 -12
  114. package/web/firestore/useSetDocMutation.js +28 -11
  115. package/web/firestore/useSetDocMutation.ts +51 -26
  116. package/web/firestore/useUpdateDocMutation.d.ts +42 -20
  117. package/web/firestore/useUpdateDocMutation.js +20 -15
  118. package/web/firestore/useUpdateDocMutation.ts +45 -41
  119. package/web/firestore/utils/buildCompositeFilter.d.ts +47 -0
  120. package/web/firestore/utils/buildCompositeFilter.js +42 -0
  121. package/web/firestore/utils/buildCompositeFilter.ts +73 -0
  122. package/web/firestore/utils/buildQueryConstraint.d.ts +23 -0
  123. package/web/firestore/utils/buildQueryConstraint.js +41 -0
  124. package/web/firestore/utils/buildQueryConstraint.ts +55 -0
  125. package/web/firestore/utils/getDocData.d.ts +13 -11
  126. package/web/firestore/utils/getDocData.js +6 -9
  127. package/web/firestore/utils/getDocData.ts +14 -20
  128. package/web/firestore/utils/getDocRef.d.ts +14 -11
  129. package/web/firestore/utils/getDocRef.js +7 -8
  130. package/web/firestore/utils/getDocRef.ts +16 -19
  131. package/web/firestore/utils/getDocSnap.d.ts +11 -13
  132. package/web/firestore/utils/getDocSnap.js +5 -10
  133. package/web/firestore/utils/getDocSnap.ts +14 -21
@@ -1,10 +1,11 @@
1
1
  import { useContext } from "react";
2
2
  import { FirebaseContext } from "../context/FirebaseContext";
3
3
  /**
4
- * Provides a hook to access the Firestore instance from the Firebase context.
5
- * It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
4
+ * Returns an instance of firestore bound to the closest context.
6
5
  *
7
- * @returns {firestore.Firestore} The Firestore instance from the Firebase context.
6
+ * @group Hook
7
+ *
8
+ * @returns {Firestore}
8
9
  */
9
10
  export const useFirestore = () => {
10
11
  const { firestore } = useContext(FirebaseContext);
@@ -2,10 +2,11 @@ import { useContext } from "react";
2
2
  import { FirebaseContext } from "../context/FirebaseContext";
3
3
 
4
4
  /**
5
- * Provides a hook to access the Firestore instance from the Firebase context.
6
- * It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
5
+ * Returns an instance of firestore bound to the closest context.
7
6
  *
8
- * @returns {firestore.Firestore} The Firestore instance from the Firebase context.
7
+ * @group Hook
8
+ *
9
+ * @returns {Firestore}
9
10
  */
10
11
  export const useFirestore = () => {
11
12
  const { firestore } = useContext(FirebaseContext);
@@ -1,18 +1,37 @@
1
- import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
1
+ import { GetDocDataOptions } from "./utils/getDocData";
2
2
  import { UseQueryOptions } from "@tanstack/react-query";
3
- type UseGetDocOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
3
+ import { AppModel } from "../../types";
4
+ /**
5
+ * @inline
6
+ */
7
+ type UseGetDocOptions<AppModelType extends AppModel = AppModel> = {
8
+ /**
9
+ * Reqct-query options that must include queryKey and shall not define queryFn
10
+ */
4
11
  options: Omit<UseQueryOptions<AppModelType | null, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
5
- path?: string;
6
- pathSegments?: string[];
7
- reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
8
- };
12
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
9
13
  /**
10
- * Custom React Hook to retrieve document data from Firestore using specified parameters.
11
- * @param {Object} options - The options for configuring the Firestore query.
12
- * @param {string} reference - The reference to the document in Firestore.
13
- * @param {string} path - The path to the document in Firestore.
14
- * @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
15
- * @returns {Object} Result of the query containing document data and query status.
14
+ * Executes a query on a Firestore data source and returns the resulting document.
15
+ *
16
+ * @group Hook
17
+ *
18
+ * @param {UseGetDocOptions<AppModelType>} options - Configuration options for the query.
19
+ *
20
+ * @returns {UseQueryResult<AppModelType, Error>} An object containing document's data
21
+ *
22
+ * @example
23
+ * ```jsx
24
+ * export const MyComponent = () => {
25
+ * const doc = useGetDocData({
26
+ * options: {
27
+ * queryKey: ['key']
28
+ * },
29
+ * reference: collection(),
30
+ * path: 'id'
31
+ * });
32
+ * console.log(doc);
33
+ * };
34
+ * ```
16
35
  */
17
- export declare const useGetDocData: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, reference, path, pathSegments }: UseGetDocOptions<AppModelType, DbModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
36
+ export declare const useGetDocData: <AppModelType extends AppModel = AppModel>({ options, reference, path, pathSegments }: UseGetDocOptions<AppModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
18
37
  export {};
@@ -2,12 +2,27 @@ import { useFirestore } from "./useFirestore";
2
2
  import { getDocData } from "./utils/getDocData";
3
3
  import { useQuery } from "@tanstack/react-query";
4
4
  /**
5
- * Custom React Hook to retrieve document data from Firestore using specified parameters.
6
- * @param {Object} options - The options for configuring the Firestore query.
7
- * @param {string} reference - The reference to the document in Firestore.
8
- * @param {string} path - The path to the document in Firestore.
9
- * @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
10
- * @returns {Object} Result of the query containing document data and query status.
5
+ * Executes a query on a Firestore data source and returns the resulting document.
6
+ *
7
+ * @group Hook
8
+ *
9
+ * @param {UseGetDocOptions<AppModelType>} options - Configuration options for the query.
10
+ *
11
+ * @returns {UseQueryResult<AppModelType, Error>} An object containing document's data
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const doc = useGetDocData({
17
+ * options: {
18
+ * queryKey: ['key']
19
+ * },
20
+ * reference: collection(),
21
+ * path: 'id'
22
+ * });
23
+ * console.log(doc);
24
+ * };
25
+ * ```
11
26
  */
12
27
  export const useGetDocData = ({ options, reference, path, pathSegments }) => {
13
28
  const db = useFirestore();
@@ -1,41 +1,52 @@
1
- import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
-
3
1
  import { useFirestore } from "./useFirestore";
4
- import { getDocData } from "./utils/getDocData";
2
+ import { getDocData, GetDocDataOptions } from "./utils/getDocData";
5
3
  import { useQuery, UseQueryOptions } from "@tanstack/react-query";
4
+ import { AppModel } from "../../types";
6
5
 
7
- type UseGetDocOptions<
8
- AppModelType extends DocumentData = DocumentData,
9
- DbModelType extends DocumentData = DocumentData
10
- > = {
6
+ /**
7
+ * @inline
8
+ */
9
+ type UseGetDocOptions<AppModelType extends AppModel = AppModel> = {
10
+ /**
11
+ * Reqct-query options that must include queryKey and shall not define queryFn
12
+ */
11
13
  options: Omit<UseQueryOptions<AppModelType | null, Error, AppModelType>, "queryFn"> &
12
14
  Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
13
- path?: string;
14
- pathSegments?: string[];
15
- reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
16
- };
15
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
17
16
 
18
17
  /**
19
- * Custom React Hook to retrieve document data from Firestore using specified parameters.
20
- * @param {Object} options - The options for configuring the Firestore query.
21
- * @param {string} reference - The reference to the document in Firestore.
22
- * @param {string} path - The path to the document in Firestore.
23
- * @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
24
- * @returns {Object} Result of the query containing document data and query status.
18
+ * Executes a query on a Firestore data source and returns the resulting document.
19
+ *
20
+ * @group Hook
21
+ *
22
+ * @param {UseGetDocOptions<AppModelType>} options - Configuration options for the query.
23
+ *
24
+ * @returns {UseQueryResult<AppModelType, Error>} An object containing document's data
25
+ *
26
+ * @example
27
+ * ```jsx
28
+ * export const MyComponent = () => {
29
+ * const doc = useGetDocData({
30
+ * options: {
31
+ * queryKey: ['key']
32
+ * },
33
+ * reference: collection(),
34
+ * path: 'id'
35
+ * });
36
+ * console.log(doc);
37
+ * };
38
+ * ```
25
39
  */
26
- export const useGetDocData = <
27
- AppModelType extends DocumentData = DocumentData,
28
- DbModelType extends DocumentData = DocumentData
29
- >({
40
+ export const useGetDocData = <AppModelType extends AppModel = AppModel>({
30
41
  options,
31
42
  reference,
32
43
  path,
33
44
  pathSegments
34
- }: UseGetDocOptions<AppModelType, DbModelType>) => {
45
+ }: UseGetDocOptions<AppModelType>) => {
35
46
  const db = useFirestore();
36
47
 
37
48
  return useQuery({
38
49
  ...options,
39
- queryFn: () => getDocData<AppModelType, DbModelType>({ db, reference, path, pathSegments })
50
+ queryFn: () => getDocData<AppModelType>({ db, reference, path, pathSegments })
40
51
  });
41
52
  };
@@ -48,9 +48,9 @@ export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }
48
48
  return () => unsubscribe();
49
49
  }, [ref, doc, isError, onError, isFetching, error]);
50
50
  return useMemo(() => ({
51
- data: doc,
51
+ data: { ...(doc || {}), uid: ref?.id ?? "" },
52
52
  isError,
53
53
  isFetching,
54
54
  error
55
- }), [doc, isError, error, isFetching]);
55
+ }), [doc, isError, error, ref, isFetching]);
56
56
  };
@@ -95,11 +95,11 @@ export const useGetRealtimeDocData = <AppModelType extends AppModel = AppModel>(
95
95
 
96
96
  return useMemo(
97
97
  () => ({
98
- data: doc,
98
+ data: { ...(doc || {}), uid: ref?.id ?? "" } as AppModelType,
99
99
  isError,
100
100
  isFetching,
101
101
  error
102
102
  }),
103
- [doc, isError, error, isFetching]
103
+ [doc, isError, error, ref, isFetching]
104
104
  );
105
105
  };
@@ -1,6 +1,6 @@
1
1
  import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
2
2
  import { UseInfiniteQueryOptions as UseReactInfiniteQueryOptions, QueryKey, UseInfiniteQueryResult, InfiniteData } from "@tanstack/react-query";
3
- import { QueryFilterConstraint } from "./useCompositeFilter";
3
+ import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
4
4
  import { AppModel } from "../../types";
5
5
  /**
6
6
  * @inline
@@ -9,7 +9,7 @@ type UseInfiniteQueryOptions<AppModelType extends AppModel = AppModel, TQueryKey
9
9
  /**
10
10
  * Reqct-query options that must include queryKey and shall not define queryFn
11
11
  */
12
- options: Omit<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryFn"> & Required<Pick<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryKey">>;
12
+ options: Omit<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryNonFilterConstraint>, "queryFn"> & Required<Pick<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryNonFilterConstraint>, "queryKey">>;
13
13
  /**
14
14
  * Reference to a Firestore collection
15
15
  */
@@ -34,7 +34,7 @@ export const useInfiniteQuery = ({ options, collectionReference, queryConstraint
34
34
  const docs = [];
35
35
  if (querySnapshot) {
36
36
  querySnapshot.forEach((doc) => {
37
- docs.push(doc.data());
37
+ docs.push({ ...doc.data(), uid: doc.id });
38
38
  });
39
39
  }
40
40
  return docs;
@@ -14,7 +14,7 @@ import {
14
14
  UseInfiniteQueryResult,
15
15
  InfiniteData
16
16
  } from "@tanstack/react-query";
17
- import { QueryFilterConstraint } from "./useCompositeFilter";
17
+ import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
18
18
  import { AppModel } from "../../types";
19
19
 
20
20
  /**
@@ -31,7 +31,7 @@ type UseInfiniteQueryOptions<AppModelType extends AppModel = AppModel, TQueryKey
31
31
  InfiniteData<AppModelType[]>,
32
32
  AppModelType[],
33
33
  TQueryKey,
34
- QueryConstraint
34
+ QueryNonFilterConstraint
35
35
  >,
36
36
  "queryFn"
37
37
  > &
@@ -43,7 +43,7 @@ type UseInfiniteQueryOptions<AppModelType extends AppModel = AppModel, TQueryKey
43
43
  InfiniteData<AppModelType[]>,
44
44
  AppModelType[],
45
45
  TQueryKey,
46
- QueryConstraint
46
+ QueryNonFilterConstraint
47
47
  >,
48
48
  "queryKey"
49
49
  >
@@ -110,7 +110,7 @@ export const useInfiniteQuery = <AppModelType extends AppModel = AppModel, TQuer
110
110
 
111
111
  if (querySnapshot) {
112
112
  querySnapshot.forEach((doc) => {
113
- docs.push(doc.data());
113
+ docs.push({ ...doc.data(), uid: doc.id });
114
114
  });
115
115
  }
116
116
  return docs;
@@ -1,6 +1,6 @@
1
1
  import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
2
2
  import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
3
- import { QueryFilterConstraint } from "./useCompositeFilter";
3
+ import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
4
4
  import { AppModel } from "../../types";
5
5
  /**
6
6
  * @inline
@@ -33,7 +33,7 @@ export const useQuery = ({ options, collectionReference, queryConstraints = [],
33
33
  const docs = [];
34
34
  if (querySnapshot) {
35
35
  querySnapshot.forEach((doc) => {
36
- docs.push(doc.data());
36
+ docs.push({ ...doc.data(), uid: doc.id });
37
37
  });
38
38
  }
39
39
  return docs;
@@ -12,7 +12,7 @@ import {
12
12
  useQuery as useReactQuery,
13
13
  UseQueryOptions as UseReactQueryOptions
14
14
  } from "@tanstack/react-query";
15
- import { QueryFilterConstraint } from "./useCompositeFilter";
15
+ import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
16
16
  import { AppModel } from "../../types";
17
17
 
18
18
  /**
@@ -85,7 +85,7 @@ export const useQuery = <AppModelType extends AppModel = AppModel>({
85
85
 
86
86
  if (querySnapshot) {
87
87
  querySnapshot.forEach((doc) => {
88
- docs.push(doc.data());
88
+ docs.push({ ...doc.data(), uid: doc.id });
89
89
  });
90
90
  }
91
91
  return docs;
@@ -10,27 +10,6 @@ export type UseQueryConstraints<AppModelType extends AppModel = AppModel> = {
10
10
  */
11
11
  constraints: NonFilterQueryConstraint<AppModelType>[];
12
12
  };
13
- /**
14
- * A generic mothod to build query constraints for firebase
15
- *
16
- * @group Utility
17
- *
18
- * @param {NonFilterQueryConstraint<AppModelType>} constraint
19
- *
20
- * @returns {QueryNonFilterConstraint}
21
- *
22
- * @example
23
- * ```jsx
24
- * const firebaseConfig = {};
25
- * export const constraint = useMemo(() => {
26
- * return buildQueryConstraint({
27
- * type: 'limit',
28
- * limit: 1
29
- * });
30
- * };
31
- * ```
32
- */
33
- export declare const buildQueryConstraint: <AppModelType extends AppModel = AppModel>(constraint: NonFilterQueryConstraint<AppModelType>) => QueryNonFilterConstraint;
34
13
  /**
35
14
  * A hook to build a list of non-filter query constraints for firebase queries.
36
15
  * It uses buildQueryConstraint method and useMemo hook.
@@ -1,43 +1,5 @@
1
- import { startAt, orderBy, limit, limitToLast, startAfter, endAt, endBefore } from "firebase/firestore";
2
1
  import { useMemo } from "react";
3
- /**
4
- * A generic mothod to build query constraints for firebase
5
- *
6
- * @group Utility
7
- *
8
- * @param {NonFilterQueryConstraint<AppModelType>} constraint
9
- *
10
- * @returns {QueryNonFilterConstraint}
11
- *
12
- * @example
13
- * ```jsx
14
- * const firebaseConfig = {};
15
- * export const constraint = useMemo(() => {
16
- * return buildQueryConstraint({
17
- * type: 'limit',
18
- * limit: 1
19
- * });
20
- * };
21
- * ```
22
- */
23
- export const buildQueryConstraint = (constraint) => {
24
- switch (constraint.type) {
25
- case "orderBy":
26
- return orderBy(constraint.fieldPath, constraint.directionStr);
27
- case "startAt":
28
- return startAt(...constraint.arguments);
29
- case "startAfter":
30
- return startAfter(...constraint.arguments);
31
- case "endAt":
32
- return endAt(constraint.value, constraint.key);
33
- case "endBefore":
34
- return endBefore(constraint.value, constraint.key);
35
- case "limit":
36
- return limit(constraint.limit);
37
- case "limitToLast":
38
- return limitToLast(constraint.limit);
39
- }
40
- };
2
+ import { buildQueryConstraint } from "./utils/buildQueryConstraint";
41
3
  /**
42
4
  * A hook to build a list of non-filter query constraints for firebase queries.
43
5
  * It uses buildQueryConstraint method and useMemo hook.
@@ -1,16 +1,8 @@
1
- import {
2
- QueryNonFilterConstraint,
3
- startAt,
4
- orderBy,
5
- limit,
6
- limitToLast,
7
- startAfter,
8
- endAt,
9
- endBefore
10
- } from "firebase/firestore";
1
+ import { QueryNonFilterConstraint } from "firebase/firestore";
11
2
  import { AppModel } from "../../types/AppModel";
12
3
  import { NonFilterQueryConstraint } from "../../types/QueryConstraints";
13
4
  import { useMemo } from "react";
5
+ import { buildQueryConstraint } from "./utils/buildQueryConstraint";
14
6
 
15
7
  /**
16
8
  * @inline
@@ -22,47 +14,6 @@ export type UseQueryConstraints<AppModelType extends AppModel = AppModel> = {
22
14
  constraints: NonFilterQueryConstraint<AppModelType>[];
23
15
  };
24
16
 
25
- /**
26
- * A generic mothod to build query constraints for firebase
27
- *
28
- * @group Utility
29
- *
30
- * @param {NonFilterQueryConstraint<AppModelType>} constraint
31
- *
32
- * @returns {QueryNonFilterConstraint}
33
- *
34
- * @example
35
- * ```jsx
36
- * const firebaseConfig = {};
37
- * export const constraint = useMemo(() => {
38
- * return buildQueryConstraint({
39
- * type: 'limit',
40
- * limit: 1
41
- * });
42
- * };
43
- * ```
44
- */
45
- export const buildQueryConstraint = <AppModelType extends AppModel = AppModel>(
46
- constraint: NonFilterQueryConstraint<AppModelType>
47
- ): QueryNonFilterConstraint => {
48
- switch (constraint.type) {
49
- case "orderBy":
50
- return orderBy(constraint.fieldPath as string, constraint.directionStr);
51
- case "startAt":
52
- return startAt(...constraint.arguments);
53
- case "startAfter":
54
- return startAfter(...constraint.arguments);
55
- case "endAt":
56
- return endAt(constraint.value, constraint.key);
57
- case "endBefore":
58
- return endBefore(constraint.value, constraint.key);
59
- case "limit":
60
- return limit(constraint.limit);
61
- case "limitToLast":
62
- return limitToLast(constraint.limit);
63
- }
64
- };
65
-
66
17
  /**
67
18
  * A hook to build a list of non-filter query constraints for firebase queries.
68
19
  * It uses buildQueryConstraint method and useMemo hook.
@@ -1,15 +1,40 @@
1
1
  import { UseMutationOptions } from "@tanstack/react-query";
2
2
  import { Transaction } from "firebase/firestore";
3
3
  import { FirebaseError } from "firebase/app";
4
- export type UseRunTransactionValues = <AppModelType = unknown>(transaction: Transaction) => AppModelType;
5
- export type UseRunTransactionOptions<AppModelType = unknown, TContext = unknown> = {
6
- options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseRunTransactionValues = <T = unknown>(transaction: Transaction) => T;
8
+ /**
9
+ * @inline
10
+ */
11
+ export type UseRunTransactionOptions<T = unknown, TContext = unknown> = {
12
+ /**
13
+ * Reqct-mutation options that shall omit mutationFn
14
+ */
15
+ options?: Omit<UseMutationOptions<T, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
7
16
  };
8
17
  /**
9
- * Custom hook to execute a Firestore transaction using the useMutation hook.
18
+ * Executes a Firestore transaction
19
+ *
20
+ * @group Hook
21
+ *
22
+ * @param {UseRunTransactionOptions<T>} options - Configuration options for the mutation.
23
+ *
24
+ * @returns {UseMutationResult<T, Error>} An object representing mutation
10
25
  *
11
- * @param {UseRunTransactionOptions<AppModelType, TContext>} options - Configuration options for running the transaction.
12
- * @param {Object} options.options - Options to customize the behavior of useMutation and runTransaction.
13
- * @returns {UseMutationResult} The result object from the useMutation hook, allowing to track the transaction state and outcome.
26
+ * @example
27
+ * ```jsx
28
+ * export const MyComponent = () => {
29
+ * const doc = useGetDocData({
30
+ * options: {
31
+ * queryKey: ['key']
32
+ * },
33
+ * reference: collection(),
34
+ * path: 'id'
35
+ * });
36
+ * console.log(doc);
37
+ * };
38
+ * ```
14
39
  */
15
- export declare const useRunTransaction: <AppModelType = unknown, TContext = unknown>({ options }: UseRunTransactionOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, FirebaseError, UseRunTransactionValues, TContext>;
40
+ export declare const useRunTransaction: <T = unknown, TContext = unknown>({ options }: UseRunTransactionOptions<T, TContext>) => import("@tanstack/react-query").UseMutationResult<T, FirebaseError, UseRunTransactionValues, TContext>;
@@ -2,11 +2,27 @@ import { useMutation } from "@tanstack/react-query";
2
2
  import { runTransaction } from "firebase/firestore";
3
3
  import { useFirestore } from "./useFirestore";
4
4
  /**
5
- * Custom hook to execute a Firestore transaction using the useMutation hook.
5
+ * Executes a Firestore transaction
6
6
  *
7
- * @param {UseRunTransactionOptions<AppModelType, TContext>} options - Configuration options for running the transaction.
8
- * @param {Object} options.options - Options to customize the behavior of useMutation and runTransaction.
9
- * @returns {UseMutationResult} The result object from the useMutation hook, allowing to track the transaction state and outcome.
7
+ * @group Hook
8
+ *
9
+ * @param {UseRunTransactionOptions<T>} options - Configuration options for the mutation.
10
+ *
11
+ * @returns {UseMutationResult<T, Error>} An object representing mutation
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const doc = useGetDocData({
17
+ * options: {
18
+ * queryKey: ['key']
19
+ * },
20
+ * reference: collection(),
21
+ * path: 'id'
22
+ * });
23
+ * console.log(doc);
24
+ * };
25
+ * ```
10
26
  */
11
27
  export const useRunTransaction = ({ options = {} }) => {
12
28
  const db = useFirestore();
@@ -4,28 +4,53 @@ import { runTransaction, Transaction } from "firebase/firestore";
4
4
  import { FirebaseError } from "firebase/app";
5
5
  import { useFirestore } from "./useFirestore";
6
6
 
7
- export type UseRunTransactionValues = <AppModelType = unknown>(transaction: Transaction) => AppModelType;
7
+ /**
8
+ * @inline
9
+ */
10
+ export type UseRunTransactionValues = <T = unknown>(transaction: Transaction) => T;
8
11
 
9
- export type UseRunTransactionOptions<AppModelType = unknown, TContext = unknown> = {
10
- options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
12
+ /**
13
+ * @inline
14
+ */
15
+ export type UseRunTransactionOptions<T = unknown, TContext = unknown> = {
16
+ /**
17
+ * Reqct-mutation options that shall omit mutationFn
18
+ */
19
+ options?: Omit<UseMutationOptions<T, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
11
20
  };
12
21
 
13
22
  /**
14
- * Custom hook to execute a Firestore transaction using the useMutation hook.
23
+ * Executes a Firestore transaction
24
+ *
25
+ * @group Hook
26
+ *
27
+ * @param {UseRunTransactionOptions<T>} options - Configuration options for the mutation.
28
+ *
29
+ * @returns {UseMutationResult<T, Error>} An object representing mutation
15
30
  *
16
- * @param {UseRunTransactionOptions<AppModelType, TContext>} options - Configuration options for running the transaction.
17
- * @param {Object} options.options - Options to customize the behavior of useMutation and runTransaction.
18
- * @returns {UseMutationResult} The result object from the useMutation hook, allowing to track the transaction state and outcome.
31
+ * @example
32
+ * ```jsx
33
+ * export const MyComponent = () => {
34
+ * const doc = useGetDocData({
35
+ * options: {
36
+ * queryKey: ['key']
37
+ * },
38
+ * reference: collection(),
39
+ * path: 'id'
40
+ * });
41
+ * console.log(doc);
42
+ * };
43
+ * ```
19
44
  */
20
- export const useRunTransaction = <AppModelType = unknown, TContext = unknown>({
45
+ export const useRunTransaction = <T = unknown, TContext = unknown>({
21
46
  options = {}
22
- }: UseRunTransactionOptions<AppModelType, TContext>) => {
47
+ }: UseRunTransactionOptions<T, TContext>) => {
23
48
  const db = useFirestore();
24
49
 
25
50
  return useMutation({
26
51
  ...options,
27
52
  mutationFn: async (transactionFn) => {
28
- return runTransaction<AppModelType>(db, transactionFn);
53
+ return runTransaction<T>(db, transactionFn);
29
54
  }
30
55
  });
31
56
  };