Architecture & Development

The boilerplate implements a strict 3-layer architecture that ensures separation of concerns, maintainability, and scalability. Each layer has specific responsibilities and communicates through well-defined interfaces.
Perfect for: Building maintainable, testable, and scalable SaaS applications with clear separation between UI, business logic, and data access.

Architecture Overview

The system follows a unidirectional data flow through three main layers:
3-Layer Architecture:
๐Ÿ–ฅ๏ธ  PRESENTATION LAYER
โ”œโ”€โ”€ React Server Components (RSC)
โ”œโ”€โ”€ Client Components ("use client")
โ”œโ”€โ”€ Server Actions (mutations)
โ””โ”€โ”€ DAL (Data Access Layer with cache)
                โฌ‡๏ธ
โš™๏ธ  SERVICE LAYER
โ”œโ”€โ”€ Business Logic
โ”œโ”€โ”€ Validation (Zod schemas)
โ”œโ”€โ”€ Authorization (CASL RBAC)
โ”œโ”€โ”€ Facades (interfaces)
โ””โ”€โ”€ Interceptors (logging, audit)
                โฌ‡๏ธ
๐Ÿ—„๏ธ  PERSISTENCE LAYER
โ”œโ”€โ”€ Drizzle ORM
โ”œโ”€โ”€ Database Models
โ”œโ”€โ”€ Repositories (DAO pattern)
โ””โ”€โ”€ Type Transformations

Core Architecture Principles

๐Ÿ—๏ธ

Layered Architecture

Strict 3-layer separation

โ€ข Presentation: RSC, Client Components, Server Actions
โ€ข Service: Business logic, validation, authorization
โ€ข Persistence: Drizzle ORM, repositories, models
โ€ข No layer bypassing: Each layer calls only the next
๐Ÿ”„

Unidirectional Flow

Clear data movement

โ€ข Reads: Component โ†’ DAL โ†’ Service โ†’ Repository
โ€ข Writes: Server Action โ†’ Service โ†’ Repository
โ€ข Caching: DAL layer with react-cache
โ€ข Validation: Service layer with Zod schemas
๐Ÿ”

Security & Authorization

RBAC with CASL

โ€ข Authorization: Every service operation checked
โ€ข Validation: Input validation at service boundary
โ€ข Type Safety: Domain types prevent data leaks
โ€ข Audit Trail: Interceptors log all operations
โšก

Performance

Optimized at every layer

โ€ข Server-first: RSC by default
โ€ข Caching: DAL layer with React cache
โ€ข Database: Connection pooling with Drizzle
โ€ข Client: Minimal client-side JavaScript

Example: Complete Data Flow

Here's how a typical user operation flows through all layers:
Get User Profile:
// ๐Ÿ–ฅ๏ธ PRESENTATION (RSC Component)
import { getUserByIdDal } from '@/app/dal/user-dal'

export default async function UserProfile({ userId }: { userId: string }) {
  const user = await getUserByIdDal(userId) // Cached read
  return <div>{user.name}</div>
}

// ๐Ÿ“Š DAL (Data Access Layer)
import { cache } from 'react'
import { getUserByIdService } from '@/services/facades/user-service-facade'

export const getUserByIdDal = cache(async (id: string) => {
  const userModel = await getUserByIdService(id)
  return transformToUserDTO(userModel) // Transform to domain type
})

// โš™๏ธ SERVICE (Business Logic)
export const getUserByIdService = async (id: string) => {
  const parsed = userUuidSchema.safeParse(id) // Validation
  if (!parsed.success) throw new ValidationError()

  const granted = await canReadUser(parsed.data) // Authorization
  if (!granted) throw new AuthorizationError()

  return await getUserByIdDao(parsed.data) // Call repository
}

// ๐Ÿ—„๏ธ REPOSITORY (Data Access)
export async function getUserByIdDao(id: string): Promise<UserModel> {
  return await db.query.users.findFirst({
    where: eq(users.id, id)
  })
}

Key Benefits

โœ…

Maintainability

  • โ€ข Clear separation of concerns
  • โ€ข Easy to locate and fix issues
  • โ€ข Consistent patterns across features
  • โ€ข Self-documenting code structure
โœ…

Testability

  • โ€ข Each layer can be tested independently
  • โ€ข Easy mocking of dependencies
  • โ€ข Business logic isolated in services
  • โ€ข Database operations isolated in repositories
โœ…

Scalability

  • โ€ข Layers can scale independently
  • โ€ข Easy to add new features
  • โ€ข Caching at appropriate layers
  • โ€ข Performance optimizations per layer

Architecture Documentation

Architecture ready! Your application follows enterprise-grade patterns that ensure maintainability, testability, and scalability as your SaaS grows.
    Architecture & Development | ShipSaaS Documentation | ShipSaaS - Launch your SaaS with AI in days