Skip to content

Commit b826d61

Browse files
authored
Merge pull request #80 from mauriceackel/feature/hydrate-reference
Possibility to populate the firebase.firestore.QuerySnapshot.ref field
2 parents faad677 + 8a2583a commit b826d61

File tree

7 files changed

+27
-9
lines changed

7 files changed

+27
-9
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 firebase from 'firebase/app';
22

33
export const snapshotToData = (
44
snapshot: firebase.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
@@ -52,10 +52,12 @@ export const useCollectionData = <T>(
5252
query?: firebase.firestore.Query | null,
5353
options?: {
5454
idField?: string;
55+
refField?: string;
5556
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
5657
}
5758
): CollectionDataHook<T> => {
5859
const idField = options ? options.idField : undefined;
60+
const refField = options ? options.refField : undefined;
5961
const snapshotListenOptions = options
6062
? options.snapshotListenOptions
6163
: undefined;
@@ -65,9 +67,9 @@ export const useCollectionData = <T>(
6567
const values = useMemo(
6668
() =>
6769
(snapshots
68-
? snapshots.docs.map((doc) => snapshotToData(doc, idField))
70+
? snapshots.docs.map(doc => snapshotToData(doc, idField, refField))
6971
: undefined) as T[],
70-
[snapshots, idField]
72+
[snapshots, idField, refField]
7173
);
7274

7375
const resArray: CollectionDataHook<T> = [values, loading, error];

firestore/useCollectionOnce.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,21 @@ export const useCollectionDataOnce = <T>(
4545
options?: {
4646
getOptions?: firebase.firestore.GetOptions;
4747
idField?: string;
48+
refField?: string;
4849
}
4950
): CollectionDataOnceHook<T> => {
5051
const idField = options ? options.idField : undefined;
52+
const refField = options ? options.refField : undefined;
5153
const getOptions = options ? options.getOptions : undefined;
5254
const [snapshots, loading, error] = useCollectionOnce<T>(query, {
5355
getOptions,
5456
});
5557
const values = useMemo(
5658
() =>
5759
(snapshots
58-
? snapshots.docs.map((doc) => snapshotToData(doc, idField))
60+
? snapshots.docs.map((doc) => snapshotToData(doc, idField, refField))
5961
: undefined) as T[],
60-
[snapshots, idField]
62+
[snapshots, idField, refField]
6163
);
6264
const resArray: CollectionDataOnceHook<T> = [values, loading, error];
6365
return useMemo(() => resArray, resArray);

firestore/useDocument.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ export const useDocumentData = <T>(
5252
docRef?: firebase.firestore.DocumentReference | null,
5353
options?: {
5454
idField?: string;
55+
refField?: string;
5556
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
5657
}
5758
): DocumentDataHook<T> => {
5859
const idField = options ? options.idField : undefined;
60+
const refField = options ? options.refField : undefined;
5961
const snapshotListenOptions = options
6062
? options.snapshotListenOptions
6163
: undefined;
6264
const [snapshot, loading, error] = useDocument<T>(docRef, {
6365
snapshotListenOptions,
6466
});
6567
const value = useMemo(
66-
() => (snapshot ? snapshotToData(snapshot, idField) : undefined) as T,
67-
[snapshot, idField]
68+
() => (snapshot ? snapshotToData(snapshot, idField, refField) : undefined) as T,
69+
[snapshot, idField, refField]
6870
);
6971

7072
const resArray: DocumentDataHook<T> = [value, loading, error];

firestore/useDocumentOnce.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ export const useDocumentDataOnce = <T>(
4545
options?: {
4646
getOptions?: firebase.firestore.GetOptions;
4747
idField?: string;
48+
refField?: string;
4849
}
4950
): DocumentDataOnceHook<T> => {
5051
const idField = options ? options.idField : undefined;
52+
const refField = options ? options.refField : undefined;
5153
const getOptions = options ? options.getOptions : undefined;
5254
const [snapshot, loading, error] = useDocumentOnce<T>(docRef, { getOptions });
5355
const value = useMemo(
54-
() => (snapshot ? snapshotToData(snapshot, idField) : undefined) as T,
55-
[snapshot, idField]
56+
() => (snapshot ? snapshotToData(snapshot, idField, refField) : undefined) as T,
57+
[snapshot, idField, refField]
5658
);
5759

5860
const resArray: DocumentDataOnceHook<T> = [value, loading, error];

0 commit comments

Comments
 (0)