0

I have this structure of my Firebase Database

enter image description here

Each library can have several albums. How can I display the albums for each library?

the albumsPerLib: first element is the library key, and underneath each library key, I store the albums keys, that belong to it.

But I'm not used to NoSQL and can't figure how to join the tables to display the albums for each library.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ron
  • 531
  • 8
  • 23
  • See http://stackoverflow.com/questions/30299972/joining-data-between-paths-based-on-id-using-angularfire – Frank van Puffelen Dec 06 '16 at 16:00
  • that is angularjs not 2, and angularfire1 not 2. which i haven't dealt with. i think but not sure that the old ones didn't used observable? – Ron Dec 06 '16 at 16:17
  • Ah, that is indeed different. I think you'll have to use a `map()` call, but have only used that for synchronous operations myself. – Frank van Puffelen Dec 06 '16 at 16:24
  • I have answered joining tables in firebase here: http://stackoverflow.com/questions/38789299/firebase-joining-tables/41090940#41090940 – codetinker Dec 12 '16 at 12:14

1 Answers1

1

so this works:

  getAlbumsThatBelongToLib(libKey:string):Observable<Album[]>{
//get the keys for the lib albums
const albumsPerLib$ = this.db.list(`albumsPerLib/${libKey}`);

//map the returned list of keys
//and get their details from the albums node
return albumsPerLib$
  .map((albumKeys) => albumKeys
      .map( (albumKey) => {
         return this.db.object(`albums/${albumKey.$key}`)
      }))
  .flatMap((res) => {
    return Observable.combineLatest(res);
  });
}
Ron
  • 531
  • 8
  • 23