Estructura backend - NestJS
Notas sobre la Estructuraβ
OrganizaciΓ³n Modularβ
/common: Cross-cutting concerns que afectan toda la aplicaciΓ³n/config: ConfiguraciΓ³n centralizada y validada/core: Servicios fundamentales del sistema/modules: LΓ³gica de negocio organizada por dominio/infrastructure: Servicios tΓ©cnicos y de infraestructura/shared: CΓ³digo compartido entre mΓ³dulos
MΓ³dulos de Dominioβ
Cada mΓ³dulo sigue la estructura:
- Controllers: Manejo de HTTP requests
- Services: LΓ³gica de negocio
- Repositories: Acceso a datos
- Entities: Modelos de dominio
- DTOs: ValidaciΓ³n de entrada/salida
- Interfaces: Contratos y tipos
- Events: Eventos del dominio (si aplica)
Principios SOLIDβ
- Single Responsibility: Cada servicio tiene una responsabilidad clara
- Open/Closed: Extensible mediante interfaces y herencia
- Liskov Substitution: Uso de abstracciones (providers base)
- Interface Segregation: Interfaces especΓficas por contexto
- Dependency Inversion: InyecciΓ³n de dependencias de NestJS
Testingβ
- Tests unitarios junto al cΓ³digo o en
/test/unit - Tests de integraciΓ³n en
/test/integration - Tests E2E en
/test/e2e - Fixtures y utilidades compartidas
Repositoriosβ
- AbstracciΓ³n del acceso a datos
- Facilita migraciΓ³n futura de Firebase
- Operaciones CRUD estandarizadas
Serviciosβ
- LΓ³gica de negocio pura
- Sin dependencias directas de HTTP
- Testeable de forma aislada
Guards y Decoradoresβ
- Reutilizables en toda la aplicaciΓ³n
- SeparaciΓ³n de concerns de seguridad
- Metadata-driven
ConfiguraciΓ³nβ
- Variables de entorno validadas
- ConfiguraciΓ³n tipada
- Separada por dominios
Esta estructura permite:
- Escalabilidad horizontal y vertical
- FΓ‘cil testing y mantenimiento
- Onboarding rΓ‘pido de desarrolladores
- MigraciΓ³n gradual de componentes
- Cumplimiento de mejores prΓ‘cticas de NestJS
prompts-backend/
βββ .cursorrules # (Opcional - especΓfico para Cursor IDE)
βββ .env # Variables de entorno
βββ .env.example # Plantilla de variables de entorno
βββ .eslintrc.js # ConfiguraciΓ³n ESLint
βββ .gitignore # Archivos ignorados por Git
βββ .prettierrc # ConfiguraciΓ³n Prettier
βββ nest-cli.json # ConfiguraciΓ³n NestJS CLI
βββ package.json # Dependencias y scripts
βββ tsconfig.json # ConfiguraciΓ³n TypeScript
βββ tsconfig.build.json # ConfiguraciΓ³n TypeScript para build
βββ docker/
β βββ Dockerfile # Dockerfile para producciΓ³n
β βββ Dockerfile.dev # Dockerfile para desarrollo
β βββ docker-compose.yml # ComposiciΓ³n de servicios
βββ src/
β βββ main.ts # Punto de entrada de la aplicaciΓ³n
β βββ app.module.ts # MΓ³dulo principal
β βββ common/ # Elementos comunes compartidos (cross-cutting concerns)
β β βββ constants/
β β β βββ api.constants.ts
β β β βββ error-codes.constants.ts
β β β βββ messages.constants.ts
β β β βββ index.ts
β β βββ decorators/
β β β βββ api-key.decorator.ts
β β β βββ current-user.decorator.ts
β β β βββ rate-limit.decorator.ts
β β β βββ roles.decorator.ts
β β β βββ public.decorator.ts
β β β βββ index.ts
β β βββ dto/
β β β βββ pagination.dto.ts
β β β βββ response.dto.ts
β β β βββ base-query.dto.ts
β β β βββ index.ts
β β βββ exceptions/
β β β βββ business.exception.ts
β β β βββ validation.exception.ts
β β β βββ unauthorized.exception.ts
β β β βββ not-found.exception.ts
β β β βββ index.ts
β β βββ filters/
β β β βββ http-exception.filter.ts
β β β βββ all-exceptions.filter.ts
β β β βββ validation-exception.filter.ts
β β β βββ index.ts
β β βββ guards/
β β β βββ jwt-auth.guard.ts
β β β βββ api-key.guard.ts
β β β βββ plan.guard.ts
β β β βββ roles.guard.ts
β β β βββ rate-limit.guard.ts
β β β βββ index.ts
β β βββ interceptors/
β β β βββ logging.interceptor.ts
β β β βββ transform.interceptor.ts
β β β βββ timeout.interceptor.ts
β β β βββ cache.interceptor.ts
β β β βββ index.ts
β β βββ interfaces/
β β β βββ request.interface.ts
β β β βββ response.interface.ts
β β β βββ pagination.interface.ts
β β β βββ index.ts
β β βββ middlewares/
β β β βββ logger.middleware.ts
β β β βββ cors.middleware.ts
β β β βββ security.middleware.ts
β β β βββ index.ts
β β βββ pipes/
β β β βββ validation.pipe.ts
β β β βββ parse-object-id.pipe.ts
β β β βββ transform.pipe.ts
β β β βββ index.ts
β β βββ types/
β β β βββ express.d.ts
β β β βββ common.types.ts
β β β βββ index.ts
β β βββ utils/
β β βββ pagination.util.ts
β β βββ response.util.ts
β β βββ index.ts
β βββ config/ # ConfiguraciΓ³n modular
β β βββ config.module.ts
β β βββ configurations/
β β β βββ app.config.ts
β β β βββ database.config.ts
β β β βββ auth.config.ts
β β β βββ redis.config.ts
β β β βββ llm.config.ts
β β β βββ storage.config.ts
β β β βββ queue.config.ts
β β β βββ monitoring.config.ts
β β βββ validation/
β β β βββ env.validation.ts
β β β βββ index.ts
β β βββ index.ts
β βββ core/ # Funcionalidad central del sistema
β β βββ database/
β β β βββ database.module.ts
β β β βββ database.service.ts
β β β βββ index.ts
β β βββ logger/
β β β βββ logger.module.ts
β β β βββ logger.service.ts
β β β βββ index.ts
β β βββ cache/
β β β βββ cache.module.ts
β β β βββ cache.service.ts
β β β βββ index.ts
β β βββ health/
β β βββ health.module.ts
β β βββ health.controller.ts
β β βββ index.ts
β βββ modules/ # MΓ³dulos de dominio de negocio
β β βββ auth/
β β β βββ auth.module.ts
β β β βββ controllers/
β β β β βββ auth.controller.ts
β β β β βββ auth.controller.spec.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ auth.service.ts
β β β β βββ auth.service.spec.ts
β β β β βββ token.service.ts
β β β β βββ password.service.ts
β β β β βββ index.ts
β β β βββ strategies/
β β β β βββ jwt.strategy.ts
β β β β βββ local.strategy.ts
β β β β βββ api-key.strategy.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ login.dto.ts
β β β β βββ register.dto.ts
β β β β βββ reset-password.dto.ts
β β β β βββ change-password.dto.ts
β β β β βββ refresh-token.dto.ts
β β β β βββ index.ts
β β β βββ entities/
β β β β βββ session.entity.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ jwt-payload.interface.ts
β β β β βββ auth-response.interface.ts
β β β β βββ index.ts
β β β βββ guards/
β β β β βββ local-auth.guard.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ users/
β β β βββ users.module.ts
β β β βββ controllers/
β β β β βββ users.controller.ts
β β β β βββ profile.controller.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ users.service.ts
β β β β βββ profile.service.ts
β β β β βββ index.ts
β β β βββ repositories/
β β β β βββ users.repository.ts
β β β β βββ index.ts
β β β βββ entities/
β β β β βββ user.entity.ts
β β β β βββ profile.entity.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ create-user.dto.ts
β β β β βββ update-user.dto.ts
β β β β βββ update-profile.dto.ts
β β β β βββ user-query.dto.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ user.interface.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ prompts/
β β β βββ prompts.module.ts
β β β βββ controllers/
β β β β βββ prompts.controller.ts
β β β β βββ prompt-templates.controller.ts
β β β β βββ prompt-execution.controller.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ prompts.service.ts
β β β β βββ prompt-optimization.service.ts
β β β β βββ prompt-validation.service.ts
β β β β βββ prompt-execution.service.ts
β β β β βββ prompt-templates.service.ts
β β β β βββ index.ts
β β β βββ repositories/
β β β β βββ prompts.repository.ts
β β β β βββ prompt-templates.repository.ts
β β β β βββ prompt-history.repository.ts
β β β β βββ index.ts
β β β βββ entities/
β β β β βββ prompt.entity.ts
β β β β βββ prompt-template.entity.ts
β β β β βββ prompt-execution.entity.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ create-prompt.dto.ts
β β β β βββ update-prompt.dto.ts
β β β β βββ execute-prompt.dto.ts
β β β β βββ optimize-prompt.dto.ts
β β β β βββ prompt-query.dto.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ prompt.interface.ts
β β β β βββ optimization.interface.ts
β β β β βββ index.ts
β β β βββ events/
β β β β βββ prompt-created.event.ts
β β β β βββ prompt-executed.event.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ flows/
β β β βββ flows.module.ts
β β β βββ controllers/
β β β β βββ flows.controller.ts
β β β β βββ flow-execution.controller.ts
β β β β βββ flow-templates.controller.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ flows.service.ts
β β β β βββ flow-executor.service.ts
β β β β βββ flow-validator.service.ts
β β β β βββ block-factory.service.ts
β β β β βββ flow-engine.service.ts
β β β β βββ index.ts
β β β βββ repositories/
β β β β βββ flows.repository.ts
β β β β βββ flow-executions.repository.ts
β β β β βββ index.ts
β β β βββ entities/
β β β β βββ flow.entity.ts
β β β β βββ block.entity.ts
β β β β βββ connection.entity.ts
β β β β βββ flow-execution.entity.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ create-flow.dto.ts
β β β β βββ update-flow.dto.ts
β β β β βββ execute-flow.dto.ts
β β β β βββ flow-query.dto.ts
β β β β βββ block.dto.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ flow.interface.ts
β β β β βββ block.interface.ts
β β β β βββ execution-context.interface.ts
β β β β βββ index.ts
β β β βββ blocks/
β β β β βββ base-block.ts
β β β β βββ prompt-block.ts
β β β β βββ conditional-block.ts
β β β β βββ loop-block.ts
β β β β βββ input-block.ts
β β β β βββ output-block.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ credits/
β β β βββ credits.module.ts
β β β βββ controllers/
β β β β βββ credits.controller.ts
β β β β βββ transactions.controller.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ credits.service.ts
β β β β βββ transaction.service.ts
β β β β βββ credit-calculator.service.ts
β β β β βββ index.ts
β β β βββ repositories/
β β β β βββ credits.repository.ts
β β β β βββ transactions.repository.ts
β β β β βββ index.ts
β β β βββ entities/
β β β β βββ credit-balance.entity.ts
β β β β βββ transaction.entity.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ purchase-credits.dto.ts
β β β β βββ transfer-credits.dto.ts
β β β β βββ transaction-query.dto.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ credit.interface.ts
β β β β βββ transaction.interface.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ api-keys/
β β β βββ api-keys.module.ts
β β β βββ controllers/
β β β β βββ api-keys.controller.ts
β β β β βββ api-key-usage.controller.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ api-keys.service.ts
β β β β βββ api-key-validation.service.ts
β β β β βββ api-key-usage.service.ts
β β β β βββ index.ts
β β β βββ repositories/
β β β β βββ api-keys.repository.ts
β β β β βββ api-key-usage.repository.ts
β β β β βββ index.ts
β β β βββ entities/
β β β β βββ api-key.entity.ts
β β β β βββ api-key-usage.entity.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ create-api-key.dto.ts
β β β β βββ update-api-key.dto.ts
β β β β βββ api-key-query.dto.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ api-key.interface.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ llm/
β β β βββ llm.module.ts
β β β βββ services/
β β β β βββ llm.service.ts
β β β β βββ llm-router.service.ts
β β β β βββ llm-health.service.ts
β β β β βββ index.ts
β β β βββ providers/
β β β β βββ base-provider.ts
β β β β βββ openai.provider.ts
β β β β βββ google.provider.ts
β β β β βββ amazon.provider.ts
β β β β βββ azure.provider.ts
β β β β βββ index.ts
β β β βββ adapters/
β β β β βββ vercel-sdk.adapter.ts
β β β β βββ response-normalizer.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ provider.interface.ts
β β β β βββ llm-response.interface.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ multimodal/
β β β βββ multimodal.module.ts
β β β βββ controllers/
β β β β βββ media.controller.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ media.service.ts
β β β β βββ file-processor.service.ts
β β β β βββ media-validator.service.ts
β β β β βββ index.ts
β β β βββ processors/
β β β β βββ image.processor.ts
β β β β βββ audio.processor.ts
β β β β βββ document.processor.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ upload-media.dto.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ media.interface.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ subscriptions/
β β β βββ subscriptions.module.ts
β β β βββ controllers/
β β β β βββ subscriptions.controller.ts
β β β β βββ plans.controller.ts
β β β β βββ index.ts
β β β βββ services/
β β β β βββ subscriptions.service.ts
β β β β βββ plan-validator.service.ts
β β β β βββ billing.service.ts
β β β β βββ index.ts
β β β βββ repositories/
β β β β βββ subscriptions.repository.ts
β β β β βββ plans.repository.ts
β β β β βββ index.ts
β β β βββ entities/
β β β β βββ subscription.entity.ts
β β β β βββ plan.entity.ts
β β β β βββ index.ts
β β β βββ dto/
β β β β βββ create-subscription.dto.ts
β β β β βββ update-subscription.dto.ts
β β β β βββ index.ts
β β β βββ interfaces/
β β β β βββ subscription.interface.ts
β β β β βββ plan.interface.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ webhooks/
β β βββ webhooks.module.ts
β β βββ controllers/
β β β βββ stripe-webhook.controller.ts
β β β βββ index.ts
β β βββ services/
β β β βββ webhook-processor.service.ts
β β β βββ index.ts
β β βββ index.ts
β βββ infrastructure/ # Servicios de infraestructura
β β βββ infrastructure.module.ts
β β βββ firebase/
β β β βββ firebase.module.ts
β β β βββ firebase.service.ts
β β β βββ firestore.service.ts
β β β βββ firebase-auth.service.ts
β β β βββ index.ts
β β βββ redis/
β β β βββ redis.module.ts
β β β βββ redis.service.ts
β β β βββ redis-cache.service.ts
β β β βββ index.ts
β β βββ queue/
β β β βββ queue.module.ts
β β β βββ queue.service.ts
β β β βββ processors/
β β β β βββ base.processor.ts
β β β β βββ prompt.processor.ts
β β β β βββ flow.processor.ts
β β β β βββ media.processor.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ storage/
β β β βββ storage.module.ts
β β β βββ storage.service.ts
β β β βββ providers/
β β β β βββ firebase-storage.provider.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ monitoring/
β β β βββ monitoring.module.ts
β β β βββ monitoring.service.ts
β β β βββ providers/
β β β β βββ new-relic.provider.ts
β β β β βββ index.ts
β β β βββ index.ts
β β βββ email/
β β βββ email.module.ts
β β βββ email.service.ts
β β βββ templates/
β β β βββ welcome.template.ts
β β β βββ reset-password.template.ts
β β β βββ index.ts
β β βββ index.ts
β βββ shared/ # Servicios y utilidades compartidas
β β βββ shared.module.ts
β β βββ services/
β β β βββ encryption.service.ts
β β β βββ validation.service.ts
β β β βββ transformation.service.ts
β β β βββ index.ts
β β βββ utils/
β β β βββ crypto.util.ts
β β β βββ date.util.ts
β β β βββ string.util.ts
β β β βββ validation.util.ts
β β β βββ index.ts
β β βββ index.ts
β βββ database/ # Capa de base de datos (si se migra de Firebase)
β βββ migrations/
β βββ seeds/
β βββ schemas/
βββ test/ # Tests organizados por tipo
β βββ unit/
β β βββ services/
β β β βββ auth.service.spec.ts
β β β βββ prompts.service.spec.ts
β β β βββ flows.service.spec.ts
β β βββ controllers/
β β β βββ auth.controller.spec.ts
β β β βββ prompts.controller.spec.ts
β β βββ utils/
β β βββ crypto.util.spec.ts
β βββ integration/
β β βββ auth/
β β β βββ auth.integration.spec.ts
β β βββ prompts/
β β β βββ prompts.integration.spec.ts
β β βββ flows/
β β βββ flows.integration.spec.ts
β βββ e2e/
β β βββ app.e2e-spec.ts
β β βββ auth.e2e-spec.ts
β β βββ prompts.e2e-spec.ts
β βββ fixtures/
β β βββ users.fixture.ts
β β βββ prompts.fixture.ts
β β βββ flows.fixture.ts
β βββ utils/
β βββ test-setup.ts
β βββ test-helpers.ts
βββ scripts/ # Scripts de utilidad
β βββ seed.ts # Seed de base de datos
β βββ generate-api-docs.ts
β βββ check-env.ts
β βββ migrate.ts
βββ docs/ # DocumentaciΓ³n
βββ api/
β βββ swagger.yaml
β βββ postman-collection.json
βββ architecture/
β βββ diagrams/
β βββ decisions/
βββ guides/
βββ development.md
βββ deployment.md