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

Svelte is really cool

Svelte is a radical new approach to building user interfaces....

let count = 0;
   $: if (count >= 10) {
       alert(`count is dangerously high!`);
        count = 9;
}

The :valid CSS pseudo-class

The :valid CSS pseudo-class represents any  <input> or other <form> element whose...

input:valid {
  background-color: powderblue;
}