Skip to content

Commit 9cdf178

Browse files
committed
Possibility to populate the firebase.firestore.QuerySnapshot.ref field
1 parent 048dcb4 commit 9cdf178

File tree

7 files changed

+25
-7
lines changed

7 files changed

+25
-7
lines changed

firestore/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ The `useCollectionData` hook takes the following parameters:
118118
- `query`: (optional) `firebase.firestore.Query` for the data you would like to load
119119
- `options`: (optional) `Object` with the following parameters:
120120
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.id` property.
121+
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
121122
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
122123

123124
Returns:
@@ -140,6 +141,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
140141
- `options`: (optional) `Object` with the following parameters:
141142
- `getOptions`: (optional) `firebase.firestore.GetOptions` to customise how the collection is loaded
142143
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.id` property.
144+
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
143145

144146
Returns:
145147

@@ -225,6 +227,7 @@ The `useDocumentData` hook takes the following parameters:
225227
- `reference`: (optional) `firebase.firestore.DocumentReference` for the data you would like to load
226228
- `options`: (optional) `Object` with the following parameters:
227229
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.DocumentSnapshot.id` property.
230+
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
228231
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded
229232

230233
Returns:
@@ -247,6 +250,7 @@ The `useDocumentDataOnce` hook takes the following parameters:
247250
- `options`: (optional) `Object` with the following parameters:
248251
- `getOptions`: (optional) `firebase.firestore.GetOptions` to customise how the collection is loaded
249252
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.DocumentSnapshot.id` property.
253+
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
250254

251255
Returns:
252256

firestore/helpers/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { firestore } from 'firebase';
22

33
export const snapshotToData = (
44
snapshot: firestore.DocumentSnapshot,
5-
idField?: string
5+
idField?: string,
6+
refField?: string,
67
) => {
78
if (!snapshot.exists) {
89
return undefined;
@@ -11,5 +12,6 @@ export const snapshotToData = (
1112
return {
1213
...snapshot.data(),
1314
...(idField ? { [idField]: snapshot.id } : null),
15+
...(refField ? { [refField]: snapshot.ref } : null),
1416
};
1517
};

firestore/index.js.flow

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare export function useCollectionData<T>(
3131
query?: Query | null,
3232
options?: {
3333
idField?: string,
34+
refField?: string,
3435
snapshotListenOptions?: SnapshotListenOptions,
3536
}
3637
): CollectionDataHook<T>;
@@ -39,6 +40,7 @@ declare export function useCollectionDataOnce<T>(
3940
options?: {
4041
getOptions?: GetOptions,
4142
idField?: string,
43+
refField?: string,
4244
}
4345
): CollectionDataHook<T>;
4446
declare export function useDocument(
@@ -57,6 +59,7 @@ declare export function useDocumentData<T>(
5759
ref?: DocumentReference | null,
5860
options?: {
5961
idField?: string,
62+
refField?: string,
6063
snapshotListenOptions?: SnapshotListenOptions,
6164
}
6265
): DocumentDataHook<T>;
@@ -65,5 +68,6 @@ declare export function useDocumentDataOnce<T>(
6568
options?: {
6669
getOptions?: GetOptions,
6770
idField?: string,
71+
refField?: string,
6872
}
6973
): DocumentDataHook<T>;

firestore/useCollection.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ export const useCollectionData = <T>(
4747
query?: firestore.Query | null,
4848
options?: {
4949
idField?: string;
50+
refField?: string;
5051
snapshotListenOptions?: firestore.SnapshotListenOptions;
5152
}
5253
): CollectionDataHook<T> => {
5354
const idField = options ? options.idField : undefined;
55+
const refField = options ? options.refField : undefined;
5456
const snapshotListenOptions = options
5557
? options.snapshotListenOptions
5658
: undefined;
@@ -60,9 +62,9 @@ export const useCollectionData = <T>(
6062
const values = useMemo(
6163
() =>
6264
(snapshot
63-
? snapshot.docs.map(doc => snapshotToData(doc, idField))
65+
? snapshot.docs.map(doc => snapshotToData(doc, idField, refField))
6466
: undefined) as T[],
65-
[snapshot, idField]
67+
[snapshot, idField, refField]
6668
);
6769
return [values, loading, error];
6870
};

firestore/useCollectionOnce.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ export const useCollectionDataOnce = <T>(
4040
options?: {
4141
getOptions?: firestore.GetOptions;
4242
idField?: string;
43+
refField?: string;
4344
}
4445
): CollectionDataOnceHook<T> => {
4546
const idField = options ? options.idField : undefined;
47+
const refField = options ? options.refField : undefined;
4648
const getOptions = options ? options.getOptions : undefined;
4749
const [value, loading, error] = useCollectionOnce(query, { getOptions });
4850
return [
4951
(value
50-
? value.docs.map(doc => snapshotToData(doc, idField))
52+
? value.docs.map(doc => snapshotToData(doc, idField, refField))
5153
: undefined) as T[],
5254
loading,
5355
error,

firestore/useDocument.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,21 @@ export const useDocumentData = <T>(
4747
docRef?: firestore.DocumentReference | null,
4848
options?: {
4949
idField?: string;
50+
refField?: string;
5051
snapshotListenOptions?: firestore.SnapshotListenOptions;
5152
}
5253
): DocumentDataHook<T> => {
5354
const idField = options ? options.idField : undefined;
55+
const refField = options ? options.refField : undefined;
5456
const snapshotListenOptions = options
5557
? options.snapshotListenOptions
5658
: undefined;
5759
const [snapshot, loading, error] = useDocument(docRef, {
5860
snapshotListenOptions,
5961
});
6062
const value = useMemo(
61-
() => (snapshot ? snapshotToData(snapshot, idField) : undefined) as T,
62-
[snapshot, idField]
63+
() => (snapshot ? snapshotToData(snapshot, idField, refField) : undefined) as T,
64+
[snapshot, idField, refField]
6365
);
6466
return [value, loading, error];
6567
};

firestore/useDocumentOnce.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ export const useDocumentDataOnce = <T>(
4040
options?: {
4141
getOptions?: firestore.GetOptions;
4242
idField?: string;
43+
refField?: string;
4344
}
4445
): DocumentDataOnceHook<T> => {
4546
const idField = options ? options.idField : undefined;
47+
const refField = options ? options.refField : undefined;
4648
const getOptions = options ? options.getOptions : undefined;
4749
const [value, loading, error] = useDocumentOnce(docRef, { getOptions });
4850
return [
49-
(value ? snapshotToData(value, idField) : undefined) as T,
51+
(value ? snapshotToData(value, idField, refField) : undefined) as T,
5052
loading,
5153
error,
5254
];

0 commit comments

Comments
 (0)