react-query-firebase 2.3.3 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +1 -1
  35. package/react-native/firestore/useInfiniteQuery.ts +1 -1
  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 +0 -21
  39. package/react-native/firestore/useQueryConstraints.js +1 -39
  40. package/react-native/firestore/useQueryConstraints.ts +2 -51
  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 +9 -10
  61. package/react-native/firestore/utils/getDocRef.ts +14 -15
  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 +1 -1
  102. package/web/firestore/useInfiniteQuery.js +1 -1
  103. package/web/firestore/useInfiniteQuery.ts +2 -2
  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 +9 -10
  130. package/web/firestore/utils/getDocRef.ts +21 -24
  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
@@ -12,13 +12,13 @@ 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
  /**
19
19
  * @inline
20
20
  */
21
- type UseCountQueryOptions<AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel> = {
21
+ type UseCountQueryOptions<AppModelType extends AppModel = AppModel> = {
22
22
  /**
23
23
  * Reqct-query options that must include queryKey and shall not define queryFn
24
24
  */
@@ -28,7 +28,7 @@ type UseCountQueryOptions<AppModelType extends AppModel = AppModel, DbModelType
28
28
  /**
29
29
  * Reference to a Firestore collection
30
30
  */
31
- collectionReference: CollectionReference<AppModelType, DbModelType>;
31
+ collectionReference: CollectionReference<AppModelType, AppModelType>;
32
32
 
33
33
  /**
34
34
  * Non composite filter constraints such as limit, order, where
@@ -63,12 +63,12 @@ type UseCountQueryOptions<AppModelType extends AppModel = AppModel, DbModelType
63
63
  * };
64
64
  * ```
65
65
  */
66
- export const useCountQuery = <AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel>({
66
+ export const useCountQuery = <AppModelType extends AppModel = AppModel>({
67
67
  options,
68
68
  collectionReference,
69
69
  queryConstraints = [],
70
70
  compositeFilter
71
- }: UseCountQueryOptions<AppModelType, DbModelType>): UseQueryResult<number> => {
71
+ }: UseCountQueryOptions<AppModelType>): UseQueryResult<number> => {
72
72
  return useReactQuery({
73
73
  ...options,
74
74
  queryFn: async () => {
@@ -1,18 +1,48 @@
1
1
  import { UseMutationOptions } from "@tanstack/react-query";
2
- import { DocumentData, WithFieldValue, DocumentReference } from "firebase/firestore";
2
+ import { WithFieldValue, DocumentReference } from "firebase/firestore";
3
3
  import { FirebaseError } from "firebase/app";
4
- export type UseDeleteDocMutationValues<AppModelType> = {
4
+ import { AppModel } from "../../types";
5
+ /**
6
+ * @inline
7
+ */
8
+ export type UseDeleteDocMutationValues<AppModelType extends AppModel = AppModel> = {
5
9
  data: WithFieldValue<AppModelType>;
6
10
  };
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">;
11
+ /**
12
+ * @inline
13
+ */
14
+ export type UseDeleteDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
15
+ /**
16
+ * A reference to a firestore document
17
+ */
18
+ reference: DocumentReference<AppModelType, AppModelType> | null;
19
+ /**
20
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
21
+ */
22
+ options?: Omit<UseMutationOptions<void, FirebaseError, void, TContext>, "mutationFn">;
10
23
  };
11
24
  /**
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.
25
+ * Executes a query with specified constraints and returns the count of matched documents.
26
+ *
27
+ * @group Hook
28
+ *
29
+ * @param {UseDeleteDocMutationOptions<AppModelType>} options - Configuration options for the query.
30
+ *
31
+ * @returns {UseMutationResult<void, Error, UseDeleteDocMutationValues<AppModelType>, TContext>} A mutation result
32
+ *
33
+ * @example
34
+ * ```jsx
35
+ * export const MyComponent = () => {
36
+ * const {mutate} = useDeleteDocMutation({
37
+ * options: {
38
+ * },
39
+ * reference: document(),
40
+ * });
41
+ *
42
+ * // ....
43
+ * mutate();
44
+ * // ....
45
+ * };
46
+ * ```
17
47
  */
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>;
48
+ export declare const useDeleteDocMutation: <AppModelType extends AppModel = AppModel, TContext = unknown>({ reference, options }: UseDeleteDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<void, FirebaseError, void, TContext>;
@@ -2,22 +2,39 @@ import { useMutation } from "@tanstack/react-query";
2
2
  import { deleteDoc } from "firebase/firestore";
3
3
  import { useMemo } from "react";
4
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.
5
+ * Executes a query with specified constraints and returns the count of matched documents.
6
+ *
7
+ * @group Hook
8
+ *
9
+ * @param {UseDeleteDocMutationOptions<AppModelType>} options - Configuration options for the query.
10
+ *
11
+ * @returns {UseMutationResult<void, Error, UseDeleteDocMutationValues<AppModelType>, TContext>} A mutation result
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const {mutate} = useDeleteDocMutation({
17
+ * options: {
18
+ * },
19
+ * reference: document(),
20
+ * });
21
+ *
22
+ * // ....
23
+ * mutate();
24
+ * // ....
25
+ * };
26
+ * ```
10
27
  */
11
28
  export const useDeleteDocMutation = ({ reference, options = {} }) => {
12
29
  const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
13
30
  return useMutation({
31
+ mutationKey,
14
32
  ...options,
15
33
  mutationFn: async () => {
16
34
  if (!reference) {
17
35
  throw new Error("Reference is undefined");
18
36
  }
19
37
  await deleteDoc(reference);
20
- },
21
- mutationKey
38
+ }
22
39
  });
23
40
  };
@@ -1,47 +1,69 @@
1
1
  import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
- import { DocumentData, deleteDoc, WithFieldValue, DocumentReference } from "firebase/firestore";
2
+ import { deleteDoc, WithFieldValue, DocumentReference } from "firebase/firestore";
3
3
 
4
4
  import { FirebaseError } from "firebase/app";
5
5
  import { useMemo } from "react";
6
+ import { AppModel } from "../../types";
6
7
 
7
- export type UseDeleteDocMutationValues<AppModelType> = {
8
+ /**
9
+ * @inline
10
+ */
11
+ export type UseDeleteDocMutationValues<AppModelType extends AppModel = AppModel> = {
8
12
  data: WithFieldValue<AppModelType>;
9
13
  };
10
14
 
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">;
15
+ /**
16
+ * @inline
17
+ */
18
+ export type UseDeleteDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
19
+ /**
20
+ * A reference to a firestore document
21
+ */
22
+ reference: DocumentReference<AppModelType, AppModelType> | null;
23
+ /**
24
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
25
+ */
26
+ options?: Omit<UseMutationOptions<void, FirebaseError, void, TContext>, "mutationFn">;
18
27
  };
19
28
 
20
29
  /**
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.
30
+ * Executes a query with specified constraints and returns the count of matched documents.
31
+ *
32
+ * @group Hook
33
+ *
34
+ * @param {UseDeleteDocMutationOptions<AppModelType>} options - Configuration options for the query.
35
+ *
36
+ * @returns {UseMutationResult<void, Error, UseDeleteDocMutationValues<AppModelType>, TContext>} A mutation result
37
+ *
38
+ * @example
39
+ * ```jsx
40
+ * export const MyComponent = () => {
41
+ * const {mutate} = useDeleteDocMutation({
42
+ * options: {
43
+ * },
44
+ * reference: document(),
45
+ * });
46
+ *
47
+ * // ....
48
+ * mutate();
49
+ * // ....
50
+ * };
51
+ * ```
26
52
  */
27
- export const useDeleteDocMutation = <
28
- AppModelType extends DocumentData = DocumentData,
29
- DbModelType extends DocumentData = DocumentData,
30
- TContext = unknown
31
- >({
53
+ export const useDeleteDocMutation = <AppModelType extends AppModel = AppModel, TContext = unknown>({
32
54
  reference,
33
55
  options = {}
34
- }: UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>) => {
56
+ }: UseDeleteDocMutationOptions<AppModelType, TContext>) => {
35
57
  const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
36
58
 
37
59
  return useMutation({
60
+ mutationKey,
38
61
  ...options,
39
62
  mutationFn: async () => {
40
63
  if (!reference) {
41
64
  throw new Error("Reference is undefined");
42
65
  }
43
- await deleteDoc<AppModelType, DbModelType>(reference);
44
- },
45
- mutationKey
66
+ await deleteDoc<AppModelType, AppModelType>(reference);
67
+ }
46
68
  });
47
69
  };
@@ -1,19 +1,27 @@
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
- };
1
+ import { DocumentReference } from "firebase/firestore";
2
+ import { AppModel } from "../../types";
3
+ import { GetDocRefOptions } from "./utils/getDocRef";
7
4
  /**
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.
5
+ * @inline
6
+ */
7
+ export type UseDocReferenceOptions<AppModelType extends AppModel = AppModel> = Omit<GetDocRefOptions<AppModelType>, "db">;
8
+ /**
9
+ * Gets a `CollectionReference` instance
10
+ *
11
+ * @group Hook
12
+ *
13
+ * @param {UseDocReferenceOptions<AppModelType>} options - Options
11
14
  *
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.
15
+ * @returns {DocumentReference<AppModelType, AppModelType> | null} A reference to a Firestore document
16
16
  *
17
- * @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
17
+ * @example
18
+ * ```jsx
19
+ * export const MyComponent = () => {
20
+ * const ref = useDocReference({
21
+ * reference: collection('todos')
22
+ * path: 'first'
23
+ * });
24
+ * };
25
+ * ```
18
26
  */
19
- export declare const useDocReference: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, reference, pathSegments }: UseDocReferenceOptions<AppModelType, DbModelType>) => DocumentReference<AppModelType, DbModelType> | null;
27
+ export declare const useDocReference: <AppModelType extends AppModel = AppModel>({ path, reference, pathSegments }: UseDocReferenceOptions<AppModelType>) => DocumentReference<AppModelType, AppModelType> | null;
@@ -1,45 +1,30 @@
1
- import { doc } from "firebase/firestore";
2
1
  import { useEffect, useRef } from "react";
3
2
  import { useFirestore } from "./useFirestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
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.
5
+ * Gets a `CollectionReference` instance
7
6
  *
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.
7
+ * @group Hook
13
8
  *
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.
9
+ * @param {UseDocReferenceOptions<AppModelType>} options - Options
30
10
  *
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.
11
+ * @returns {DocumentReference<AppModelType, AppModelType> | null} A reference to a Firestore document
35
12
  *
36
- * @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const ref = useDocReference({
17
+ * reference: collection('todos')
18
+ * path: 'first'
19
+ * });
20
+ * };
21
+ * ```
37
22
  */
38
23
  export const useDocReference = ({ path, reference, pathSegments }) => {
39
24
  const db = useFirestore();
40
- const ref = useRef(getDocReference(db, { path, pathSegments, reference }));
25
+ const ref = useRef(getDocRef({ db, path, pathSegments, reference }));
41
26
  useEffect(() => {
42
- ref.current = getDocReference(db, { path, pathSegments, reference });
27
+ ref.current = getDocRef({ db, path, pathSegments, reference });
43
28
  }, [path, reference, pathSegments, db]);
44
29
  return ref.current;
45
30
  };
@@ -1,67 +1,49 @@
1
- import { doc, CollectionReference, DocumentData, DocumentReference, Firestore } from "firebase/firestore";
1
+ import { DocumentReference } from "firebase/firestore";
2
2
 
3
3
  import { useEffect, useRef } from "react";
4
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
- };
5
+ import { AppModel } from "../../types";
6
+ import { getDocRef, GetDocRefOptions } from "./utils/getDocRef";
11
7
 
12
8
  /**
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.
9
+ * @inline
23
10
  */
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
- };
11
+ export type UseDocReferenceOptions<AppModelType extends AppModel = AppModel> = Omit<
12
+ GetDocRefOptions<AppModelType>,
13
+ "db"
14
+ >;
40
15
 
41
16
  /**
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.
17
+ * Gets a `CollectionReference` instance
18
+ *
19
+ * @group Hook
20
+ *
21
+ * @param {UseDocReferenceOptions<AppModelType>} options - Options
45
22
  *
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.
23
+ * @returns {DocumentReference<AppModelType, AppModelType> | null} A reference to a Firestore document
50
24
  *
51
- * @returns {DocumentReference<AppModelType, DbModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
25
+ * @example
26
+ * ```jsx
27
+ * export const MyComponent = () => {
28
+ * const ref = useDocReference({
29
+ * reference: collection('todos')
30
+ * path: 'first'
31
+ * });
32
+ * };
33
+ * ```
52
34
  */
53
- export const useDocReference = <AppModelType, DbModelType extends DocumentData = DocumentData>({
35
+ export const useDocReference = <AppModelType extends AppModel = AppModel>({
54
36
  path,
55
37
  reference,
56
38
  pathSegments
57
- }: UseDocReferenceOptions<AppModelType, DbModelType>) => {
39
+ }: UseDocReferenceOptions<AppModelType>) => {
58
40
  const db = useFirestore();
59
- const ref = useRef<DocumentReference<AppModelType, DbModelType> | null>(
60
- getDocReference(db, { path, pathSegments, reference })
41
+ const ref = useRef<DocumentReference<AppModelType, AppModelType> | null>(
42
+ getDocRef({ db, path, pathSegments, reference })
61
43
  );
62
44
 
63
45
  useEffect(() => {
64
- ref.current = getDocReference(db, { path, pathSegments, reference });
46
+ ref.current = getDocRef({ db, path, pathSegments, reference });
65
47
  }, [path, reference, pathSegments, db]);
66
48
 
67
49
  return ref.current;
@@ -1,23 +1,28 @@
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[];
1
+ import { AppModel } from "../../types";
2
+ import { GetDocRefOptions } from "./utils/getDocRef";
3
+ /**
4
+ * @inline
5
+ */
6
+ export type UseDocReferencesOptions<AppModelType extends AppModel = AppModel> = {
7
+ references: Omit<GetDocRefOptions<AppModelType>, "db">[];
6
8
  };
7
9
  /**
8
- * A custom hook to generate document references for Firebase Firestore documents using given
9
- * reference options.
10
+ * Gets `CollectionReference` instances
11
+ *
12
+ * @group Hook
13
+ *
14
+ * @param {UseDocReferencesOptions<AppModelType>} options - Options
10
15
  *
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
+ * @returns {Array<DocumentReference<AppModelType, AppModelType> | null>} A reference to a Firestore document
16
17
  *
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.
18
+ * @example
19
+ * ```jsx
20
+ * export const MyComponent = () => {
21
+ * const ref = useDocReference({
22
+ * reference: collection('todos')
23
+ * path: 'first'
24
+ * });
25
+ * };
26
+ * ```
22
27
  */
23
- export declare const useDocReferences: <AppModelType, DbModelType extends DocumentData = DocumentData>(references: UseDocReferencesOptions<AppModelType, DbModelType>[]) => DocumentReference<AppModelType, DbModelType>[];
28
+ export declare const useDocReferences: <AppModelType extends AppModel = AppModel>({ references }: UseDocReferencesOptions<AppModelType>) => (import("@firebase/firestore").DocumentReference<AppModelType, AppModelType> | null)[];
@@ -1,31 +1,28 @@
1
- import { doc } from "firebase/firestore";
2
1
  import { useMemo } from "react";
3
2
  import { useFirestore } from "./useFirestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
4
  /**
5
- * A custom hook to generate document references for Firebase Firestore documents using given
6
- * reference options.
5
+ * Gets `CollectionReference` instances
7
6
  *
8
- * The hook accepts an array of options, where each option may contain a path,
9
- * an existing reference, or path segments, and returns an array of generated
10
- * Firestore document references based on those options. The references can be used
11
- * to interact with the Firestore database documents. It uses memoization to optimize
12
- * reference generation.
7
+ * @group Hook
13
8
  *
14
- * @param {UseDocReferencesOptions<AppModelType, DbModelType>[]} references - An array of options
15
- * specifying how to generate document references. Each option may include a `path`
16
- * as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
17
- * @returns {DocumentReference<AppModelType, DbModelType>[]} An array of Firestore document references
18
- * generated from the options provided.
9
+ * @param {UseDocReferencesOptions<AppModelType>} options - Options
10
+ *
11
+ * @returns {Array<DocumentReference<AppModelType, AppModelType> | null>} A reference to a Firestore document
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const ref = useDocReference({
17
+ * reference: collection('todos')
18
+ * path: 'first'
19
+ * });
20
+ * };
21
+ * ```
19
22
  */
20
- export const useDocReferences = (references) => {
23
+ export const useDocReferences = ({ references }) => {
21
24
  const db = useFirestore();
22
25
  return useMemo(() => {
23
- return references.map(({ path, reference, pathSegments }) => {
24
- return (!reference
25
- ? doc(db, path || "", ...(pathSegments || []))
26
- : reference.type === "collection"
27
- ? doc(reference, path, ...(pathSegments || []))
28
- : doc(reference, path, ...(pathSegments || [])));
29
- });
26
+ return references.map(({ path, reference, pathSegments }) => getDocRef({ db, reference, path, pathSegments }));
30
27
  }, [references, db]);
31
28
  };
@@ -1,44 +1,40 @@
1
- import { CollectionReference, doc, DocumentData, DocumentReference } from "firebase/firestore";
2
-
3
1
  import { useMemo } from "react";
4
2
  import { useFirestore } from "./useFirestore";
3
+ import { AppModel } from "../../types";
4
+ import { getDocRef, GetDocRefOptions } from "./utils/getDocRef";
5
5
 
6
- export type UseDocReferencesOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
7
- reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
8
- path: string;
9
- pathSegments?: string[];
6
+ /**
7
+ * @inline
8
+ */
9
+ export type UseDocReferencesOptions<AppModelType extends AppModel = AppModel> = {
10
+ references: Omit<GetDocRefOptions<AppModelType>, "db">[];
10
11
  };
11
12
 
12
13
  /**
13
- * A custom hook to generate document references for Firebase Firestore documents using given
14
- * reference options.
14
+ * Gets `CollectionReference` instances
15
+ *
16
+ * @group Hook
17
+ *
18
+ * @param {UseDocReferencesOptions<AppModelType>} options - Options
15
19
  *
16
- * The hook accepts an array of options, where each option may contain a path,
17
- * an existing reference, or path segments, and returns an array of generated
18
- * Firestore document references based on those options. The references can be used
19
- * to interact with the Firestore database documents. It uses memoization to optimize
20
- * reference generation.
20
+ * @returns {Array<DocumentReference<AppModelType, AppModelType> | null>} A reference to a Firestore document
21
21
  *
22
- * @param {UseDocReferencesOptions<AppModelType, DbModelType>[]} references - An array of options
23
- * specifying how to generate document references. Each option may include a `path`
24
- * as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
25
- * @returns {DocumentReference<AppModelType, DbModelType>[]} An array of Firestore document references
26
- * generated from the options provided.
22
+ * @example
23
+ * ```jsx
24
+ * export const MyComponent = () => {
25
+ * const ref = useDocReference({
26
+ * reference: collection('todos')
27
+ * path: 'first'
28
+ * });
29
+ * };
30
+ * ```
27
31
  */
28
- export const useDocReferences = <AppModelType, DbModelType extends DocumentData = DocumentData>(
29
- references: UseDocReferencesOptions<AppModelType, DbModelType>[]
30
- ) => {
32
+ export const useDocReferences = <AppModelType extends AppModel = AppModel>({
33
+ references
34
+ }: UseDocReferencesOptions<AppModelType>) => {
31
35
  const db = useFirestore();
32
36
 
33
37
  return useMemo(() => {
34
- return references.map(({ path, reference, pathSegments }) => {
35
- return (
36
- !reference
37
- ? doc(db, path || "", ...(pathSegments || []))
38
- : reference.type === "collection"
39
- ? doc(reference, path, ...(pathSegments || []))
40
- : doc(reference, path, ...(pathSegments || []))
41
- ) as DocumentReference<AppModelType, DbModelType>;
42
- });
38
+ return references.map(({ path, reference, pathSegments }) => getDocRef({ db, reference, path, pathSegments }));
43
39
  }, [references, db]);
44
40
  };
@@ -1,7 +1,8 @@
1
1
  /**
2
- * Provides a hook to access the Firestore instance from the Firebase context.
3
- * It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
2
+ * Returns an instance of firestore bound to the closest context.
4
3
  *
5
- * @returns {firestore.Firestore} The Firestore instance from the Firebase context.
4
+ * @group Hook
5
+ *
6
+ * @returns {Firestore}
6
7
  */
7
8
  export declare const useFirestore: () => import("@firebase/firestore").Firestore;