[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 ...
随机推荐
- Logstash读写性能调整优化
继续
- BZOJ2555: SubString(后缀自动机,LCT维护Parent树)
Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...
- 内核中的宏定义__init、__initdata和__exit、__exitdata
__init.__initdata和__exit.__exitdata的定义位于<kernel/include/linux/init.h> /* These are for everybo ...
- Shiro学习总结(3)——Apache Shiro身份认证
身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...
- 题目1205:N阶楼梯上楼问题(2008年华中科技大学计算机保研机试真题:递推求解)
题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2447 解决:927 题目描写叙述: N阶楼梯上楼问题:一次能够走两阶或一阶,问有多少种上楼方式. (要求 ...
- php的数据类型和变量的作用域
1)php支持例如以下所看到的的基本数据类型: Integer(整数).Float(浮点数).String(字符串).Boolean(布尔值).Array(数组).Object(对象),此外还有两个特 ...
- 1.15 Python基础知识 - 函数
函数是可重用的程序代码段. 一.函数的声明和调用 声明格式: def 函数名([形参列表]): 函数体 调用格式: 函数名([实参列表]) 函数名:是一种标识符,命名规则为全小写字母,可以使用下划线增 ...
- ubuntu下安装phpredis的模块扩展
1.前提是先安装好redis,然后再安装phpredis. .. 2.先下载phpredis-master.tar.gz安装包... 详细详情例如以下: <span style="co ...
- Eclipse导出Library
在工作中遇到开发SDK,记录下导出Library的过程. 1.导出 选中项目>属性>Android 配置Is library例如以下图: 选中项目>导出>JAR ...
- 【Java并发编程实战】-----“J.U.C”:CLH队列锁
在前面介绍的几篇博客中总是提到CLH队列,在AQS中CLH队列是维护一组线程的严格依照FIFO的队列.他可以确保无饥饿,严格的先来先服务的公平性.下图是CLH队列节点的示意图: 在CLH队列的节点QN ...