Using the schema
As mentioned before, we will use the schema we just defined to instantiate a new data model that we will be able to use in our code. Mongoose models are the ones that do the heavy lifting in regards to mapping objects to database documents, and also abstract common methods for operating with the data, such as .find()
and .save()
.
If you’ve come from the TypeORM chapter, models in Mongoose are very similar to repositories in TypeORM.
When having to connect requests to data models, the typical approach in Nest.js is building dedicated services, which serve as the “touch point” with each model, and controllers. This links the services to the requests reaching the API. We follow the data model -> service -> controller
approach in the following steps.
The interface
Before we create our service and controller, we need to write a small interface for our blog entries. This is because, as mentioned before, Mongoose schemas are not TypeScript classes...