Angular join query for firebase database

 

Angular join query for firebase database

 

ngOnInit() {
this.joined$ = this.af.collection<BlogPost>('blog-posts').valueChanges()
.pipe(
switchMap(blogPosts => {
const authorIds = uniq(blogPosts.map(bp => bp.authorId))
return combineLatest(
of(blogPosts),
combineLatest(
authorIds.map(authorId =>
this.af.collection<Author>('authors', ref => ref.where('id', '==', authorId)).valueChanges().pipe(
map(authors => authors[0])
)
)
)
)
}),
map(([blogPosts, authors]) => {
return blogPosts.map(blogPost => {
return {
...blogPost,
author: authors.find(a => a.id === blogPost.authorId)
}
})
})
)
}
}

Share this

Related Posts

Previous
Next Post »