1.1.9 • Published 3 years ago

generate-crud3 v1.1.9

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Generate crud Nestjs

This package provides a CRUD service for database MySQL build with TypeORM

Installation

npm i generate-crud3

1. REST API with swagger

Usage

Assume you have some TypeORM enitity:

@Entity()
export class ProductEntityEntity{
  @PrimaryGeneratedColumn()
  id: number;
  @Column()
  name: string;
  @Column()
  price: number;

}

Note: id is required

Then you need to create a service:

import { CrudTypeormService } from 'generate-crud3';
@Injectable()
export class ProductService extends CrudTypeormService<ProductEntity> {
  constructor(
    @InjectRepository(ProductEntity)
    private userRepository: Repository<ProductEntity>,
  ) {
    super(userRepository);
  }
}

DTO is used for creating or updating entity, we will need it in controller

import { CrudTypeormController } from 'generate-crud3';
import { CrudTypeormController } from 'simple-crud-nestjs';
@Controller('product')
export class ProductController extends CrudTypeormController(ProductEntity, InputProductDto) {
  constructor(private readonly productService: ProductService) {
    super(productService);
  }
}

After that you need to provide your service in a controller:

import { CrudTypeormController } from 'generate-crud3';
import { CrudTypeormController } from 'simple-crud-nestjs';
@Controller('product')
export class ProductController extends CrudTypeormController(ProductEntity, InputProductDto) {
  constructor(private readonly productService: ProductService) {
    super(productService);
  }
}

Finally in main.js

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const config = new DocumentBuilder()
    .setTitle('Test api')
    .setVersion('1.0')
    .addTag('testapi')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);
  await app.listen(3000);
}
bootstrap();

2. Authentication with swagger and JWT

First you have some entity with other information: (username and password are included in Auth)

import { Auth } from 'generate-crud3';
import { Entity, Column } from 'typeorm';

@Entity()
export class UserEntity extends Auth {
  @Column()
  firstName: string;
  @Column()
  lastName: string;
}

Then you need to create a Auth Service:

import { AuthService } from 'generate-crud3';
@Injectable()
export class UserService extends AuthService<UserEntity> {
  constructor(
    @InjectRepository(UserEntity)
    private userRepository: Repository<UserEntity>,
  ) {
    super(userRepository);
  }
}

InputUserDto for creating an account for user, there is no need to add username and password, only other information like firstName, lastName ...

import { Credential } from 'generate-crud3';
import { ApiProperty } from '@nestjs/swagger';
export class InputUserDto extends Credential {
  @ApiProperty()
  firstName: string;
  @ApiProperty()
  lastName: string;
}

And create UserController extends from AuthController

import { Controller } from '@nestjs/common';
import { UserService } from './user.service';
import { AuthController } from 'generate-crud3';
import { UserEntity } from './user.entity';
import { InputUserDto } from './create-user.dto';
@Controller('user')
export class UserController extends AuthController(UserEntity, InputUserDto) {
  constructor(private readonly userService: UserService) {
    super(userService);
  }
}

Add JwtStrategy in providers of app.module.ts file

import { JwtStrategy } from 'generate-crud3';
@Module({
  ...
  providers: [JwtStrategy],
  ...
})

And in main.ts, edit config

const config = new DocumentBuilder()
    .setTitle('Test api')
    .setVersion('1.0')
    .addTag('testapi')
    .addBearerAuth(
      {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
        name: 'JWT',
        description: 'Enter JWT token',
        in: 'header',
      },
      'JWT-auth',
    )
    .build();

3. Using GraphQL

Usage

Assume you have some GraphQL schema :

@ObjectType('Product')
export class Product {
  @Field((type) => ID)
  id: number;
  @Field()
  name: string;
  @Field()
  price: number;
}

(Note: id is required)

After that you need to provide your service in a resolver:

@Resolver((type) => Product)
export class ProductResolver extends CrudTypeormResolver(Product) {
  constructor(private productService: ProductService) {
    super(productService);
  }
}

GraphQl

License

MIT

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago