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) => {

        .then(csvData => {
                { useNewUrlParser: true, useUnifiedTopology: true },
                (error, client) => {
                    if (error) return console.log(error);
                    let dbo = client.db('databsename');
                        .deleteMany({},(err, data) => {
                            if (err) return console.log('err', err);
                                .insertMany(csvData, (err1, res) => {
                                    if (err1) return console.log(err1);

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

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

You can also read this

How to create a unique list of elements using the Set object

Creating unique lists in javascript is a common task, this...

const list = [1, 2, 3, 5, 2, 5, 7];

Simple REST API with ExpressJS

Express is a minimal and flexible Node.js web application framework...

app.use(bodyParser.urlencoded({ extended: true }));