I'm currently building a project using firebase with the firestore database and Angular. Inside my firestore database i have a collection of tasks and a collection of owners. Each task has a reference (foreign key) to an owner like owner: -l3sdfkl3sdflksdf
. An owner can be linked to multiple tasks, where a task can have only one owner.
In one of my Angular components i like to show a list of all my tasks, including some owner information per task. I wrote a service method to subscribe to the collection of tasks and inside that subscription i wan't to subscribe to the owner, per task. Because my component also wants to be triggered when one of the owner properties (for an owner linked to one of the tasks) changes.
I currently thinking about something like this, however i wonder if this is smart, maintainable, how i should unsubscribe etc etc etc:
getTasks(): Observable<Task[]> {
return this.afs.collection<Task>('tasks').snapshotchanges().map((actions: DocumentChangeAction[]) => {
let tasks: Task[] = [];
actions.forEach((action: DocumentChangeAction) => {
let task: any = { id: action.payload.doc.id, ...action.payload.doc.data() };
// Per task, subscribe to the owner, how?
this.getOwner(task.id).subscribe((owner: Owner) => {
task.owner = owner;
return tasks;
});
tasks.push(task);
});
return tasks;
});
}
I don't like to do the retrieval of each owner in the component itself, it could be that in the future i have multiple components that need this list of tasks including the owner and i simply think it's not the responsibility of the component.
Any tips on how to solve stuff like this? Thanks!