Skip to content

Commit 3cb4468

Browse files
authored
Rxfire object args (#427)
* change rxfire idfield * update objectVal * regenerate docs
1 parent b5c4c67 commit 3cb4468

File tree

5 files changed

+15
-41
lines changed

5 files changed

+15
-41
lines changed

docs/reference/modules/database.md

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"typescript": "^4.2.4"
100100
},
101101
"dependencies": {
102-
"rxfire": "6.0.0-rc.1",
102+
"rxfire": "6.0.0-rc.2",
103103
"rxjs": "^6.6.3 || ^7.0.1"
104104
},
105105
"resolutions": {

src/database.tsx

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { list, object, QueryChange, listVal } from 'rxfire/database';
1+
import { list, object, QueryChange, listVal, objectVal } from 'rxfire/database';
22
import { ReactFireOptions, useObservable, checkIdField, ObservableStatus, ReactFireGlobals } from './';
33

4-
import { Observable } from 'rxjs';
5-
import { map } from 'rxjs/operators';
6-
74
import type { Query as DatabaseQuery, DatabaseReference } from 'firebase/database';
85

96
// Since we're side-effect free, we need to ensure our observableId cache is global
@@ -34,33 +31,10 @@ export function useDatabaseObject<T = unknown>(ref: DatabaseReference, options?:
3431
return useObservable(observableId, observable$, options);
3532
}
3633

37-
// ============================================================================
38-
// TODO: switch to rxfire's objectVal once this PR is merged:
39-
// https://github.com/firebase/firebase-js-sdk/pull/2352
40-
41-
function objectVal<T>(query: DatabaseQuery, keyField?: string): Observable<T> {
42-
return object(query).pipe(map((change) => changeToData(change, keyField) as T));
43-
}
44-
45-
function changeToData(change: QueryChange, keyField?: string): {} {
46-
const val = change.snapshot.val();
47-
48-
// don't worry about setting IDs if the value is a primitive type
49-
if (typeof val !== 'object') {
50-
return val;
51-
}
52-
53-
return {
54-
...change.snapshot.val(),
55-
...(keyField ? { [keyField]: change.snapshot.key } : null),
56-
};
57-
}
58-
// ============================================================================
59-
6034
export function useDatabaseObjectData<T>(ref: DatabaseReference, options?: ReactFireOptions<T>): ObservableStatus<T> {
6135
const idField = options ? checkIdField(options) : 'NO_ID_FIELD';
6236
const observableId = `database:objectVal:${ref.toString()}:idField=${idField}`;
63-
const observable$ = objectVal<T>(ref, idField);
37+
const observable$ = objectVal<T>(ref, { keyField: idField });
6438

6539
return useObservable(observableId, observable$, options);
6640
}
@@ -87,6 +61,6 @@ export function useDatabaseListData<T = { [key: string]: unknown }>(
8761
): ObservableStatus<T[] | null> {
8862
const idField = options ? checkIdField(options) : 'NO_ID_FIELD';
8963
const observableId = `database:listVal:${getUniqueIdForDatabaseQuery(ref)}:idField=${idField}`;
90-
const observable$ = listVal<T>(ref, idField);
64+
const observable$ = listVal<T>(ref, { keyField: idField });
9165
return useObservable(observableId, observable$, options);
9266
}

src/firestore.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function useFirestoreDocData<T = unknown>(ref: DocumentReference<T>, opti
6363
const idField = options ? checkIdField(options) : 'NO_ID_FIELD';
6464

6565
const observableId = `firestore:docData:${ref.firestore.app.name}:${ref.path}:idField=${idField}`;
66-
const observable = docData(ref, idField);
66+
const observable = docData(ref, { idField });
6767

6868
return useObservable(observableId, observable, options);
6969
}
@@ -75,7 +75,7 @@ export function useFirestoreDocDataOnce<T = unknown>(ref: DocumentReference<T>,
7575
const idField = options ? checkIdField(options) : 'NO_ID_FIELD';
7676

7777
const observableId = `firestore:docDataOnce:${ref.firestore.app.name}:${ref.path}:idField=${idField}`;
78-
const observable$ = docData(ref, idField).pipe(first());
78+
const observable$ = docData(ref, { idField }).pipe(first());
7979

8080
return useObservable(observableId, observable$, options);
8181
}
@@ -96,7 +96,7 @@ export function useFirestoreCollection<T = DocumentData>(query: FirestoreQuery<T
9696
export function useFirestoreCollectionData<T = DocumentData>(query: FirestoreQuery<T>, options?: ReactFireOptions<T[]>): ObservableStatus<T[]> {
9797
const idField = options ? checkIdField(options) : 'NO_ID_FIELD';
9898
const observableId = `firestore:collectionData:${getUniqueIdForFirestoreQuery(query)}:idField=${idField}`;
99-
const observable$ = collectionData(query, idField);
99+
const observable$ = collectionData(query, { idField });
100100

101101
return useObservable(observableId, observable$, options);
102102
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10554,10 +10554,10 @@ run-queue@^1.0.0, run-queue@^1.0.3:
1055410554
dependencies:
1055510555
aproba "^1.1.1"
1055610556

10557-
10558-
version "6.0.0-rc.1"
10559-
resolved "https://registry.yarnpkg.com/rxfire/-/rxfire-6.0.0-rc.1.tgz#a3c95cdcecf5bd26823377d9836ecb5005156ea1"
10560-
integrity sha512-2wGzy/iMkYcBnTCwk1JNnX0tL0h/MXh9dki1bR+/rsxzr4S6T6uhLYAknp7lsPvbxSpvpis4io3k1yWXnUMjsQ==
10557+
10558+
version "6.0.0-rc.2"
10559+
resolved "https://registry.yarnpkg.com/rxfire/-/rxfire-6.0.0-rc.2.tgz#fde8f18341ec0362a07c2bc99499b5d01f5f0abf"
10560+
integrity sha512-iiDOSTwY9j1v0CKedkReOLywbEzOutP+Xc9Vl5kFp+wHBCEWStlbCk1VulQQwyOdPb/lAiQYRMKM5gQrs0ipTQ==
1056110561
dependencies:
1056210562
tslib "^1.9.0 || ~2.1.0"
1056310563

0 commit comments

Comments
 (0)