react-query-firebase 2.0.0-rc6 → 2.0.0-rc7

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 (274) hide show
  1. package/package.json +16 -13
  2. package/web/analytics/index.d.ts +3 -0
  3. package/web/analytics/index.js +3 -0
  4. package/web/analytics/index.ts +3 -0
  5. package/web/analytics/useAnalytics.d.ts +5 -0
  6. package/web/analytics/useAnalytics.js +10 -0
  7. package/web/analytics/useAnalytics.ts +12 -0
  8. package/web/analytics/useLogEvent.d.ts +17 -0
  9. package/web/analytics/useLogEvent.js +21 -0
  10. package/web/analytics/useLogEvent.ts +34 -0
  11. package/web/analytics/useSetAnalyticsCollectionEnabled.d.ts +10 -0
  12. package/web/analytics/useSetAnalyticsCollectionEnabled.js +16 -0
  13. package/web/analytics/useSetAnalyticsCollectionEnabled.ts +22 -0
  14. package/web/auth/index.d.ts +13 -0
  15. package/web/auth/index.js +13 -0
  16. package/web/auth/index.ts +13 -0
  17. package/web/auth/mutation-keys.d.ts +7 -0
  18. package/web/auth/mutation-keys.js +19 -0
  19. package/web/auth/mutation-keys.ts +19 -0
  20. package/web/auth/useAuth.d.ts +5 -0
  21. package/web/auth/useAuth.js +10 -0
  22. package/web/auth/useAuth.ts +12 -0
  23. package/web/auth/useAuthStateReady.d.ts +6 -0
  24. package/web/auth/useAuthStateReady.js +19 -0
  25. package/web/auth/useAuthStateReady.ts +24 -0
  26. package/web/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +15 -0
  27. package/web/auth/useCreateUserWitEmailAndPasswordMutation.js +19 -0
  28. package/web/auth/useCreateUserWitEmailAndPasswordMutation.ts +33 -0
  29. package/web/auth/useCurrentUser.d.ts +6 -0
  30. package/web/auth/useCurrentUser.js +20 -0
  31. package/web/auth/useCurrentUser.ts +24 -0
  32. package/web/auth/useIdToken.d.ts +30 -0
  33. package/web/auth/useIdToken.js +66 -0
  34. package/web/auth/useIdToken.ts +83 -0
  35. package/web/auth/useReauthenticateWitCredentialMutation.d.ts +16 -0
  36. package/web/auth/useReauthenticateWitCredentialMutation.js +18 -0
  37. package/web/auth/useReauthenticateWitCredentialMutation.ts +31 -0
  38. package/web/auth/useReauthenticateWitRedirectMutation.d.ts +17 -0
  39. package/web/auth/useReauthenticateWitRedirectMutation.js +18 -0
  40. package/web/auth/useReauthenticateWitRedirectMutation.ts +37 -0
  41. package/web/auth/useSendEmailVerificationMutation.d.ts +13 -0
  42. package/web/auth/useSendEmailVerificationMutation.js +16 -0
  43. package/web/auth/useSendEmailVerificationMutation.ts +28 -0
  44. package/web/auth/useSignInWitEmailAndPasswordMutation.d.ts +15 -0
  45. package/web/auth/useSignInWitEmailAndPasswordMutation.js +19 -0
  46. package/web/auth/useSignInWitEmailAndPasswordMutation.ts +34 -0
  47. package/web/auth/useSignInWitRedirectMutation.d.ts +14 -0
  48. package/web/auth/useSignInWitRedirectMutation.js +18 -0
  49. package/web/auth/useSignInWitRedirectMutation.ts +33 -0
  50. package/web/auth/useSignOutMutation.d.ts +10 -0
  51. package/web/auth/useSignOutMutation.js +19 -0
  52. package/web/auth/useSignOutMutation.ts +25 -0
  53. package/web/auth/useUpdateProfileMutation.d.ts +15 -0
  54. package/web/auth/useUpdateProfileMutation.js +17 -0
  55. package/web/auth/useUpdateProfileMutation.ts +30 -0
  56. package/web/context/FirebaseContext.d.ts +19 -0
  57. package/web/context/FirebaseContext.js +8 -0
  58. package/web/context/FirebaseContext.ts +25 -0
  59. package/web/context/FirebaseContextProvider.d.ts +127 -0
  60. package/web/context/FirebaseContextProvider.js +100 -0
  61. package/web/context/FirebaseContextProvider.tsx +231 -0
  62. package/web/context/index.d.ts +1 -0
  63. package/web/context/index.js +1 -0
  64. package/web/context/index.ts +1 -0
  65. package/web/firestore/index.d.ts +16 -0
  66. package/web/firestore/index.js +16 -0
  67. package/web/firestore/index.ts +16 -0
  68. package/web/firestore/useAddDocMutation.d.ts +23 -0
  69. package/web/firestore/useAddDocMutation.js +26 -0
  70. package/web/firestore/useAddDocMutation.ts +62 -0
  71. package/web/firestore/useBatchWrite.d.ts +15 -0
  72. package/web/firestore/useBatchWrite.js +21 -0
  73. package/web/firestore/useBatchWrite.ts +31 -0
  74. package/web/firestore/useCollectionReference.d.ts +18 -0
  75. package/web/firestore/useCollectionReference.js +24 -0
  76. package/web/firestore/useCollectionReference.ts +37 -0
  77. package/web/firestore/useCompositeFilter.d.ts +30 -0
  78. package/web/firestore/useCompositeFilter.js +43 -0
  79. package/web/firestore/useCompositeFilter.ts +86 -0
  80. package/web/firestore/useCountQuery.d.ts +23 -0
  81. package/web/firestore/useCountQuery.js +30 -0
  82. package/web/firestore/useCountQuery.ts +65 -0
  83. package/web/firestore/useDeleteDocMutation.d.ts +18 -0
  84. package/web/firestore/useDeleteDocMutation.js +23 -0
  85. package/web/firestore/useDeleteDocMutation.ts +47 -0
  86. package/web/firestore/useDocReference.d.ts +19 -0
  87. package/web/firestore/useDocReference.js +45 -0
  88. package/web/firestore/useDocReference.ts +68 -0
  89. package/web/firestore/useDocReferences.d.ts +23 -0
  90. package/web/firestore/useDocReferences.js +31 -0
  91. package/web/firestore/useDocReferences.ts +44 -0
  92. package/web/firestore/useFirestore.d.ts +7 -0
  93. package/web/firestore/useFirestore.js +12 -0
  94. package/web/firestore/useFirestore.ts +13 -0
  95. package/web/firestore/useGetDocData.d.ts +18 -0
  96. package/web/firestore/useGetDocData.js +18 -0
  97. package/web/firestore/useGetDocData.ts +41 -0
  98. package/web/firestore/useGetRealtimeDocData.d.ts +57 -0
  99. package/web/firestore/useGetRealtimeDocData.js +56 -0
  100. package/web/firestore/useGetRealtimeDocData.ts +104 -0
  101. package/web/firestore/useInfiniteQuery.d.ts +18 -0
  102. package/web/firestore/useInfiniteQuery.js +28 -0
  103. package/web/firestore/useInfiniteQuery.ts +91 -0
  104. package/web/firestore/useQuery.d.ts +25 -0
  105. package/web/firestore/useQuery.js +34 -0
  106. package/web/firestore/useQuery.ts +72 -0
  107. package/web/firestore/useRunTransaction.d.ts +15 -0
  108. package/web/firestore/useRunTransaction.js +19 -0
  109. package/web/firestore/useRunTransaction.ts +31 -0
  110. package/web/firestore/useSetDocMutation.d.ts +20 -0
  111. package/web/firestore/useSetDocMutation.js +25 -0
  112. package/web/firestore/useSetDocMutation.ts +52 -0
  113. package/web/firestore/useUpdateDocMutation.d.ts +31 -0
  114. package/web/firestore/useUpdateDocMutation.js +37 -0
  115. package/web/firestore/useUpdateDocMutation.ts +74 -0
  116. package/web/firestore/utils/getDocData.d.ts +18 -0
  117. package/web/firestore/utils/getDocData.js +22 -0
  118. package/web/firestore/utils/getDocData.ts +39 -0
  119. package/web/firestore/utils/getDocRef.d.ts +19 -0
  120. package/web/firestore/utils/getDocRef.js +25 -0
  121. package/web/firestore/utils/getDocRef.ts +46 -0
  122. package/web/firestore/utils/getDocSnap.d.ts +20 -0
  123. package/web/firestore/utils/getDocSnap.js +25 -0
  124. package/web/firestore/utils/getDocSnap.ts +41 -0
  125. package/web/index.d.ts +5 -0
  126. package/web/index.js +5 -0
  127. package/web/index.ts +5 -0
  128. package/web/remoteConfig/index.d.ts +3 -0
  129. package/web/remoteConfig/index.js +3 -0
  130. package/web/remoteConfig/index.ts +3 -0
  131. package/web/remoteConfig/useFetchAndActivate.d.ts +11 -0
  132. package/web/remoteConfig/useFetchAndActivate.js +31 -0
  133. package/web/remoteConfig/useFetchAndActivate.ts +36 -0
  134. package/web/remoteConfig/useGetValue.d.ts +7 -0
  135. package/web/remoteConfig/useGetValue.js +15 -0
  136. package/web/remoteConfig/useGetValue.ts +16 -0
  137. package/web/remoteConfig/useRemoteConfig.d.ts +5 -0
  138. package/web/remoteConfig/useRemoteConfig.js +10 -0
  139. package/web/remoteConfig/useRemoteConfig.ts +11 -0
  140. /package/{dist/src → react-native}/analytics/index.d.ts +0 -0
  141. /package/{dist/src → react-native}/analytics/index.js +0 -0
  142. /package/{src → react-native}/analytics/index.ts +0 -0
  143. /package/{dist/src → react-native}/analytics/useAnalytics.d.ts +0 -0
  144. /package/{dist/src → react-native}/analytics/useAnalytics.js +0 -0
  145. /package/{src → react-native}/analytics/useAnalytics.ts +0 -0
  146. /package/{dist/src → react-native}/analytics/useLogEvent.d.ts +0 -0
  147. /package/{dist/src → react-native}/analytics/useLogEvent.js +0 -0
  148. /package/{src → react-native}/analytics/useLogEvent.ts +0 -0
  149. /package/{dist/src → react-native}/analytics/useSetAnalyticsCollectionEnabled.d.ts +0 -0
  150. /package/{dist/src → react-native}/analytics/useSetAnalyticsCollectionEnabled.js +0 -0
  151. /package/{src → react-native}/analytics/useSetAnalyticsCollectionEnabled.ts +0 -0
  152. /package/{dist/src → react-native}/auth/index.d.ts +0 -0
  153. /package/{dist/src → react-native}/auth/index.js +0 -0
  154. /package/{src → react-native}/auth/index.ts +0 -0
  155. /package/{dist/src → react-native}/auth/mutation-keys.d.ts +0 -0
  156. /package/{dist/src → react-native}/auth/mutation-keys.js +0 -0
  157. /package/{src → react-native}/auth/mutation-keys.ts +0 -0
  158. /package/{dist/src → react-native}/auth/useAuth.d.ts +0 -0
  159. /package/{dist/src → react-native}/auth/useAuth.js +0 -0
  160. /package/{src → react-native}/auth/useAuth.ts +0 -0
  161. /package/{dist/src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +0 -0
  162. /package/{dist/src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.js +0 -0
  163. /package/{src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.ts +0 -0
  164. /package/{dist/src → react-native}/auth/useCurrentUser.d.ts +0 -0
  165. /package/{dist/src → react-native}/auth/useCurrentUser.js +0 -0
  166. /package/{src → react-native}/auth/useCurrentUser.ts +0 -0
  167. /package/{dist/src → react-native}/auth/useIdToken.d.ts +0 -0
  168. /package/{dist/src → react-native}/auth/useIdToken.js +0 -0
  169. /package/{src → react-native}/auth/useIdToken.ts +0 -0
  170. /package/{dist/src → react-native}/auth/useReauthenticateWitCredentialMutation.d.ts +0 -0
  171. /package/{dist/src → react-native}/auth/useReauthenticateWitCredentialMutation.js +0 -0
  172. /package/{src → react-native}/auth/useReauthenticateWitCredentialMutation.ts +0 -0
  173. /package/{dist/src → react-native}/auth/useReauthenticateWitRedirectMutation.d.ts +0 -0
  174. /package/{dist/src → react-native}/auth/useReauthenticateWitRedirectMutation.js +0 -0
  175. /package/{src → react-native}/auth/useReauthenticateWitRedirectMutation.ts +0 -0
  176. /package/{dist/src → react-native}/auth/useSendEmailVerificationMutation.d.ts +0 -0
  177. /package/{dist/src → react-native}/auth/useSendEmailVerificationMutation.js +0 -0
  178. /package/{src → react-native}/auth/useSendEmailVerificationMutation.ts +0 -0
  179. /package/{dist/src → react-native}/auth/useSignInWitEmailAndPasswordMutation.d.ts +0 -0
  180. /package/{dist/src → react-native}/auth/useSignInWitEmailAndPasswordMutation.js +0 -0
  181. /package/{src → react-native}/auth/useSignInWitEmailAndPasswordMutation.ts +0 -0
  182. /package/{dist/src → react-native}/auth/useSignInWitRedirectMutation.d.ts +0 -0
  183. /package/{dist/src → react-native}/auth/useSignInWitRedirectMutation.js +0 -0
  184. /package/{src → react-native}/auth/useSignInWitRedirectMutation.ts +0 -0
  185. /package/{dist/src → react-native}/auth/useSignOutMutation.d.ts +0 -0
  186. /package/{dist/src → react-native}/auth/useSignOutMutation.js +0 -0
  187. /package/{src → react-native}/auth/useSignOutMutation.ts +0 -0
  188. /package/{dist/src → react-native}/auth/useUpdateProfileMutation.d.ts +0 -0
  189. /package/{dist/src → react-native}/auth/useUpdateProfileMutation.js +0 -0
  190. /package/{src → react-native}/auth/useUpdateProfileMutation.ts +0 -0
  191. /package/{dist/src → react-native}/context/FirebaseContext.d.ts +0 -0
  192. /package/{dist/src → react-native}/context/FirebaseContext.js +0 -0
  193. /package/{src → react-native}/context/FirebaseContext.ts +0 -0
  194. /package/{dist/src → react-native}/context/FirebaseContextProvider.d.ts +0 -0
  195. /package/{dist/src → react-native}/context/FirebaseContextProvider.js +0 -0
  196. /package/{src → react-native}/context/FirebaseContextProvider.tsx +0 -0
  197. /package/{dist/src → react-native}/context/index.d.ts +0 -0
  198. /package/{dist/src → react-native}/context/index.js +0 -0
  199. /package/{src → react-native}/context/index.ts +0 -0
  200. /package/{dist/src → react-native}/firestore/index.d.ts +0 -0
  201. /package/{dist/src → react-native}/firestore/index.js +0 -0
  202. /package/{src → react-native}/firestore/index.ts +0 -0
  203. /package/{dist/src → react-native}/firestore/useAddDocMutation.d.ts +0 -0
  204. /package/{dist/src → react-native}/firestore/useAddDocMutation.js +0 -0
  205. /package/{src → react-native}/firestore/useAddDocMutation.ts +0 -0
  206. /package/{dist/src → react-native}/firestore/useBatchWrite.d.ts +0 -0
  207. /package/{dist/src → react-native}/firestore/useBatchWrite.js +0 -0
  208. /package/{src → react-native}/firestore/useBatchWrite.ts +0 -0
  209. /package/{dist/src → react-native}/firestore/useCollectionReference.d.ts +0 -0
  210. /package/{dist/src → react-native}/firestore/useCollectionReference.js +0 -0
  211. /package/{src → react-native}/firestore/useCollectionReference.ts +0 -0
  212. /package/{dist/src → react-native}/firestore/useCompositeFilter.d.ts +0 -0
  213. /package/{dist/src → react-native}/firestore/useCompositeFilter.js +0 -0
  214. /package/{src → react-native}/firestore/useCompositeFilter.ts +0 -0
  215. /package/{dist/src → react-native}/firestore/useCountQuery.d.ts +0 -0
  216. /package/{dist/src → react-native}/firestore/useCountQuery.js +0 -0
  217. /package/{src → react-native}/firestore/useCountQuery.ts +0 -0
  218. /package/{dist/src → react-native}/firestore/useDeleteDocMutation.d.ts +0 -0
  219. /package/{dist/src → react-native}/firestore/useDeleteDocMutation.js +0 -0
  220. /package/{src → react-native}/firestore/useDeleteDocMutation.ts +0 -0
  221. /package/{dist/src → react-native}/firestore/useDocReference.d.ts +0 -0
  222. /package/{dist/src → react-native}/firestore/useDocReference.js +0 -0
  223. /package/{src → react-native}/firestore/useDocReference.ts +0 -0
  224. /package/{dist/src → react-native}/firestore/useDocReferences.d.ts +0 -0
  225. /package/{dist/src → react-native}/firestore/useDocReferences.js +0 -0
  226. /package/{src → react-native}/firestore/useDocReferences.ts +0 -0
  227. /package/{dist/src → react-native}/firestore/useFirestore.d.ts +0 -0
  228. /package/{dist/src → react-native}/firestore/useFirestore.js +0 -0
  229. /package/{src → react-native}/firestore/useFirestore.ts +0 -0
  230. /package/{dist/src → react-native}/firestore/useGetDocData.d.ts +0 -0
  231. /package/{dist/src → react-native}/firestore/useGetDocData.js +0 -0
  232. /package/{src → react-native}/firestore/useGetDocData.ts +0 -0
  233. /package/{dist/src → react-native}/firestore/useGetRealtimeDocData.d.ts +0 -0
  234. /package/{dist/src → react-native}/firestore/useGetRealtimeDocData.js +0 -0
  235. /package/{src → react-native}/firestore/useGetRealtimeDocData.ts +0 -0
  236. /package/{dist/src → react-native}/firestore/useInfiniteQuery.d.ts +0 -0
  237. /package/{dist/src → react-native}/firestore/useInfiniteQuery.js +0 -0
  238. /package/{src → react-native}/firestore/useInfiniteQuery.ts +0 -0
  239. /package/{dist/src → react-native}/firestore/useQuery.d.ts +0 -0
  240. /package/{dist/src → react-native}/firestore/useQuery.js +0 -0
  241. /package/{src → react-native}/firestore/useQuery.ts +0 -0
  242. /package/{dist/src → react-native}/firestore/useRunTransaction.d.ts +0 -0
  243. /package/{dist/src → react-native}/firestore/useRunTransaction.js +0 -0
  244. /package/{src → react-native}/firestore/useRunTransaction.ts +0 -0
  245. /package/{dist/src → react-native}/firestore/useSetDocMutation.d.ts +0 -0
  246. /package/{dist/src → react-native}/firestore/useSetDocMutation.js +0 -0
  247. /package/{src → react-native}/firestore/useSetDocMutation.ts +0 -0
  248. /package/{dist/src → react-native}/firestore/useUpdateDocMutation.d.ts +0 -0
  249. /package/{dist/src → react-native}/firestore/useUpdateDocMutation.js +0 -0
  250. /package/{src → react-native}/firestore/useUpdateDocMutation.ts +0 -0
  251. /package/{dist/src → react-native}/firestore/utils/getDocData.d.ts +0 -0
  252. /package/{dist/src → react-native}/firestore/utils/getDocData.js +0 -0
  253. /package/{src → react-native}/firestore/utils/getDocData.ts +0 -0
  254. /package/{dist/src → react-native}/firestore/utils/getDocRef.d.ts +0 -0
  255. /package/{dist/src → react-native}/firestore/utils/getDocRef.js +0 -0
  256. /package/{src → react-native}/firestore/utils/getDocRef.ts +0 -0
  257. /package/{dist/src → react-native}/firestore/utils/getDocSnap.d.ts +0 -0
  258. /package/{dist/src → react-native}/firestore/utils/getDocSnap.js +0 -0
  259. /package/{src → react-native}/firestore/utils/getDocSnap.ts +0 -0
  260. /package/{dist/src → react-native}/index.d.ts +0 -0
  261. /package/{dist/src → react-native}/index.js +0 -0
  262. /package/{src → react-native}/index.ts +0 -0
  263. /package/{dist/src → react-native}/remoteConfig/index.d.ts +0 -0
  264. /package/{dist/src → react-native}/remoteConfig/index.js +0 -0
  265. /package/{src → react-native}/remoteConfig/index.ts +0 -0
  266. /package/{dist/src → react-native}/remoteConfig/useFetchAndActivate.d.ts +0 -0
  267. /package/{dist/src → react-native}/remoteConfig/useFetchAndActivate.js +0 -0
  268. /package/{src → react-native}/remoteConfig/useFetchAndActivate.ts +0 -0
  269. /package/{dist/src → react-native}/remoteConfig/useGetValue.d.ts +0 -0
  270. /package/{dist/src → react-native}/remoteConfig/useGetValue.js +0 -0
  271. /package/{src → react-native}/remoteConfig/useGetValue.ts +0 -0
  272. /package/{dist/src → react-native}/remoteConfig/useRemoteConfig.d.ts +0 -0
  273. /package/{dist/src → react-native}/remoteConfig/useRemoteConfig.js +0 -0
  274. /package/{src → react-native}/remoteConfig/useRemoteConfig.ts +0 -0
@@ -0,0 +1,31 @@
1
+ import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
+ import { writeBatch, WriteBatch } from "firebase/firestore";
3
+
4
+ import { FirebaseError } from "firebase/app";
5
+ import { useFirestore } from "./useFirestore";
6
+
7
+ export type UseBatchWriteVariables = (batch: WriteBatch) => Promise<void> | void;
8
+
9
+ export type UseBatchWriteOptions<TContext = unknown> = {
10
+ options?: Omit<UseMutationOptions<void, FirebaseError, UseBatchWriteVariables, TContext>, "mutationFn">;
11
+ };
12
+
13
+ /**
14
+ * Custom hook to perform batch write operations using Firestore.
15
+ * Utilizes a mutation to carry out the batch write transaction.
16
+ * @template TContext - The type of context that can be passed into the hook, defaults to unknown.
17
+ * @param {Object} options - The configuration options for the mutation operation.
18
+ * @returns {Object} Returns an object composed of elements returned by useMutation, including properties such as status, and functions to trigger and control the mutation process.
19
+ */
20
+ export const useBatchWrite = <TContext = unknown>({ options = {} }: UseBatchWriteOptions<TContext> = {}) => {
21
+ const db = useFirestore();
22
+
23
+ return useMutation({
24
+ ...options,
25
+ mutationFn: async (batchWriteFn) => {
26
+ const batch = writeBatch(db);
27
+ await batchWriteFn(batch);
28
+ return batch.commit();
29
+ }
30
+ });
31
+ };
@@ -0,0 +1,18 @@
1
+ import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
+ export type UseCollectionReferenceOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
3
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
4
+ path: string;
5
+ pathSegments?: string[];
6
+ };
7
+ /**
8
+ * Creates a reference to a Firestore collection based on the provided path, reference, and path segments.
9
+ *
10
+ * This hook utilizes useMemo for optimization, ensuring the collection reference is recalculated only when its dependencies change.
11
+ *
12
+ * @param {UseCollectionReferenceOptions<AppModelType, DbModelType>} options - The options including path, reference, and pathSegments to construct the Firestore collection reference.
13
+ * @param {string} options.path - The base path for the collection.
14
+ * @param {FirestoreReference} options.reference - An optional Firestore reference object that should be of type "collection".
15
+ * @param {string[]} options.pathSegments - Additional path segments to append to the base path.
16
+ * @returns {CollectionReference} A Firestore collection reference constructed using the specified path, reference, and path segments.
17
+ */
18
+ export declare const useCollectionReference: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, reference, pathSegments }: UseCollectionReferenceOptions<AppModelType, DbModelType>) => CollectionReference<DocumentData, DocumentData>;
@@ -0,0 +1,24 @@
1
+ import { collection } from "firebase/firestore";
2
+ import { useMemo } from "react";
3
+ import { useFirestore } from "./useFirestore";
4
+ /**
5
+ * Creates a reference to a Firestore collection based on the provided path, reference, and path segments.
6
+ *
7
+ * This hook utilizes useMemo for optimization, ensuring the collection reference is recalculated only when its dependencies change.
8
+ *
9
+ * @param {UseCollectionReferenceOptions<AppModelType, DbModelType>} options - The options including path, reference, and pathSegments to construct the Firestore collection reference.
10
+ * @param {string} options.path - The base path for the collection.
11
+ * @param {FirestoreReference} options.reference - An optional Firestore reference object that should be of type "collection".
12
+ * @param {string[]} options.pathSegments - Additional path segments to append to the base path.
13
+ * @returns {CollectionReference} A Firestore collection reference constructed using the specified path, reference, and path segments.
14
+ */
15
+ export const useCollectionReference = ({ path, reference, pathSegments }) => {
16
+ const db = useFirestore();
17
+ return useMemo(() => {
18
+ return !reference
19
+ ? collection(db, path || "", ...(pathSegments || []))
20
+ : reference.type === "collection"
21
+ ? collection(reference, path, ...(pathSegments || []))
22
+ : collection(reference, path, ...(pathSegments || []));
23
+ }, [path, reference?.path, pathSegments]);
24
+ };
@@ -0,0 +1,37 @@
1
+ import { collection, CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
+
3
+ import { useMemo } from "react";
4
+ import { useFirestore } from "./useFirestore";
5
+
6
+ export type UseCollectionReferenceOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
7
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
8
+ path: string;
9
+ pathSegments?: string[];
10
+ };
11
+
12
+ /**
13
+ * Creates a reference to a Firestore collection based on the provided path, reference, and path segments.
14
+ *
15
+ * This hook utilizes useMemo for optimization, ensuring the collection reference is recalculated only when its dependencies change.
16
+ *
17
+ * @param {UseCollectionReferenceOptions<AppModelType, DbModelType>} options - The options including path, reference, and pathSegments to construct the Firestore collection reference.
18
+ * @param {string} options.path - The base path for the collection.
19
+ * @param {FirestoreReference} options.reference - An optional Firestore reference object that should be of type "collection".
20
+ * @param {string[]} options.pathSegments - Additional path segments to append to the base path.
21
+ * @returns {CollectionReference} A Firestore collection reference constructed using the specified path, reference, and path segments.
22
+ */
23
+ export const useCollectionReference = <AppModelType, DbModelType extends DocumentData = DocumentData>({
24
+ path,
25
+ reference,
26
+ pathSegments
27
+ }: UseCollectionReferenceOptions<AppModelType, DbModelType>) => {
28
+ const db = useFirestore();
29
+
30
+ return useMemo(() => {
31
+ return !reference
32
+ ? collection(db, path || "", ...(pathSegments || []))
33
+ : reference.type === "collection"
34
+ ? collection(reference, path, ...(pathSegments || []))
35
+ : collection(reference, path, ...(pathSegments || []));
36
+ }, [path, reference?.path, pathSegments]);
37
+ };
@@ -0,0 +1,30 @@
1
+ import { DocumentData, QueryCompositeFilterConstraint, WhereFilterOp } from "firebase/firestore";
2
+ type CompositeFilterDocumentData = DocumentData;
3
+ export type QueryElement<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = Partial<QueryCompositeFilterConstraint> & {
4
+ children?: QueryElement[];
5
+ field?: keyof (DbModelType & {
6
+ documentId?: string[];
7
+ });
8
+ value?: DbModelType[keyof DbModelType];
9
+ op?: WhereFilterOp;
10
+ };
11
+ export type CompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = QueryCompositeFilterConstraint & {
12
+ children: QueryElement<DbModelType & {
13
+ documentId?: string[];
14
+ }>[];
15
+ };
16
+ export type UseCompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
17
+ query?: CompositeFilter<DbModelType>;
18
+ };
19
+ /**
20
+ * A custom hook that generates a composite filter for database queries, using the provided query configuration.
21
+ * It applies either an 'OR' or 'AND' logical operation based on the type specified in the query.
22
+ *
23
+ * @param {Object} query - The query configuration object that contains subqueries and a type for logical combination.
24
+ * @param {string} query.type - The type of composite operation ('or'/'and').
25
+ * @param {Array} query.children - An array of subqueries that will be processed to form the composite filter.
26
+ *
27
+ * @returns {(Function|undefined)} A composite query filter constraint function formed by combining subqueries or undefined if there are no valid constraints.
28
+ */
29
+ export declare const useCompositeFilter: <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>({ query }: UseCompositeFilter<DbModelType>) => QueryCompositeFilterConstraint | undefined;
30
+ export {};
@@ -0,0 +1,43 @@
1
+ import { and, documentId, or, where } from "firebase/firestore";
2
+ import { useMemo } from "react";
3
+ /**
4
+ * Constructs a composite query filter based on the provided query structure.
5
+ * It recursively builds query constraints using logical "or" or "and" operators.
6
+ *
7
+ * @param {QueryElement<DbModelType>} query - The query element or structure to be evaluated and transformed into filter constraints.
8
+ * @returns {QueryFilterConstraint | null} A constructed query filter constraint based on the input query, or null if no valid constraints can be derived.
9
+ */
10
+ const buildCompositeQuery = (query) => {
11
+ if (query.children) {
12
+ const queryConstraints = query.children
13
+ .map((subQuery) => buildCompositeQuery(subQuery))
14
+ .filter((constraint) => !!constraint);
15
+ if (queryConstraints.length <= 0) {
16
+ return null;
17
+ }
18
+ return query.type === "or" ? or(...queryConstraints) : and(...queryConstraints);
19
+ }
20
+ if (query.field && query.op) {
21
+ return where(query.field === "documentId" ? documentId() : query.field, query.op, query.value);
22
+ }
23
+ return null;
24
+ };
25
+ /**
26
+ * A custom hook that generates a composite filter for database queries, using the provided query configuration.
27
+ * It applies either an 'OR' or 'AND' logical operation based on the type specified in the query.
28
+ *
29
+ * @param {Object} query - The query configuration object that contains subqueries and a type for logical combination.
30
+ * @param {string} query.type - The type of composite operation ('or'/'and').
31
+ * @param {Array} query.children - An array of subqueries that will be processed to form the composite filter.
32
+ *
33
+ * @returns {(Function|undefined)} A composite query filter constraint function formed by combining subqueries or undefined if there are no valid constraints.
34
+ */
35
+ export const useCompositeFilter = ({ query }) => {
36
+ return useMemo(() => {
37
+ const queryConstraints = (query?.children?.map?.((subQuery) => buildCompositeQuery(subQuery))?.filter)?.((constraint) => !!constraint) ?? [];
38
+ if (queryConstraints.length <= 0) {
39
+ return undefined;
40
+ }
41
+ return query?.type === "or" ? or(...queryConstraints) : and(...queryConstraints);
42
+ }, [query]);
43
+ };
@@ -0,0 +1,86 @@
1
+ import {
2
+ DocumentData,
3
+ QueryCompositeFilterConstraint,
4
+ QueryFilterConstraint,
5
+ WhereFilterOp,
6
+ and,
7
+ documentId,
8
+ or,
9
+ where
10
+ } from "firebase/firestore";
11
+ import { useMemo } from "react";
12
+
13
+ type CompositeFilterDocumentData = DocumentData;
14
+
15
+ export type QueryElement<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> =
16
+ Partial<QueryCompositeFilterConstraint> & {
17
+ children?: QueryElement[];
18
+ field?: keyof (DbModelType & { documentId?: string[] });
19
+ value?: DbModelType[keyof DbModelType];
20
+ op?: WhereFilterOp;
21
+ };
22
+
23
+ export type CompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> =
24
+ QueryCompositeFilterConstraint & {
25
+ children: QueryElement<DbModelType & { documentId?: string[] }>[];
26
+ };
27
+
28
+ export type UseCompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
29
+ query?: CompositeFilter<DbModelType>;
30
+ };
31
+
32
+ /**
33
+ * Constructs a composite query filter based on the provided query structure.
34
+ * It recursively builds query constraints using logical "or" or "and" operators.
35
+ *
36
+ * @param {QueryElement<DbModelType>} query - The query element or structure to be evaluated and transformed into filter constraints.
37
+ * @returns {QueryFilterConstraint | null} A constructed query filter constraint based on the input query, or null if no valid constraints can be derived.
38
+ */
39
+
40
+ const buildCompositeQuery = <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>(
41
+ query: QueryElement<DbModelType>
42
+ ): QueryFilterConstraint | null => {
43
+ if (query.children) {
44
+ const queryConstraints = query.children
45
+ .map((subQuery) => buildCompositeQuery(subQuery))
46
+ .filter<QueryFilterConstraint>((constraint) => !!constraint);
47
+
48
+ if (queryConstraints.length <= 0) {
49
+ return null;
50
+ }
51
+
52
+ return (query as CompositeFilter).type === "or" ? or(...queryConstraints) : and(...queryConstraints);
53
+ }
54
+
55
+ if (query.field && query.op) {
56
+ return where(query.field === "documentId" ? documentId() : (query.field as string), query.op, query.value);
57
+ }
58
+
59
+ return null;
60
+ };
61
+
62
+ /**
63
+ * A custom hook that generates a composite filter for database queries, using the provided query configuration.
64
+ * It applies either an 'OR' or 'AND' logical operation based on the type specified in the query.
65
+ *
66
+ * @param {Object} query - The query configuration object that contains subqueries and a type for logical combination.
67
+ * @param {string} query.type - The type of composite operation ('or'/'and').
68
+ * @param {Array} query.children - An array of subqueries that will be processed to form the composite filter.
69
+ *
70
+ * @returns {(Function|undefined)} A composite query filter constraint function formed by combining subqueries or undefined if there are no valid constraints.
71
+ */
72
+ export const useCompositeFilter = <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>({
73
+ query
74
+ }: UseCompositeFilter<DbModelType>) => {
75
+ return useMemo(() => {
76
+ const queryConstraints =
77
+ query?.children?.map?.((subQuery) => buildCompositeQuery(subQuery))?.filter<QueryFilterConstraint>?.(
78
+ (constraint) => !!constraint
79
+ ) ?? [];
80
+
81
+ if (queryConstraints.length <= 0) {
82
+ return undefined;
83
+ }
84
+ return query?.type === "or" ? or(...queryConstraints) : and(...queryConstraints);
85
+ }, [query]);
86
+ };
@@ -0,0 +1,23 @@
1
+ import { DocumentData, Query, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
2
+ import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
3
+ type UseCountQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
4
+ options: Omit<UseReactQueryOptions<number, Error, number>, "queryFn"> & Required<Pick<UseReactQueryOptions<number, Error, number>, "queryKey">>;
5
+ query: Query<AppModelType, DbModelType>;
6
+ queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
7
+ compositeFilter?: QueryCompositeFilterConstraint;
8
+ };
9
+ /**
10
+ * Executes a query with specified constraints and returns the count of matched documents.
11
+ *
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.
14
+ *
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
+ * @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
21
+ */
22
+ export declare const useCountQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, query: queryReference, queryConstraints, compositeFilter }: UseCountQueryOptions<AppModelType, DbModelType>) => UseQueryResult<number>;
23
+ export {};
@@ -0,0 +1,30 @@
1
+ import { getCountFromServer, query } from "firebase/firestore";
2
+ import { useQuery as useReactQuery } from "@tanstack/react-query";
3
+ /**
4
+ * Executes a query with specified constraints and returns the count of matched documents.
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.
8
+ *
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
+ * @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
15
+ */
16
+ export const useCountQuery = ({ options, query: queryReference, queryConstraints = [], compositeFilter }) => {
17
+ return useReactQuery({
18
+ ...options,
19
+ queryFn: async () => {
20
+ const queryToExecute = compositeFilter
21
+ ? query(queryReference, compositeFilter, ...queryConstraints)
22
+ : query(queryReference, ...queryConstraints);
23
+ const querySnapshot = await getCountFromServer(queryToExecute);
24
+ if (querySnapshot) {
25
+ return querySnapshot.data().count;
26
+ }
27
+ return 0;
28
+ }
29
+ });
30
+ };
@@ -0,0 +1,65 @@
1
+ import {
2
+ DocumentData,
3
+ getCountFromServer,
4
+ Query,
5
+ query,
6
+ QueryCompositeFilterConstraint,
7
+ QueryConstraint,
8
+ QueryNonFilterConstraint
9
+ } from "firebase/firestore";
10
+
11
+ import {
12
+ UseQueryResult,
13
+ useQuery as useReactQuery,
14
+ UseQueryOptions as UseReactQueryOptions
15
+ } from "@tanstack/react-query";
16
+
17
+ type UseCountQueryOptions<
18
+ AppModelType extends DocumentData = DocumentData,
19
+ DbModelType extends DocumentData = DocumentData
20
+ > = {
21
+ options: Omit<UseReactQueryOptions<number, Error, number>, "queryFn"> &
22
+ Required<Pick<UseReactQueryOptions<number, Error, number>, "queryKey">>;
23
+ query: Query<AppModelType, DbModelType>;
24
+ queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
25
+ compositeFilter?: QueryCompositeFilterConstraint;
26
+ };
27
+
28
+ /**
29
+ * Executes a query with specified constraints and returns the count of matched documents.
30
+ *
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.
33
+ *
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
+ * @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
40
+ */
41
+
42
+ export const useCountQuery = <
43
+ AppModelType extends DocumentData = DocumentData,
44
+ DbModelType extends DocumentData = DocumentData
45
+ >({
46
+ options,
47
+ query: queryReference,
48
+ queryConstraints = [],
49
+ compositeFilter
50
+ }: UseCountQueryOptions<AppModelType, DbModelType>): UseQueryResult<number> => {
51
+ return useReactQuery({
52
+ ...options,
53
+ queryFn: async () => {
54
+ const queryToExecute = compositeFilter
55
+ ? query(queryReference, compositeFilter, ...(queryConstraints as QueryNonFilterConstraint[]))
56
+ : query(queryReference, ...queryConstraints);
57
+
58
+ const querySnapshot = await getCountFromServer(queryToExecute);
59
+ if (querySnapshot) {
60
+ return querySnapshot.data().count;
61
+ }
62
+ return 0;
63
+ }
64
+ });
65
+ };
@@ -0,0 +1,18 @@
1
+ import { UseMutationOptions } from "@tanstack/react-query";
2
+ import { DocumentData, WithFieldValue, DocumentReference } from "firebase/firestore";
3
+ import { FirebaseError } from "firebase/app";
4
+ export type UseDeleteDocMutationValues<AppModelType> = {
5
+ data: WithFieldValue<AppModelType>;
6
+ };
7
+ export type UseDeleteDocMutationOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TContext = unknown> = {
8
+ reference: DocumentReference<AppModelType, DbModelType> | null;
9
+ options?: Omit<UseMutationOptions<void, FirebaseError, void, TContext>, "mutationFn" | "mutationKey">;
10
+ };
11
+ /**
12
+ * A custom hook that provides a mutation function to delete a document from the database.
13
+ * @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
14
+ * @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
15
+ * @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
16
+ * @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
17
+ */
18
+ export declare const useDeleteDocMutation: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TContext = unknown>({ reference, options }: UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<void, FirebaseError, void, TContext>;
@@ -0,0 +1,23 @@
1
+ import { useMutation } from "@tanstack/react-query";
2
+ import { deleteDoc } from "firebase/firestore";
3
+ import { useMemo } from "react";
4
+ /**
5
+ * A custom hook that provides a mutation function to delete a document from the database.
6
+ * @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
7
+ * @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
8
+ * @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
9
+ * @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
10
+ */
11
+ export const useDeleteDocMutation = ({ reference, options = {} }) => {
12
+ const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
13
+ return useMutation({
14
+ ...options,
15
+ mutationFn: async () => {
16
+ if (!reference) {
17
+ throw new Error("Reference is undefined");
18
+ }
19
+ await deleteDoc(reference);
20
+ },
21
+ mutationKey
22
+ });
23
+ };
@@ -0,0 +1,47 @@
1
+ import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
+ import { DocumentData, deleteDoc, WithFieldValue, DocumentReference } from "firebase/firestore";
3
+
4
+ import { FirebaseError } from "firebase/app";
5
+ import { useMemo } from "react";
6
+
7
+ export type UseDeleteDocMutationValues<AppModelType> = {
8
+ data: WithFieldValue<AppModelType>;
9
+ };
10
+
11
+ export type UseDeleteDocMutationOptions<
12
+ AppModelType extends DocumentData = DocumentData,
13
+ DbModelType extends DocumentData = DocumentData,
14
+ TContext = unknown
15
+ > = {
16
+ reference: DocumentReference<AppModelType, DbModelType> | null;
17
+ options?: Omit<UseMutationOptions<void, FirebaseError, void, TContext>, "mutationFn" | "mutationKey">;
18
+ };
19
+
20
+ /**
21
+ * A custom hook that provides a mutation function to delete a document from the database.
22
+ * @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
23
+ * @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
24
+ * @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
25
+ * @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
26
+ */
27
+ export const useDeleteDocMutation = <
28
+ AppModelType extends DocumentData = DocumentData,
29
+ DbModelType extends DocumentData = DocumentData,
30
+ TContext = unknown
31
+ >({
32
+ reference,
33
+ options = {}
34
+ }: UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>) => {
35
+ const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
36
+
37
+ return useMutation({
38
+ ...options,
39
+ mutationFn: async () => {
40
+ if (!reference) {
41
+ throw new Error("Reference is undefined");
42
+ }
43
+ await deleteDoc<AppModelType, DbModelType>(reference);
44
+ },
45
+ mutationKey
46
+ });
47
+ };
@@ -0,0 +1,19 @@
1
+ import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
+ export type UseDocReferenceOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
3
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
4
+ path?: string;
5
+ pathSegments?: string[];
6
+ };
7
+ /**
8
+ * Custom hook to generate and manage a Firestore document reference.
9
+ * This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
10
+ * The reference is updated whenever the associated path, reference, or path segments change.
11
+ *
12
+ * @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - Configuration options for setting up the document reference.
13
+ * @param {string} options.path - The path to the Firestore document.
14
+ * @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
15
+ * @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
16
+ *
17
+ * @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
18
+ */
19
+ export declare const useDocReference: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, reference, pathSegments }: UseDocReferenceOptions<AppModelType, DbModelType>) => DocumentReference<AppModelType, DbModelType> | null;
@@ -0,0 +1,45 @@
1
+ import { doc } from "firebase/firestore";
2
+ import { useEffect, useRef } from "react";
3
+ import { useFirestore } from "./useFirestore";
4
+ /**
5
+ * Generates a document reference for a specified path or reference in Firestore.
6
+ * If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
7
+ *
8
+ * @param {Firestore} db - The Firestore database instance used to create the document reference.
9
+ * @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - An object containing the path, path segments, and optional reference.
10
+ * @param {string} options.path - The path to the document in the Firestore database.
11
+ * @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
12
+ * @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
13
+ *
14
+ * @returns {DocumentReference<AppModelType, DbModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
15
+ */
16
+ const getDocReference = (db, { path, pathSegments, reference }) => {
17
+ if (!path) {
18
+ return null;
19
+ }
20
+ return (!reference
21
+ ? doc(db, path || "", ...(pathSegments || []))
22
+ : reference.type === "collection"
23
+ ? doc(reference, path, ...(pathSegments || []))
24
+ : doc(reference, path, ...(pathSegments || [])));
25
+ };
26
+ /**
27
+ * Custom hook to generate and manage a Firestore document reference.
28
+ * This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
29
+ * The reference is updated whenever the associated path, reference, or path segments change.
30
+ *
31
+ * @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - Configuration options for setting up the document reference.
32
+ * @param {string} options.path - The path to the Firestore document.
33
+ * @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
34
+ * @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
35
+ *
36
+ * @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
37
+ */
38
+ export const useDocReference = ({ path, reference, pathSegments }) => {
39
+ const db = useFirestore();
40
+ const ref = useRef(getDocReference(db, { path, pathSegments, reference }));
41
+ useEffect(() => {
42
+ ref.current = getDocReference(db, { path, pathSegments, reference });
43
+ }, [path, reference, pathSegments]);
44
+ return ref.current;
45
+ };
@@ -0,0 +1,68 @@
1
+ import { doc, CollectionReference, DocumentData, DocumentReference, Firestore } from "firebase/firestore";
2
+
3
+ import { useEffect, useRef } from "react";
4
+ import { useFirestore } from "./useFirestore";
5
+
6
+ export type UseDocReferenceOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
7
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
8
+ path?: string;
9
+ pathSegments?: string[];
10
+ };
11
+
12
+ /**
13
+ * Generates a document reference for a specified path or reference in Firestore.
14
+ * If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
15
+ *
16
+ * @param {Firestore} db - The Firestore database instance used to create the document reference.
17
+ * @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - An object containing the path, path segments, and optional reference.
18
+ * @param {string} options.path - The path to the document in the Firestore database.
19
+ * @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
20
+ * @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
21
+ *
22
+ * @returns {DocumentReference<AppModelType, DbModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
23
+ */
24
+ const getDocReference = <AppModelType, DbModelType extends DocumentData = DocumentData>(
25
+ db: Firestore,
26
+ { path, pathSegments, reference }: UseDocReferenceOptions<AppModelType, DbModelType>
27
+ ) => {
28
+ if (!path) {
29
+ return null;
30
+ }
31
+
32
+ return (
33
+ !reference
34
+ ? doc(db, path || "", ...(pathSegments || []))
35
+ : reference.type === "collection"
36
+ ? doc(reference, path, ...(pathSegments || []))
37
+ : doc(reference, path, ...(pathSegments || []))
38
+ ) as DocumentReference<AppModelType, DbModelType>;
39
+ };
40
+
41
+ /**
42
+ * Custom hook to generate and manage a Firestore document reference.
43
+ * This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
44
+ * The reference is updated whenever the associated path, reference, or path segments change.
45
+ *
46
+ * @param {UseDocReferenceOptions<AppModelType, DbModelType>} options - Configuration options for setting up the document reference.
47
+ * @param {string} options.path - The path to the Firestore document.
48
+ * @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
49
+ * @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
50
+ *
51
+ * @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
52
+ */
53
+ export const useDocReference = <AppModelType, DbModelType extends DocumentData = DocumentData>({
54
+ path,
55
+ reference,
56
+ pathSegments
57
+ }: UseDocReferenceOptions<AppModelType, DbModelType>) => {
58
+ const db = useFirestore();
59
+ const ref = useRef<DocumentReference<AppModelType, DbModelType> | null>(
60
+ getDocReference(db, { path, pathSegments, reference })
61
+ );
62
+
63
+ useEffect(() => {
64
+ ref.current = getDocReference(db, { path, pathSegments, reference });
65
+ }, [path, reference, pathSegments]);
66
+
67
+ return ref.current;
68
+ };
@@ -0,0 +1,23 @@
1
+ import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
2
+ export type UseDocReferencesOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
3
+ reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
4
+ path: string;
5
+ pathSegments?: string[];
6
+ };
7
+ /**
8
+ * A custom hook to generate document references for Firebase Firestore documents using given
9
+ * reference options.
10
+ *
11
+ * The hook accepts an array of options, where each option may contain a path,
12
+ * an existing reference, or path segments, and returns an array of generated
13
+ * Firestore document references based on those options. The references can be used
14
+ * to interact with the Firestore database documents. It uses memoization to optimize
15
+ * reference generation.
16
+ *
17
+ * @param {UseDocReferencesOptions<AppModelType, DbModelType>[]} references - An array of options
18
+ * specifying how to generate document references. Each option may include a `path`
19
+ * as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
20
+ * @returns {DocumentReference<AppModelType, DbModelType>[]} An array of Firestore document references
21
+ * generated from the options provided.
22
+ */
23
+ export declare const useDocReferences: <AppModelType, DbModelType extends DocumentData = DocumentData>(references: UseDocReferencesOptions<AppModelType, DbModelType>[]) => DocumentReference<AppModelType, DbModelType>[];