Simple Nodejs watcher

I built a simple Nodejs watcher that is syncing changes made in your CSV file to MongoDB. So let’s say you have some service that exports periodically CSV file which you want later to sync with the database, in this case, MongoDB.

My approach was to have a script that will run (continuously) and whenever you made some changes in the CSV file it should update the database.
For my scenario, I have one collection in the database called properties.

const dataCsv = './sample_data.csv';
const mongodb = require("mongodb").MongoClient;
const csvtojson = require("csvtojson");
const fs = require('fs');
let url = "mongodb://yoururl";

As you can see I am using MongoClient, CSVTOJSON and FS.
Below is the rest of the code, as you can see it is very simple, connect to MongoDB and watch changes when they appear. Also, I put console.log to see what is happening.

fs.watchFile(dataCsv, (curr, prev) => {

console.log('changes')
    csvtojson()
        .fromFile(dataCsv)
        .then(csvData => {
            mongodb.connect(
                url,
                { useNewUrlParser: true, useUnifiedTopology: true },
                (error, client) => {
                    if (error) return console.log(error);
                    console.log('connected')
                    let dbo = client.db('databsename');
                    dbo.collection('properties')
                        .deleteMany({},(err, data) => {
                            if (err) return console.log('err', err);
                            dbo.collection('properties')
                                .insertMany(csvData, (err1, res) => {
                                    if (err1) return console.log(err1);

                                    console.log(`Inserted: ${res.insertedCount} rows`);
                                    client.close();
                                })
                        })
                }
            );
        });

    console.log(`${dataCsv} file Changed`);
});

You can also read this

Croatia recession prediction

This LSTM recurrent neural network learns to predict Croatia economic...

pred.train_LSTM()
pred.evaluate_LSTM()
pred.display_results(

Weather app with Vuejs

In this app I’m going to build a weather app...

var vm = new Vue({
  // options
})