Devanshi Sukhija
Devanshi Sukhija

Reputation: 167

How can I check if same data exists before pushing new values in Firebase Database?

I am trying to develop a backend for my college dissertation project wherein I am developing an Attendance system for my college. My system will be working on the data generated by a time-table web-application already in use at my college, by which my system will identify the lectures in the time-table and take attendance for it.

The time-table application has a mysql backend but my application has a firebase backend. So far, i am able to fetch data from mysql tables and push them into firebase database. But the problem is every-time I run the above code, it push the same already there data back again, which makes sense.

But what I want to achieve is to only add all the data once and then only add the new values or any updated values.

How should I tackle with this? I know, i have to add more checks before i push that data but I am really confused how should i tackle this hurdle.

// Connection to MySql sever.
mysql = require('mysql');
con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "mrbs"
});

// Connection to Firebase.
firebase = require('firebase');
firebase.initializeApp({
  serviceAccount: "./SICSR-d924e501f52d.json",
  databaseURL: "https://sicsr-d4771.firebaseio.com"
});


let fetchRecords = function(sql) {
  return new Promise (function(resolve, reject) {
    con.query(sql, function (err ,  result){
      if(err) reject(err);
      resolve(result);
    });
  });
};

fetchRecords("SELECT start_time, end_time, room_id, timestamp, Program, Batch, Course, Semester, Faculty, Division FROM mrbs_entry").then(function(fromResolve){
  Resultset = fromResolve;
  setRoomName(Resultset);
  //console.log(global);
}).catch(function(fromReject){
  console.log(fromReject);
});

//set room_id to the room_name retrive and send it forward to firebase.
function setRoomName(value){
  let objValue = value;

  for(let i = 0; i<objValue.length; i++) {

    con.query("Select room_name , id from mrbs_room where id = '" + objValue[i].room_id + "'", function (err, result2){
      if (err) throw err;
      let j =0;
      while(j<result2.length){
         roomName = result2[j].room_name;
        if(roomName == null && objValue[i] == null){
          // TODO add some code here.
        } else {
            objValue[i].room_id = roomName;
            saveRecord(objValue[i]);
        }
        j++;
      }
    });
  }
}

function saveRecord(data){
  let Resultset = [];
  Resultset = data;
  feedDatainFirebase(Resultset);
}

function feedDatainFirebase(value){
  var Resultset = value;
   let ref = firebase.database().ref("Lecture");
       ref.push({
         course_name: Resultset.Course,
         program_name : Resultset.Program,
         room_number : Resultset.room_id,
         start_time : Resultset.start_time,
         end_time : Resultset.end_time,
         teacher_name : Resultset.Faculty,
         timestamp : Resultset.timestamp

       });
}

Upvotes: 1

Views: 324

Answers (1)

mwarger
mwarger

Reputation: 857

If you're wanting to sync from the mysql database to firebase, you'll need to have a key of some type that you can reference in each to tie the records together. I would start by querying only the records you want to sync in mysql. Then, loop through them and for each one, you'll want to update the reference in firebase based on the key you have defined.

Upvotes: 1

Related Questions