zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    Win8系统 Python安装
    一些安卓开源框架整理
    Android 媒体键监听以及模拟媒体键盘的实现 demo
    android View 自动 GONE 问题
    Android 定时器TimerTask 简单使用
    关于Android studio 相对 eclipse 优点
    Java序列化与反序列化
    android shape的使用 边框
    Android Studio 修改 包名 package name
    Android WebView Long Press长按保存图片到手机
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6108124.html
Copyright © 2011-2022 走看看