Advertisement
ROODAY

Uploading data to Firebase

Jul 25th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var firebase = require('firebase');
  2. var Converter = require("csvtojson").Converter;
  3. firebase.initializeApp({
  4.   serviceAccount: "./credentials.json",
  5.   databaseURL: "url went here"
  6. });
  7. var converter = new Converter({
  8.     constructResult:false,
  9.   workerNum:4
  10. });
  11. var db = firebase.database();
  12. var ref = db.ref("/");
  13.  
  14. var lastindex = 0;
  15. var count = 0;
  16. var section = 0;
  17. var sectionRef;
  18. converter.on("record_parsed",function(resultRow,rawRow,rowIndex){
  19.     if (rowIndex >= 0) {
  20.         sectionRef = ref.child("reports" + section);
  21.         var reportRef = sectionRef.child(resultRow.Report_ID);
  22.         reportRef.set(resultRow);
  23.         console.log("Report uploaded, count at " + count + ", section at " + section);
  24.         count += 1;
  25.         lastindex = rowIndex;
  26.         if (count >= 1000) {
  27.             count = 0;
  28.             section += 1;
  29.         }
  30.         if (section >= 100) {
  31.             console.log("last completed index: " + lastindex);
  32.             process.exit();
  33.         }
  34.     } else {
  35.         console.log("we out of indices");
  36.         process.exit();
  37.     }
  38.    
  39. });
  40. var readStream=require("fs").createReadStream("./vUPLOAD_MASTER.csv");
  41. readStream.pipe(converter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement