[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, ...
随机推荐
- POJ 2433 枚举
题意: 思路: 每回枚举去哪个山包 枚举的姿势很重要 //By SiriusRen #include <cstdio> #include <algorithm> using n ...
- Spark MLlib回归算法LinearRegression
算法说明 线性回归是利用称为线性回归方程的函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析方法,只有一个自变量的情况称为简单回归,大于一个自变量情况的叫做多元回归,在实际情况中大多数都是多 ...
- PHP 获取完整URL地址
/** * 获取当前完整URL * @return string */ function get_url() { $sys_protocal = isset($_SERVER['SERVER_PORT ...
- mysql存储emoji表情报错处理,qq互联mysql存储昵称中带表情的数据时报错。
前言 做qq互联登录时发现一个问题,如果qq昵称中有表情时存入mysql数据库会报错. java.sql.SQLException: Incorrect string value: "ð&q ...
- 【Codeforces Round #427 (Div. 2) A】Key races
[Link]:http://codeforces.com/contest/835/problem/A [Description] [Solution] 傻逼题. [NumberOf WA] [Revi ...
- Red Hat Enterprise Linux Server 5.5新增功能简介
一.概况 昨日对刚发布的redhat新产品RHEL5.5进行了小范围的测试,红帽企业版 Linux 5.5 发行本中的亮点包括针对 Intel Boxboro-EX 平台.AMD Magny-Cour ...
- 洛谷 P1724 东风早谷苗
洛谷 P1724 东风早谷苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走, ...
- 开源课程管理系统(CMS):Moodle
开源课程管理系统(CMS):Moodle 一.总结 1.php开发的cms,可借鉴参考用 二.Moodle(百度) Moodle(Modular Object-Oriented Dynamic Lea ...
- matlab 辅助函数 —— 文件下载与文件解压
0. 可读性的提升 为了提升代码的交互友好性,可在代码执行一些耗时操作时,显示地输出一些文本信息,以显示进度: fprintf('Downloading xxfilename...\n') urlwr ...
- 第二遍回顾--①前端flex布局
1.flex: 弯曲,收缩 2.概念 2条主轴,main axis,cross axis; 每个单元为flex item,主轴空间main size,交叉轴空间cross size; 3.容器 .co ...