[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(‘[email protected]‘, ‘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

时间: 2024-10-28 07:47:53

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

ue4 material custom node - global function and method

在ue4 material中定义全局函数 1. 背景 原文unreal-engine-4-custom-shaders-tutorial 中,提出了一种在material生成的hlsl文件中定义全局函数的方法,记录到这里以备复习. ue4 材质中的custom节点是用来使用hlsl代码的地方.一般来说都是直接编辑逻辑,最后添加return返回.类似这样: 1 float4 color = {1,0,0,1}; 2 return color; 使用ue4材质 menu window->hlsl s

Build a Custom Android Kernel Guide

1.package to install in ubuntu or Debian $ sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2 2.Prepare Kernel Source and excute the command $ make clean && make mrproper 3.Excute the command :the default config file loca

How to install Node.js on Linux

How to install Node.js on Linux Posted on November 13, 2015 by Dan Nanni Leave a comment Question: How can I install Node.js on [insert your Linux distro]? Node.js is a server-side software platform built on Google's V8 JavaScript engine. Node.js has

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

项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatroom for all! Part 1 - Introduction to Node.js Rami Sayar 4 Sep 2014 11:00 AM 7 This node.js tutorial series will help you build a node.js powered real-time

Vue2最低支持Node版本调查

背景 确定Vue2最低支持的Node版本,可以在CI环境中,确定Node的一些信息, 是否适合后端环境共享同一个Node版本呢. Vue2项目 https://github.com/vuejs/vue/blob/dev/package.json 调研了Vue 2.6.11版本的package.json 其中Vue2使用Node是在编译事态使用,即是 webpack 使用. 从这个文件中,我们发现 Vue2使用 webpack4.28.4 "webpack": "~4.28.4

I finally made sense of front end build tools. You can, too.

来源于:https://medium.freecodecamp.com/making-sense-of-front-end-build-tools-3a1b3a87043b#.nvnd2vsd8 Front end build tools can be confusing even to experienced developers like me. The solution is to understand how they work - and work together - on a co

[React Native + Firebase] React Native: Real time database with Firebase -- setup &amp; CRUD

Install: npm i --save firebase // v3.2.1 Config Firebase: First we need to require Firebase: import firebase from 'firebase'; Then in the component constructor, we need to init Firebase: constructor(props){ super(props); // Initialize Firebase var co

Node.js 添加 C/C++ Addon

一直想要开一个博客,总结记录一下自己学到的东西,今天终于动笔写了第一篇,希望能够坚持下去. 我的博客主要会分享一些自己最近学习的东西,主要是给自己看的,如果能帮到别人的话当然最好了. ----------------------我是华丽的分割线------------------------------- 实验室最近正在做一个基于Node.js的项目,之前对Front End的知识了解很少,所以从JavaScript一点点学起慢慢熟悉. 我的主要任务是把一个已经写好的C语言程序转化为Node.j

Node.js 和 C++ 之间的类型转换

我非常喜欢使用 Node.js开发,但是当涉及到计算密集型的场景时 Node.js 就不能够很好地胜任了.而在这样的情况下 C++ 是一个很好的选择,非常幸运 Node.js 官方提供了C/C++ Addons 的机制让我们能够使用 V8 API 把Node.js 和 C++ 结合起来. 虽然在 Node.js 官方网站有很多的关于怎么使用这些 API 的文档,但是在 JavaScript 和 C++ 之间传递数据是一件非常麻烦的事情,C++ 是强类型语言("1024" 是字符串类型而