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.

Github

[AngularFire2] Build a Custom Node Backend Using Firebase Queue的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. [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 ...

  4. [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 ...

  5. How to build your custom release bazel version?

    一般情况下用源代码编译,生成的都是开发版本,这种版本做版本号校验方面会有很多问题,所以需要编译自己的release版本. export USE_BAZEL_VERSION=1.2.1 # 选择使用版本 ...

  6. node c/c++扩展模块build失败.

    "深入浅出nodejs 朴灵" 例子 c/c++扩展模块 http://diveintonode.org/ 在作者的帮助下,build成功. 下面贴出的hello.cc和bindi ...

  7. vue-cli的webpack模版项目配置解析-build/dev-server.js

    我们在使用vue-cli搭建vuejs项目(Vuejs实例-01使用vue-cli脚手架搭建Vue.js项目)的时候,会自动生成一系列文件,其中就包含webpack配置文件.我们现在来看下,这些配置到 ...

  8. vue-cli脚手架build目录中的dev-server.js配置文件

    本文系统讲解vue-cli脚手架build目录中的dev-server.js配置文件 这个配置文件是命令npm run dev 和 npm run start 的入口配置文件,主要用于开发环境 由于这 ...

  9. A chatroom for all! Part 1 - Introduction to Node.js(转发)

    项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...

随机推荐

  1. Xcode 6 打包ipa文件

    随着Xcode6.1的普遍应用.随之而来的非常多问题就会出现.这里来说一下怎样在Xcode6.1上生成Ad-hoc ipa.首先是要在你的开发人员账号上生成一个.ipa的主要应用就是在你公布到AppS ...

  2. php7 兼容 MySQL 相关函数

    php7 兼容 MySQL 相关函数 PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql http://PHP.net/manual/zh/mysqlinfo ...

  3. js03 数组

    变量的自动转换=== 等同符:不会发生类型的自动转化! == 等值符:会发生类型自动转化.自动匹配!判断相等没有equals()方法,只有2个等号3个等号. <!DOCTYPE HTML PUB ...

  4. Python-根据成绩分析是否继续深造

    案例:该数据集的是一个关于每个学生成绩的数据集,接下来我们对该数据集进行分析,判断学生是否适合继续深造 数据集特征展示 GRE 成绩 (290 to 340) TOEFL 成绩(92 to 120) ...

  5. 1.3 Quick Start中 Step 3: Create a topic官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 3: Create a topic Step 3: 创建一个主题(topi ...

  6. BZOJ3262: 陌上花开(三维偏序,CDQ分治)

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美 ...

  7. Shiro学习总结(4)——Shrio登陆验证实例详细解读

    最终效果如下: 工程整体的目录如下: Java代码如下: 配置文件如下: 页面资源如下: 好了,下面来简单说下过程吧! 准备工作: 先建表: [sql] view plain copy drop ta ...

  8. PatentTips - Supporting address translation in a virtual machine environment

    BACKGROUND A conventional virtual-machine monitor (VMM) typically runs on a computer and presents to ...

  9. 手撕面试题ThreadLocal!!!

    说明 面试官:讲讲你对ThreadLocal的一些理解. 那么我们该怎么回答呢????你也可以思考下,下面看看零度的思考: ThreadLocal用在什么地方? ThreadLocal一些细节! Th ...

  10. js常用数据转换&判断

    数组转字符串 var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); //"0-1-2-3-4" 字符串转数组 ...