react-query-firebase 2.15.1 → 3.1.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.
@@ -8,7 +8,7 @@ import { useAuth } from "./useAuth.js";
8
8
  */
9
9
  export const useCurrentUser = () => {
10
10
  const firebaseAuth = useAuth();
11
- const [currentUser, setCurrentUser] = useState(firebaseAuth.currentUser);
11
+ const [currentUser, setCurrentUser] = useState(firebaseAuth?.currentUser ?? null);
12
12
  useEffect(() => {
13
13
  const unsubscribe = onAuthStateChanged(firebaseAuth, (user) => {
14
14
  setCurrentUser(user);
@@ -21,3 +21,4 @@ export * from "./utils/getDocRef.js";
21
21
  export * from "./utils/buildQueryConstraint.js";
22
22
  export * from "./utils/buildCompositeFilter.js";
23
23
  export * from "./useEnsureDoc.js";
24
+ export * from "./useFirestoreQueryEngine.js";
@@ -21,3 +21,4 @@ export * from "./utils/getDocRef.js";
21
21
  export * from "./utils/buildQueryConstraint.js";
22
22
  export * from "./utils/buildCompositeFilter.js";
23
23
  export * from "./useEnsureDoc.js";
24
+ export * from "./useFirestoreQueryEngine.js";
@@ -0,0 +1,44 @@
1
+ import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
+ import { AppModel } from "../../types/index.js";
3
+ import { type NonFilterQueryConstraint } from "../../types/QueryConstraints.js";
4
+ import { type CompositeFilter } from "./utils/buildCompositeFilter.js";
5
+ /**
6
+ * @inline
7
+ */
8
+ type UseFirestoreQueryEngineOptions<AppModelType extends AppModel = AppModel> = {
9
+ /**
10
+ * Reference to a Firestore collection
11
+ */
12
+ collectionReference: FirebaseFirestoreTypes.CollectionReference<AppModelType>;
13
+ /**
14
+ * Non composite filter constraints such as limit, order, where
15
+ */
16
+ queryConstraints?: NonFilterQueryConstraint<AppModelType>[];
17
+ /**
18
+ * Composite filter
19
+ */
20
+ compositeFilter?: CompositeFilter<AppModelType>;
21
+ };
22
+ /**
23
+ * Returns an async wrapper around native firestore query engine.
24
+ *
25
+ * @group Hook
26
+ *
27
+ * @returns {(options: UseFirestoreQueryEngineOptions<AppModelType>): Promise<AppModelType[]>} A function to execute firestore query
28
+ *
29
+ * @example
30
+ * ```jsx
31
+ * export const MyComponent = () => {
32
+ * const query = useFirestoreQueryEngine();
33
+ * const callback = useCallback(async () => {
34
+ * const docs = await query({
35
+ * collectionReference: collection(),
36
+ * queryConstraints: [],
37
+ * });
38
+ * // do something with docs
39
+ * });
40
+ * };
41
+ * ```
42
+ */
43
+ export declare const useFirestoreQueryEngine: <AppModelType extends AppModel = AppModel>() => ((options: UseFirestoreQueryEngineOptions<AppModelType>) => Promise<AppModelType[]>);
44
+ export {};
@@ -0,0 +1,47 @@
1
+ import { getDocs, query, or, and } from "@react-native-firebase/firestore";
2
+ import { useCallback } from "react";
3
+ import { buildCompositeFilter } from "./utils/buildCompositeFilter.js";
4
+ import { buildQueryConstraint } from "./utils/buildQueryConstraint.js";
5
+ /**
6
+ * Returns an async wrapper around native firestore query engine.
7
+ *
8
+ * @group Hook
9
+ *
10
+ * @returns {(options: UseFirestoreQueryEngineOptions<AppModelType>): Promise<AppModelType[]>} A function to execute firestore query
11
+ *
12
+ * @example
13
+ * ```jsx
14
+ * export const MyComponent = () => {
15
+ * const query = useFirestoreQueryEngine();
16
+ * const callback = useCallback(async () => {
17
+ * const docs = await query({
18
+ * collectionReference: collection(),
19
+ * queryConstraints: [],
20
+ * });
21
+ * // do something with docs
22
+ * });
23
+ * };
24
+ * ```
25
+ */
26
+ export const useFirestoreQueryEngine = () => {
27
+ return useCallback(async ({ collectionReference, queryConstraints = [], compositeFilter: inputCompositeFilter }) => {
28
+ const compositeFilter = (inputCompositeFilter?.children?.map?.((subQuery) => buildCompositeFilter(subQuery))
29
+ ?.filter)?.((constraint) => !!constraint) ?? [];
30
+ const finalCompositeFilter = compositeFilter.length > 0
31
+ ? inputCompositeFilter?.operator === "OR"
32
+ ? or(...compositeFilter)
33
+ : and(...compositeFilter)
34
+ : undefined;
35
+ const queryToExecute = finalCompositeFilter
36
+ ? query(collectionReference, finalCompositeFilter, ...queryConstraints.map(buildQueryConstraint))
37
+ : query(collectionReference, ...queryConstraints.map(buildQueryConstraint));
38
+ const querySnapshot = await getDocs(queryToExecute);
39
+ const docs = [];
40
+ if (querySnapshot) {
41
+ querySnapshot.forEach((doc) => {
42
+ docs.push({ ...doc.data(), uid: doc.id });
43
+ });
44
+ }
45
+ return docs;
46
+ }, []);
47
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./react-native/index.js";
2
+ export * from "./types/index.js";
@@ -0,0 +1,2 @@
1
+ export * from "./react-native/index.js";
2
+ export * from "./types/index.js";
@@ -8,7 +8,7 @@ import { useAuth } from "./useAuth.js";
8
8
  */
9
9
  export const useCurrentUser = () => {
10
10
  const firebaseAuth = useAuth();
11
- const [currentUser, setCurrentUser] = useState(firebaseAuth.currentUser);
11
+ const [currentUser, setCurrentUser] = useState(firebaseAuth?.currentUser ?? null);
12
12
  useEffect(() => {
13
13
  const unsubscribe = onAuthStateChanged(firebaseAuth, (user) => {
14
14
  setCurrentUser(user);
@@ -21,3 +21,4 @@ export * from "./utils/getDocSnap.js";
21
21
  export * from "./utils/getDocRef.js";
22
22
  export * from "./utils/buildQueryConstraint.js";
23
23
  export * from "./utils/buildCompositeFilter.js";
24
+ export * from "./useFirestoreQueryEngine.js";
@@ -21,3 +21,4 @@ export * from "./utils/getDocSnap.js";
21
21
  export * from "./utils/getDocRef.js";
22
22
  export * from "./utils/buildQueryConstraint.js";
23
23
  export * from "./utils/buildCompositeFilter.js";
24
+ export * from "./useFirestoreQueryEngine.js";
@@ -0,0 +1,44 @@
1
+ import { CollectionReference } from "firebase/firestore";
2
+ import { NonFilterQueryConstraint } from "../../types/QueryConstraints.js";
3
+ import { AppModel } from "../../types/index.js";
4
+ import { CompositeFilter } from "./utils/buildCompositeFilter.js";
5
+ /**
6
+ * @inline
7
+ */
8
+ type UseFirestoreQueryEngineOptions<AppModelType extends AppModel = AppModel> = {
9
+ /**
10
+ * Reference to a Firestore collection
11
+ */
12
+ collectionReference: CollectionReference<AppModelType, AppModelType>;
13
+ /**
14
+ * Non composite filter constraints such as limit, order, where
15
+ */
16
+ queryConstraints: NonFilterQueryConstraint<AppModelType>[];
17
+ /**
18
+ * Composite filter
19
+ */
20
+ compositeFilter?: CompositeFilter;
21
+ };
22
+ /**
23
+ * Returns an async wrapper around native firestore query engine.
24
+ *
25
+ * @group Hook
26
+ *
27
+ * @returns {(options: UseFirestoreQueryEngineOptions<AppModelType>): Promise<AppModelType[]>} A function to execute firestore query
28
+ *
29
+ * @example
30
+ * ```jsx
31
+ * export const MyComponent = () => {
32
+ * const query = useFirestoreQueryEngine();
33
+ * const callback = useCallback(async () => {
34
+ * const docs = await query({
35
+ * collectionReference: collection(),
36
+ * queryConstraints: [],
37
+ * });
38
+ * // do something with docs
39
+ * });
40
+ * };
41
+ * ```
42
+ */
43
+ export declare const useFirestoreQueryEngine: <AppModelType extends AppModel = AppModel>() => ((options: UseFirestoreQueryEngineOptions<AppModelType>) => Promise<AppModelType[]>);
44
+ export {};
@@ -0,0 +1,47 @@
1
+ import { getDocs, query, or, and } from "firebase/firestore";
2
+ import { useCallback } from "react";
3
+ import { buildCompositeFilter } from "./utils/buildCompositeFilter.js";
4
+ import { buildQueryConstraint } from "./utils/buildQueryConstraint.js";
5
+ /**
6
+ * Returns an async wrapper around native firestore query engine.
7
+ *
8
+ * @group Hook
9
+ *
10
+ * @returns {(options: UseFirestoreQueryEngineOptions<AppModelType>): Promise<AppModelType[]>} A function to execute firestore query
11
+ *
12
+ * @example
13
+ * ```jsx
14
+ * export const MyComponent = () => {
15
+ * const query = useFirestoreQueryEngine();
16
+ * const callback = useCallback(async () => {
17
+ * const docs = await query({
18
+ * collectionReference: collection(),
19
+ * queryConstraints: [],
20
+ * });
21
+ * // do something with docs
22
+ * });
23
+ * };
24
+ * ```
25
+ */
26
+ export const useFirestoreQueryEngine = () => {
27
+ return useCallback(async ({ collectionReference, queryConstraints = [], compositeFilter: inputCompositeFilter }) => {
28
+ const compositeFilter = (inputCompositeFilter?.children?.map?.((subQuery) => buildCompositeFilter(subQuery))
29
+ ?.filter)?.((constraint) => !!constraint) ?? [];
30
+ const finalCompositeFilter = compositeFilter.length > 0
31
+ ? inputCompositeFilter?.operator === "OR"
32
+ ? or(...compositeFilter)
33
+ : and(...compositeFilter)
34
+ : undefined;
35
+ const queryToExecute = finalCompositeFilter
36
+ ? query(collectionReference, finalCompositeFilter, ...queryConstraints.map(buildQueryConstraint))
37
+ : query(collectionReference, ...queryConstraints);
38
+ const querySnapshot = await getDocs(queryToExecute);
39
+ const docs = [];
40
+ if (querySnapshot) {
41
+ querySnapshot.forEach((doc) => {
42
+ docs.push({ ...doc.data(), uid: doc.id });
43
+ });
44
+ }
45
+ return docs;
46
+ }, []);
47
+ };
package/dist/web.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./web/index.js";
2
+ export * from "./types/index.js";
package/dist/web.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./web/index.js";
2
+ export * from "./types/index.js";
package/package.json CHANGED
@@ -29,12 +29,12 @@
29
29
  "@react-native-firebase/firestore": "^23.7.0",
30
30
  "@react-native-firebase/installations": "^23.7.0",
31
31
  "@react-native-firebase/remote-config": "^23.7.0",
32
- "@tanstack/react-query": "^5.90.12",
32
+ "@tanstack/react-query": "^5.90.15",
33
33
  "@types/react": "^19.2.7",
34
34
  "eslint": "^9.39.2",
35
35
  "eslint-config-prettier": "^10.1.8",
36
36
  "eslint-plugin-import": "^2.32.0",
37
- "eslint-plugin-jest": "^29.5.0",
37
+ "eslint-plugin-jest": "^29.11.2",
38
38
  "eslint-plugin-jsonc": "^2.21.0",
39
39
  "eslint-plugin-prettier": "^5.5.4",
40
40
  "eslint-plugin-react": "^7.37.5",
@@ -48,7 +48,7 @@
48
48
  "typedoc-plugin-markdown": "^4.9.0",
49
49
  "typedoc-vitepress-theme": "^1.1.2",
50
50
  "typescript": "^5.9.3",
51
- "typescript-eslint": "^8.50.0",
51
+ "typescript-eslint": "^8.51.0",
52
52
  "vitepress": "^1.6.4"
53
53
  },
54
54
  "homepage": "https://github.com/vpishuk/react-query-firebase",
@@ -66,24 +66,11 @@
66
66
  },
67
67
  "exports": {
68
68
  ".": {
69
- "import": "./dist/index.js",
70
- "require": "./dist/index.js",
71
- "types": "./dist/index.d.ts"
72
- },
73
- "./react-native": {
74
- "import": "./dist/react-native/index.js",
75
- "require": "./dist/react-native/index.js",
76
- "types": "./dist/react-native/index.d.ts"
77
- },
78
- "./web": {
79
- "import": "./dist/web/index.js",
80
- "require": "./dist/web/index.js",
81
- "types": "./dist/web/index.d.ts"
82
- },
83
- "./types": {
84
- "import": "./dist/types/index.js",
85
- "require": "./dist/types/index.js",
86
- "types": "./dist/types/index.d.ts"
69
+ "browser": "./dist/web.js",
70
+ "react-native": "./dist/react-native.js",
71
+ "import": "./dist/web.js",
72
+ "require": "./dist/web.js",
73
+ "types": "./dist/web.d.ts"
87
74
  }
88
75
  },
89
76
  "files": [
@@ -104,5 +91,5 @@
104
91
  "docs:build": "vitepress build docs",
105
92
  "docs:preview": "vitepress preview docs"
106
93
  },
107
- "version": "2.15.1"
94
+ "version": "3.1.0"
108
95
  }
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * as RN from "./react-native/index.js";
2
- export * as Web from "./web/index.js";
package/dist/index.js DELETED
@@ -1,4 +0,0 @@
1
- import * as RN_1 from "./react-native/index.js";
2
- export { RN_1 as RN };
3
- import * as Web_1 from "./web/index.js";
4
- export { Web_1 as Web };