[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. 切换数据库,如果数据库不存在也并不创建,直到插入数据或创建集合时数 ...
随机推荐
- 【习题 8-1 UVA - 1149】Bin Packing
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个背包只能装两个东西. 而且每个东西都要被装进去. 那么我们随意考虑某个物品.(不必要求顺序 这个物品肯定要放进某个背包里面的. ...
- [Docker 官方文档] 理解 Docker
http://segmentfault.com/a/1190000002609286 什么是 Docker? Docker 是一个用于开发.交付和执行应用的开放平台,Docker 设计用来更快的交付你 ...
- Makefile 文件格式
Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o code.o gcc -o test prog.o code.o 当中 ...
- js29--装饰着模式
//装饰者模式:就是在保证不改变原有对象的基础上,去扩展一些想要的方法或去求 var CarInterface = new BH.Interface('CarInterface' , ['getPri ...
- menu-代码添加以及add方法参数意义
今天需要给一个menu动态添加一个item,先把方法记录如下 @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(Me ...
- 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- js-轮播图的总结
/*两种播放行为:(一种自动播放,一种控制播放),一个定时器控制. *一个定时器控制两种播放状态. * 布局说明:装图片的盒子足够宽,让图片左浮,排成一排,最后一张重新放置第一张. * 定时器里执行自 ...
- ubuntu-安装中文拼音输入法
一下内容转载自http://blog.chinaunix.net/uid-24410388-id-3501873.html 自己验证了可用.转载了,已留做日后使用 步骤: step1:安装ibus所需 ...
- 90.#define高级用法
define把参数变成字符串 #define f(x) printf("%s",#x); define连接两个字符串 #define a(x) a##x define把参数变成字符 ...
- 开发板 Linux驱动视频 驱动是什么
内存管理单元很重要. linux把设备看成文件,(open,read,write,ioctrl,close)主要写这几个函数. 哈弗结构,取指令和取数据同时进行. arm处理器体系架构以及发展方向 单 ...