Deprecated: Function get_magic_quotes_gpc() is deprecated in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 99

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 619

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1169

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176

Warning: Cannot modify header information - headers already sent by (output started at /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php:99) in /hermes/walnacweb04/walnacweb04ab/b2791/pow.jasaeld/htdocs/De1337/nothing/index.php on line 1176
10BC0 GitHub - thaitype/thaitype-stack-mongodb-template: Type-Safe Next.js Stack using Simple Clean Architecture with MongoDB Template For AI Friendly
Nothing Special   »   [go: up one dir, main page]

Skip to content

thaitype/thaitype-stack-mongodb-template

Repository files navigation

Todo App Template - Next.js Enterprise Stack

A production-ready Todo Application Template built with Next.js 15, implementing enterprise-grade patterns and modern development practices. This template serves as a robust foundation for building scalable full-stack applications with authentication, CRUD operations, and real-time updates.

✨ Features

  • πŸ” Authentication System - Better Auth with email/password
  • πŸ“ Todo Management - Full CRUD operations with real-time updates
  • 🎨 Modern UI - Mantine components with responsive design
  • πŸ—οΈ Enterprise Architecture - Entity-based repository pattern
  • πŸ”’ Type Safety - Full TypeScript with tRPC API layer
  • πŸ“Š Database - MongoDB with audit logging via Monguard
  • ⚑ Performance - Next.js 15 with App Router and React 19
  • 🎯 Production Ready - ESLint, Prettier, structured logging

Screenshot

πŸš€ Tech Stack

Core Framework

Backend & API

  • tRPC - End-to-end typesafe APIs
  • MongoDB - Document database
  • Monguard - MongoDB wrapper with audit logging
  • Better Auth - Modern authentication library
  • Zod - TypeScript-first schema validation

Frontend 8000 & UI

Development Tools

πŸ—οΈ Architecture

This template implements an Entity-Based Repository Architecture with strict separation of concerns:

src/
β”œβ”€β”€ app/                    # Next.js App Router
β”‚   β”œβ”€β”€ _components/        # UI components
β”‚   β”œβ”€β”€ api/trpc/          # tRPC API routes  
β”‚   └── (auth pages)       # Login, register, profile
β”œβ”€β”€ server/
β”‚   β”œβ”€β”€ api/               # tRPC routers and procedures
β”‚   β”œβ”€β”€ domain/            # Business domain layer
β”‚   β”‚   β”œβ”€β”€ models/        # Domain interfaces (string IDs)
β”‚   β”‚   └── repositories/  # Repository interfaces
β”‚   β”œβ”€β”€ infrastructure/    # Data access layer
β”‚   β”‚   β”œβ”€β”€ entities/      # Database entities (ObjectIds)
β”‚   β”‚   └── repositories/  # Repository implementations
β”‚   β”œβ”€β”€ services/          # Business logic (database-agnostic)
β”‚   └── lib/               # Shared utilities
└── trpc/                  # Client-side tRPC setup

Key Patterns

  • Entity-First Design: All types derive from database entities
  • Domain-Driven Services: Business logic with string-based IDs
  • Type-Safe Validation: Zod schemas with matches<T>() utility
  • Audit Logging: Automatic change tracking via Monguard
  • Structured Logging: Request tracing and error context

🚦 Quick Start

Prerequisites

  • Node.js 18+
  • pnpm (recommended) or npm
  • MongoDB instance (local or MongoDB Atlas)

1. Clone and Install

git clone <repository-url> my-todo-app
cd my-todo-app
pnpm install

2. Environment Setup

cp .env.example .env

Configure your .env file:

# Database
MONGODB_URI="mongodb://localhost:27017"
DB_NAME="todo_app"

# Authentication
BETTER_AUTH_SECRET="your-secret-key-here"
NEXT_PUBLIC_BETTER_AUTH_URL="http://localhost:3000/api/auth"

# App Configuration  
NODE_ENV="development"
PORT="3000"

3. Start Development

pnpm dev

Visit http://localhost:3000 to see your app!

πŸ“‹ Development Commands

# Development
pnpm dev              # Start development server with Turbo
pnpm build            # Build for production
pnpm start            # Start production server
pnpm preview          # Build and start production server

# Code Quality
pnpm lint             # Run ESLint
pnpm lint:fix         # Fix ESLint errors
pnpm typecheck        # Run TypeScript check
pnpm check            # Run lint + typecheck together

# Formatting
pnpm format:check     # Check Prettier formatting
pnpm format:write     # Apply Prettier formatting

πŸ›οΈ Design Patterns

This template follows enterprise-grade design patterns documented in docs/entity-architecture.md.

Core Principles

  1. Single Source of Truth - Types derive from database entities
  2. Dedicated Methods - Explicit operations over generic CRUD
  3. Repository Validation - Single validation point at data boundary
  4. Service Abstraction - Database-agnostic business logic

Example: Todo Repository Pattern

// Domain Model (strings)
interface Todo {
  id: string;
  title: string;
  completed: boolean;
  userId: string;
}

// Database Entity (ObjectIds)  
interface DbTodoEntity {
  _id: ObjectId;
  title: string;
  completed: boolean;
  userId: ObjectId;
}

// Repository Interface (domain types)
interface ITodoRepository {
  create(input: TodoCreateData, context: RepositoryContext): Promise<Todo>;
  updateContent(id: string, input: TodoContentUpdate, userId: string): Promise<void>;
  updateStatus(id: string, input: TodoStatusUpdate, userId: string): Promise<void>;
}

// Service Layer (business logic)
class TodoService {
  async createTodo(userId: string, request: CreateTodoRequest): Promise<Todo> {
    // Business validation and logic
    const context = createRepositoryContext(userId);
    return this.todoRepository.create(request, context);
  }
}

πŸ” Authentication

Built-in authentication system with Better Auth:

  • Email/Password authentication
  • Session Management with secure cookies
  • Protected Routes with middleware
  • User Context propagation throughout the app

Usage Example

// Client-side
import { useSession, signIn, signOut } from '~/lib/auth-client';

function MyComponent() {
  const { data: session, isPending } = useSession();
  
  if (session?.user) {
    return <div>Hello, {session.user.name}!</div>;
  }
  
  return <button onClick={() => signIn.email(credentials)}>Sign In</button>;
}

πŸ“Š Database Schema

User Collection

interface DbUserEntity extends AuditableDocument {
  name: string;
  email: string;
  emailVerified: boolean;
  roles: string[];
}

Todo Collection

interface DbTodoEntity extends AuditableDocument {
  title: string;
  description?: string;
  completed: boolean;
  userId: ObjectId;
}

All entities include automatic audit fields via Monguard:

  • createdAt, updatedAt, deletedAt
  • createdBy, updatedBy, deletedBy
  • Version tracking with __v

πŸš€ Deployment

Production Build

pnpm build

Environment Variables

Set these in your production environment:

NODE_ENV="production"
MONGODB_URI="mongodb+srv://..."
BETTER_AUTH_SECRET="secure-production-secret"
NEXT_PUBLIC_BETTER_AUTH_URL="https://yourdomain.com/api/auth"

Hosting Options

Vercel (Recommended)

pnpm dlx vercel

Railway

pnpm dlx @railway/cli deploy

Docker

docker build -t todo-app .
docker run -p 3000:3000 todo-app

πŸ› οΈ Customization

Adding New Features

  1. Define Domain Model in ~/server/domain/models/
  2. Create Database Entity in ~/server/infrastructure/entities/
  3. Build Repository Interface in ~/server/domain/repositories/
  4. Implement Repository in ~/server/infrastructure/repositories/
  5. Create Service Layer in ~/server/services/
  6. Add tRPC Router in ~/server/api/routers/
  7. Build UI Components in ~/app/_components/

Template Structure

β”œβ”€β”€ Authentication System βœ…
β”œβ”€β”€ Todo CRUD Operations βœ…  
β”œβ”€β”€ Real-time Updates βœ…
β”œβ”€β”€ Responsive UI βœ…
β”œβ”€β”€ Production Logging βœ…
β”œβ”€β”€ Type Safety βœ…
└── Audit Trail βœ…

πŸ“š Documentation

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes following the established patterns
  4. Run quality checks: pnpm check
  5. Commit your changes: git commit -m 'Add amazing feature'
  6. Push to the branch: git push origin feature/amazing-feature
  7. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments


Made with ❀️ for modern web development

This template represents production-ready patterns and can be used as a foundation for enterprise applications.

About

Type-Safe Next.js Stack using Simple Clean Architecture with MongoDB Template For AI Friendly

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0