Learn NestJs by creating a user management system, Part -2.
While you follow along with us it's highly recommended to go through the official documentation.
In Part 1 you learned to work with the Controller and Routing.
The goal of part 2 is to learn to work with:
- interface
- Providers (Service)
Providers are plain JavaScript classes that are declared as providers in a module – services, repositories, factories, helpers, and so on.
Let's start with where we left off. Type check could be managed with an interface
.
You may use CLI to generate an interface
nest generate interface users
And update the response type for each route in the controller.
import { Controller, Delete, Get, Param, Patch, Post, Put } from '@nestjs/common';
import { Users } from './users.interface';
let sampleUsers = [{
id: 1,
name: 'user1',
email: 'user1@email.com'
}]
@Controller('users')
export class UsersController {
@Get()
findAll() : Users []{
return sampleUsers
}
@Get(':id')
findOne(@Param() params) : Users{
const id = params.id
return sampleUsers.find(user => user.id === id)
}
@Post()
createOne() : Users []{
const paramsBody = {
id: 2,
name: 'user2',
email: 'user2@email.com'
}
sampleUsers.push(paramsBody)
return sampleUsers
}
@Put(':id')
updateOne(@Param() params) : Users {
const id = params.id
const paramsBody = {
id: id,
name: 'updatedUser',
email: 'email-updated@email.com'
}
const indexOfUser = sampleUsers.findIndex(item => item.id === id)
sampleUsers[indexOfUser] = paramsBody
return sampleUsers[indexOfUser]
}
@Patch(':id')
updateOnePartly(@Param() params) : Users {
const id = params.id
const paramsBody = {
email: 'email-update-only@email.com'
}
const indexOfUser = sampleUsers.findIndex(item => item.id === id)
sampleUsers[indexOfUser].email = paramsBody.email
return sampleUsers[indexOfUser]
}
@Delete(':id')
deleteOne(@Param() params) : Users []{
const id = params.id
sampleUsers = sampleUsers.filter(user => user.id === id)
return sampleUsers
}
}
Services are a good example of a Provider. Services are responsible for data storage and retrieval and are designed to be used by the Controllers. Instead of data storing or retrieving in the controller (like you did in part 1), data management will be done inside the service and the controller will use the service at its need.
Let's transfer codes from the controller and create a service src/users/users.service.ts
and use the interface.
nest generate service users
Instead of using the CLI if you manually create the service then do remember to import the service inside app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersController } from './users/users.controller';
import { UsersService } from './users/users.service';
@Module({
imports: [],
controllers: [AppController, UsersController],
providers: [AppService, UsersService],
})
export class AppModule {}
So the service would look like the following:
In order for the services to be digested by another class (controller) @Injectable()
decorator needs to be declared and the controller must inject the service in its constructor to use the service.
constructor(private usersServices: UsersService) {}
If you create the service with CLI you may notice that a .spec.ts
is generated at src/users/users.service.spec.ts
.
In next lesson we will see the usage of this file.
Part 2.
Source code: https://github.com/sifatul/user-management-with-nestjs/tree/part-2
Topic: Provider (Service)
Part 1.
Source code: https://github.com/sifatul/user-management-with-nestjs/tree/part-1
Topic: Routing (Controller)
I will be posting bi-weekly for javascript developers. Feel free to request topics that you would like to learn or recommend in the comment section.
Top comments (0)