Skip to content

feat(firestore): Added option to include document IDs on valueChanges() #1976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/firestore/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ interface DocumentSnapshot {

There are multiple ways of streaming collection data from Firestore.

### `valueChanges()`
### `valueChanges(idField?: string)`

**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.
**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.

**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.

Expand Down
14 changes: 13 additions & 1 deletion src/firestore/collection/collection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FirebaseApp, AngularFireModule } from '@angular/fire';
import { AngularFirestore } from '../firestore';
import { AngularFirestoreModule } from '../firestore.module';
import { AngularFirestoreDocument } from '../document/document';
import { AngularFirestoreCollection } from './collection';
import { QueryFn } from '../interfaces';
import { Observable, BehaviorSubject, Subscription } from 'rxjs';
Expand Down Expand Up @@ -70,6 +69,19 @@ describe('AngularFirestoreCollection', () => {

});

it('should optionally map the doc ID to the emitted data object', async (done: any) => {
const ITEMS = 1;
const { ref, stocks, names } = await collectionHarness(afs, ITEMS);
const idField = 'myCustomID';
const sub = stocks.valueChanges(idField).subscribe(data => {
sub.unsubscribe();
const stock = data[0];
expect(stock[idField]).toBeDefined();
expect(stock).toEqual(jasmine.objectContaining(FAKE_STOCK_DATA));
deleteThemAll(names, ref).then(done).catch(fail);
})
});

it('should handle multiple subscriptions (hot)', async (done: any) => {
const ITEMS = 4;
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
Expand Down
10 changes: 8 additions & 2 deletions src/firestore/collection/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,18 @@ export class AngularFirestoreCollection<T=DocumentData> {
/**
* Listen to all documents in the collection and its possible query as an Observable.
*/
valueChanges(): Observable<T[]> {
valueChanges(idField?: string): Observable<T[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this for future proofing? Not a huge deal but it could help.

Suggested change
valueChanges(idField?: string): Observable<T[]> {
valueChanges({ idField: string }: ValueChangeOptions?): Observable<T[]> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that 👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second using an options object for future proofing. Great PR otherwise.

const fromCollectionRef$ = fromCollectionRef<T>(this.query);
const scheduled$ = this.afs.scheduler.runOutsideAngular(fromCollectionRef$);
return this.afs.scheduler.keepUnstableUntilFirst(scheduled$)
.pipe(
map(actions => actions.payload.docs.map(a => a.data()))
map(actions => actions.payload.docs.map(a => {
return {
...a.data() as Object,
...(idField ? { [idField]: a.id } : null)
} as T;
})
)
);
}

Expand Down