MongoDB
See how to use the mongodb database preset
You will need to have a MongoDB database to use this preset! If you want to create one in the cloud for free, follow this guide
Env
The environment variable validation scheme receives a new property, now it is necessary to define the URI of your database in the MONGO_URI
variable:
Here's an example of the format of a MongoDB database URI:
mongodb+srv://<user>:<password>@<database>.subdomain.mongodb.net
Estrutura
The structure of this template was made using the mongoose library, so to create our data we used mongoose schemas
The index.ts
file in the src/database
folder must connect to the database:
The document models are stored in the db
variable object, that is, for each new document schema that you create, place the model as a property of this variable
So when you need to create, read, update or delete data, simply import the db
variable from #database
and use the model methods:
Always use the db
variable from the "#database"
import shortcut, this way the file containing the code that makes the connection to the database is called.
Schemas
Create your mongoose schemas in the src/database/schemas/
folder for better organization. See the example schemas that come by default:
After creating your schema, import it into the index.ts
file in the src/database
folder and create a model for it in the db
variable object:
If you need the types of the document properties, extract the type from the schemas: