Skip to content
generate-repository logo

Generate Repository

generate-repository

Generate repository class for SQLite data access with CRUD methods, row mapping, and TypeScript types. Use when creating new database tables or data access layers.

SKILL.md

Full skill instructions

Generate Repository

Generate a repository class for SQLite data access following the repository pattern.

Usage

When user requests to create a repository, ask for:

  1. Entity name (e.g., "WaterLog", "SleepLog", "WeightTracking")
  2. Table name (snake_case, e.g., "water_logs", "sleep_logs")
  3. Fields and their types (with database column names)
  4. Which fields are JSON (arrays, objects, etc.)
  5. Whether to include getByDate() method

Implementation Pattern

Based on src/​lib/​database/​repositories/​mealLogRepository.ts pattern.

File Structure

Create file: src/​lib/​database/​repositories/​{entityName}Repository.ts

import { getDatabase } from '../​connection';
import type { EntityType } from '@/​lib/​types/​health';
import { v4 as uuidv4 } from 'uuid';

export class EntityRepository {
  private db = getDatabase();

  addEntity(data: Omit<EntityType, 'id' | 'createdAt'>): EntityType {
    const id = uuidv4();
    const createdAt = new Date().toISOString();
    const newEntity = { ...data, id, createdAt };

    const stmt = this.db.prepare(`
      INSERT INTO table_name (id, field1, field2, created_at)
      VALUES (?, ?, ?, ?)
    `);

    stmt.run(
      newEntity.id,
      newEntity.field1,
      JSON.stringify(newEntity.field2), // for JSON fields
      newEntity.createdAt
    );

    return newEntity;
  }

  getEntitiesByDate(date: string): EntityType[] {
    const stmt = this.db.prepare('SELECT * FROM table_name WHERE date = ?');
    const rows = stmt.all(date) as any[];

    return rows.map((row) => ({
      id: row.id,
      date: row.date,
      field1: row.field_1, // snake_case → camelCase
      field2: JSON.parse(row.field_2), // JSON fields
      createdAt: row.created_at,
    }));
  }

  getEntityById(id: string): EntityType | null {
    const stmt = this.db.prepare('SELECT * FROM table_name WHERE id = ?');
    const row = stmt.get(id) as any;

    if (!row) return null;

    return {
      id: row.id,
      date: row.date,
      field1: row.field_1,
      field2: JSON.parse(row.field_2),
      createdAt: row.created_at,
    };
  }

  getAllEntities(): EntityType[] {
    const stmt = this.db.prepare('SELECT * FROM table_name');
    const rows = stmt.all() as any[];

    return rows.map((row) => ({
      id: row.id,
      date: row.date,
      field1: row.field_1,
      field2: JSON.parse(row.field_2),
      createdAt: row.created_at,
    }));
  }

  updateEntity(id: string, updates: Partial<EntityType>): void {
    const stmt = this.db.prepare('SELECT * FROM table_name WHERE id = ?');
    const current = stmt.get(id) as any;
    if (!current) throw new Error('Entity not found');

    const updated = {
      ...current,
      ...updates,
      field_2: JSON.stringify(updates.field2 || JSON.parse(current.field_2)),
    };

    const updateStmt = this.db.prepare(`
      UPDATE table_name SET
        field_1 = ?,
        field_2 = ?
      WHERE id = ?
    `);

    updateStmt.run(updated.field_1, updated.field_2, id);
  }

  deleteEntity(id: string): void {
    const stmt = this.db.prepare('DELETE FROM table_name WHERE id = ?');
    stmt.run(id);
  }
}

Key Conventions

  • Class name: {Entity}Repository (PascalCase)
  • Private db property via getDatabase()
  • Use uuid() for IDs, ISO strings for timestamps
  • Throw errors with descriptive messages (e.g., 'Entity not found')
  • Map DB columns (snake_case) to TS props (camelCase)
  • JSON fields use JSON.stringify() on write, JSON.parse() on read
  • Date-based queries use WHERE date = ? pattern
  • Include both getByDate and getById methods when applicable

Steps

  1. Ask user for entity name, table name, fields, and JSON fields
  2. Create file: src/​lib/​database/​repositories/​{entityName}Repository.ts
  3. Generate class with private db connection
  4. Generate CRUD methods (add, get, update, delete)
  5. Add row mapping for snake_case → camelCase conversion
  6. Handle JSON fields with JSON.stringify/​parse
  7. Export class for use in API routes

Implementation Checklist

  • Repository class properly exported
  • CRUD methods implemented
  • Row mapping handles snake_case to camelCase
  • JSON fields properly serialized
  • Error handling for not found cases
  • Timestamps use ISO format
  • UUIDs generated for new records

More Other skills

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View

Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.

179.7K 100.1K
View

Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.

179.7K 89.5K
View

A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill whenever asked to write some sort of internal communications (status reports, leadership updates, 3P updates, company newsletters, FAQs, incident reports, project updates, etc.).

179.7K 76.1K
View

Fundamental design principles for writing maintainable, testable Python code.

40.2K 21.9K
View

Modern responsive layouts using container queries, fluid typography, CSS Grid, and mobile-first strategies.

40.2K 20K
View

Generate Playwright tests from scenarios using interactive browser exploration and validation.

39.7K 18.2K
View

Modern React state management with Redux Toolkit, Zustand, Jotai, and React Query for every state category.

40.2K 14.7K
View

GDPR-compliant data handling with consent management, data subject rights, and privacy controls.

40.2K 14.3K
View

Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.

40.2K 13.5K
View

Detailed multi-file refactor planning with safety checks and rollback guidance.

39.7K 13K
View