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

最新文章

  1. goroutine
  2. CoreDataStack
  3. linux下定时执行任务方法【转】
  4. django 注册、登录及第三方接口程序(4):扩展邮箱注册,登录,微博登录
  5. Android 模拟器检测
  6. OS X下安装Redis及配置开机启动
  7. Python学习笔记-Day2-Python基础之列表操作
  8. Application Designer Security
  9. Spring框架学习之第5节
  10. js+css实现带缓冲效果右键弹出菜单
  11. Hive[5] HiveQL 数据操作
  12. IIS应用地址池监控
  13. 阿里巴巴算法工程师四面(三轮技术+hr面)详细面经
  14. CDQ分治与整体二分学习笔记
  15. CCF关于NOIP竞赛程序提交的管理规则
  16. Codeforces Round #322 (Div. 2) E F
  17. PHP5和PHP7的安装、PHP和apache的整合!
  18. css优化建议
  19. Castle ActiveRecord学习(一)简介
  20. ubuntu 命令 dpkg -l

热门文章

  1. Xcode 6 打包ipa文件
  2. 任务调度(四)——ScheduledExecutorService替代Timer,实现多线程任务调度
  3. vmware-虚拟机播放器的下载、安装
  4. php课程 13-43 mysql的数据结构是什么
  5. Codeforces Round #193 (Div. 2) 部分题解
  6. Linux文本编辑器
  7. 洛谷 P3669 [USACO17OPEN]Paired Up 牛牛配对
  8. 103.tcp通信实现远程控制
  9. Python的数组合并
  10. 有关Canvas的一点小事—图像绘制