react-query-firebase 2.5.0 → 2.6.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 CHANGED
@@ -74,5 +74,5 @@
74
74
  "docs:build": "vitepress build docs",
75
75
  "docs:preview": "vitepress preview docs"
76
76
  },
77
- "version": "2.5.0"
77
+ "version": "2.6.0"
78
78
  }
@@ -20,3 +20,4 @@ export * from "./utils/getDocSnap";
20
20
  export * from "./utils/getDocRef";
21
21
  export * from "./utils/buildQueryConstraint";
22
22
  export * from "./utils/buildCompositeFilter";
23
+ export * from "./useEnsureDoc";
@@ -20,3 +20,4 @@ export * from "./utils/getDocSnap";
20
20
  export * from "./utils/getDocRef";
21
21
  export * from "./utils/buildQueryConstraint";
22
22
  export * from "./utils/buildCompositeFilter";
23
+ export * from "./useEnsureDoc";
@@ -20,3 +20,4 @@ export * from "./utils/getDocSnap";
20
20
  export * from "./utils/getDocRef";
21
21
  export * from "./utils/buildQueryConstraint";
22
22
  export * from "./utils/buildCompositeFilter";
23
+ export * from "./useEnsureDoc";
@@ -0,0 +1,40 @@
1
+ import { UseQueryOptions } from "@tanstack/react-query";
2
+ import { AppModel } from "../../types";
3
+ import { GetDocDataOptions } from "./utils/getDocData";
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
8
+ /**
9
+ * Reference to a document that must be written
10
+ */
11
+ defaults: AppModelType;
12
+ /**
13
+ * Options for useMutation hook excluding mutationFn.
14
+ */
15
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
16
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
17
+ /**
18
+ * This hook checks if a doc with a requested reference exists.
19
+ * It creates a document with requested data if it does not exist.
20
+ *
21
+ * @group Hook
22
+ *
23
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
24
+ *
25
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
26
+ *
27
+ * @example
28
+ * ```jsx
29
+ * export const MyComponent = () => {
30
+ * const {data} = useEnsureDocQuery({
31
+ * options: {
32
+ * },
33
+ * reference: collection().doc(),
34
+ * defaults: {prop1: 'value1'}
35
+ * });
36
+ *
37
+ * };
38
+ * ```
39
+ */
40
+ export declare const useEnsureDoc: <AppModelType extends AppModel = AppModel>({ reference, path, pathSegments, defaults, options }: UseEnsureDocOptions<AppModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
@@ -0,0 +1,47 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "@react-native-firebase/firestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
+ import { getDocSnap } from "./utils/getDocSnap";
5
+ import { useFirestore } from "./useFirestore";
6
+ /**
7
+ * This hook checks if a doc with a requested reference exists.
8
+ * It creates a document with requested data if it does not exist.
9
+ *
10
+ * @group Hook
11
+ *
12
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
13
+ *
14
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
15
+ *
16
+ * @example
17
+ * ```jsx
18
+ * export const MyComponent = () => {
19
+ * const {data} = useEnsureDocQuery({
20
+ * options: {
21
+ * },
22
+ * reference: collection().doc(),
23
+ * defaults: {prop1: 'value1'}
24
+ * });
25
+ *
26
+ * };
27
+ * ```
28
+ */
29
+ export const useEnsureDoc = ({ reference, path, pathSegments, defaults, options }) => {
30
+ const db = useFirestore();
31
+ return useQuery({
32
+ ...options,
33
+ queryFn: async () => {
34
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
35
+ if (existingDocSnap?.exists) {
36
+ return { ...existingDocSnap.data(), uid: existingDocSnap.id };
37
+ }
38
+ const docRef = getDocRef({ db, reference, path, pathSegments });
39
+ if (!docRef) {
40
+ throw new Error(`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`);
41
+ }
42
+ await setDoc(docRef, defaults);
43
+ const docSnap = await getDoc(docRef);
44
+ return { ...docSnap.data(), uid: docSnap.id };
45
+ }
46
+ });
47
+ };
@@ -0,0 +1,79 @@
1
+ import { useQuery, UseQueryOptions } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "@react-native-firebase/firestore";
3
+
4
+ import { AppModel } from "../../types";
5
+ import { GetDocDataOptions } from "./utils/getDocData";
6
+ import { getDocRef } from "./utils/getDocRef";
7
+ import { getDocSnap } from "./utils/getDocSnap";
8
+ import { useFirestore } from "./useFirestore";
9
+
10
+ /**
11
+ * @inline
12
+ */
13
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
14
+ /**
15
+ * Reference to a document that must be written
16
+ */
17
+ defaults: AppModelType;
18
+
19
+ /**
20
+ * Options for useMutation hook excluding mutationFn.
21
+ */
22
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> &
23
+ Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
24
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
25
+
26
+ /**
27
+ * This hook checks if a doc with a requested reference exists.
28
+ * It creates a document with requested data if it does not exist.
29
+ *
30
+ * @group Hook
31
+ *
32
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
33
+ *
34
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
35
+ *
36
+ * @example
37
+ * ```jsx
38
+ * export const MyComponent = () => {
39
+ * const {data} = useEnsureDocQuery({
40
+ * options: {
41
+ * },
42
+ * reference: collection().doc(),
43
+ * defaults: {prop1: 'value1'}
44
+ * });
45
+ *
46
+ * };
47
+ * ```
48
+ */
49
+ export const useEnsureDoc = <AppModelType extends AppModel = AppModel>({
50
+ reference,
51
+ path,
52
+ pathSegments,
53
+ defaults,
54
+ options
55
+ }: UseEnsureDocOptions<AppModelType>) => {
56
+ const db = useFirestore();
57
+
58
+ return useQuery({
59
+ ...options,
60
+ queryFn: async () => {
61
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
62
+
63
+ if (existingDocSnap?.exists) {
64
+ return { ...(existingDocSnap.data() as AppModelType), uid: existingDocSnap.id };
65
+ }
66
+
67
+ const docRef = getDocRef({ db, reference, path, pathSegments });
68
+ if (!docRef) {
69
+ throw new Error(
70
+ `Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`
71
+ );
72
+ }
73
+
74
+ await setDoc<AppModelType>(docRef, defaults);
75
+ const docSnap = await getDoc(docRef);
76
+ return { ...(docSnap.data() as AppModelType), uid: docSnap.id };
77
+ }
78
+ });
79
+ };
@@ -15,6 +15,7 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./useEnsureDoc";
18
19
  export * from "./utils/getDocData";
19
20
  export * from "./utils/getDocSnap";
20
21
  export * from "./utils/getDocRef";
@@ -15,6 +15,7 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./useEnsureDoc";
18
19
  export * from "./utils/getDocData";
19
20
  export * from "./utils/getDocSnap";
20
21
  export * from "./utils/getDocRef";
@@ -15,6 +15,7 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./useEnsureDoc";
18
19
  export * from "./utils/getDocData";
19
20
  export * from "./utils/getDocSnap";
20
21
  export * from "./utils/getDocRef";
@@ -0,0 +1,40 @@
1
+ import { UseQueryOptions } from "@tanstack/react-query";
2
+ import { AppModel } from "../../types";
3
+ import { GetDocDataOptions } from "./utils/getDocData";
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
8
+ /**
9
+ * Reference to a document that must be written
10
+ */
11
+ defaults: AppModelType;
12
+ /**
13
+ * Options for useMutation hook excluding mutationFn.
14
+ */
15
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
16
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
17
+ /**
18
+ * This hook checks if a doc with a requested reference exists.
19
+ * It creates a document with requested data if it does not exist.
20
+ *
21
+ * @group Hook
22
+ *
23
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
24
+ *
25
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
26
+ *
27
+ * @example
28
+ * ```jsx
29
+ * export const MyComponent = () => {
30
+ * const {data} = useEnsureDocQuery({
31
+ * options: {
32
+ * },
33
+ * reference: collection().doc(),
34
+ * defaults: {prop1: 'value1'}
35
+ * });
36
+ *
37
+ * };
38
+ * ```
39
+ */
40
+ export declare const useEnsureDoc: <AppModelType extends AppModel = AppModel>({ reference, path, pathSegments, defaults, options }: UseEnsureDocOptions<AppModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
@@ -0,0 +1,47 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "firebase/firestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
+ import { getDocSnap } from "./utils/getDocSnap";
5
+ import { useFirestore } from "./useFirestore";
6
+ /**
7
+ * This hook checks if a doc with a requested reference exists.
8
+ * It creates a document with requested data if it does not exist.
9
+ *
10
+ * @group Hook
11
+ *
12
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
13
+ *
14
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
15
+ *
16
+ * @example
17
+ * ```jsx
18
+ * export const MyComponent = () => {
19
+ * const {data} = useEnsureDocQuery({
20
+ * options: {
21
+ * },
22
+ * reference: collection().doc(),
23
+ * defaults: {prop1: 'value1'}
24
+ * });
25
+ *
26
+ * };
27
+ * ```
28
+ */
29
+ export const useEnsureDoc = ({ reference, path, pathSegments, defaults, options }) => {
30
+ const db = useFirestore();
31
+ return useQuery({
32
+ ...options,
33
+ queryFn: async () => {
34
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
35
+ if (existingDocSnap?.exists) {
36
+ return { ...existingDocSnap.data(), uid: existingDocSnap.id };
37
+ }
38
+ const docRef = getDocRef({ db, reference, path, pathSegments });
39
+ if (!docRef) {
40
+ throw new Error(`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`);
41
+ }
42
+ await setDoc(docRef, defaults);
43
+ const docSnap = await getDoc(docRef);
44
+ return { ...docSnap.data(), uid: docSnap.id };
45
+ }
46
+ });
47
+ };
@@ -0,0 +1,79 @@
1
+ import { useQuery, UseQueryOptions } from "@tanstack/react-query";
2
+ import { getDoc, setDoc } from "firebase/firestore";
3
+
4
+ import { AppModel } from "../../types";
5
+ import { GetDocDataOptions } from "./utils/getDocData";
6
+ import { getDocRef } from "./utils/getDocRef";
7
+ import { getDocSnap } from "./utils/getDocSnap";
8
+ import { useFirestore } from "./useFirestore";
9
+
10
+ /**
11
+ * @inline
12
+ */
13
+ export type UseEnsureDocOptions<AppModelType extends AppModel = AppModel> = {
14
+ /**
15
+ * Reference to a document that must be written
16
+ */
17
+ defaults: AppModelType;
18
+
19
+ /**
20
+ * Options for useMutation hook excluding mutationFn.
21
+ */
22
+ options: Omit<UseQueryOptions<AppModelType, Error, AppModelType>, "queryFn"> &
23
+ Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
24
+ } & Omit<GetDocDataOptions<AppModelType>, "db">;
25
+
26
+ /**
27
+ * This hook checks if a doc with a requested reference exists.
28
+ * It creates a document with requested data if it does not exist.
29
+ *
30
+ * @group Hook
31
+ *
32
+ * @param {UseEnsureDocOptions<AppModelType>} options - Configuration options for mutation.
33
+ *
34
+ * @returns {UseQueryResult<AppModelType, Error>} A mutation result
35
+ *
36
+ * @example
37
+ * ```jsx
38
+ * export const MyComponent = () => {
39
+ * const {data} = useEnsureDocQuery({
40
+ * options: {
41
+ * },
42
+ * reference: collection().doc(),
43
+ * defaults: {prop1: 'value1'}
44
+ * });
45
+ *
46
+ * };
47
+ * ```
48
+ */
49
+ export const useEnsureDoc = <AppModelType extends AppModel = AppModel>({
50
+ reference,
51
+ path,
52
+ pathSegments,
53
+ defaults,
54
+ options
55
+ }: UseEnsureDocOptions<AppModelType>) => {
56
+ const db = useFirestore();
57
+
58
+ return useQuery({
59
+ ...options,
60
+ queryFn: async () => {
61
+ const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
62
+
63
+ if (existingDocSnap?.exists) {
64
+ return { ...(existingDocSnap.data() as AppModelType), uid: existingDocSnap.id };
65
+ }
66
+
67
+ const docRef = getDocRef({ db, reference, path, pathSegments });
68
+ if (!docRef) {
69
+ throw new Error(
70
+ `Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`
71
+ );
72
+ }
73
+
74
+ await setDoc<AppModelType, AppModelType>(docRef, defaults);
75
+ const docSnap = await getDoc(docRef);
76
+ return { ...(docSnap.data() as AppModelType), uid: docSnap.id };
77
+ }
78
+ });
79
+ };