Create a Command Line Interface (CLI) Using Node JS 02
Skills:
API Design70%
Key Takeaways
Creates a Command Line Interface using Node JS
Full Transcript
avianna guys how old were doing welcome to a new video or the part 2 of making a command-line interface on now James so as you have probably watched the last video story was stopped on making or adding a contacts command from the commander so we've just submitted the other contacts and saved the data into the MongoDB database itself just works on over here just doing some test data and yeah everything is working fine well alongside its or the contacts phone number so now we need to implement or like resume our implementation of the old functionalities that are needed from our application so the rest of all the funky on it the functionality is actually gonna need to implement from this particular to be territorial and not to like finish this CLI is like delete from the database to list a specific contact after searching on it and obviously are updating a contact and finding a contact on the database so let's just start by adding in you our new command so let's say command command before searching and this is gonna be like helpful for searching for searching contacts from the database fetching it and just show us the data on for this particular content so to do that we're also gonna need to adjust a command using the program with a description and an action and using choir and all these kind of things so it just just jump right into this use program so program command and this command we're gonna just I don't know something you can use search or you can I'm going to use fine because a little bit more a little bit more precise for this job and you're gonna on the description find a contact and the option is going to be submitted once this command is going to be so better make sure you so takes a call back and here we are going to use inquiry for asking for the name so we're just gonna ask for name if this contact in order to fetch it from the database so us for name and make sure to grab this I don't know of the full source code from github because I'm gonna just put it on github so just feel free to go and get hub and just clone this source code not just minute play that play with it so you can briefly understand we're actually doing here and just take something from this tutorial and be some kind of beneficial thing from this but so anyway so as I've said we use the contact so as I've curated the contacts the contacts model so we use that in order to reach the database and fine as you know for MongoDB you need to use the schema all the model in order to search either to search or to see or to update or to do any up or operation on the database you need the model for this particular DL you want to find so the schema for this particular one so we're gonna just use fine you can use fine but ID if you have the ID but for this particular sake of this job we're not gonna use the ID we're just gonna provide it with an attribute which must be like unknowingly attribute like a unique attribute so every contact has a unique name so you can just do a checking over here so once an another user or the same user wants to add the contact twice so double contact or yeah you know twice of this the same DN a-- or the same name so you just must prevents him from that and just return an error error like you can't save this person twice or something like that so yeah you shouldn't save to two names on the database or the same names on the database the name must be unique one not to find so here we're passing the on object that has the attribute you want to search for so as i've said the name and we're gonna just give it the name from being quiet so for that sweetie using query first so using choir dot prompt function which was to prompt a message for them and this measure is going to be an array and in this array since we're gonna have only an object so i'm just kind of define it in line over here so just gonna have time for this is going to be an input of course because we're gonna input the name the contacts we want to search for and the name for that is I don't have contacts or just give it a name or just name it a name actually and the inputs and the message is going to be insured contact contact me okay and yeah here we go so you can have a some bond agent as I've said in here and to validate the name or something but yeah this is not really necessary for this sake of thing so here we you get the name and we just can use the damn function so then function is gonna take a callback as you have talked about and it's gonna just hold on our answers so the object is going to hold on the answers and here we are going to ship passing the answers into the content so contacts wine will find the name in here we get the name from the answers so answers don't name since we have only one so yeah this should do the job perfectly for us so after contacts find the contacts wine takes a call back as the second argument so this callback has an error as a first argument and a response with any and this area is an object spot array of objects to be more specific so this array of objects have all to the data that we have fetched from the database according to this arch abuse which is the name in this particular case we only have a unique name so we're just gonna get one particular elements on this array so you should access the first element from this array in order to grab your number phone number from for this particular name so we're as I've said we need to call back so first thing ever the second thing is for example call just see n shortcut for the contacts yeah I know there's a lot of imbricated or deprecated contour like callbacks this Idol or callbacks but this is shouldn't be frustrating if you are familiar with Java Script that much or if you have been working photographer JavaScript for a very long time so here finding and we're just gonna come so long so call so long the number and we talk don't know let's use it a blue Clara and contact number or let's give it or he's me so we're just gonna use answers top name plus I'm just gonna close this and contact me is or number yes just to be more precise and I'm just giving C and make sure this is this array to access the first index unless it's just gonna give you an in defined data open you so make sure to access the first index a subset and here you can get or access your network attributes or any other activities you have in your database here you can get it successfully without with no problems I should easily this should work for us now we'll try to clear after it squared or any but we're just gonna go note Minto genius in using the find method and here we should press ENTER we should get some promising input into the contact name for example to go ahead into the companies in order to see what we have for names and we have here Alex for example so we'll just take a look on on this Alex in its house number two three one four five six seven and ends with a zero at the end and it's Alex the name Alex so they're just input Pollock's press and Alex number is two three as I said and ends with a zero so you can match this it's the same thing over here so it's the as you can see it is working pretty much fun as simple as that you can just pitch from the database also for the delete operation I don't know delete from database so we can go for program again command let's for example this time delete and description so the leaves of contacts so if this particular one I'm gonna delete the contacts from via base so the beach contacts the auction and the auction takes a call back so yeah we already differently with that so we was in choir in choir will let him know just prone to us or give us the name but for the deletion we need the ID so over that we're just gonna go ahead and find or this particular function so the better thing to do is to wrap this into a function so we can use it twice when we want to find from from the one of the user need to find from the database find a number or phone number of a contacts using his or her name or the second one when we want to find his ID in order to delete it since the delete operation only takes the ID as the find parameter so you can't importance or you can't give it the name since the name can cannot be unique since the name isn't unique also MongoDB won't take that as a unique thing but the name for you since you're checking for this so just pretty much fine but it's for that Elise you need so you need to actually find that so we'll just go ahead and I don't know it just take this one and put it into a function so I'm gonna just name a function find on database or find contents to be more precise find contacts I don't know I can't just spell contact that correctly blog yeah here we do proms yeah with your problem we do everything in here we just gonna just say this is the number but instead of saying that this is going to be a number it's is there gonna be a function so we are going to return so we're just gonna return the C N and the C and the first index so we're just gonna return the first and that's not the whole array for the first index which holds our name and number and ID of course so this is an object holds an ID a name and a number so we're just gonna use this ID in order to access that so let me just copy all of that not all about the cost of the log so by coming over here we can call the function which is find content and point contact tix no parameter pretty much but it's just gonna return less contact or calm or CN okay C it will do the job for us perfectly and C an I'm just gonna just call so long to see n since doing the job I'm just gonna say C naught 0 now call we have already returned the zero index of this array and I'm just gonna say not answers name but Francis and Susan isn't defined anymore so we can use the CN and the number from that particular one this should work without lot problems in which we can use the point concept now find first or let's ask him for this or the fine contacts would do the job for us so we can find okay so find contacts by name and we can use this function that we have already defined so five contacts will use inquiry to ask our user for engineering name to find a contact so as you can see enter a name so then it's gonna return us the found contacts on the database that we can use the idea not to delete it so here we should say let see an for our contact or for our found contacts and graft or fixed contacts from the database then we can just delete so the leads and for the lead team we use our model and we use find my ID and updates or deletes pretty much so you can use the updates but we're not going we are actually not going to cover it on this tutorial because it's pretty much the same thing all they do we just change for bonfire or find by ID and remove your changes by find by ID and updates and that sets your passing the same parameters you do same thing so it just kind of waste of time for this particular tutorial you can do it as an exercise for you just if you can like yourself understand a bit of things over yourself there if you can just say it takes like the ID for any and its callback with honorees response or the deleted objects on the database and in error if there is any so here we give it the first the first actually the idea of gaps and this isn't the end of the ID so if you can go to the campus as you can see the ID has an underscore and it's already predefined for us so underscore ID so we should access this attribute into the cons objects here and the second one as I've said it's in herre and the other one is our deleted objects so we're just gonna call it DLC or for the latest stands for delete objects it's just optional thing you can require or you can import it or now so it's just and here we can use console.log any huge hole dance I don't know red let's use the red so the red function I'm just gonna give it the contact name so the CM dot name has been deleted from database okay and here we should can cancel me this which he loved each other and we aren't guchigi up so hopefully here we this should do the job for us to find and remove and find the content now they were using the delete or we came in very saying it is removed so this will do the job for us for us and it's clear just one note again and we will use remove make sure just save this and just solve this and then make sure to use the remove you can pass in a custom file or you can pass in actually come on already this is big sitting see it's exiting I don't know one so I believe since you have just change this function but just take this function all of it back oh no I think it's not working properly so it just gonna take you and not return anything in here just go ahead and take all of that under here and the fine method should work now with with no problems and just comment this one I think that we are returning something in here so the return will interrupt the inquiry process or the prompt process so it won't ask you anymore something just gonna interrupt the process and it just exists no matter what so we will try to use it that way or the custom way or the default way and therefore the more also gonna just use it that way not passing this actually Bloods let's see we have required okay so just take all that and put it up here and enter here instead of just constant along we're just gonna take our contacts delete function take it all the way down let me just come to this one and this should do the job for us so here the leads and we have the C n so the CN is an array if you cannot remember so we should access the first the first index so we can access or get or grab the first objects from this array and also for the fine method is should do the job the same thing so we should just go ahead and grab the first one the zero index since this array is going to only hold one index which is our desired or our search contacts from the database so but try this again remove I don't know this should work for now hopefully so let's try to answer your Alex or delete Alex and Alex is already in the database over here so entering Alex Alex has been deleted from the database as you can see we get it as a red and shiny and chalk help us a lot for achieving this really nice cool color thing and if you can just go back try to refresh and we should see Alex goes from there but it doesn't go or it does well I don't know something is wrong something isn't working right so a check for error or I know what's going on so here we are not including this pretty much guys sorry for this so let's Gina since we weren't actually including or exceeds the first index from this database that's why or that's was the problem so we've changed everything only that let you drop this clear and stop this or restore it from the beginning we should get the prompts we enter Alex we get Alex leave from the other base again but we are not sure so just go ahead and boom yeah awesome so alex has gone from the database and yeah we are good to go so all this has been successfully deleted from the database or removed from the database with no problems just by finding and just passing in the ID and yeah we are ready to go so they this Louis is actually guys that was very very basic thing and just very simple I was showing you how you can use commands enquirer a proper or like commander and inquire a power of power with MongoDB in order to manage your databases and or using a command line application that was very very nice guys so I think us watching I really do hope you enjoy you can take just take and implements the update function since it's pretty much the same thing for the find by ID and update you'll find my ID and remove the same procedure the same functions or the same methods calls and everything so just take a non-sex Nick zips eyes and try to implement it by yourself in order to like I don't know have a little bit of challenge you know something less something else for watching again I will catch you in the next video tutorial
Original Description
Tutorial:
http://go.ipenywis.com/ipenyc597
Code on Github:
https://goo.gl/ZLLr2V
--- Official Links----
Official Website: http://ipenywis.com
Official Facebook: http://go.ipenywis.com/ipeny4907
Official Twitter: http://go.ipenywis.com/islem9a03
Official GitHub Repo: http://go.ipenywis.com/ipeny5c0e
---Support US---
By Becoming a Patreon: http://go.ipenywis.com/ipeny5024
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CoderOne · CoderOne · 32 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
▶
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Laravel CMS | N-01 | Getting Started and Environment Set Up
CoderOne
Laravel CMS | N-02 | Routes, Controllers and Views
CoderOne
Laravel CMS | N-03 | Dealing With Assets and Page Layouts
CoderOne
Laravel CMS | N-04 | Migration, Models and Adding Categories
CoderOne
Laravel CMS | N-05 | Showing Added Categories, and Session Messages
CoderOne
Laravel CMS | N-06 | Edit\Remove Categories using AJAX and Popups
CoderOne
Laravel CMS | N-07 | Add New Post
CoderOne
Laravel CMS | N-08 | Creating a Basic Laravel Blog
CoderOne
HTML5 Canvas For Beginners | Getting Started | #01 with Webpack
CoderOne
HTML5 Canvas For Beginners | Drawing Basic Shapes | 02
CoderOne
HTML5 Canvas | Drawing Text With Circles | Part 01
CoderOne
HTML5 Canvas | Text Animation and Circle Collision | Part 02
CoderOne
Complete Guide To Web Development In 2018
CoderOne
AJAX & JSON Explained | Examples
CoderOne
Visual Studio Code Web Development Setup and Extensions
CoderOne
Electron Build Desktop Apps HTML/CSS/JS - Getting Started 01
CoderOne
Electron Build Desktop Apps HTML/CSS/JS | Creating Windows and Processes 02
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Dialog, Sessions and Cookies 03
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Custom App Menu & Context Menu 04
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Package, Build and Deploy your App 05
CoderOne
Node.js Brief Beginners Introduction #01
CoderOne
Node.js How Modules Works & NPM #02
CoderOne
Node.js Working With Events and Event Emitter #03
CoderOne
Node.js File System (Read and Write) FS 04
CoderOne
Node.js Create a Basic Server Using Express 05
CoderOne
Json Web Token Authentication JWT Explained Securing API
CoderOne
CSS Flex Box Design | Practical Examples |
CoderOne
API How It Works With Practical Examples
CoderOne
MongoDB Getting Started 01
CoderOne
MongoDB Working with APIs and Practical Examples
CoderOne
Create a Command Line Interface (CLI) Using Node JS #01
CoderOne
Create a Command Line Interface (CLI) Using Node JS 02
CoderOne
Build React Apps | Introduction And Getting Started #01
CoderOne
Build React Apps | Working With Components And Multiple Rendering #02
CoderOne
Build React Apps | App State Management | Practical Examples #03
CoderOne
Build React Apps | Getting Started With Flux (Intro) #04
CoderOne
Build React Apps | Create a Clock Timer Using React 05
CoderOne
Build React Apps | Create a Clock Timer Using React 05 | PART2|
CoderOne
CSS Preprocessors Getting Started | SASS and LESS | Practical Examples
CoderOne
Regex Introduction | Learn Regular Expressions 01
CoderOne
PHP VS Node.js Which is Best For Web Development
CoderOne
Drag and Drop Using Native Javascript 01
CoderOne
Drag And Drop Using Native Javascript 02
CoderOne
Master Git (Version Control) in One Video From Scratch
CoderOne
Let's Learn The New Javascript ES6 Class Syntax
CoderOne
Let's Create A BlockChain On Node.js
CoderOne
Best Online Code Editors For Web Developers
CoderOne
Let's Create a Modern Login Form on React #01
CoderOne
Let's Create a Modern Login Form on React #02
CoderOne
Laravel CMS | N-09 | Admin Registration
CoderOne
Laravel CMS | N-10 | Login and Authentication
CoderOne
Let's Create a Twitter Bot (Listen and Retweet)
CoderOne
Rapid Webpage Creation With Emmet (HTML & CSS)
CoderOne
Create Popups and Modals Using Native Javascript, HTML and CSS
CoderOne
Promises and Callbacks on Javascript With Practical Examples
CoderOne
Create Collapses using Native Javascript, HTML and CSS
CoderOne
Let's Learn Typescript (Javascript Devs) | Getting Started 01
CoderOne
Let's Learn Typescript | Interfaces and Generic Types (The Typing System) 02
CoderOne
Let's Learn Typescript | Work With Classes, Modules and Enums 03
CoderOne
Let's Learn Typescript | React and Webpack With TS 04
CoderOne
More on: API Design
View skill →Related Reads
📰
📰
📰
📰
Decide targets workplaces with enterprise AI rollout through CafeOne
TechCabal
Stop Ruining Your Daytime Shots: Why AI is Replacing Traditional Vintage Camera Apps
Medium · AI
The AI Tool I Built for Myself Ended Up Becoming the Product Clients Asked About the Most
Medium · Programming
Can AI Help Plan Your Next Holiday?
Medium · AI
🎓
Tutor Explanation
DeepCamp AI