Skip to content

Latest commit

 

History

History
257 lines (204 loc) · 5.65 KB

File metadata and controls

257 lines (204 loc) · 5.65 KB

React Admin + NestJS CRUD Quick Start

Overview     Frontend     Backend    

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.

Prerequisites

NodeJs 10+, Npm ( or Yarn ) installed, also if you dont have NestCLI run npm install -g @nestjs/cli.

Installation

$ 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

yarn install

NPM

npm install

Running the app

NPM

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Yarn

# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod

Test

NPM

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Yarn

# unit tests
$ yarn run test

# e2e tests
$ yarn run test:e2e

# test coverage
$ yarn run test:cov

Creating from scratch

Initializing API application

Run nest new api inside main project folder ( it will create folder named api).
Move to api folder cd api.

Prepare required dependencies

Typeorm

npm install @nestjs/typeorm typeorm mysql class-validator class-transformer

NestJSX

npm i @nestjsx/crud nestjs-config

Create all required files.

App Module

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],
    }),
Guests Module

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.

Guests Controller

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) { }
Guests Service

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);
 }
Guests Entity

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;
}
Environment

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 = true

Go to main.ts and inside NestFactory.create add second argument , { cors: true } then change port from 3000 to 3001.

Now, follow installation.

We are done!

Read NestJs's documentation if you need to implement more complex scenarious and behaviours.

Readme Navigation

Further details on each of the systems contained in this project can be found via the following links:

Follow us

Website - Facebook - LinkedIn

Website     Facebook     LinkedIn