Check out a free preview of the full Introduction to Node.js, v3 course

The "Using the CRUD Methods" Lesson is part of the full, Introduction to Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott updates the commands to utilize the previously created CRUD methods, allowing for better functionality and interaction with the notes in the database.

Preview
Close

Transcript from the "Using the CRUD Methods" Lesson

[00:00:00]
>> We are almost done with actually doing something and making it look cool. The last thing we got to do is update our commands. Our commands still don't do anything, so we got to go put all these things back in our commands. And then we can test these things and see what we broke because it never works on the first time.

[00:00:17]
We know that no one no one's safe from that, so [LAUGH] let's do that. Let me pull up the page for that. So updating the commands. So and imagine all of this in one file, right? Like before we get to it, imagine, all this stuff here, plus all of this stuff here Plus all this stuff here in one file, would be terrible, right?

[00:00:45]
This is where modules come in, this is why it's great, and you can see how I abstracted things by logic. Database stuff, command stuff, note stuff, and it all kinda just comes back into the command stuff, which kinda comes into the index stuff. So, It all flows together it's a very natural thing it's not it doesn't require a lot of planning.

[00:01:05]
This is eventually you just get used to just like yeah, this is different. We open up another file, make a new module, this is just different stuff, so I guess unless you're like writing Vue, and then everything's just on one file. So [LAUGH] that's just how I like to Vue so I'm not hating on it, but that's just how it is.

[00:01:24]
Okay, anyway, let's do it. First thing we need to do is actually what we'll get to this when we get to this but we let's start with the was this one the new note? Yeah, let's see the new note one. So, the new note one is pretty simple.

[00:01:41]
So I'm just going to type in new, Note, I'm gonna hit enter, it's going to auto-import it for me like that. If not, you'll have to write it yourself from the slash notes. We got our note here, so I'm gonna say note. But if we look at the arguments for new note, it takes in note and tags.

[00:02:00]
We do have a thing going on here, let me get rid of that file. The tags is a string. But if we go look at new note, it's expecting. I mean, I guess it doesn't really care what this is, but this should be an array should be an array of tags.

[00:02:15]
So I kind of need to convert them to an array of tags, I'm sorry, an array of strings. So I'll say tags equals argv tags, it's optional, so maybe you didn't pass it in. So if you did, I'm gonna say argv dot tags dot split. I'm gonna say all my tags are split off of comma.

[00:02:41]
And if you didn't do that, then I'm just gonna give you given an empty array. You can make your tag split on whatever you want, space, colon, whatever you want, I'm going to make this split on a comma. So that means you can pass me a string of comma-separated words, and each one is going to be a tag.

[00:02:57]
That way you can have tags with spaces in them. Okay, so we got our tags. Now we need to create our note. So I can say note equals. And this is gonna be async, so I'm gonna put async here, Await New note takes in a argv dot. Note this thing right here, argv dot note, and it takes in our tags, right?

[00:03:38]
And then depending on how we wanna return this Like how we want to show it on the screen, we could log it. What did I do? Yeah, it looks like I just logged it. So yeah, we could log it, we could do whatever we want. So I'll just log it just to be consistent.

[00:03:51]
New note, and then I'll say note like that. We'll just do that All right, so let's just do the ID. Let's just do the whole note, let's not be picky. Okay, so let's give it a try. So now I'm gonna go into my terminal, I'm gonna say, notes, new, clean my room, tags, work, serious, inner.

[00:04:22]
We get a error because it can't find notes import it from command.js. So let's see. Yeah, I need a dot js here. There we go. My auto import, my auto import doesn't know you got to do.js. It's like why do we got to do that? Okay, then we get another error from dB, I'm guessing the same thing there's no dot js on notes.

[00:04:51]
There we go. All right, that JS, when you use type module, cool and then boom once you fix those errors, we get a new note. And you can see right here our tags are an array of strings, our ID is just number and then here's our content. So that's really cool, but the even cooler thing is if we go look in DB dot JSON, there it is.

[00:05:14]
We have our notes in this array it got inserted into the file. So don't touch this file, this is not a file for you to go messing around with you'll mess up the database. This is the equivalent of going to your production database and deleting stuff. You'll get fired, so, [LAUGH] Don't do that.

[00:05:35]
You might even get sued. So yeah, don't ever touch a database just let the code do its job. Okay, so, and you can see it's put in here, formatted. And that's because we did that JSON.tringify, we did this right? We did this which like formats it, it makes it look pretty otherwise all just be like on one flat line it'll be terrible.

[00:05:57]
Okay, so we have that, the next command is all. So just get all the notes and we have a function for that, right? So I think it's get all notes. There we go. So let's do that so we can say notes equal await get all notes. There we go, we got all notes.

[00:06:20]
I just don't wanna log them out. I wanna show them in a certain way. I have this utility function that I created called listallnotes, and all it really does is just log each thing on a separate line. I made it a utility function because we're gonna use it more than once.

[00:06:35]
If I do anything more than once, I'm gonna make a function out of it. So I'm just gonna just make that function again. So I'm gonna go up here and honestly, I would make another file here. I'd make another file here and call it utils and I'll put that in there because this code has nothing to do with commands and it should be in its own file.

[00:06:52]
But I'm just gonna put it here because, I'm that lazy developer. So lists, notes, takes an array of notes and it's just gonna log some stuff. So we can just say console log, what do I put here? I mean, there's really no right way for this. So let's just say notes dot for each, note that you have take a note and then we'll just say console.log.

[00:07:21]
ID, note.id and this is actually where I'm gonna destructure because I don't wanna do note dot everywhere I go that's just my fingers already hurting from doing it. So I'm gonna destructure this I'm gonna get the id I'm gonna get the content, and I'm going to get the, what does the other one?

[00:07:46]
Id tag there we go like there's something else, okay? So now I'm gonna go here to say id and then I can say tags, and then I can go on here and I can say content, And then because I don't want my notes touching each other, I want some padding.

[00:08:12]
So I'm gonna add a new line character, which is basically just backslash and that's a newline character. So after each note add a new line. All right, so now we got our list notes function. I can go down here to my notes and I can just say, list these notes, please, like that.

[00:08:38]
So let's give this a try. So now I can just go here and I can say, and boom, we see our one note that we added. Let's add some more notes. I'm just keep adding the same note over and over and over and over, just to verify, we can see that they all got added here.

[00:09:01]
And now I can just say give me all of them. And then they go. Here's all of our notes, separated by brand new line.
>> If we don't pass a tag, it will be saved as undefined in our database?
>> If you don't pass a tag, it will be saved as undefined.

[00:09:14]
I believe I handled that default, but let's check. It depends on what abstraction you use, if you use the notes abstraction. For instance, new note my friends, if you wrote this code like I did, it'll make an empty array. So, it should be good if I don't pass a tag.

[00:09:31]
You can also go down a layer deeper into new note, and you could just do this. You could say tags, or actually you could just do this. You could do that to make that a default to empty array if they don't pass one. So there's a lot of places you can do defaults, so it's not undefined.

[00:09:54]
Or you can keep it undefined if you want to, but undefined is probably gonna get stripped out when you, JSON.stringify, so it'll just be removed. Null will stay there, but undefined won't, or I think null will stay there, yeah. So we also need to do filter. So to do filter, we also got to get all the notes.

[00:10:17]
So I'm gonna grab all of those notes here, just like that. And then we're gonna say, filtered notes and or I'm just gonna say, let's just say matches. That's gonna be await find or what is it called findNotes, okay, yeah, await findNotes that takes in a filter, or wait, hold on, let's see, find notes, that's right, okay, I'm tripping.

[00:10:52]
We don't need to get all notes, it gets the notes. We just need to take the filter so the filter would be, in this case, argv.filter. So argv.filter, will give us the filter here we got our matches that we just wanna list notes of the matches because filter returns an array of notes.

[00:11:09]
So let's just list them because list notes takes an array of notes so it should work that's why I wrote the function. So let's do that before we do that though I'm just gonna clean my database because they're all the same note and if I filter which is gonna get back the entire database and so like let's make some new notes.

[00:11:29]
So I'll make a new note for that one. Then I'll make a new note that is way different this time I will add a tag. This will be like, walk my dog. This one I'll make a note that says like eat more food. Cool, so I got three different notes in here.

[00:11:51]
Clean my room, walk my dog, eat more food. So now when I say note find, and I can say clean, I'll get back the one that only says clean my room. If I say find dog I get what it says walk my dog, and if I just filter on something literally not here then shall get back nothing.

[00:12:26]
Cool, any questions on that? Okay, let's work on remove, so remove an ID. We know we have our utility method, remove a note and it takes an id. So we're assuming that this command takes an id, and it does. So we can just say const id equals await remove notes by argv.id and let's just log the id like that.

[00:13:06]
Or you can do like an if statement. If there was no ID that means it wasn't found. So let's try that. So I'm going to find the one with the dog. We're gonna grab the ID for it, like this. Copy that, then I can say note remove and pass in that ID.

[00:13:26]
You can see it sent me the id back, which is a success that it found it and deleted it. If I go back to my Json, the dog one is gone. All right, and the last one is what do we have? We got the web one. We're not gonna do the web one right now, [LAUGH] but we will do the clean one.

[00:13:46]
So the clean one's, just removing all the notes and then just letting us know that we did that. So let's go back to our commands, we'll come back to the web one in a minute. For clean, super free, let's just say await, remove. All notes like that and then just console.log db reseted or something like that whatever you want to log so we got that.

[00:14:11]
So now if we go back verify I have two notes in here if I call the remove, so let's see what was it, it is clean so note clean, db reseted and the notes are empty, okay, perfect so looks like our CLI is, I mean it's getting there.

[00:14:36]
You can do some pretty cool stuff with this as far as like persisting things. It's funny just how adding a file like goes from like, okay, this is cool to like, well hold on now this is kind of like an app now like we're persisting things, we're saving things and it becomes more functional.

[00:14:51]
So, all right, we have that. What we're gonna do next is work on the testing and then we got the web version of this which is kinda like extra credit I think is cool because Node.js can do servers so why not talk about servers.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now