[AngularFire2] Update multi collections at the same time with FirebaseRef
At some point, you might need to udpate multi collections and those collections should all updated successfully, otherwise we don't update anything. You can use 'FirebaseRef' to get the root node (the parent all for the collections).
constructor(private rt: RealtimeService, @Inject(FirebaseRef) fb) {
this.rootDb = fb.database().ref(); // To get the root firebase ref
}
You can inject 'FirebaseRef' by using @Inject.
For example we want to update 'lessons' and 'lessonsPreCourse' at the same time, make sure if both sucess, both get udpated, one has error then abort the transaction:

To add value into lessonsPerCourse key, we need to grap the new Lesson key first.

To do that, we can generate a new lesson key by doing:
const newLessonKey = this.rootDb.child('lessons').push().key;
This won't actually insert a row into the 'lessons' table, it just create a new key for lessons collection for us to use locally.
Once we got the new lesson key, then we can update the table by calling 'update()' on 'this.rootDb':
this.rootDb.update(dataToSave)
.then( (val) => { }, (err) => { });
The full code for create new lesson:
createNewLesson(courseId: string, lesson: Lesson): Observable<any>{
const lessonToSave = Object.assign({}, lesson, {courseId});
// Generate a new key under 'lessons' collection, db won't change currently
const newLessonKey = this.rootDb.child('lessons').push().key;
const dataToSave = {};
dataToSave[`lessons/${newLessonKey}`] = lessonToSave;
dataToSave[`lessonsPerCourse/${courseId}/${newLessonKey}`] = true;
const subject = new Subject();
this.rootDb.update(dataToSave)
.then( (val) => {
subject.next(val);
subject.complete();
}, (err) => {
subject.error(err);
subject.complete();
});
return subject.asObservable();
}
Notice that, we also use Subject(), because the function expect to return an Observable. So we call:
return subject.asObservable();
Also we call complete() method everytime, this is important to complete a subject.
subject.complete();
To mention that, subject is not the only way to can convert Promise to Observable, we can also do:
return Observable.fromPromise(this.rootDb.update(dataToSave));
[AngularFire2] Update multi collections at the same time with FirebaseRef的更多相关文章
- 【网络爬虫入门05】分布式文件存储数据库MongoDB的基本操作与爬虫应用
[网络爬虫入门05]分布式文件存储数据库MongoDB的基本操作与爬虫应用 广东职业技术学院 欧浩源 1.引言 网络爬虫往往需要将大量的数据存储到数据库中,常用的有MySQL.MongoDB和Red ...
- mongoDB的安装及基本使用
1.mongoDB简介 1.1 NoSQL数据库 数据库:进行高效的.有规则的进行数据持久化存储的软件 NoSQL数据库:Not only sql,指代非关系型数据库 优点:高可扩展性.分布式计算.低 ...
- MongoDB基础一篇就够了
MongoDB linux安装MongoDB Windows安装MongoDB 查看当前数据库名称 db 查看所有数据库名称 列出所有在物理上存在的数据库 show dbs 切换数据库 如果数据库不存 ...
- mongodb细讲
一. 关系型数据库(sql) 1.建表 二.非关系型数据库(nosql 98提出的概念) 1.不用建库建表数据直接存入就可 优缺点: 关系型:节约资源(学生姓名和课程名不重复出现),开发不方便(需先 ...
- mongo 数据库
一.管理mongo 配置文件在/etc/mongod.conf 默认端口27017 启动 sudo service mongod start 停止 ...
- python数据库进阶
第1节 MySQL基础 一,说明 1,认识MySQL与创建用户 MySQL是最流行的关系型数据库管理系统之一,由瑞典MySQL AB公司开发,目前属于Oracle公司.MySQL是一种关联数据管理系统 ...
- [转] mongoDB与mongoose
mongoDB简介 mongoDB与一些关系型数据库相比,它更显得轻巧.灵活,非常适合在数据规模很大.事务性不强的场合下使用.同时它也是一个对象数据库,没有表.行等概念,也没有固定的模式和结构,所有的 ...
- MongDB-基础
首先吐槽一下,MongDB用到了JS的引擎,只要涉及到了JS,语法就变得又臭又长,真是无语 还有,MongDB的安装真是麻烦,我用的是win10环境,怎么装都报服务错误,redis一装就可以用,希望m ...
- MangoDB学习笔记
01. 数据库操作 1. 查看当前数据库名称 db 2. 查看所有数据库名称,列出所有在物理上存在的数据库 show dbs; 3. 切换数据库,如果数据库不存在也并不创建,直到插入数据或创建集合时数 ...
随机推荐
- Python的正则表达概述
本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...
- python stomp activemq客户端
#coding=utf-8import timeimport sysimport stomp class MyListener(object): def on_error(self, headers, ...
- Problem C: Celebrity Split
题目描写叙述 Problem C: Celebrity Split Jack and Jill have decided to separate and divide their property e ...
- Linux 获取上个月的第一秒和上个月的最后一秒
因为写脚本需求须要获得上个月的第一秒和上个月的最后一秒,查阅了相关资料,并通过自己实践.找到了以下这样的方法能满足要求.在此备注,若有其它好的方法.请留言.本人将不胜感激. 获取上个月的第一秒: da ...
- Codeforces Round #316 (Div. 2) A B C
A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 24.Node.js Stream(流)
转自:http://www.runoob.com/nodejs/nodejs-stream.html Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...
- 33.promise future多线程通信
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <thread> #include <futur ...
- Tuple<int, int> Dictionary<string, object>妙用
Tuple<int, int> Dictionary<string, object>妙用
- Vue Invalid handler for event "": got undefined
原因:绑定的方法不是放在methods:{}里.比如我把绑定的函数写在了computed:{}里就会报这个错.
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...