Backend portion of this boilerplate is an opinionated TypeScript boilerplate for Nest, including nodemon, TSLint, Jest. At this point API allows to perform CRUD operations about guests list.
NodeJs 10+, Npm ( or Yarn ) installed, also if you dont have NestCLI run npm install -g @nestjs/cli.
$ cp .env.example .env
# update .env file to match your MySQL settings.
$ mkdir config Create file inside src/config named database.ts with context:
export default {
host: process.env.DB_HOST,
type: 'mysql',
port: process.env.DB_PORT || 3306,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: ['src/**/*.entity{.ts,.js}'],
synchronize: process.env.DB_SYNCRONIZE === 'true',
logging: process.env.DB_LOGGING === 'true',
};yarn installnpm install# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod# development
$ yarn run start
# watch mode
$ yarn run start:dev
# production mode
$ yarn run start:prod# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov# unit tests
$ yarn run test
# e2e tests
$ yarn run test:e2e
# test coverage
$ yarn run test:covRun nest new api inside main project folder ( it will create folder named api).
Move to api folder cd api.
npm install @nestjs/typeorm typeorm mysql class-validator class-transformer
npm i @nestjsx/crud nestjs-config
Go to app.module.ts
At the very top add:
import { ConfigModule, ConfigService } from 'nestjs-config';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as path from 'path';Inside imports: [] add:
ConfigModule.load(path.resolve(__dirname, 'config', '*.{ts,js}')),
TypeOrmModule.forRootAsync({
useFactory: (config: ConfigService) => config.get('database'),
inject: [ConfigService],
}),Run nest generate module guests or nest g mo guests.
At the very top add:
import { TypeOrmModule } from '@nestjs/typeorm';
import { GuestEntity } from './guest.entity';Make sure @Module({}) hasimports: [], controllers: [] and providers: [], like: `
@Module({
imports: [],
providers: [],
controllers: [],
})Into imports: [] add value TypeOrmModule.forFeature([GuestEntity]).
Into providers: [] add value GuestsService.
Into controllers: [] add value GuestsController.
Run nest generate controller guests or nest g co guests.
Open src/guests/guests.controller.ts.
At the very top add:
import { Crud } from '@nestjsx/crud';
import { GuestsService } from './guests.service';
import { GuestEntity } from './guest.entity';Add @Crud(GuestEntity) before @Controller('guests').
Add constructor:
constructor(public service: GuestsService) { }Run nest generate service guests or nest g s guests.
Open src/guests/guests.service.ts.
At the very top add:
import { GuestEntity } from './guest.entity';
import { RepositoryService } from '@nestjsx/crud/typeorm';
import { InjectRepository } from '@nestjs/typeorm';Extend class GuestsService with extends RepositoryService<GuestEntity>.
Add constructor:
constructor(@InjectRepository(GuestEntity) repository) {
super(repository);
}Go to src/guests folder and create file named guest.entity.ts with context:
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
import { IsEmail } from 'class-validator';
@Entity({ name: 'guests' })
export class GuestEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column({
unique: true,
})
@IsEmail()
email: string;
@Column({ default: false })
isPresent: boolean;
}Create file .env.example with content:
DB_HOST = localhost
DB_PORT = 3306
DB_USER = USER
DB_PASSWORD = PASS
DB_DATABASE = DB
DB_SYNCRONIZE = true
DB_LOGGING = trueGo to main.ts and inside NestFactory.create add second argument , { cors: true } then change port from 3000 to 3001.
Now, follow installation.
Read NestJs's documentation if you need to implement more complex scenarious and behaviours.
Further details on each of the systems contained in this project can be found via the following links: