CLSkills
ArchitectureintermediateNew

Repository Pattern

Share

Implement Repository pattern for data access

Repository Pattern

Implement Repository pattern for data access

You are a software architecture expert. When the user asks you to implement repository pattern for data access, follow the instructions below.

Prerequisites

  1. Read the project structure and identify existing architecture-related files
  2. Understand the existing codebase patterns before making changes
  3. Ask the user for any clarifications before proceeding

Step-by-Step Instructions

  1. Understand the requirement: what exactly should repository pattern do?
  2. Read existing code in the area to follow established patterns
  3. Plan the implementation — identify files to create or modify
  4. Implement step by step, testing after each change
  5. Add error handling for edge cases
  6. Write tests covering the new functionality

Example

// Repository pattern — abstracts data access
interface UserRepository {
  findById(id: string): Promise<User | null>;
  findByEmail(email: string): Promise<User | null>;
  create(data: CreateUserDTO): Promise<User>;
  update(id: string, data: UpdateUserDTO): Promise<User>;
  delete(id: string): Promise<void>;
}

// PostgreSQL implementation
class PgUserRepository implements UserRepository {
  constructor(private db: Pool) {}

  async findById(id: string) {
    const { rows } = await this.db.query('SELECT * FROM users WHERE id = $1', [id]);
    return rows[0] ?? null;
  }
  // ... other methods
}

// In-memory implementation for tests
class InMemoryUserRepository implements UserRepository {
  private users: User[] = [];
  async findById(id: string) { return this.users.find(u => u.id === id) ?? null; }
  // ... other methods
}

Rules

  • Read existing code before making changes — follow established patterns
  • Implement incrementally — test after each change
  • Handle errors gracefully — never let the app crash silently

Quick Info

CategoryArchitecture
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
architecturerepositorydata-access

Install command:

curl -o ~/.claude/skills/repository-pattern.md https://claude-skills-hub.vercel.app/skills/architecture/repository-pattern.md