Skip to content

Commit

Permalink
feat(database): add option to include key on valueChanges() (#2126)
Browse files Browse the repository at this point in the history
Co-authored-by: James Daniels <jamesdaniels@google.com>
  • Loading branch information
sarunint and jamesdaniels authored Nov 11, 2020
1 parent 09ed22a commit 5cdb8ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/database/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export type FirebaseOperation = string | firebase.database.Reference | firebase.

export interface AngularFireList<T> {
query: DatabaseQuery;
valueChanges(events?: ChildEvent[]): Observable<T[]>;
valueChanges(events?: ChildEvent[], options?: {}): Observable<T[]>;
valueChanges<K extends string>(events?: ChildEvent[], options?: {idField: K}): Observable<(T & {[T in K]?: string})[]>;
snapshotChanges(events?: ChildEvent[]): Observable<SnapshotAction<T>[]>;
stateChanges(events?: ChildEvent[]): Observable<SnapshotAction<T>>;
auditTrail(events?: ChildEvent[]): Observable<SnapshotAction<T>[]>;
Expand Down
15 changes: 13 additions & 2 deletions src/database/list/create-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ export function createListReference<T= any>(query: DatabaseQuery, afDatabase: An
auditTrail(events?: ChildEvent[]) {
return auditTrail<T>(query, events, outsideAngularScheduler).pipe(afDatabase.keepUnstableUntilFirst);
},
valueChanges(events?: ChildEvent[]) {
valueChanges<K extends string>(events?: ChildEvent[], options?: {idField?: K}) {
const snapshotChanges$ = snapshotChanges<T>(query, events, outsideAngularScheduler);
return snapshotChanges$.pipe(
map(actions => actions.map(a => a.payload.val() as T)),
map(actions => actions.map(a => {
if (options && options.idField) {
return {
...a.payload.val() as T,
...{
[options.idField]: a.key
}
};
} else {
return a.payload.val() as T;
}
})),
afDatabase.keepUnstableUntilFirst
);
}
Expand Down

0 comments on commit 5cdb8ce

Please sign in to comment.