Create a Command Line Interface (CLI) Using Node JS 02

Video Tutorial

Overview

In the last tutorial, we have covered the basic setup of CLI using commander for argument processing and inquirer for user questioning

And we have implemented the add new contact to the database, So we are going to cover the rest of the functionalities for a complete command line app.

Finding Contacts

Now we need to let the user search for a specific contact on the database using the name in order to get the phone number (you can add more personal contact information if you want)

And for searching the database we need our Model and we either use the contact’s ID which isn’t good for this situation or we can make the contact’s name unique (which means do a checking if the name already exists upon every add new contact request), And this will allow us to ask the user for the contact’s name then search for it in the database.

We will register a new commander find command

program.command("find").description("Find a Contact").action(() => {
    //Ask for Name
    //let cn = findContact();
    inquirer.prompt([{
        type: "input",
        name: "name",
        message: "Enter Contact Name..."
    }]).then((answers) => {
        Contact.find({
            name: answers.name
        }, (err, cn) => {
            console.log(chalk.blue(cn[0].name + " number is : " + cn[0].number));
        });
    });
});

Now if you try to run the CLI and search for a Contact (for ex. Alex) using the Find Command

It searches and logs the contact’s phone number, using chalk this may give us a little bit more colorful output. AWESOME.

please note that mongoose gives you the found records in the database as an array so make sure to access the first index since the contact’s name is unique so there only the one we are looking for.

Deleting Contacts

Now for deleting an existing contact from the database we use the same procedure, but this time mongoose provides us with a ready-made method that you feed it in the Contact’s ID and it removes the record, and we can get the contact’s unique ID by looking for its name then grabbing the id so we can use it on the remove method.

//Delete from Database (Using Remove Command)
program.command("remove").description("Delete a Conatct").action(() => {
    //Find conatct by name
    inquirer.prompt([{
        type: "input",
        name: "name",
        message: "Enter Contact Name..."
    }]).then((answers) => {
        //We find the contact using name and grab it's _id 
        Contact.find({
            name: answers.name
        }, (err, cn) => {
            Contact.findByIdAndRemove(cn[0]._id, (err, dlt) => {
                if (err) console.error("Error Deleting Contact " + answers[0].name);
                console.log(chalk.red(answers[0].name + " Has been deleted from Database"));
            });
        });
    });

});

And that should do the job perfectly for us since we are going to search for the contact’s ID using the unique name and pass it into the remove by id method.

For updating contacts either name or phone number we use the same method for doing that in the next method.

What’s Next

Feel free to add more attributes to the database for more complicated tasks, you can implement any such functionality using inquirer, commander, and MongoDB as a database.

No Comments Yet