From 0fe4c17bc1c058ce8209db77da601e6f6a37dd0c Mon Sep 17 00:00:00 2001 From: benjrome Date: Thu, 7 May 2020 18:58:07 +0100 Subject: [PATCH] allow use of valuechanges for collectiongroup --- .../collection-group/collection-group.ts | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/firestore/collection-group/collection-group.ts b/src/firestore/collection-group/collection-group.ts index b20942d14..6193b84ae 100644 --- a/src/firestore/collection-group/collection-group.ts +++ b/src/firestore/collection-group/collection-group.ts @@ -81,12 +81,27 @@ export class AngularFirestoreCollectionGroup { /** * Listen to all documents in the collection and its possible query as an Observable. + * + * If the `idField` option is provided, document IDs are included and mapped to the + * provided `idField` property name. + * @param options */ - valueChanges(): Observable { - const fromCollectionRefScheduled$ = fromCollectionRef(this.query, this.afs.schedulers.outsideAngular); - return fromCollectionRefScheduled$ + valueChanges(): Observable + valueChanges({}): Observable + valueChanges(options: {idField: K}): Observable<(T & { [T in K]: string })[]> + valueChanges(options: {idField?: K} = {}): Observable { + return fromCollectionRef(this.query, this.afs.schedulers.outsideAngular) .pipe( - map(actions => actions.payload.docs.map(a => a.data())), + map(actions => actions.payload.docs.map(a => { + if (options.idField) { + return { + ...a.data() as Object, + ...{ [options.idField]: a.id } + } as T & { [T in K]: string }; + } else { + return a.data() + } + })), this.afs.keepUnstableUntilFirst ); }