[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. 切换数据库,如果数据库不存在也并不创建,直到插入数据或创建集合时数 ...
随机推荐
- zookeeper提供了什么
简单的说,zookeeper=文件系统+通知机制. 每个子目录项如 NameService 都被称作为 znode,和文件系统一样,我们能够自由的增加.删除znode,在一个znode下增加.删除子z ...
- win7-时间更新
今天发现电脑的时间不对,后来就自己摸索了时间的自动更新方法.自己记录下来,以方便以后忘了查询 点击电脑右下角的时间->选择更改日期和时间设置->选择internet->更改设置-&g ...
- LinearLayout -设置负值属性
1.我们调整两个控件的位置的时候,数值不仅仅是正数,负的也行,这时候只不过是跑到相对位置的另一边去了,例如 <ImageView android:id="@+id/image" ...
- resource-color 的引用
1.在xml文件中引用 android:textColor="@color/tv_top_title_color" 2.在代码中引用 1)在color.xml中定义 <?xm ...
- native.js是什么且如何使用
native.js是什么且如何使用 一.总结 一句话总结:Native.js技术,简称NJS,是一种将手机操作系统的原生对象转义,映射为JS对象,在JS里编写原生代码的技术.Native.js不是一个 ...
- 嵌入式 Linux应用程序如何读取(修改)芯片寄存器的值
这一问题来自项目中一个实际的需求:我需要在Linux启动之后,确认我指定的芯片寄存器是否与我在uboot的配置一致. 举个例子:寄存器地址:0x20000010负责对DDR2的时序配置,该寄存器是在u ...
- Windows/Linux下磁盘使用的图形化工具简介
Windows/Linux下磁盘使用的图形化工具简介 如何以图形界面查看磁盘及分区的大小及剩余容量呢?今天我为大家介绍两款Windows/Linux下磁盘使用的图形化工具分别是Spacesniff ...
- System and method for critical address space protection in a hypervisor environment
A system and method in one embodiment includes modules for detecting an access attempt to a critical ...
- Excel查询序列所相应的值-vLoopup函数,求比例分子改变但分母不变
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWV3ZWlvdXlhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- eclipse中导入zico Maven项目
zico源代码地址:https://github.com/jitlogic/zico 简单的说,git上同步的源代码需要先进行maven编译,然后导入eclipse. 如果没有配置好maven,请参考 ...