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

 

Github

posted @   Zhentiw  阅读(472)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-11-28 [CSS3 Animation] TweenMax.staggerTo()
点击右上角即可分享
微信分享提示