Transcript from the "CRUD Methods: Create" Lesson
>> We left off on writing our methods for our database and talked about why we're gonna set up another level of abstraction for that to make to keep our database functions somewhat generic enough to help us out in the future if we want to do anything else on the database.
[00:00:18] So now we're gonna make some abstractions. Interact specifically with the notes in the database. So let's do that. So what we want to do first is let's catch up on where we are on the notes. And, Yeah, we're here. Notepad, there we go. Note crud will be the next one.
[00:00:40] Anybody know what crud means? C-R-U-D. Anybody ever heard that term? Yeah. What is it?
>> Create, read, no. Yeah. Create, read, update, delete.
>> Yeah. Create, read, update, delete. Pretty much, I don't know. 99% of every app you'll ever make will be a credit app pretty much. Unless you're like working on a game or, yeah, something like that.
[00:01:07] It's probably a credit app. You're allowing someone to create something. You're pulling information from a database, you're updating something from a database, and you're. Probably deleting something from a database. So it's at the end of the day, it's just like, how do I build a CRUD app, right?
[00:01:24] And that's why there's so many abstractions around so many things and people make automations for so many things because like, this is just a CRUD app. Like, cool, this is Twitter, but like, I'm just creating tweets and deleting tweets and things like that, so. To crochet. So that's what we're going to do.
[00:01:39] We're going to create some abstractions to help us do crud on notes. So first thing is we want to create a note J S and our source. So let's do that. And then we're going to use the abstractions that we made From the database, because those are how we were, we're never going to use the FS module directly again to interface with our database, you're gonna get the FS module is like SQL, we're not going to use it anymore.
[00:02:14] We're gonna use these methods instead of using SQL, right, so let's import those methods. I like that quick trick and most Vyas code setups you can just type in like the name of something that's already exported and it'll kind of automatically import it for you right. So if I type in insert, you can see it brings that in like that.
[00:02:38] I could type them in and then from here I can bring these in like that. There we go. Cool, so we have those. And then when you think of CRUD, it's not one-for-one, so it's not like we're going to create four methods, because you might be able to read one or read many or update one or update many or delete one or delete many.
[00:03:00] So there could be many different, Some functions you write to do to do crud. So there really isn't just for the ones we're going to do is be able to create a new note, get all notes, find matching notes, and remove a note and then remove all notes.
[00:03:16] So we got five here. So let's start with creating a new note. So to create a new notes. We're just going to say export cons new notes and it's going to take in the note content it's also going to take in tags because we know our command has the ability to take in tags so we want to take in those tags as well so we got those tags it's going to be async how do I know it's going to be async well because I know We're interacting with a file, and every file thing is async if I didn't know that I wrote these functions and other all async so because these functions are async, and I'm using these functions inside the function I'm making now my function has to be async.
[00:03:57] Or I could just return the async function and not do anything async and just say, Hey, this is just returning your promise. When you use this function, you will have to await it eventually, but I know that I'm going to be doing things before I return. So I'm just going to go ahead and make it a sync.
[00:04:12] So we got that. So now we can make our new notes and which one of these do you think we'll use to save our note in the database? Save DB.
>> Save DB, so let's see what save DB does. So save DB takes an entire DB and overwrites the file with that db.
[00:04:32] Is that the one we wanna use? Do we wanna overwrite the entire database every time we get a new note? Yeah, we wanna use insert, cuz insert takes in a note. And just updates the note array with that one note, and it doesn't erase anything else. So we want to do an insert and not a save here.
[00:04:53] So first, we need to make a note. So I'm just going to say a new note, and this is going to be an object. I'm going to say the content is the note, and then I'm going to say the ID is the date.now. And then I'm just gonna say tags or tags.
[00:05:08] I'm just gonna move that up like that. Cool, so we got a new note. Next thing we wanna do is save it so I can say await, InsertDB and I wanna insert this new note. All right so now we have our new note. And then from there we could just return the new note.
[00:05:40] Here I think I called it insert But same thing you So, are we doing is making this note is going to be a string. Some be a tag are. It's going to be a string of content that we type into terminal. This is going to be an array of tags and yeah, we're making objects.
[00:05:56] We're saving it for inserted into the database, which we know if you go look at Takes that thing grabs the entire database pushes our object into the array, saves the database back to the file, and then returns the thing that we gave it. So that's exactly what we want.
[00:06:13] So we have new notes. Alright, the next thing we need to do is get all notes. This one's basically free. So let's export this file. As I say, it's basically free. I mean, the database abstractions that we wrote pretty much does this for us automatically. They don't really have to do much here.
[00:06:30] So we can just say, costs notes, you async here. And this is just equals just get the database. Await getDB, and then what I can do is I can just actually just do structure like that and return notes. Right, because getDB is gonna return an object. That is this whole thing, get rid of the users, and I just want to get the notes property from it.
[00:07:08] So that'll get all the notes.
>> Can you clarify that destructuring? On line 15.
>> Yeah, destructuring. Sorry. Let's talk about destructuring. Destructuring is cool. Let me just make a new spin-off file very quick. So I can illustrate that so let's make a let's make some data here so we have some data and this data looks like this it has let's talk about a basketball player so in their skill so they're shooting Is 99, they can shoot really good, they're dribbling, it's pretty bad.
[00:07:48] They can't dribble. And they're jumping is also really bad. So they're like I don't know how they got an NVA. So they got that and now let's say, I want to reference one of these properties, right? So let's say I want to reference jumping. So I could say const jumping equals data.jumping.
[00:08:12] I could do that. And that works. That's totally fine. But let's say I also want to reference shooting. So then I can say const shooting. Equals data.shooting. Okay, now that was annoying. I had to do this twice. So the first time it was fine, but now I'm basically doing this dot thing twice.
[00:08:32] I could do this in one line by destructuring. So what I can do is instead of having these two, I could replace it with just one line, and I can say I will come back to the object in a minute. I can say on the right hand side, I can say the object that I wanna do structure, in this case, the data object, and I want to pick these properties off of it.
[00:08:53] And the properties that I wanna pick are gonna be the shooting property and the jumping proper This one line is equivalent to both of these lines. What it's doing is it's making a new variable called shooting, and assigning it to data.shooting. It's making a new variable called jumping, and assigning it to data.jumping.
[00:09:12] So these variable names must match with the names of the properties of the object in which you're destructuring. And that's basically it. That's That's how this works. This also works with an array. So if I had an array of data, let's call it Nums. And let's say I just want to get the SEC.
[00:09:33] Let's say I want to get the first one. Let's keep it simple. I can say const. I can restructure with an array like this. And I just want to get the first one and I'm gonna call it first. So what this is going to do is it's going to grab the first thing in the array and assign its value to this variable called first.
[00:09:51] All right, and let's say I want to get the first thing and the last thing so now I can just say skip This one, skip this one, skip this one, and give me the last one. But you'll probably do something like this instead, just so people don't look at you funny.
[00:10:15] So yeah, destructuring works for objects and arrays. Not to be confused with what's just something else that looks just like destruction, but it's not destruction. I can't think of the name of it. If I come across it, I'll see it, but the same syntax can do something very different in a different context.
[00:10:34] But when you see a variable name to the left of it, that's destructed. Okay, you can also destructure inside of a function argument as well. All right, so if I have a function called action. Like this and it takes in like a config object. This object's got like, hell of stuff on it like config.this, config.that.
[00:10:58] If I can just skip that and just like cool, Let me stretch this object and give me that thing that I want and the other thing on that object that I want, and some more things I want, and then everything else, I'm just gonna say dot dot dot rest or whatever I want to call it.
[00:11:13] So this is basically me plucking these three properties off of the object that I know is coming into action. Assigning them their own names, and then everything else on the object that isn't these three, make an entirely new object and call it rest. So I went from just having one argument for this whole object to basically for this one, this one, this one and a whole new object with everything on the original object that aren't these three That's called a spread operator or rest.
[00:11:48] Cool. Any other questions?
>> Related to the inserting or reading or the crud operations.
>> We wanna handle the scenario where one person may be reading while that other person is inserting
>> Maybe it's not important for this example. We're [LAUGH] no our app only works for one person so we don't have to worry about concurrency issues and locking databases and locking files.
[00:12:16] Because someone is inserting and deleting we're assuming that is this. This is not multi tenant. This is one person on their computer But yeah, that is something you would have to think about in a real app. So whoever's doing that is definitely down bad one time trying to figure that out.
[00:12:31] And I'm sorry, [LAUGH] I'm sorry you had to go through that. But no, we won't be considering that today. That's a whole different can of worms.