Skip to content

JimmyBastos/hono-domain-driven-design

Repository files navigation

Honor API - Domain Driven Architecture

A clean, scalable API built with Hono, Drizzle ORM, PostgreSQL, and Domain-Driven Design principles.

🏗️ Architecture Overview

This project follows a layered domain-driven architecture with clear separation of concerns:

src/
├── domain/           # Domain layer (business logic)
│   ├── entities/     # Domain entities with business rules
│   ├── repositories/ # Repository interfaces
│   └── services/     # Domain services
├── infrastructure/   # Infrastructure layer (external concerns)
│   ├── repositories/ # Repository implementations
│   └── container/    # Dependency injection container
├── presentation/     # Presentation layer (HTTP)
│   ├── controllers/  # HTTP controllers
│   ├── routes/       # Route definitions
│   ├── middleware/   # HTTP middleware
│   └── validations/  # Request/response validation schemas
├── database/         # Database layer
│   ├── schema.ts     # Drizzle schema definitions
│   └── connection.ts # Database connection management
├── app.ts           # Application setup and middleware
└── server.ts        # Server entry point

✨ Features

  • Domain-Driven Design: Clear separation between domain logic and infrastructure
  • Type Safety: Full TypeScript support with Zod validation
  • Database: PostgreSQL with Drizzle ORM for type-safe database operations
  • HTTP Framework: Hono for fast, lightweight HTTP handling
  • Dependency Injection: Clean dependency management with tsyringe
  • Validation: Request/response validation with Zod schemas
  • Error Handling: Centralized error handling middleware
  • Database Migrations: Automated schema migrations with Drizzle Kit

🚀 Getting Started

Prerequisites

  • Node.js 18+
  • PostgreSQL database
  • Yarn or npm

Environment Setup

Copy the example environment file and configure it:

cp env.example .env

The .env file should contain:

DATABASE_URL=postgres://postgres:postgres@localhost:5432/honor
PORT=3000
NODE_ENV=development

Installation

Option 1: Using Docker Compose (Recommended)

# Start PostgreSQL database
docker-compose up -d postgres

# Wait for database to be ready, then install dependencies
yarn install

# Generate database schema
yarn db:generate

# Run migrations
yarn db:migrate

# Start development server
yarn dev

Option 2: Local PostgreSQL

# Install dependencies
yarn install

# Generate database schema
yarn db:generate

# Run migrations
yarn db:migrate

# Start development server
yarn dev

The API will be available at http://localhost:3000

🛠️ Development

Available Scripts

# Development
yarn dev          # Start development server with hot reload
yarn build        # Build for production
yarn start        # Start production server

# Database
yarn db:generate  # Generate migrations from schema changes
yarn db:migrate   # Apply migrations to database
yarn db:studio    # Open Drizzle Studio (database GUI)

# Docker
docker-compose up -d postgres     # Start PostgreSQL database
docker-compose up -d drizzle-studio  # Start Drizzle Studio (with studio profile)
docker-compose down               # Stop all services
docker-compose down -v            # Stop and remove volumes

Database Commands

# Generate migrations from schema changes
yarn db:generate

# Apply migrations to database
yarn db:migrate

# Open Drizzle Studio (database GUI)
yarn db:studio

Docker Commands

# Start PostgreSQL database
docker-compose up -d postgres

# Start Drizzle Studio (requires postgres to be running)
docker-compose --profile studio up -d drizzle-studio

# View logs
docker-compose logs postgres

# Stop all services
docker-compose down

# Stop and remove volumes (will delete all data)
docker-compose down -v

Access Points:

  • PostgreSQL: localhost:5432
  • Drizzle Studio: http://localhost:4983

🛡️ Error Handling

The API uses centralized error handling with consistent error responses:

{
  "success": false,
  "error": "Error message",
  "statusCode": 400
}

🧪 Testing

# Run tests (when implemented)
yarn test

# Run tests in watch mode
yarn test:watch

# Run tests with coverage
yarn test:coverage

🚀 Deployment

Production Build

# Build the application
yarn build

# Start production server
yarn start

Environment Variables

Variable Description Default
DATABASE_URL PostgreSQL connection string postgres://postgres:postgres@localhost:5432/honor
PORT Server port 3000
NODE_ENV Environment mode development

🛠️ Technologies Used

  • Hono: Fast, lightweight web framework
  • Drizzle ORM: Type-safe database ORM
  • PostgreSQL: Robust relational database
  • TypeScript: Type-safe JavaScript
  • Zod: Schema validation
  • tsyringe: Dependency injection container
  • tsx: TypeScript execution environment

📄 License

MIT

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📞 Support

For support and questions, please open an issue in the GitHub repository.

About

A clean, scalable API built with Hono, Drizzle ORM, PostgreSQL, and Domain-Driven Design principles.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published