Repository Structure
The repository is organized as a Yarn monorepo:Architecture Overview
graph TB
A[Web/Desktop/Mobile] —> B[Frontend Core]
B —> C[BlockSuite Editor]
B —> D[GraphQL Client]
D —> E[Backend Server]
E —> F[PostgreSQL + pgvector]
E —> G[Redis]
E —> H[Storage S3/FS]
B —> I[Y.js CRDT]
I —> J[IndexedDB]
I —> E
Frontend Architecture
Package Structure
Core Technologies
- React 19: UI framework
- TypeScript: Type-safe development
- Jotai: State management
- Vite: Build tool
- Vanilla Extract: Type-safe CSS
Application Flow
-
Bootstrap (
packages/frontend/core/src/bootstrap/)- Initialize environment
- Set up error tracking (Sentry)
- Load polyfills
- Start the app
-
Routing (
packages/frontend/routes/)- React Router v6
- Lazy-loaded routes
- Protected routes
-
State Management
- Jotai atoms for global state
- React Context for component state
- Framework service for complex state
-
Data Layer
- GraphQL for server communication
- Y.js CRDT for documents
- IndexedDB for local storage
Backend Architecture
Package Structure
Core Technologies
- NestJS: Backend framework
- GraphQL: API layer (Apollo Server)
- Prisma: Database ORM
- PostgreSQL: Primary database (with pgvector)
- Redis: Caching and queues
- Socket.io: WebSocket for real-time sync
- BullMQ: Background jobs
Request Flow
- HTTP Request → Express middleware
- Authentication → Session/JWT validation
- GraphQL → Resolver execution
- Authorization → Permission checks
- Business Logic → Service layer
- Database → Prisma ORM
- Response → GraphQL formatter
BlockSuite Editor
BlockSuite is the editor framework powering AFFiNE:Key Concepts
- Block: Smallest content unit (paragraph, image, etc.)
- Page: Collection of blocks
- Store: Y.js document store
- Extension: Plugin for blocks/UI/functionality
Common Packages
Shared code between frontend and backend:Data Flow
Document Editing Flow
Authentication Flow
Storage Architecture
Client-Side Storage
- IndexedDB: Document data (Y.js updates)
- LocalStorage: User preferences
- CacheStorage: App assets (Service Worker)
Server-Side Storage
- PostgreSQL: Metadata, users, permissions
- Redis: Sessions, cache, job queues
- S3/Filesystem: Blob storage (images, files)
Real-Time Sync
WebSocket Protocol
AFFiNE uses Socket.io for real-time sync:CRDT Conflict Resolution
Y.js CRDT automatically merges concurrent edits:- No locking required
- Eventually consistent
- Deterministic merge
- Offline-capable
Build System
Development
Production Build
Electron Build
Testing Architecture
Test Types
- Unit Tests: Vitest
- E2E Tests: Playwright
- Integration Tests: Ava (backend)
- Component Tests: React Testing Library
Deployment
Web Deployment
- Build static assets
- Upload to CDN/static hosting
- Configure reverse proxy (Nginx/Caddy)
- Set up SSL/TLS
Desktop Distribution
- Build native app
- Code sign (macOS/Windows)
- Notarize (macOS)
- Upload to release servers
- Auto-update via electron-updater
Self-Hosted
- Pull Docker image
- Configure environment
- Run with Docker Compose
- Set up reverse proxy
- Configure SSL