react-query-firebase 2.6.1 → 2.6.2
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/useAddDocMutation.d.ts +3 -2
- package/react-native/firestore/useAddDocMutation.js +1 -1
- package/react-native/firestore/useAddDocMutation.ts +4 -9
- package/react-native/firestore/useGetRealtimeDocData.js +2 -1
- package/react-native/firestore/useGetRealtimeDocData.ts +2 -1
- package/react-native/firestore/useInfiniteQuery.js +1 -1
- package/react-native/firestore/useInfiniteQuery.ts +1 -1
- package/react-native/firestore/useQuery.js +1 -1
- package/react-native/firestore/useQuery.ts +1 -1
- package/react-native/firestore/utils/getDocData.js +1 -1
- package/react-native/firestore/utils/getDocData.ts +1 -1
- package/web/firestore/useEnsureDoc.js +21 -9
- package/web/firestore/useEnsureDoc.ts +27 -14
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
2
|
import { FirebaseFirestoreTypes, WithFieldValue } from "@react-native-firebase/firestore";
|
|
3
3
|
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
import { AppModel } from "../../types";
|
|
4
5
|
/**
|
|
5
6
|
* @inline
|
|
6
7
|
*/
|
|
@@ -13,7 +14,7 @@ export type UseAddDocMutationValues<AppModelType> = {
|
|
|
13
14
|
/**
|
|
14
15
|
* @inline
|
|
15
16
|
*/
|
|
16
|
-
export type UseAddDocMutationOptions<AppModelType extends
|
|
17
|
+
export type UseAddDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
|
|
17
18
|
/**
|
|
18
19
|
* Reference to a collection where document must be added
|
|
19
20
|
*/
|
|
@@ -47,4 +48,4 @@ export type UseAddDocMutationOptions<AppModelType extends FirebaseFirestoreTypes
|
|
|
47
48
|
* };
|
|
48
49
|
* ```
|
|
49
50
|
*/
|
|
50
|
-
export declare const useAddDocMutation: <AppModelType extends
|
|
51
|
+
export declare const useAddDocMutation: <AppModelType extends AppModel = AppModel, TContext = unknown>({ collectionReference, options }: UseAddDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, ReactNativeFirebase.NativeFirebaseError, UseAddDocMutationValues<AppModelType>, TContext>;
|
|
@@ -33,7 +33,7 @@ export const useAddDocMutation = ({ collectionReference, options = {} }) => {
|
|
|
33
33
|
mutationFn: async ({ data }) => {
|
|
34
34
|
const docRef = await addDoc(collectionReference, data);
|
|
35
35
|
const docSnap = await getDoc(docRef);
|
|
36
|
-
return docSnap.data();
|
|
36
|
+
return { ...docSnap.data(), uid: docRef.id };
|
|
37
37
|
}
|
|
38
38
|
});
|
|
39
39
|
};
|
|
@@ -3,6 +3,7 @@ import { FirebaseFirestoreTypes, addDoc, WithFieldValue, getDoc } from "@react-n
|
|
|
3
3
|
|
|
4
4
|
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
5
5
|
import { useMemo } from "react";
|
|
6
|
+
import { AppModel } from "../../types";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @inline
|
|
@@ -17,10 +18,7 @@ export type UseAddDocMutationValues<AppModelType> = {
|
|
|
17
18
|
/**
|
|
18
19
|
* @inline
|
|
19
20
|
*/
|
|
20
|
-
export type UseAddDocMutationOptions<
|
|
21
|
-
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
|
|
22
|
-
TContext = unknown
|
|
23
|
-
> = {
|
|
21
|
+
export type UseAddDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
|
|
24
22
|
/**
|
|
25
23
|
* Reference to a collection where document must be added
|
|
26
24
|
*/
|
|
@@ -64,10 +62,7 @@ export type UseAddDocMutationOptions<
|
|
|
64
62
|
* };
|
|
65
63
|
* ```
|
|
66
64
|
*/
|
|
67
|
-
export const useAddDocMutation = <
|
|
68
|
-
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
|
|
69
|
-
TContext = unknown
|
|
70
|
-
>({
|
|
65
|
+
export const useAddDocMutation = <AppModelType extends AppModel = AppModel, TContext = unknown>({
|
|
71
66
|
collectionReference,
|
|
72
67
|
options = {}
|
|
73
68
|
}: UseAddDocMutationOptions<AppModelType, TContext>) => {
|
|
@@ -79,7 +74,7 @@ export const useAddDocMutation = <
|
|
|
79
74
|
mutationFn: async ({ data }) => {
|
|
80
75
|
const docRef = await addDoc<AppModelType>(collectionReference, data);
|
|
81
76
|
const docSnap = await getDoc(docRef);
|
|
82
|
-
return docSnap.data() as AppModelType;
|
|
77
|
+
return { ...docSnap.data(), uid: docRef.id } as AppModelType;
|
|
83
78
|
}
|
|
84
79
|
});
|
|
85
80
|
};
|
|
@@ -34,12 +34,13 @@ export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }
|
|
|
34
34
|
? onSnapshot(ref, {
|
|
35
35
|
next: async (snapshot) => {
|
|
36
36
|
setIsFetching(false);
|
|
37
|
-
setDoc(snapshot.data()
|
|
37
|
+
setDoc({ ...snapshot.data(), uid: snapshot.id });
|
|
38
38
|
setError(null);
|
|
39
39
|
setIsError(false);
|
|
40
40
|
},
|
|
41
41
|
error: (e) => {
|
|
42
42
|
setIsError(true);
|
|
43
|
+
setDoc(null);
|
|
43
44
|
setError(e);
|
|
44
45
|
onError?.(e);
|
|
45
46
|
}
|
|
@@ -78,12 +78,13 @@ export const useGetRealtimeDocData = <AppModelType extends AppModel = AppModel>(
|
|
|
78
78
|
? onSnapshot<AppModelType>(ref, {
|
|
79
79
|
next: async (snapshot) => {
|
|
80
80
|
setIsFetching(false);
|
|
81
|
-
setDoc(snapshot.data()
|
|
81
|
+
setDoc({ ...snapshot.data(), uid: snapshot.id } as AppModelType);
|
|
82
82
|
setError(null);
|
|
83
83
|
setIsError(false);
|
|
84
84
|
},
|
|
85
85
|
error: (e) => {
|
|
86
86
|
setIsError(true);
|
|
87
|
+
setDoc(null);
|
|
87
88
|
setError(e);
|
|
88
89
|
onError?.(e);
|
|
89
90
|
}
|
|
@@ -34,7 +34,7 @@ export const useInfiniteQuery = ({ options, collectionReference, queryConstraint
|
|
|
34
34
|
const docs = [];
|
|
35
35
|
if (querySnapshot) {
|
|
36
36
|
querySnapshot.forEach((doc) => {
|
|
37
|
-
docs.push(doc.data());
|
|
37
|
+
docs.push({ ...doc.data(), uid: doc.id });
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
return docs;
|
|
@@ -13,7 +13,7 @@ import { getDocSnap } from "./getDocSnap";
|
|
|
13
13
|
export const getDocData = async ({ db, reference, path, pathSegments }) => {
|
|
14
14
|
const docSnap = await getDocSnap({ db, reference, path, pathSegments });
|
|
15
15
|
if (docSnap && docSnap?.exists) {
|
|
16
|
-
return docSnap.data();
|
|
16
|
+
return { ...docSnap.data(), uid: docSnap.id };
|
|
17
17
|
}
|
|
18
18
|
return null;
|
|
19
19
|
};
|
|
@@ -26,7 +26,7 @@ export const getDocData = async <AppModelType extends AppModel = AppModel>({
|
|
|
26
26
|
const docSnap = await getDocSnap<AppModelType>({ db, reference, path, pathSegments });
|
|
27
27
|
|
|
28
28
|
if (docSnap && docSnap?.exists) {
|
|
29
|
-
return docSnap.data() as AppModelType;
|
|
29
|
+
return { ...docSnap.data(), uid: docSnap.id } as AppModelType;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
return null;
|
|
@@ -31,17 +31,29 @@ export const useEnsureDoc = ({ reference, path, pathSegments, defaults, options
|
|
|
31
31
|
return useQuery({
|
|
32
32
|
...options,
|
|
33
33
|
queryFn: async () => {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
const createDoc = async () => {
|
|
35
|
+
const docRef = getDocRef({ db, reference, path, pathSegments });
|
|
36
|
+
if (!docRef) {
|
|
37
|
+
throw new Error(`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`);
|
|
38
|
+
}
|
|
39
|
+
await setDoc(docRef, defaults);
|
|
40
|
+
const docSnap = await getDoc(docRef);
|
|
41
|
+
return { ...docSnap.data(), uid: docSnap.id };
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
|
|
45
|
+
if (existingDocSnap?.exists) {
|
|
46
|
+
return { ...existingDocSnap.data(), uid: existingDocSnap.id };
|
|
47
|
+
}
|
|
48
|
+
return createDoc();
|
|
37
49
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
catch (e) {
|
|
51
|
+
// permission denied error may arise because of a security rule
|
|
52
|
+
if (e.code === "permission-denied") {
|
|
53
|
+
return createDoc();
|
|
54
|
+
}
|
|
55
|
+
throw e;
|
|
41
56
|
}
|
|
42
|
-
await setDoc(docRef, defaults);
|
|
43
|
-
const docSnap = await getDoc(docRef);
|
|
44
|
-
return { ...docSnap.data(), uid: docSnap.id };
|
|
45
57
|
}
|
|
46
58
|
});
|
|
47
59
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
|
|
2
|
-
import { getDoc, setDoc } from "firebase/firestore";
|
|
2
|
+
import { FirestoreError, getDoc, setDoc } from "firebase/firestore";
|
|
3
3
|
|
|
4
4
|
import { AppModel } from "../../types";
|
|
5
5
|
import { GetDocDataOptions } from "./utils/getDocData";
|
|
@@ -58,22 +58,35 @@ export const useEnsureDoc = <AppModelType extends AppModel = AppModel>({
|
|
|
58
58
|
return useQuery({
|
|
59
59
|
...options,
|
|
60
60
|
queryFn: async () => {
|
|
61
|
-
const
|
|
61
|
+
const createDoc = async () => {
|
|
62
|
+
const docRef = getDocRef({ db, reference, path, pathSegments });
|
|
63
|
+
if (!docRef) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
62
68
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
await setDoc<AppModelType, AppModelType>(docRef, defaults);
|
|
70
|
+
const docSnap = await getDoc(docRef);
|
|
71
|
+
return { ...(docSnap.data() as AppModelType), uid: docSnap.id };
|
|
72
|
+
};
|
|
66
73
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
try {
|
|
75
|
+
const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
|
|
76
|
+
|
|
77
|
+
if (existingDocSnap?.exists) {
|
|
78
|
+
return { ...(existingDocSnap.data() as AppModelType), uid: existingDocSnap.id };
|
|
79
|
+
}
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
return createDoc();
|
|
82
|
+
} catch (e) {
|
|
83
|
+
// permission denied error may arise because of a security rule
|
|
84
|
+
if ((e as FirestoreError).code === "permission-denied") {
|
|
85
|
+
return createDoc();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
77
90
|
}
|
|
78
91
|
});
|
|
79
92
|
};
|