In this article, we'll walk through the setup process for integrating Nx, Nest.js, MongoDB, and Mongoose. This integration is crucial for building scalable and efficient applications. We'll cover the installation of necessary packages, updating the main application file (main.ts), modifying the app.module.ts file, generating projects, creating configuration files, and setting up the MongoDB connection.
Install Packages
Let's start by installing the required packages for our project. Open your terminal and run the following commands:
GraphQL Packages
Class Validator Packages
Bcrypt
Passport and JWT Packages
Cookie Parser
Update main.ts
Next, update the main.ts file located at apps/api/src/main.ts. Add the necessary configurations for cookies and validation pipes.
In this case, apps/api folder is a nextjs project.
Update app.module.ts
Now, let's update the app.module.ts file located at apps/api/src/app/app.module.ts. This modification includes the integration of Apollo and GraphQL.
If any module that has a resolver does not exist, an error will occur!
Install @nestjs/config Package
To handle configuration settings, install the @nestjs/config package:
Generate Project
Now, generate the shared configuration project:
Create env.ts
Create the env.ts file inside libs/shared/config/src/lib/ with the following content:
Update the index.ts file.
Install @nestjs/mongoose Package
To interact with MongoDB using Nest.js, install the @nestjs/mongoose and mongoose packages:
Generate Mongoose Project
Generate the project for Mongoose shared configuration:
Create Mongoose Files
Create the following files within libs/api/mongoose/shared/src/lib/:
model/abstract.model.ts
database/abstract.schema.ts
database/abstract.repository.ts
database/database.module.ts
Update the index.ts file.
This completes the setup for the first part of Nx, Nest.js, MongoDB, and Mongoose integration. Stay tuned for the next part where we will continue with the configuration and integration process.