[AngularFire2] Build a Custom Node Backend Using Firebase Queue
In this lesson we are going to learn how to build a custom Node process for batch processing of Firebase data using the Firebase queue library.
From UI, we create a request to add 'queue/tasks' node in database which contains the data to be deleted by queue later.
Controller:
requestLessonDeletion() {
this.courseService.deleteLEssonById(
this.lesson.$key,
this.lesson.courseId
)
.then(() => alert("lesson delete successfully"))
.catch((err) => console.error(err));
}
Service:
this.rootDb = fb.database().ref()
...
deleteLEssonById(lessonId: string, courseId) {
return this.rootDb.child('queue/tasks')
.push({
lessonId,
courseId
})
}

Then we will build a node server to do the deletion:
package.json:
"batch-server": "./node_modules/.bin/ts-node ./batch-server.ts",
Install:
npm install --save-dev ts-promise firebase-queue
import {firebaseConfig} from "./src/environments/firebase.config";
import {initializeApp, auth,database} from 'firebase';
const Queue = require('firebase-queue');
import Promise from "ts-promise";
console.log('Running batch server ...');
initializeApp(firebaseConfig);
auth()
.signInWithEmailAndPassword('o@you.com', 'youdon'tknow')
.then(runConsumer)
.catch(onError);
function onError(err) {
console.error("Could not login", err);
process.exit();
}
function runConsumer() {
console.log("Running consumer ...");
const lessonsRef = database().ref("lessons");
const lessonsPerCourseRef = database().ref("lessonsPerCourse");
const queueRef = database().ref('queue');
const queue = new Queue(queueRef, function(data, progress, resolve, reject) {
console.log('received delete request ...',data);
const deleteLessonPromise = lessonsRef.child(data.lessonId).remove();
const deleteLessonPerCoursePromise =
lessonsPerCourseRef.child(`${data.courseId}/${data.lessonId}`).remove();
Promise.all([deleteLessonPromise, deleteLessonPerCoursePromise])
.then(
() => {
console.log("lesson deleted");
resolve();
}
)
.catch(() => {
console.log("lesson deletion in error");
reject();
});
});
}
Run 'npm run batch-server', then the data inside "lessons" & "lessonsPreCourse" & "queue/tasks" will all be deleted.
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"courses": {
".indexOn": ["url"]
},
"lessons": {
".indexOn": ["url"]
},
"queue": {
"tasks": {
".indexOn": ["_state"]
}
}
}
}
Need to add "queue" to the firebase ruels to get rid of error message.
[AngularFire2] Build a Custom Node Backend Using Firebase Queue的更多相关文章
- [Docker] Build a Simple Node.js Web Server with Docker
Learn how to build a simple Node.js web server with Docker. In this lesson, we'll create a Dockerfil ...
- 【Problem】前端项目运行:Module build failed:Error Node Sass does not yet support my current environmen
我在运行renren-fast-vue前端项目时,安装完依赖cnpm install 启动服务npm run dev 出现问题. Module build failed: Error: Node Sa ...
- [MDX] Build a Custom Provider Component for MDX Deck
MDX Deck is a great library for building slides using Markdown and JSX. Creating a custom Providerco ...
- [Node.js] Build microservices in Node.js with micro
micro is a small module that makes it easy to write high performance and asynchronous microservices ...
- How to build your custom release bazel version?
一般情况下用源代码编译,生成的都是开发版本,这种版本做版本号校验方面会有很多问题,所以需要编译自己的release版本. export USE_BAZEL_VERSION=1.2.1 # 选择使用版本 ...
- node c/c++扩展模块build失败.
"深入浅出nodejs 朴灵" 例子 c/c++扩展模块 http://diveintonode.org/ 在作者的帮助下,build成功. 下面贴出的hello.cc和bindi ...
- vue-cli的webpack模版项目配置解析-build/dev-server.js
我们在使用vue-cli搭建vuejs项目(Vuejs实例-01使用vue-cli脚手架搭建Vue.js项目)的时候,会自动生成一系列文件,其中就包含webpack配置文件.我们现在来看下,这些配置到 ...
- vue-cli脚手架build目录中的dev-server.js配置文件
本文系统讲解vue-cli脚手架build目录中的dev-server.js配置文件 这个配置文件是命令npm run dev 和 npm run start 的入口配置文件,主要用于开发环境 由于这 ...
- A chatroom for all! Part 1 - Introduction to Node.js(转发)
项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...
随机推荐
- Java总结之线程
[线程的基本概念] 线程是一个程序内部的顺序控制流. 线程和进程的差别: 每一个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销. 线程能够看成是轻量级的进程,同一类线程 ...
- Python-根据成绩分析是否继续深造
案例:该数据集的是一个关于每个学生成绩的数据集,接下来我们对该数据集进行分析,判断学生是否适合继续深造 数据集特征展示 GRE 成绩 (290 to 340) TOEFL 成绩(92 to 120) ...
- WPF 入门《数据绑定》
简单而言, 数据绑定是一种关系, 这种关系告诉WPF 从一个源目标对象中提取一些信息, 并且使用该信息设置为目标对象的属性.目标属性总是依赖项属性, 并且通常位于WPF元素中. 然而, 源对象可以是任 ...
- Apache httpd.conf 配置文件语法验证
Apache 的 httpd.conf文件改动之后,必须重新启动server才干生效. 有时server在提供服务的时候,直接更改配置,重新启动服务.会带来非常大的危急性. 假设能在改动配置之后,先验 ...
- Leetcode:populating_next_right_pointers_in_each_node题解
一. 题目 对于结构体:struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...
- Eclipse使用方法和技巧二十七:定义自己的高速联想词
某天在调试代码的时候.尽管是android的project还是习惯的输入syso.然后在ALT+/一下. 旁边的同事就问了一下,这个log打印输出的tag是什么. 接着又问了为什么syso可以智能联想 ...
- 关于laravel框架分页报错的问题
因为laravel框架有自己的分页封装,所以与其他框架相比laravel框架的分页的实现要方便的多 只要分别在php脚本与视图中使用 $data=DB::table('index_pic')-> ...
- PythonNET网络编程3
IO IO input output 在内存中存在数据交换的操作都可以认为是IO操作 和终端交互 : input print 和磁盘交互 : read write 和网络交互 : recv send ...
- Android RGB颜色对比表
一 RGB颜色对照表: 注:内容来自http://rgb.phpddt.com/ 里面有在线颜色摄取器 #FFFFFF #FFFFF0 #FFFFE0 #FFFF0 ...
- (笑话)切,我也是混血儿,我爸是A型血,我妈是B型血!
1.中午,在家里看电视,电视里正在说起食品安全问题.侄儿突然感叹道:“现在的食品真不让人放心啊!”嘿,没想到侄儿小小年纪竟有这般认识,我正要抓住机会教育他不要乱吃零食.这时侄儿幽怨的瞪着我说:“我昨晚 ...