Build NestJs Restful API with mongoose

nest new nestjs-api

Install mongoose package and create mongo db connection helper, this helper can works fine with local mongodb and mongodb altas

Ref: https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options

Import mongoose module into App module

Order Module

Generate our first module using generator, ref: https://docs.nestjs.com/recipes/crud-generator

nest g resource orders

As I need to works with mongodb, I need to rename folders

Now I define sample order schema

Define interface and service

Use postman to test, first setup environment variables for better manage the variables for later use.

and folder structure is what I create

create API, sample JSON object define as

{
"consignee": {
"name": "Peter Chan",
"phone": "91234567"
},
"contactAddress": "1211, IFC, Central, Hong Kong",
"charge": {
"currency": "HKD",
"totalAmount": 100
},
"remarks": "test remarks"
}

Run your API

yarn start

and Run the above request use Postman, here is the result

find all and find by id API which are similar

And here is the sample source link, hope this help, enjoy !

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kingyinma

Kingyinma

More from Medium

Solid