generate-crud3 v1.1.9
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);
}
}
License
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago