react-query-firebase 2.3.3 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/react-native/firestore/index.d.ts +5 -0
- package/react-native/firestore/index.js +5 -0
- package/react-native/firestore/index.ts +5 -0
- package/react-native/firestore/useAddDocMutation.d.ts +38 -9
- package/react-native/firestore/useAddDocMutation.js +25 -11
- package/react-native/firestore/useAddDocMutation.ts +43 -13
- package/react-native/firestore/useBatchWrite.d.ts +31 -7
- package/react-native/firestore/useBatchWrite.js +22 -4
- package/react-native/firestore/useBatchWrite.ts +31 -7
- package/react-native/firestore/useCollectionReference.d.ts +30 -9
- package/react-native/firestore/useCollectionReference.js +15 -7
- package/react-native/firestore/useCollectionReference.ts +34 -25
- package/react-native/firestore/useCompositeFilter.d.ts +1 -46
- package/react-native/firestore/useCompositeFilter.js +2 -45
- package/react-native/firestore/useCompositeFilter.ts +2 -74
- package/react-native/firestore/useCountQuery.d.ts +1 -1
- package/react-native/firestore/useCountQuery.ts +1 -1
- package/react-native/firestore/useDeleteDocMutation.d.ts +39 -10
- package/react-native/firestore/useDeleteDocMutation.js +22 -5
- package/react-native/firestore/useDeleteDocMutation.ts +38 -15
- package/react-native/firestore/useDocReference.d.ts +22 -14
- package/react-native/firestore/useDocReference.js +20 -26
- package/react-native/firestore/useDocReference.ts +34 -47
- package/react-native/firestore/useDocReferences.d.ts +23 -18
- package/react-native/firestore/useDocReferences.js +18 -19
- package/react-native/firestore/useDocReferences.ts +26 -34
- package/react-native/firestore/useFirestore.d.ts +4 -3
- package/react-native/firestore/useFirestore.js +4 -3
- package/react-native/firestore/useFirestore.ts +4 -3
- package/react-native/firestore/useGetDocData.d.ts +33 -14
- package/react-native/firestore/useGetDocData.js +21 -6
- package/react-native/firestore/useGetDocData.ts +34 -22
- package/react-native/firestore/useInfiniteQuery.d.ts +1 -1
- package/react-native/firestore/useInfiniteQuery.ts +1 -1
- package/react-native/firestore/useQuery.d.ts +1 -1
- package/react-native/firestore/useQuery.ts +1 -1
- package/react-native/firestore/useQueryConstraints.d.ts +0 -21
- package/react-native/firestore/useQueryConstraints.js +1 -39
- package/react-native/firestore/useQueryConstraints.ts +2 -51
- package/react-native/firestore/useRunTransaction.d.ts +33 -9
- package/react-native/firestore/useRunTransaction.js +20 -4
- package/react-native/firestore/useRunTransaction.ts +35 -16
- package/react-native/firestore/useSetDocMutation.d.ts +43 -11
- package/react-native/firestore/useSetDocMutation.js +28 -11
- package/react-native/firestore/useSetDocMutation.ts +49 -28
- package/react-native/firestore/useUpdateDocMutation.d.ts +40 -18
- package/react-native/firestore/useUpdateDocMutation.js +18 -13
- package/react-native/firestore/useUpdateDocMutation.ts +40 -31
- package/react-native/firestore/utils/buildCompositeFilter.d.ts +47 -0
- package/react-native/firestore/utils/buildCompositeFilter.js +45 -0
- package/react-native/firestore/utils/buildCompositeFilter.ts +75 -0
- package/react-native/firestore/utils/buildQueryConstraint.d.ts +23 -0
- package/react-native/firestore/utils/buildQueryConstraint.js +41 -0
- package/react-native/firestore/utils/buildQueryConstraint.ts +54 -0
- package/react-native/firestore/utils/getDocData.d.ts +11 -11
- package/react-native/firestore/utils/getDocData.js +5 -8
- package/react-native/firestore/utils/getDocData.ts +12 -16
- package/react-native/firestore/utils/getDocRef.d.ts +12 -9
- package/react-native/firestore/utils/getDocRef.js +7 -8
- package/react-native/firestore/utils/getDocRef.ts +12 -13
- package/react-native/firestore/utils/getDocSnap.d.ts +11 -13
- package/react-native/firestore/utils/getDocSnap.js +5 -10
- package/react-native/firestore/utils/getDocSnap.ts +12 -17
- package/types/AppModel.d.ts +4 -1
- package/types/AppModel.ts +4 -1
- package/web/firestore/index.d.ts +5 -0
- package/web/firestore/index.js +5 -0
- package/web/firestore/index.ts +5 -0
- package/web/firestore/useAddDocMutation.d.ts +41 -13
- package/web/firestore/useAddDocMutation.js +27 -14
- package/web/firestore/useAddDocMutation.ts +49 -37
- package/web/firestore/useBatchWrite.d.ts +34 -7
- package/web/firestore/useBatchWrite.js +22 -4
- package/web/firestore/useBatchWrite.ts +34 -7
- package/web/firestore/useCollectionReference.d.ts +32 -11
- package/web/firestore/useCollectionReference.js +15 -7
- package/web/firestore/useCollectionReference.ts +34 -13
- package/web/firestore/useCompositeFilter.d.ts +2 -47
- package/web/firestore/useCompositeFilter.js +2 -42
- package/web/firestore/useCompositeFilter.ts +2 -72
- package/web/firestore/useCountQuery.d.ts +4 -4
- package/web/firestore/useCountQuery.ts +5 -5
- package/web/firestore/useDeleteDocMutation.d.ts +41 -11
- package/web/firestore/useDeleteDocMutation.js +24 -7
- package/web/firestore/useDeleteDocMutation.ts +45 -23
- package/web/firestore/useDocReference.d.ts +23 -15
- package/web/firestore/useDocReference.js +16 -31
- package/web/firestore/useDocReference.ts +28 -46
- package/web/firestore/useDocReferences.d.ts +23 -18
- package/web/firestore/useDocReferences.js +18 -21
- package/web/firestore/useDocReferences.ts +26 -30
- package/web/firestore/useFirestore.d.ts +4 -3
- package/web/firestore/useFirestore.js +4 -3
- package/web/firestore/useFirestore.ts +4 -3
- package/web/firestore/useGetDocData.d.ts +32 -13
- package/web/firestore/useGetDocData.js +21 -6
- package/web/firestore/useGetDocData.ts +34 -23
- package/web/firestore/useGetRealtimeDocData.js +2 -2
- package/web/firestore/useGetRealtimeDocData.ts +2 -2
- package/web/firestore/useInfiniteQuery.d.ts +1 -1
- package/web/firestore/useInfiniteQuery.js +1 -1
- package/web/firestore/useInfiniteQuery.ts +2 -2
- package/web/firestore/useQuery.d.ts +1 -1
- package/web/firestore/useQuery.js +1 -1
- package/web/firestore/useQuery.ts +2 -2
- package/web/firestore/useQueryConstraints.d.ts +0 -21
- package/web/firestore/useQueryConstraints.js +1 -39
- package/web/firestore/useQueryConstraints.ts +2 -51
- package/web/firestore/useRunTransaction.d.ts +33 -8
- package/web/firestore/useRunTransaction.js +20 -4
- package/web/firestore/useRunTransaction.ts +35 -10
- package/web/firestore/useSetDocMutation.d.ts +45 -12
- package/web/firestore/useSetDocMutation.js +28 -11
- package/web/firestore/useSetDocMutation.ts +51 -26
- package/web/firestore/useUpdateDocMutation.d.ts +42 -20
- package/web/firestore/useUpdateDocMutation.js +20 -15
- package/web/firestore/useUpdateDocMutation.ts +45 -41
- package/web/firestore/utils/buildCompositeFilter.d.ts +47 -0
- package/web/firestore/utils/buildCompositeFilter.js +42 -0
- package/web/firestore/utils/buildCompositeFilter.ts +73 -0
- package/web/firestore/utils/buildQueryConstraint.d.ts +23 -0
- package/web/firestore/utils/buildQueryConstraint.js +41 -0
- package/web/firestore/utils/buildQueryConstraint.ts +55 -0
- package/web/firestore/utils/getDocData.d.ts +13 -11
- package/web/firestore/utils/getDocData.js +6 -9
- package/web/firestore/utils/getDocData.ts +14 -20
- package/web/firestore/utils/getDocRef.d.ts +14 -11
- package/web/firestore/utils/getDocRef.js +7 -8
- package/web/firestore/utils/getDocRef.ts +16 -19
- package/web/firestore/utils/getDocSnap.d.ts +11 -13
- package/web/firestore/utils/getDocSnap.js +5 -10
- 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 "./
|
|
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
|
|
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,
|
|
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
|
|
66
|
+
export const useCountQuery = <AppModelType extends AppModel = AppModel>({
|
|
67
67
|
options,
|
|
68
68
|
collectionReference,
|
|
69
69
|
queryConstraints = [],
|
|
70
70
|
compositeFilter
|
|
71
|
-
}: UseCountQueryOptions<AppModelType
|
|
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 {
|
|
2
|
+
import { WithFieldValue, DocumentReference } from "firebase/firestore";
|
|
3
3
|
import { FirebaseError } from "firebase/app";
|
|
4
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @
|
|
15
|
-
*
|
|
16
|
-
* @
|
|
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
|
|
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
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
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 {
|
|
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
|
-
|
|
8
|
+
/**
|
|
9
|
+
* @inline
|
|
10
|
+
*/
|
|
11
|
+
export type UseDeleteDocMutationValues<AppModelType extends AppModel = AppModel> = {
|
|
8
12
|
data: WithFieldValue<AppModelType>;
|
|
9
13
|
};
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* @
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
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,
|
|
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,
|
|
44
|
-
}
|
|
45
|
-
mutationKey
|
|
66
|
+
await deleteDoc<AppModelType, AppModelType>(reference);
|
|
67
|
+
}
|
|
46
68
|
});
|
|
47
69
|
};
|
|
@@ -1,19 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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(
|
|
25
|
+
const ref = useRef(getDocRef({ db, path, pathSegments, reference }));
|
|
41
26
|
useEffect(() => {
|
|
42
|
-
ref.current =
|
|
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 {
|
|
1
|
+
import { DocumentReference } from "firebase/firestore";
|
|
2
2
|
|
|
3
3
|
import { useEffect, useRef } from "react";
|
|
4
4
|
import { useFirestore } from "./useFirestore";
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
17
|
+
* Gets a `CollectionReference` instance
|
|
18
|
+
*
|
|
19
|
+
* @group Hook
|
|
20
|
+
*
|
|
21
|
+
* @param {UseDocReferenceOptions<AppModelType>} options - Options
|
|
45
22
|
*
|
|
46
|
-
* @
|
|
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
|
-
* @
|
|
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
|
|
35
|
+
export const useDocReference = <AppModelType extends AppModel = AppModel>({
|
|
54
36
|
path,
|
|
55
37
|
reference,
|
|
56
38
|
pathSegments
|
|
57
|
-
}: UseDocReferenceOptions<AppModelType
|
|
39
|
+
}: UseDocReferenceOptions<AppModelType>) => {
|
|
58
40
|
const db = useFirestore();
|
|
59
|
-
const ref = useRef<DocumentReference<AppModelType,
|
|
60
|
-
|
|
41
|
+
const ref = useRef<DocumentReference<AppModelType, AppModelType> | null>(
|
|
42
|
+
getDocRef({ db, path, pathSegments, reference })
|
|
61
43
|
);
|
|
62
44
|
|
|
63
45
|
useEffect(() => {
|
|
64
|
-
ref.current =
|
|
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 {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
*
|
|
9
|
-
*
|
|
10
|
+
* Gets `CollectionReference` instances
|
|
11
|
+
*
|
|
12
|
+
* @group Hook
|
|
13
|
+
*
|
|
14
|
+
* @param {UseDocReferencesOptions<AppModelType>} options - Options
|
|
10
15
|
*
|
|
11
|
-
*
|
|
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
|
-
* @
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
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
|
|
28
|
+
export declare const useDocReferences: <AppModelType extends AppModel = AppModel>({ references }: UseDocReferencesOptions<AppModelType>) => import("@firebase/firestore").DocumentReference<AppModelType, AppModelType>[];
|
|
@@ -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
|
-
*
|
|
6
|
-
* reference options.
|
|
5
|
+
* Gets `CollectionReference` instances
|
|
7
6
|
*
|
|
8
|
-
*
|
|
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
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
*
|
|
14
|
-
*
|
|
14
|
+
* Gets `CollectionReference` instances
|
|
15
|
+
*
|
|
16
|
+
* @group Hook
|
|
17
|
+
*
|
|
18
|
+
* @param {UseDocReferencesOptions<AppModelType>} options - Options
|
|
15
19
|
*
|
|
16
|
-
*
|
|
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
|
-
* @
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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
|
|
29
|
-
references
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
4
|
+
* @group Hook
|
|
5
|
+
*
|
|
6
|
+
* @returns {Firestore}
|
|
6
7
|
*/
|
|
7
8
|
export declare const useFirestore: () => import("@firebase/firestore").Firestore;
|