[AngularFire 2] Joins in Firebase

Lets see how to query Firebase.

First thing, when we do query, ‘index‘ will always help, for both SQL and NoSQL.

In Firebase, we can also set index on props, for example we want to set an ‘index‘ on ‘courses‘ --> ‘url‘ prop, we will use ‘url‘ to locate course object.

How to set up index?

In Firebase console --> database --> Rules:

We tell Firebase, for ‘courses‘ document, we want to set index on ‘url‘.

After setting up index, then how can we do query?

  findCourseByUrl(courseUrl): Observable<Course>{
    return this.angularFire.database.list(‘courses‘, {
      query: {
        orderByChild: ‘url‘,
        equalTo: courseUrl
      }
    })
    .map((courses) => courses[0]); // get courses document which url = courseUrl
  }

We need to pass ‘query‘ object for searching course. Notice we are using ‘orderByChild‘ and ‘equalTo‘.

Get courses‘ lessons:

  findAllCourseLessons(courseUrl){
    const course$ = this.findCourseByUrl(courseUrl);

    const lessonsPreCourse$ = course$
      .filter(course => !!course)
      .switchMap((course) => {
        console.log(course);
        return this.db.list(`lessonsPerCourse/${course.$key}`)
      });

    return lessonsPreCourse$
      .map((lessonKeys) => lessonKeys
          .map( (lessonKey) => {
             return this.db.object(`lessons/${lessonKey.$key}`)
          }))
      .flatMap((res) => {
        return Observable.combineLatest(res);
      });

  }

We have a document ‘lessonsPreCourse‘ to maintain the lessons which course includes:

This is normalize the dataset, to avoid nest array.

After getting ‘lessonsPreCourse‘, we are going to get all the lessons in ‘lessons‘ document.

Then in the UI, we can use ‘async‘ pipe to show the lessons:

<md-list>
  <md-list-item *ngFor="let lesson of lessons$ | async">
    <a *ngIf="lesson.hasVideoUrl" [href]="lesson.videoUrl">{{lesson.description}}</a>
    <span *ngIf="!lesson.hasVideoUrl">{{lesson.url}}</span>
  </md-list-item>
</md-list>
  ngOnInit() {
    if(this.route.snapshot.params[‘url‘]){
      const url = this.route.snapshot.params[‘url‘];
      this.lessons$ = this.courseService.findAllCourseLessons(url);
    }
  }

Github

时间: 2024-10-23 01:35:01

[AngularFire 2] Joins in Firebase的相关文章

[AngularFire 2] Object Observables - How to Read Objects from a Firebase Database?

In this lesson we are going to learn how to use AngularFire 2 to query objects, and read them from the Firebase realtime database. const course$: FirebaseObjectObservable<any> = af.database.object('courses/-KT0LsbuhHZGr5F4v7OV'); course$.subscribe((

[AngularFire 2 ] Hello World - How To Write your First Query using AngularFire 2 List Observables ?

In this lesson we are going to use AngularFire 2 for the first time. We are going to configure the AngularFire 2 module, inject the AngularFire service in our service layer and use it do our first Firebase query: we are going to retrieve a list of ob

[Firebase + PWA] Keynote: Progressive Web Apps on Firebase

Link : Video. 1. Firebase Auth: provides simple login with Github, Google, Facebook, Twittr. Link 2. Manifest: 3. Generate Mainfest and icons: Link 4. Read-only offline cache: 5. What Web can do Today?: Link Github: link https://firebase.google.com/d

SQL Server性能优化:subquery VS joins

在数据库中创建表如下,统计每个Task对应的TaskNote有多少条? 第一种解决方案: select t.TaskId, (select count(n.ID) from TaskNote n where n.TaskId = t.TaskId) 'Notes', --exec every row (select count(n.Comment) from TaskNote n where n.TaskId = t.TaskId) 'Notes1' , --exec every row (se

[AngularFire 2] Protect Write Access Using Security Rules

We cannot allow un-auth user to change the database data as they want, for Firebase, it is easy just need to overwirte the rules: { "rules": { ".read": "true", ".write": "auth != null", "courses"

Google Firebase Unity接入的坑

就说跑demo碰到的坑吧 https://firebase.google.com/docs/unity/setup 这是Firebase Unity的setup指南 大概写写步骤: 1. Firebase Console(https://console.firebase.google.com/)里设置好你的api key, 创建好app 2. 下载好GoogleService-Info.plist(IOS),google-services.json(Android),扔进Unity工程目录, 这

Firebase 远程配置 iOS 教程

原文:Firebase Remote Config Tutorial for iOS 作者:Todd Kerpelman 译者:kmyhy 记得发布 App 的时候吗?App 每个方面都已经做到最好了吗?你永远不需要碰别的代码了,因为在第一次提交时你就已经做到完美无缺了? 不,我不敢说. 事实是,作为一个功成名就的开发者,通常意味着对 App 没完没了地修改.有时候这种修改是为了增加功能或修复 Bug.但有时候,影响最大的更新无非是一行代码的事儿,比如调整某段文字,或者降低某个塔防游戏中能量单位

[AngularFire2] Auth with Firebase auth -- email

First, you need to enable the email auth in Firebase console. Then implement the auth service: login(email, password) { return this.fromFirebaseAuthPromise(this.auth$.login({ email, password },{ method: AuthMethods.Password, provider: AuthProviders.P

[React Native + Firebase] React Native: Real time database with Firebase -- setup &amp; CRUD

Install: npm i --save firebase // v3.2.1 Config Firebase: First we need to require Firebase: import firebase from 'firebase'; Then in the component constructor, we need to init Firebase: constructor(props){ super(props); // Initialize Firebase var co