[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[]); // 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);
}
}
[AngularFire 2] Joins in Firebase的更多相关文章
- [Firebase] 1. AngularFire, $save, $add and $remove, Forge
Basic angularFire options: $save, $add and $remove. The way connect firebase: var app = angular.modu ...
- [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 t ...
- [AngularFire] Firebase OAuth Login With Custom Firestore User Data
import { NgModule } from '@angular/core'; import { AuthService } from './auth.service'; import { Ang ...
- [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor
The upload class will be used in the service layer. Notice it has a constructor for file attribute, ...
- [Firebase] 4. Firebase Object related Database
The idea: This post we are going to learn how to build a Firebase Forage with object related databas ...
- [Firebase] 3. Firebase Simple Login Form
Using $firebaseSimpleLogin service. Here we use three methods for login, logout, register and getCur ...
- [Angular] AuthService and AngularFire integration
Config AngularFire, we need database and auth module from firebase. import {NgModule} from '@angular ...
- [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 A ...
- SQL Tuning 基础概述07 - SQL Joins
N多年之前,刚刚接触SQL的时候,就被多表查询中的各种内连接,外连接,左外连接,右外连接等各式各样的连接弄的晕头转向. 更坑的是书上看到的各种表连接还有两种不同的写法, 比如对于表A,表B的查询 1, ...
随机推荐
- HttpWebRequest使用证书请求
HttpWebRequest使用证书请求 //是否使用证书 if (isUseCert) { stri ...
- Redis操作使用规范
Windows 64位操作系统 Redis 安装包(当前教程版本2.8.12) 百度经验:jingyan.baidu.com 方法/步骤 1 在D盘新建文件夹[redis],右键解压Redis Z ...
- 【例题 8-15 UVA - 12174】Shuffle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举第一段的范围[0..i] (0<=i<s) 然后看看[i+1..i+s-1],[i+s,i+s+s-1]..这些区间 ...
- Funui-overlay 如何添加theme 的 overlay
昨天更改theme主题的时候,发现所有仓库下的theme都是共用的.也就是说,如果你更改了52平台下的theme,那么你提交了代码以后,82下也会发生相应的更改.但是,昨天修改的theme属性,只在3 ...
- 单位阶跃函数(Heaviside/unit step function)—— 化简分段函数
注意,单位阶跃函数一种不连续函数. 1. 常见定义 最经典的定义来自于 Ramp function(斜坡函数,max{x,0})的微分形式: H(x)=ddxmax{x,0} 2. 化简分段函数 如对 ...
- $routeParams 实现路由指定参数
[摘要]后台管理系统权限控制到按钮级别,将每一个资源的key绑定在url中,渲染页面的时候去根据key来获取当前页面的按钮列表. router.js angular.module("app. ...
- listctrl调整表头高度
CListCtrl派生类下CMyListCtrl.h class CMyListCtrl :public CListCtrl { public: // 设置表头高度 void SetHeadHeigh ...
- 【Python】用Python的“结巴”模块进行分词
之前都是用计算所的分词工具进行分词,效果不错可是比較麻烦,近期開始用Python的"结巴"模块进行分词,感觉很方便.这里将我写的一些小程序分享给大家,希望对大家有所帮助. 以下这个 ...
- animation-list -帧动画
帧动画实现起来比较简单,今天接触到使用xml来创建帧动画,记录下来. 它说白了,其实就是动态的展示图片而已 1.在xml中定义帧动画,如下 <?xml version="1.0&quo ...
- ProgressBar-style属性分析
首先我们看下framework下关于进度条的style定义,如下 <style name="Widget.ProgressBar"> <item name=&qu ...