DEV Community

Cover image for Building a RESTful API with NestJS and MongoDB (Mongoose)

Building a RESTful API with NestJS and MongoDB (Mongoose)

Carlo Miguel Dy on March 28, 2021

Introduction We will learn how to implement a RESTful API for a simple todo application using NestJS framework. But what is NestJS? "A...
Collapse
 
estimpson profile image
Eric Stimpson

Excellent post!

I've been hacking NodeJS, MongoDB, Mongoose for last month working on REST API for client-side Blazor app. I've been wanting to learn TypeScript and came across this post via a look into Nest.

Since I'm new to TypeScript, I'll definitely checkout the links at the top to gain a deeper understanding of the code presented here.

Any thoughts on where to head next to gain a broader understanding of Nest?

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

Hey thanks for reading and glad you enjoyed it!

That's good to know, hope you'll enjoy writing more of TypeScript. I can suggest you to take a look at how validation rules is being implemented in Nest by creating Pipes, middleware, all related to security.

Then I think you can take a look at how you can implement web sockets with Nest, which is also something I want to explore/learn more about it.

Collapse
 
babytoy profile image
Ian Bautista

Ang galing nito Miguel, thanks for writing the article.

How do I force the schema to use a collection name?

Collapse
 
babytoy profile image
Ian Bautista • Edited

OK na Miguel, I figured it out.
@Schema({ collection: 'collection name' })

Mongoose will pluralize the collection name from the schema name unless explicitly declared like this.

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

Sounds good! Thanks Ian 🎉

Collapse
 
riyas145 profile image
Riyas Ahamed

thanks i also have same doubt but you cleared now

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

Thank you po Ian! Happy that you enjoyed reading it!

Collapse
 
vitalijalbu profile image
vit

Anyone has a complete starter with mongoose?
Too simple this article, _id must be id, also population needed… module in module.
Entities? No one is using them? Also about the mapping? And creating custom fileds (like name + surname)

Collapse
 
kleguizamon profile image
Kevin Leguizamon

Greats!

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

Thanks man!

Collapse
 
denis_lubyannikov_7611ec2 profile image
Denis Lubyannikov

How did you connected to MongoDB without any credentials?

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

I didn't setup a password when I installed MongoDB in Ubuntu.

Collapse
 
techyez_com profile image
Techyez

Good one!

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

Thanks!

Collapse
 
aralroca profile image
Aral Roca

Is possible to have all the validations only in one place (the schema), and reuse the schema to create the dto?

Collapse
 
seven_77 profile image
Seven

This tutorial is what I want exactly, thanks for sharing!

Collapse
 
aliiiimansour94 profile image
Ali Mansour

hi buddy, when we are using: async function(....) { return await ..... },
should we add Promise or not? (like below):
async func(....): Promise {
return await ......
}

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

Hey, yea it's good too if you explicitly define the return type of the function. You can pass down a generic type argument to Promise as well so you don't accidentally return a different type of data and that might cause bugs to your code.

Collapse
 
muhammadawaisshaikh profile image
Muhammad Awais

i love this write. it will be a next power if you had added the auth module in it.

Collapse
 
carlomigueldy profile image
Carlo Miguel Dy

Hey, thanks! That's a good idea, I'll create an Auth implementation hopefully soon. Just got a lot of things going on my end.

Collapse
 
thangsuperman profile image
Phan Tấn Thắng

Thank you so much for your valuable content :)

Collapse
 
nthinh226 profile image
Ngoc Thinh

Thanks for post <3