Repository Pattern
ELI5 — The Vibe Check
Repository Pattern puts a layer between your business logic and your database, so your business code never writes SQL directly. It's like having a librarian — you ask for 'all users over 30' and the librarian goes and finds them. You don't care how the books are organized.
Real Talk
The Repository pattern mediates between the domain and data mapping layers, acting like an in-memory collection of domain objects. It centralizes data access logic, providing a clean API for querying and persisting domain objects while hiding database implementation details.
Show Me The Code
interface UserRepository {
findById(id: string): Promise<User | null>;
findByEmail(email: string): Promise<User | null>;
save(user: User): Promise<User>;
delete(id: string): Promise<void>;
}
When You'll Hear This
"Business logic should go through the repository, not raw SQL." / "The repository pattern makes switching databases trivial."
Related Terms
Clean Architecture
Clean Architecture is like an onion with strict rules: the inner layers (your core business logic) have absolutely no idea the outer layers (databases, API...
Dependency Injection
Instead of your UserService creating its own DatabaseConnection (tight coupling), you pass the database in from outside: new UserService(db).
Design Pattern
Design patterns are like recipe cards for solving common coding problems.
Hexagonal Architecture
Hexagonal Architecture (aka Ports and Adapters) treats your app like a USB hub.