DEV Community

Stokry
Stokry

Posted on

I built a 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";
Enter fullscreen mode Exit fullscreen mode

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`);
});
Enter fullscreen mode Exit fullscreen mode

I hope that this code will help somebody, have all a nice day.
Keep on coding.

Top comments (0)