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.
- package/dist/react-native/auth/useCurrentUser.js +1 -1
- package/dist/react-native/firestore/index.d.ts +1 -0
- package/dist/react-native/firestore/index.js +1 -0
- package/dist/react-native/firestore/useFirestoreQueryEngine.d.ts +44 -0
- package/dist/react-native/firestore/useFirestoreQueryEngine.js +47 -0
- package/dist/react-native.d.ts +2 -0
- package/dist/react-native.js +2 -0
- package/dist/web/auth/useCurrentUser.js +1 -1
- package/dist/web/firestore/index.d.ts +1 -0
- package/dist/web/firestore/index.js +1 -0
- package/dist/web/firestore/useFirestoreQueryEngine.d.ts +44 -0
- package/dist/web/firestore/useFirestoreQueryEngine.js +47 -0
- package/dist/web.d.ts +2 -0
- package/dist/web.js +2 -0
- package/package.json +9 -22
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -4
|
@@ -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
|
|
11
|
+
const [currentUser, setCurrentUser] = useState(firebaseAuth?.currentUser ?? null);
|
|
12
12
|
useEffect(() => {
|
|
13
13
|
const unsubscribe = onAuthStateChanged(firebaseAuth, (user) => {
|
|
14
14
|
setCurrentUser(user);
|
|
@@ -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
|
+
};
|
|
@@ -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
|
|
11
|
+
const [currentUser, setCurrentUser] = useState(firebaseAuth?.currentUser ?? null);
|
|
12
12
|
useEffect(() => {
|
|
13
13
|
const unsubscribe = onAuthStateChanged(firebaseAuth, (user) => {
|
|
14
14
|
setCurrentUser(user);
|
|
@@ -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
package/dist/web.js
ADDED
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.
|
|
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.
|
|
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.
|
|
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
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
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": "
|
|
94
|
+
"version": "3.1.0"
|
|
108
95
|
}
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED