We now need to create a database model that describes the data we want to store. In this case, it will be cats with names. Read more about typegoose here.
Now we have the service created we need to connect this with the actual API calls. The CatsController will receive GET and POST requests on the URL /cats and will get and create cats respectively.
We have to make sure we provide the needed models to our service with TypegooseModule.forFeature for the InjectModel to work. This helps prevents unauthorized access to other models.
cat.module.ts
import{Module}from"@nestjs/common";
import{TypegooseModule}from"nestjs-typegoose";
import{Cat}from"./cat.model";
import{CatsController}from"./cats.controller";
import{CatsService}from"./cats.service";
@Module({
imports:[TypegooseModule.forFeature([Cat])],
controllers:[CatsController],
providers:[CatsService]
})
exportclassCatsModule{}
That's it, you have created a simple working api with nestjs-typegoose!