By Thomas Smyth
Published 16th February 2022
In my previous guide, I showed you how to set up a new NestJS project with the Nest CLI and then deploy it on Northflank. In this guide, I will show you how to add persistence to your NestJS project through the addition of a MongoDB database. The full source code used in this guide can be found in this git repository.
In this guide, we will store our data inside a managed MongoDB database instance provided by a Northflank addon. To create the MongoDB database, inside the same Northflank project as your NestJS service, create a new addon and select the “MongoDB” type.
To pass the database’s connection details to your NestJS service, we will use a Northflank secret group to automatically inject them into the service’s runtime environment. To do this, also inside the same Northflank project:
Create a new secret group.
Then link the following variables from your new MongoDB addon:
Variable name Alias host
DB_HOST
port
DB_PORT
username
DB_USERNAME
password
DB_PASSWORD
database
DB_DATABASE
By linking variables from the addon you ensure that the database’s connection details are automatically injected into your service’s runtime environment without needing to manually manage them.
To provide database connectivity, NestJS uses TypeORM, but before we go about using TypeORM we will need to install it and a MongoDB driver as dependencies in your NestJS project by running
npm install --save @nestjs/typeorm typeorm mongodb@3.7.3
.Next, we need to import the
TypeOrmModule
into the rootAppModule
, that the NestJS CLI generated in the previous guide, and specify the environmental variables your database connection details are injected into. Once this is done, yourapp.module.ts
file should contain the following:import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { DestinationsModule } from './destinations/destinations.module'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'mongodb', host: process.env.DB_HOST, port: parseInt(process.env.DB_PORT), username: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, ssl: true, autoLoadEntities: true, // Only enable this option if your application is in development, // otherwise use TypeORM migrations to sync entity schemas: // https://typeorm.io/#/migrations synchronize: true, }), DestinationsModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
Now your NestJS project is configured to connect to your MongoDB database you can go about using it in your project. In the rest of this guide, I will show you how to set up a REST API, to manage a list of holiday destinations to visit, to demonstrate the database in action.
In the previous guide, we used the NestJS CLI to create a new NestJS project containing the basic setup we needed to start developing your application. We will now use the same CLI to generate additional elements within our project. In particular, we will create a “resource” that provides us with a set of REST API endpoints through which we can run CRUD (create, read, update, delete) operations on a list of holiday destinations. To do this:
Run
nest generate resource
.When prompted enter the resource name
destinations
.Select
REST API
.Finally enter
Y
when asked whether or not we want to “generate CRUD entry points”.
Once the NestJS CLI has generated the
destinations
resource your project should contain a new folder nameddestinations
that contains some of the logic needed to add the set of endpoints, however, we will have to complete this logic before we can start using these endpoints to manage the list of holiday destinations.Firstly, we need to populate the
Destination
entity schema with the fields we want to store for each destination:- An id, to make it easier to reference each destination.
- A name of each destination.
- A boolean value stating whether or not we have visited the destination.
Once this is done, your
destination.entity.ts
file should contain the following:import { Column, Entity, ObjectID, ObjectIdColumn } from 'typeorm'; @Entity() export class Destination { @ObjectIdColumn() id: ObjectID; @Column() name: string; @Column({ default: false }) visited: boolean; }
For more information on how to define an entity’s schema, check out the TypeORM documentation.
Next, we need to tell TypeORM that we have defined a new modal in the resource by importing it into
DestinationsModule
. Once this is done, yourdestinations.module.ts
file should contain the following:import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { DestinationsService } from './destinations.service'; import { DestinationsController } from './destinations.controller'; import { Destination } from './entities/destination.entity'; @Module({ imports: [TypeOrmModule.forFeature([Destination])], controllers: [DestinationsController], providers: [DestinationsService], }) export class DestinationsModule {}
Then, we need to inject the
Destination
entity as aMongoRepository
, a class that allows us to manage instances of theDestination
entity in the MongoDB database, into theDestinationsService
, a class that implements the CRUD operations. Then we need to use the Repository API to implement each method in theDestinationsServices
. Once this is done, yourdestinations.service.ts
file should contain the following:import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { MongoRepository } from 'typeorm'; import { CreateDestinationDto } from './dto/create-destination.dto'; import { UpdateDestinationDto } from './dto/update-destination.dto'; import { Destination } from './entities/destination.entity'; @Injectable() export class DestinationsService { constructor( @InjectRepository(Destination) private destinationsRepository: MongoRepository<Destination>, ) {} create(createDestinationDto: CreateDestinationDto) { return this.destinationsRepository.save(createDestinationDto); } findAll() { return this.destinationsRepository.find(); } findOne(id: number) { return this.destinationsRepository.findOne(id); } update(id: number, updateDestinationDto: UpdateDestinationDto) { return this.destinationsRepository.update(id, updateDestinationDto); } remove(id: string) { return this.destinationsRepository.delete(id); } }
Finally, we need to update the
DestinationsController
to work with MongoDB’s object ids rather than the numerical ids the NestJS CLI initially configured it to work with. Once this is done, yourdestinations.controller.ts
file should contain the following:import { Controller, Get, Post, Body, Patch, Param, Delete, } from '@nestjs/common'; import { DestinationsService } from './destinations.service'; import { CreateDestinationDto } from './dto/create-destination.dto'; import { UpdateDestinationDto } from './dto/update-destination.dto'; @Controller('destinations') export class DestinationsController { constructor(private readonly destinationsService: DestinationsService) {} @Post() create(@Body() createDestinationDto: CreateDestinationDto) { return this.destinationsService.create(createDestinationDto); } @Get() findAll() { return this.destinationsService.findAll(); } @Get(':id') findOne(@Param('id') id: string) { return this.destinationsService.findOne(id); } @Patch(':id') update( @Param('id') id: string, @Body() updateDestinationDto: UpdateDestinationDto, ) { return this.destinationsService.update(id, updateDestinationDto); } @Delete(':id') remove(@Param('id') id: string) { return this.destinationsService.remove(id); } }
Before we test the API endpoints, we need to commit and push them to your Git repository and then wait for Northflank to build and deploy them. Once this is done we can test each of the API endpoints using Postman:
The full source code used in this guide can be found in this git repository.
Northflank allows you to deploy your code and databases within minutes. Sign up for a Northflank account and create a free project to get started.
- Multiple read and write replicas
- Observe & monitor with real-time metrics & logs
- Low latency and high performance
- Backup, restore and fork databases
- Private and optional public load balancing as well as Northflank local proxy
Related articles