TanStack Start Clean Architecture
September 28, 2025 ยท View on GitHub
A modern boilerplate for building full-stack React applications with TanStack Start, implementing Clean Architecture principles for maintainable, testable, and scalable code.
๐ Tech Stack
Frontend
- React 19.1.1 with TypeScript
- TanStack Start 1.132.19 - Full-stack React framework
- TanStack Router 1.132.19 - Type-safe file-based routing
- Tailwind CSS 4.1.13 - Utility-first CSS framework
- shadcn/ui - High-quality accessible component library
- Radix UI - Primitive components for complex UI
- Lucide React - Beautiful icon library
Backend & Database
- TanStack Start Server Functions - Full-stack capabilities
- Prisma - Modern database toolkit and ORM
- SQLite - Lightweight database
Development Tools
- Vite 7.1.7 - Lightning-fast build tool
- TypeScript 5.9.2 - Type safety and enhanced DX
- ESLint - Code linting with comprehensive presets
- Prettier - Code formatting
- Vitest - Fast unit testing framework
๐ Prerequisites
- Node.js (version 18 or higher)
- pnpm or yarn or pnpm
๐ ๏ธ Installation
-
Clone the repository:
git clone git@github.com:felipestanzani/tanstack-start-ca.git cd tanstack-start-ca -
Install dependencies:
pnpm install -
Set up environment variables:
Create a
.envfile in the root directory with the following content:# Database DATABASE_URL="file:./dev.db" -
Set up the database:
Generate the Prisma client from the schema:
pnpm db:generateCreate the SQLite database and apply the schema:
pnpm db:push
๐ Development
Start the development server:
pnpm dev
The application will be available at http://localhost:3000
Available Scripts
Development
pnpm dev- Start development serverpnpm build- Build for productionpnpm preview- Preview production build
Code Quality
pnpm lint- Run ESLintpnpm format- Format code with Prettier
Testing
pnpm test- Run tests with Vitestpnpm test:run- Run tests oncepnpm test:watch- Run tests in watch modepnpm test:coverage- Run tests with coverage
Database
pnpm db:generate- Generate Prisma clientpnpm db:push- Push schema changes to databasepnpm db:migrate- Create and apply migrationspnpm db:studio- Open Prisma Studio for database management
๐๏ธ Clean Architecture Structure
This project follows Clean Architecture principles with clear separation of concerns and dependency inversion. For detailed architecture documentation, see CLEAN_ARCHITECTURE.md.
tanstack-start-clean-architecture/
โโโ prisma/ # Database schema and migrations
โ โโโ schema.prisma
โโโ src/
โ โโโ core/ # Core Business Logic (Domain + Application Layers)
โ โ โโโ domain/ # ๐ฆ Domain Layer (Innermost)
โ โ โ โโโ entities/ # Business entities with domain logic
โ โ โ โ โโโ base.entity.ts # Base entity with common properties
โ โ โ โ โโโ counter.entity.ts # Counter business entity
โ โ โ โ โโโ index.ts # Domain entities barrel export
โ โ โ โโโ events/ # Domain events
โ โ โ โ โโโ index.ts
โ โ โ โโโ repositories/ # Repository interfaces (contracts)
โ โ โ โ โโโ counter.repository.ts
โ โ โ โ โโโ index.ts
โ โ โ โโโ value-objects/ # Domain value objects
โ โ โ โโโ index.ts
โ โ โโโ application/ # ๐จ Application Layer
โ โ โโโ dtos/ # Data Transfer Objects
โ โ โ โโโ index.ts
โ โ โโโ services/ # Application services
โ โ โ โโโ index.ts
โ โ โโโ use-cases/ # Use cases (business operations)
โ โ โโโ counter/
โ โ โ โโโ get-counter.use-case.ts
โ โ โ โโโ increment-counter.use-case.ts
โ โ โ โโโ index.ts
โ โ โโโ index.ts
โ โโโ infrastructure/ # ๐ฉ Infrastructure Layer
โ โ โโโ di/ # Dependency Injection
โ โ โ โโโ container.ts # DI container
โ โ โ โโโ index.ts
โ โ โโโ repositories/ # Repository implementations
โ โ โโโ prisma-counter.repository.ts
โ โ โโโ index.ts
โ โโโ presentation/ # ๐ช Presentation Layer
โ โ โโโ controllers/ # HTTP request handlers
โ โ โโโ counter.controller.ts
โ โ โโโ index.ts
โ โโโ components/ # React UI components
โ โ โโโ theme-provider.tsx
โ โ โโโ ui/ # shadcn/ui components
โ โ โโโ button.tsx
โ โโโ lib/ # Utility functions
โ โ โโโ utils.ts
โ โโโ routes/ # File-based routing (TanStack Router)
โ โ โโโ __root.tsx # Root layout
โ โ โโโ index.tsx # Home page
โ โโโ styles/ # Global styles
โ โ โโโ app.css
โ โโโ tests/ # Test files organized by layer
โ โ โโโ application/
โ โ โ โโโ use-cases/
โ โ โ โโโ get-counter.use-case.test.ts
โ โ โโโ domain/
โ โ โ โโโ entities/
โ โ โ โโโ counter.entity.test.ts
โ โ โโโ setup.ts
โ โโโ router.tsx # Router configuration
โ โโโ routeTree.gen.ts # Generated route tree
โโโ components.json # shadcn/ui configuration
โโโ package.json
โโโ tsconfig.json # TypeScript configuration
โโโ vite.config.ts # Vite configuration
โโโ eslint.config.mjs # ESLint configuration
โโโ CLEAN_ARCHITECTURE.md # Detailed architecture documentation
โโโ README.md
๐งฑ Architecture Layers
๐ฆ Domain Layer (src/core/domain/)
The innermost layer containing pure business logic:
- Entities: Core business objects with domain rules (e.g.,
Counter) - Repository Interfaces: Contracts for data access
- Domain Events: Business events within the domain
- Value Objects: Immutable domain concepts
๐จ Application Layer (src/core/application/)
Orchestrates business operations:
- Use Cases: Application-specific business rules
- DTOs: Data transfer objects for layer communication
- Services: Application services coordinating domain objects
๐ฉ Infrastructure Layer (src/infrastructure/)
Handles external concerns:
- Repository Implementations: Concrete data access implementations
- Dependency Injection: DI container for managing dependencies
- External Services: Third-party integrations
๐ช Presentation Layer (src/presentation/)
User interface and API endpoints:
- Controllers: HTTP request/response handlers using TanStack Start
- Routes: Application routing with TanStack Router
- Components: React UI components
๐งช Testing Strategy
The project includes comprehensive testing organized by architecture layer:
src/tests/
โโโ application/ # Application layer tests
โโโ domain/ # Domain layer tests
โโโ infrastructure/ # Infrastructure layer tests
โโโ presentation/ # Presentation layer tests
Test Types
- Unit Tests: Testing individual components and business logic
- Integration Tests: Testing layer interactions
- End-to-End Tests: Testing complete user workflows
๐๏ธ Database
This project uses SQLite with Prisma for data persistence.
Database Schema
The application uses a simple Counter table with the following structure:
model Counter {
id String @id
value Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Repository Implementation
The counter persistence uses database storage (PrismaCounterRepository):
- Implements the
CounterRepositoryinterface - Uses SQLite for data persistence
- Supports the default counter with automatic creation
- Handles upsert operations for counter updates
- Maintains full compatibility with existing use cases
Note: The Prisma client is generated to the standard location (node_modules/@prisma/client) for better ES module compatibility with Vite/TanStack Start.
โก State Management with TanStack Query
This project uses TanStack Query for all client-server state synchronization and UI data fetching. Benefits include:
- Automatic caching and background updates
- Optimistic UI for instant feedback on mutations
- Error handling and retry logic out of the box
- DevTools for query/mutation debugging
Query Setup
- The
QueryClientis configured insrc/lib/query-client.tsand provided at the root viaQueryClientProviderinsrc/routes/__root.tsx. - React Query DevTools are enabled in development for easy debugging.
Custom Hooks
useCounter()โ Fetches the current counter valueuseIncrementCounter()โ Increments the counter (with optimistic update)
See src/hooks/use-counter.ts for implementation details.
Example Usage (Home Page)
The home page demonstrates:
- Live counter value with auto-refresh
- Increment button with instant UI feedback
- Loading and error states
- Optimistic updates for a snappy user experience
๐ Counter Feature Demo
- Increment the counter with instant feedback
- UI disables button during loading/mutation
- Error messages and retry options for failed requests
- Powered by TanStack Query hooks and server functions
๐ฏ Features
This boilerplate includes:
- โ Clean Architecture implementation
- โ Type-safe routing with TanStack Router
- โ Server-side rendering with TanStack Start
- โ Database integration with Prisma + SQLite
- โ UI components with shadcn/ui
- โ Testing setup with Vitest
- โ Code quality with ESLint and Prettier
- โ Dependency injection container
- โ Example counter feature demonstrating architecture
- โ State management with TanStack Query (caching, mutations, optimistic updates)
- โ React Query DevTools for development
- โ Increment counter with optimistic UI
๐ Learn More
- TanStack Start Documentation
- TanStack Router Documentation
- Clean Architecture by Robert C. Martin
- Prisma Documentation
- shadcn/ui Documentation
๐ค Contributing
Contributions are welcome! Please read our contributing guidelines and submit pull requests for any improvements.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Clean Architecture principles by Robert C. Martin
- TanStack team for amazing React tools
- shadcn for the beautiful UI components
- The open-source community for inspiration and tools