API Design in Node.js, v3 API Design in Node.js, v3

Read, Update & Delete Documents

Check out a free preview of the full API Design in Node.js, v3 course:
The "Read, Update & Delete Documents" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott covers the other letters of CRUD, and live codes examples of how to find and update models.

Get Unlimited Access Now

Transcript from the "Read, Update & Delete Documents" Lesson

[00:00:00]
>> Scott Moss: For read, I talked about find, findOne and findById. So if I were to do, I'll just do a console.log here within a wait, so magical. Item.findById, then, I could pass the ID of this item right here. So I'll say item._id like that. Get rid of this log.

[00:00:33]
>> Scott Moss: And I'll do a .exec. So the .exec, basically. What this does is, it's kinda tricky, because mongoose returns these fake promises. They aren't real promises, they just have .then functionality at the end of them, but they're not real promises, .exec, which is short for execute, turns it into a real promise.

[00:00:58] So I have to do .exec here to make sure it's a real promise, so it doesn't have any issues. Also, because mongoose allows you to do tons of different querying after this, so you could do .where, .limit, .all this stuff. And .exec is just like I'm done. I'm not adding anymore query parameters to this, so that's why I have a .exec there.

[00:01:16] So I'll log that, build it.
>> Scott Moss: And again, I was able to find that one by ID. So that's that. If I change this to just say find, and give it no parameters, it'll find every single one of them, which in this case, should be like a couple.

[00:01:38] So looks like we have, yeah, like three or four. So it sent back an array of them, so that's fine. So that's the R for read. And then, we have update, so you got model.update, findByIdAndUpdate, and findOneAndUpdate. The thing about update, is that what you'll have to do, is let me write this here.

[00:02:05]
>> Scott Moss: Because rest, when you do an update on res, the response is expecting back the updated object. So you wanna get the updated object back that was just updated. So mongoose doesn't do that by default, so you gotta tell it to. So if I want the updated one, I could do item.find.

[00:02:23] In this case, we'll do findByIdAndUpdate. So the first parameter's gonna be the Id that we want in this case it's gonna be item.Id, cuz that's the one that was created up here. And then, the second parameter's what we wanna update. So let's change the name to eat, and then, that's it as far as requirements.

[00:02:45] But the third option one that you should definitely pass, because rest wants back the updated object, is you have to put new true. And what that means, is this goes away. What new true means, is return the new object that was updated. That's what it means. New true, if you don't put new true, it will just return the update, what field are updated and not the whole object.

[00:03:08] Or I'm sorry, it'll return the object before it was update, that's what it would return. We don't want that, so we wouldn't have the updates applied to it that you just update it, it will have the object before it got updates, which is not what you want. Because if your client was expecting the updated object, it would not get back those right fields.

[00:03:25] So you wanna put new true there, so that'll give you an updated object. If I did that,
>> Scott Moss: Or,
>> Scott Moss: Console.log(updated),
>> Scott Moss: Looks like we, I forgot to put an await there, there we go. So we got back to updated one with the name so that's gonna be the U for updating CRUD.

[00:04:02] And again, there's other ones. Update, which allows you to update many of them. FindByIdAndUpdate is one that we just did, which takes an Id and updates it. And then, findOne is like findById, except for, instead of taking an Id, it takes a list of things. So if I wanted to update, or one that had the name of cleanup, it'll find out when an updated, instead.

[00:04:25] But this is where you get into indexing stuff. So you wanna make sure you have an appropriate index for that. So, and then, for a delete, we have, it's exactly the same as update, except for, it's just, our remove. So we got .remove, this should be findById and remove, and then, findOne and remove.

[00:04:45] So we'll just do the findById and remove here. So if I got this, I'll just change this to remove. So I'm gonna remove that, I don't need a second argument, I don't need a new true. I'll just remove this. And now, we'll just change that to removed.
>> Scott Moss: And I build it.

[00:05:08]
>> Scott Moss: And then, boom, you can see it went ahead and sent back the one that it removed from the database, so that one will no longer be in there. So that's the crud methods that you would perform using the mongo models, inside the controllers, that combine with the routes and the verbs.

[00:05:24] So for instance, if we had a git request to /user, or, that's not a good one, let's say, a git request to /item, that means you wanna get all items. You wanna get many items, cuz it's not /id, which is get one ID. Yes?
>> Speaker 2: So did it remove the one for, is it sending back the object that got removed?

[00:05:44]
>> Scott Moss: Yes.
>> Speaker 2: Okay.
>> Scott Moss: It's sending back the object.
>> Speaker 2: Before it gets removed?
>> Scott Moss: Before it gets removed, yes. Yeah, cuz once it's removed, it's gone, it's no longer there. So it's like, here you go, which is exactly what arrest wants, or at least an ID. You, at least need an ID back on most applications, cuz you might have some state management on the application, like redux, or whatever you're using, Narwhal, NGRX, and you need to remove it from your local cache.

[00:06:09] And you're probably storing things by IDs. So you need an ID, at least, to be able to remove it.