Build NestJs Restful API with mongoose

Kingyinma
2 min readApr 23, 2021
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 !

--

--