Cursor Rules TemplatesCursor Rules Template

NestJS TypeORM MySQL CQRS Cursor Rules Template for Cursor AI

Cursor Rules Template for NestJS with TypeORM and MySQL CQRS, providing a copyable .cursorrules configuration to guide Cursor AI in backend development.

cursor-rules-templatenestjstypeormmysqlcqrscursor-aibackendtypescripteslintlintingsecurity

Target User

Backend developers using NestJS, TypeORM, MySQL, and CQRS who want Cursor AI integration

Use Cases

  • API endpoints powered by CQRS in NestJS
  • TypeORM entities mapped to MySQL
  • AI-assisted code generation and review using Cursor AI

Markdown Template

NestJS TypeORM MySQL CQRS Cursor Rules Template for Cursor AI

# Cursor Rules Template: NestJS TypeORM MySQL CQRS
framework: nestjs
stack: typeorm/mysql/cqrs
role: Backend Engineer
context: >
  You are a NestJS backend engineer building a CQRS API with TypeORM and MySQL. Provide copyable rules for Cursor AI to follow in coding and architecture.
codeStyle: eslint && prettier
linting: enabled
architecture:
  root: src
  modules:
    - app
    - domain
    - application
    - infrastructure
    - config
auth:
  strategy: jwt
  guard: jwt-auth.guard
database:
  orm: typeorm
  dialect: mysql
  migrations: true
  host: ${DB_HOST}
patterns:
  entities: src/domain/entities
  repositories: src/infrastructure/repositories
  cqrs:
    commands: src/application/commands
    queries: src/application/queries
    handlers: src/application/handlers
security:
  secrets: env
  encryption: bcrypt
  httpOnlyCookies: true
testing:
  unit: jest
  integration: jest --config test/integration
  e2e: none
prohibited:
  - active_record
  - string_concat_sql
  - hard_code_credentials
  - global_mutable_state
  - bypass_of_guards
  - log_secrets

Overview

This Cursor rules template configures a NestJS, TypeORM, MySQL, and CQRS stack for Cursor AI. It provides a copyable .cursorrules configuration you can paste into your project root to guide AI-assisted development, coding standards, and security practices.

When to Use These Cursor Rules

  • When your backend uses NestJS with TypeORM and MySQL in a CQRS pattern
  • When you want to enforce consistent file layout, repository patterns, and testing workflows
  • When you need secure authentication, role-based access, and safe data access with AI guidance
  • When you require a copyable configuration that your team can drop into any NestJS project

Copyable .cursorrules Configuration

# Cursor Rules Template: NestJS TypeORM MySQL CQRS
framework: nestjs
stack: typeorm/mysql/cqrs
role: Backend Engineer
context: >
  You are a NestJS backend engineer building a CQRS API with TypeORM and MySQL. Provide copyable rules for Cursor AI to follow in coding and architecture.
codeStyle: eslint && prettier
linting: enabled
architecture:
  root: src
  modules:
    - app
    - domain
    - application
    - infrastructure
    - config
auth:
  strategy: jwt
  guard: jwt-auth.guard
database:
  orm: typeorm
  dialect: mysql
  migrations: true
  host: ${DB_HOST}
patterns:
  entities: src/domain/entities
  repositories: src/infrastructure/repositories
  cqrs:
    commands: src/application/commands
    queries: src/application/queries
    handlers: src/application/handlers
security:
  secrets: env
  encryption: bcrypt
  httpOnlyCookies: true
testing:
  unit: jest
  integration: jest --config test/integration
  e2e: none
prohibited:
  - active_record
  - string_concat_sql
  - hard_code_credentials
  - global_mutable_state
  - bypass_of_guards
  - log_secrets

Recommended Project Structure

src/
  main.ts
  app/
    controllers/
    modules/
  domain/
    entities/
  application/
    commands/
    queries/
    handlers/
  infrastructure/
    config/
    database/
      migrations/
      entities/
  config/
  tests/

Core Engineering Principles

  • Clear separation of concerns and boundaries between domain, application, and infrastructure
  • Explicit CQRS boundaries with commands and queries
  • Strong typing using TypeScript and DTO-driven validation
  • Safe AI-assisted development with Cursor AI rules and guardrails
  • Security by default: JWT, protected routes, and encrypted secrets
  • Testability through unit, integration, and end-to-end checks

Code Construction Rules

  • Use DTOs for all external inputs and validate with class-validator
  • Follow the CQRS pattern: separate commands and queries with dedicated handlers
  • Use TypeORM with repositories, not active-record patterns
  • Keep entities pure and avoid business logic in persistence models
  • Prefer dependency injection and avoid static singletons for services
  • Do not bypass type safety with any or dynamic SQL assembly
  • Do not use raw SQL concatenation; use query builders or parameters
  • Do not hard-code credentials; rely on env-based configuration

Security and Production Rules

  • Store secrets in environment variables and secret managers
  • Hash passwords with bcrypt and compare with stable timing-safe methods
  • Implement JWT with short lifetimes and refresh tokens; rotate keys periodically
  • Apply rate limiting and input validation to guard against abuse
  • Use TLS, secure cookies, and proper CORS configuration in production

Testing Checklist

  • Unit tests for every command, query, and handler
  • Integration tests for repository interactions with a test database
  • End-to-end tests for common API flows using CQRS
  • Lint and type-check in CI; enforce 100% type safety
  • Review test data and avoid leaking real credentials

Common Mistakes to Avoid

  • Mixing domain logic into persistence models (anti-pattern)
  • Skipping validation or using unsafe string concatenation in queries
  • Ignoring DI patterns and relying on global state or singletons
  • Weak error handling and insufficient logging for production issues
  • Failing to isolate tests from real database instances

FAQ

What is the Cursor Rules Template for this stack?

The Cursor Rules Template for NestJS with TypeORM, MySQL, and CQRS provides a ready-to-paste copyable .cursorrules configuration. It enforces a CQRS-driven architecture, secure authentication, typed DTOs, and a clean project structure to guide Cursor AI’s code generation and analysis specifically for this stack.

How do I integrate the .cursorrules file with a NestJS CQRS project?

Place the pasted .cursorrules configuration at the project root. Ensure the directory hints in the rules align with your NestJS module structure (application, domain, infrastructure). Cursor AI will then suggest patterns consistent with TypeORM repositories, CQRS handlers, and MySQL data access without bypassing DI and guards.

Which security rules are enforced by this template?

The template enforces JWT-based authentication, environment-based secrets, bcrypt password hashing, secure cookie usage, and guarded routes. It discourages exposing secrets, disallows unsafe SQL concatenation, and requires input validation through DTOs and class-validator before persistence.

Can I adapt this template to other databases or stacks?

Yes, but you should adjust the ORM configuration and CQRS boundaries to align with the target database. The Cursor Rules Template is stack-specific here; for other databases or ORMs, provide a corresponding adaptation to the code structure, tests, and security patterns while preserving Cursor AI safety practices.

What does the recommended project structure enforce for CQRS?

It enforces a clear separation between commands and queries, dedicated handlers, and repository-based data access. It also delineates domain, application, and infrastructure layers to support scalable, testable backend services in NestJS with TypeORM and MySQL.