[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 ...
随机推荐
- golang 数组
数组是Go语言编程中最常用的数据结构之一.顾名思义,数组就是指一系列同一类型数据的集合.数组中包含的每个数据被称为数组元素(element),一个数组包含的元素个数被称为数组的长度. 在Go语言中数组 ...
- [AngularFire] Firebase OAuth Login With Custom Firestore User Data
import { NgModule } from '@angular/core'; import { AuthService } from './auth.service'; import { Ang ...
- JS / CSS 实现的便签记录本
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- 洛谷 P1598 垂直柱状图
P1598 垂直柱状图 题目描述 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. ...
- [Node] Stateful Session Management for login, logout and signup
Stateful session management: Store session which associate with user, and store in the menory on ser ...
- 发送 email 过程
发送 email 过程 SMTP基本命令集: 命令 描述 ---------- HELO 向服务器标识用户身份发送者能欺骗,说谎,但一般情况下服务器都能检测到. MAIL 初始化邮件传输 mail f ...
- vue实现多语言国际化(vue-i18n),结合element ui、vue-router、echarts以及joint等。
老板说我们的项目要和国际接轨,于是乎,加上了多语言(vue-i18n).项目用到的UI框架是element ui ,后续echarts.joint等全都得加上多语言. 一.言归正传,i18n在vue项 ...
- Hadoop ecosystem 生态圈
Cascading: hadoop上面的workflow Sqoop(发音:skup)是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行 ...
- Qt5官方demo解析集28——Extending QML - Signal Support Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集27--Extendin ...