Be A Hero
NestJS, Docker, Circle, CI/CD, Testing
-
NestJS
-
Docker
-
Circle
-
CI/CD
-
Testing
Agenda
Presentation
Need to install
NestJS Links
APP
NestJS: APP
User Management
Endpoint | Method | Description |
---|---|---|
users/ | GET | Get all users |
users/:id | GET | Get one user |
users/ | POST | Create user |
users/:id | DELETE | Delete user |
users/:id | PUT/PATCH | Update user |
Endpoints which we will implement
NestJS: APP
User Management
- In the first part, we will implement endpoints without integration with DataBase.
- In the second part, we will integrate with DataBase.
NestJS: How To Start
$ npm install -g @nestjs/cli
$ nest new my-nest-project
$ cd my-nest-project
$ npm run start:dev
NestJS: Objectives
- Modules
- Controllers
- Services
- DTO
- Validation
- Pipes
- Meegration/Seeds
NestJS: NestJS CLI
$ nest g --help
$ nest g module users
NestJS: Modules
- providers
- controllers
- exports
- imports
NestJS: Controllers
- Responsible for handling requests
- Specific path
- Contains handlers (GET, POST, PUT, DELETE)
- DI
NestJS: Controllers
$ nest g controller users
NestJS: Controllers
@Controller('/user-management')
export class UsersController {
...
}
Define path
NestJS: Controllers
@Controller('/user-management')
export class UsersController {
@Get()
getAllUsers() {
return ...
}
}
Define Handlers
NestJS: Providers
- DI Services
- exports
NestJS: Providers
@Module({
controllers: [UsersController],
providers: [],
})
export class UsersModule {}
NestJS: Services
- Services is Singleton
- Need to provide service inside the module and add as an attribute in the controller
- We can use it for:
- communication with BD
- formatting data
- logging data
NestJS: Services
$ nest g service users
NestJS: Services
Time to implement base mock GET request
NestJS: base module link
NestJS: Postman
and
Postman collection
NestJS: Model
- Provide a model for User
- Provide Handlers for create User
NestJS: DTO
DTO - Data Transfer Object
- Need to control transferred data
- Contracts
- Validations
- Can be Classes or Interfaces
- Classes provide more options
NestJS: CRUD
NestJS: Pipes
- Routes handlers
- Perform data transformation and data validation
- Can throw exceptions
- Can be async
NestJS: Custom Pipes
- @Injectable()
- Must implement Pipetransform
- transform() method accepts two params:
- value
- metadata (optional)
NestJS: Custom Pipes
- We can use it in a different way:
- As Decorator
- As Argument
- useGlobalPipes
NestJS: Validation Pipes
npm install class-validator --save
npm i class-transformer --save
NestJS: Validation Pipes
- Go to add
- Validation Pipe
- Not Found Exeption
NestJS: Validations
Small Task
NestJS: Validations
PostgreSQL
Docker
$ docker container ls
$ docker run --name postgres-nest -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres
$ docker container ls
$ docker stop $(docker ps -a -q)
$ docker rm $(docker ps -a -q)
To avoid local installation of Postgresql we will use Docker container.
PgAdmin
Go to PgAdmin:
- Create server
- Create Database
- Try to connect
TypeORM
$ npm i --save @nestjs/typeorm typeorm pg
TypeORM
Object Relation Mapping
- Writing Data model in one place
- Data types, data handling, relations
- No need to write SQL syntax
- Database abstraction
TypeORM
Depricated
@EntityRepository(User)
export class UsersRepository extends Repository<User> {
}
TypeORM
Configuration
- env variables
- docker
ENV Variables
$ npm install -g cross-env
"start:dev": "STAGE=dev nest start --watch",
"start:dev": "cross-env STAGE=dev nest start --watch",
$ npm install @nestjs/config --save
ENV Variables
$ npm i @nestjs/config --save
ENV Variables
Docker
- Docker
- docker-compose
Deploy
- Create Heroku app
- Configure Circleci
Nee to add links to 'How to' docs
Deploy
- Create app
- Go to your account and copy API key
- Create Procfile
Heroku Configuration
Deploy
- Copy config
Circleci Configuration
version: 2.1
orbs:
heroku: circleci/heroku@1.0.1
workflows:
heroku_deploy:
jobs:
- heroku/deploy-via-git
2. Go to Circleci
3. Add env variables:
HEROKU_APP_NAME and HEROKU_API_KEY
Deploy
- Copy config
- Create addon
- Update DB config
Heroku additional configuration
Testing
Testing
JS Doc
TDD
Jest
- Suites - describe
- Specs - it or test
- Expectation - expect
Jest
describe('A suite', () => {
test('contains spec with n expectation', () => {
expect(Math.max(1, 5, 10)).toBe(10);
});
})
Jest
function sum(a, b) {
return a + b;
}
describe('sum', () => {
test('should be 3 when called sum with args 1 and 2', () => {
expect(sum(1,2)).toBe(3);
});
})
describe('A suite', () => {
test('contains spec with n expectation', () => {
expect(Math.max(1, 5, 10)).toBe(10);
});
})
Jest
const mockRepository = () => ({
getById: jest.fn(),
});
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
{
provide: UsersRepository,
useFactory: mockRepository,
},
],
}).compile();
service = module.get<UsersService>(UsersService);
repository = module.get<UsersRepository>(UsersRepository);
});
Mocking
Jest
Coverage
Jest
e2e - check endpoints, factories etc
Thank You!
Questions?
Be A Hero
By Oleg Rovenskyi
Be A Hero
- 149