$npx -y skills add sabahattink/antigravity-fullstack-hq --skill backend-dev-guidelinesBackend architecture principles, layering, error handling, logging patterns for NestJS. Use when designing NestJS modules, writing service logic, structuring error handling, or setting up structured logging.
| 1 | # Backend Development Guidelines |
| 2 | |
| 3 | ## NestJS Layered Architecture |
| 4 | |
| 5 | ``` |
| 6 | src/ |
| 7 | ├── modules/ |
| 8 | │ └── users/ |
| 9 | │ ├── users.module.ts # DI wiring |
| 10 | │ ├── users.controller.ts # HTTP layer — parse, validate, delegate |
| 11 | │ ├── users.service.ts # Business logic |
| 12 | │ ├── users.repository.ts # Data access |
| 13 | │ ├── dto/ |
| 14 | │ │ ├── create-user.dto.ts |
| 15 | │ │ └── update-user.dto.ts |
| 16 | │ ├── entities/ |
| 17 | │ │ └── user.entity.ts |
| 18 | │ └── users.spec.ts |
| 19 | ├── common/ |
| 20 | │ ├── filters/ # Global exception filters |
| 21 | │ ├── guards/ # Auth/RBAC guards |
| 22 | │ ├── interceptors/ # Logging, transform |
| 23 | │ ├── decorators/ # Custom decorators |
| 24 | │ └── pipes/ # Validation pipes |
| 25 | └── config/ |
| 26 | └── configuration.ts |
| 27 | ``` |
| 28 | |
| 29 | ## Controller Layer |
| 30 | |
| 31 | Controllers should be thin: validate inputs, call services, return responses. |
| 32 | |
| 33 | ```typescript |
| 34 | // users/users.controller.ts |
| 35 | import { |
| 36 | Controller, Get, Post, Put, Delete, |
| 37 | Body, Param, Query, ParseIntPipe, |
| 38 | UseGuards, HttpCode, HttpStatus, |
| 39 | } from '@nestjs/common' |
| 40 | import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger' |
| 41 | import { JwtAuthGuard } from '../common/guards/jwt-auth.guard' |
| 42 | import { CurrentUser } from '../common/decorators/current-user.decorator' |
| 43 | import { UsersService } from './users.service' |
| 44 | import { CreateUserDto } from './dto/create-user.dto' |
| 45 | import { UpdateUserDto } from './dto/update-user.dto' |
| 46 | import { PaginationDto } from '../common/dto/pagination.dto' |
| 47 | |
| 48 | @ApiTags('users') |
| 49 | @ApiBearerAuth() |
| 50 | @UseGuards(JwtAuthGuard) |
| 51 | @Controller('users') |
| 52 | export class UsersController { |
| 53 | constructor(private readonly usersService: UsersService) {} |
| 54 | |
| 55 | @Get() |
| 56 | @ApiOperation({ summary: 'List users with pagination' }) |
| 57 | findAll(@Query() pagination: PaginationDto) { |
| 58 | return this.usersService.findAll(pagination) |
| 59 | } |
| 60 | |
| 61 | @Get(':id') |
| 62 | findOne(@Param('id', ParseIntPipe) id: number) { |
| 63 | return this.usersService.findOneOrFail(id) |
| 64 | } |
| 65 | |
| 66 | @Post() |
| 67 | @HttpCode(HttpStatus.CREATED) |
| 68 | create(@Body() dto: CreateUserDto, @CurrentUser() actor: AuthUser) { |
| 69 | return this.usersService.create(dto, actor) |
| 70 | } |
| 71 | |
| 72 | @Put(':id') |
| 73 | update( |
| 74 | @Param('id', ParseIntPipe) id: number, |
| 75 | @Body() dto: UpdateUserDto, |
| 76 | @CurrentUser() actor: AuthUser, |
| 77 | ) { |
| 78 | return this.usersService.update(id, dto, actor) |
| 79 | } |
| 80 | |
| 81 | @Delete(':id') |
| 82 | @HttpCode(HttpStatus.NO_CONTENT) |
| 83 | remove(@Param('id', ParseIntPipe) id: number) { |
| 84 | return this.usersService.remove(id) |
| 85 | } |
| 86 | } |
| 87 | ``` |
| 88 | |
| 89 | ## Service Layer |
| 90 | |
| 91 | ```typescript |
| 92 | // users/users.service.ts |
| 93 | import { Injectable, NotFoundException, ConflictException } from '@nestjs/common' |
| 94 | import { InjectRepository } from '@nestjs/typeorm' |
| 95 | import { UsersRepository } from './users.repository' |
| 96 | import { CreateUserDto } from './dto/create-user.dto' |
| 97 | import { PaginationDto } from '../common/dto/pagination.dto' |
| 98 | import { User } from './entities/user.entity' |
| 99 | import { hash } from 'bcrypt' |
| 100 | |
| 101 | @Injectable() |
| 102 | export class UsersService { |
| 103 | constructor(private readonly repo: UsersRepository) {} |
| 104 | |
| 105 | async findAll(pagination: PaginationDto) { |
| 106 | return this.repo.findPaginated(pagination) |
| 107 | } |
| 108 | |
| 109 | async findOneOrFail(id: number): Promise<User> { |
| 110 | const user = await this.repo.findById(id) |
| 111 | if (!user) { |
| 112 | throw new NotFoundException(`User #${id} not found`) |
| 113 | } |
| 114 | return user |
| 115 | } |
| 116 | |
| 117 | async create(dto: CreateUserDto, actor: AuthUser): Promise<User> { |
| 118 | const existing = await this.repo.findByEmail(dto.email) |
| 119 | if (existing) { |
| 120 | throw new ConflictException('Email already registered') |
| 121 | } |
| 122 | |
| 123 | const passwordHash = await hash(dto.password, 12) |
| 124 | |
| 125 | return this.repo.create({ |
| 126 | ...dto, |
| 127 | passwordHash, |
| 128 | createdById: actor.id, |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | async update(id: number, dto: UpdateUserDto, actor: AuthUser): Promise<User> { |
| 133 | const user = await this.findOneOrFail(id) |
| 134 | return this.repo.save({ ...user, ...dto }) |
| 135 | } |
| 136 | |
| 137 | async remove(id: number): Promise<void> { |
| 138 | const user = await this.findOneOrFail(id) |
| 139 | await this.repo.softDelete(user.id) |
| 140 | } |
| 141 | } |
| 142 | ``` |
| 143 | |
| 144 | ## Repository Layer |
| 145 | |
| 146 | ```typescript |
| 147 | // users/users.repository.ts |
| 148 | import { Injectable } from '@nestjs/common' |
| 149 | import { DataSource, Repository } from 'typeorm' |
| 150 | import { User } from './entities/user.entity' |
| 151 | import { PaginationDto } from '../common/dto/pagination.dto' |
| 152 | |
| 153 | @Injectable() |
| 154 | export class UsersRepository extends Repository<User> { |
| 155 | constructor(private dataSource: DataSource) { |
| 156 | super(User, dataSource.createEntityManager()) |
| 157 | } |
| 158 | |
| 159 | async findById(id: number): Promise<User | null> { |
| 160 | return this.findOne({ where: { id, deletedAt: undefined } }) |
| 161 | } |
| 162 | |
| 163 | async findByEmail(email: string): Promise<User | null> { |
| 164 | return this.findOne({ where: { email: email.toLowerCase() } |