Skip to content

Commit 4a4a1f5

Browse files
authored
Merge pull request #1 from thefliik/collection-idFieldUpdate
feat(firestore): changed valueChanges() arg to options object
2 parents 45dcb64 + 810cff5 commit 4a4a1f5

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

docs/firestore/collections.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ interface DocumentSnapshot {
7373

7474
There are multiple ways of streaming collection data from Firestore.
7575

76-
### `valueChanges(idField?: string)`
76+
### `valueChanges({idField?: string})`
7777

78-
**What is it?** - The current state of your collection. Returns an Observable of data as a synchronized array of JSON objects. All Snapshot metadata is stripped and just the method provides only the data. Optionally, you can pass an `idField` string to include the document ID.
78+
**What is it?** - The current state of your collection. Returns an Observable of data as a synchronized array of JSON objects. All Snapshot metadata is stripped and just the document data is included. Optionally, you can pass an options object with an `idField` key containing a string. If provided, the returned JSON objects will include their document ID mapped to a property with the name provided by `idField`.
7979

8080
**Why would you use it?** - When you just need a list of data. No document metadata is attached to the resulting array which makes it simple to render to a view.
8181

82-
**When would you not use it?** - When you need a more complex data structure than an array or you need the `id` of each document to use data manipulation methods. This method assumes you either are saving the `id` to the document data or using a "readonly" approach.
82+
**When would you not use it?** - When you need a more complex data structure than an array.
8383

8484
**Best practices** - Use this method to display data on a page. It's simple but effective. Use `.snapshotChanges()` once your needs become more complex.
8585

src/firestore/collection/collection.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('AngularFirestoreCollection', () => {
7373
const ITEMS = 1;
7474
const { ref, stocks, names } = await collectionHarness(afs, ITEMS);
7575
const idField = 'myCustomID';
76-
const sub = stocks.valueChanges(idField).subscribe(data => {
76+
const sub = stocks.valueChanges({idField}).subscribe(data => {
7777
sub.unsubscribe();
7878
const stock = data[0];
7979
expect(stock[idField]).toBeDefined();

src/firestore/collection/collection.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,20 @@ export class AngularFirestoreCollection<T=DocumentData> {
103103

104104
/**
105105
* Listen to all documents in the collection and its possible query as an Observable.
106+
*
107+
* If the `idField` option is provided, document IDs are included and mapped to the
108+
* provided `idField` property name.
109+
* @param options
106110
*/
107-
valueChanges(idField?: string): Observable<T[]> {
111+
valueChanges(options: {idField?: string} = {}): Observable<T[]> {
108112
const fromCollectionRef$ = fromCollectionRef<T>(this.query);
109113
const scheduled$ = this.afs.scheduler.runOutsideAngular(fromCollectionRef$);
110114
return this.afs.scheduler.keepUnstableUntilFirst(scheduled$)
111115
.pipe(
112116
map(actions => actions.payload.docs.map(a => {
113117
return {
114118
...a.data() as Object,
115-
...(idField ? { [idField]: a.id } : null)
119+
...(options.idField ? { [options.idField]: a.id } : null)
116120
} as T;
117121
})
118122
)

0 commit comments

Comments
 (0)