He creado dos snippets de VS Code para crear un Servicio y un Controller básico mucho más rápido. (para hacer lo que el profe hace en cámara rápida).
Para crearlos van a:
- File
- Preferences
- User Snippets
- Buscan typescript
En el archivo JSON que se les abrió copian este código dentro de las llaves:
- Servicio
"Create NestJS Service": {
"prefix": "nest:s",
"body": [
"import { Injectable, NotFoundException } from '@nestjs/common';",
"import { InjectRepository } from '@nestjs/typeorm';",
"import { Repository } from 'typeorm';",
"",
"import { $2 } from '${1:entity-route}';",
"import { ${4:createDto}, ${5:updateDto} } from '${3:dto-route}';",
"",
"@Injectable()",
"export class ${6:serviceName}Service {",
" constructor(",
" @InjectRepository($2)",
" private ${7:repo}: Repository<$2>,",
" ) {}",
"${0}",
" async findAll() {",
" return await this.$7.find();",
" }",
"",
" async findOne(id: number) {",
" const ${8:object} = await this.$7.findOne(id);",
" if (!$8) throw new NotFoundException(`${9:object} not found.`);",
" return $8;",
" }",
"",
" async create(data: $4) {",
" const ${10:newObject} = this.$7.create(data);",
" return await this.$7.save($10);",
" }",
"",
" async update(id: number, changes: $5) {",
" const $8 = await this.findOne(id);",
" this.$7.merge($8, changes);",
" return await this.$7.save($8);",
" }",
"",
" async remove(id: number) {",
" return await this.$7.delete(id);",
" }",
"}",
],
"description": "This service has a basic CRUD implemented"
},
- Controller
"Create NestJS Controller": {
"prefix": "nest:co",
"body": [
"import {",
" Controller,",
" Get,",
" Post,",
" Put,",
" Body,",
" Param,",
" Delete,",
" ParseIntPipe,",
"} from '@nestjs/common';",
"import { ApiTags } from '@nestjs/swagger';",
"",
"import { ${2:serviceName} } from '${1:service-route}';",
"import { ${4:createDto}, ${5:updateDto} } from '${3:dto-route}';",
"",
"@ApiTags('${6:controllerName}')",
"@Controller('$6')",
"export class ${7:controllerNameInUppercase}Controller {",
" constructor(private ${8:serviceName}: $2) {}",
"",
" @Get()",
" async findAll() {",
" return await this.$8.findAll();",
" }",
"",
" @Get('/:id')",
" async getCategory(@Param('id') id: number) {",
" return await this.$8.findOne(id);",
" }",
"",
" @Post()",
" async create(@Body() payload: $4) {",
" return await this.$8.create(payload);",
" }",
"",
" @Put('/:id')",
" async update(",
" @Param('id', ParseIntPipe) id: number,",
" @Body() payload: $5,",
" ) {",
" return await this.$8.update(id, payload);",
" }",
"",
" @Delete('/:id')",
" async remove(@Param('id', ParseIntPipe) id: number) {",
" return await this.$8.remove(id);",
" }",
"}",
],
"description": "This controller has a basic CRUD implemented"
}
Para usarlos simplemente tienen que crear su servicio/controlador normalmente y borrar lo que hay ahí.
Luego simplemente ponen nest:s
o nest:co
y empiezan a llenar todos los datos que se pide.
Para avanzar solo precionen la tecla tab
Espero les sirva! 😃
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?