Back to Skills

koan-relationships

sylin-org
Updated 7 days ago
13 views
2
1
2
View on GitHub
Othergeneral

About

This skill provides entity relationship patterns for Claude Code using foreign keys with navigation helpers. It focuses on batch loading to prevent N+1 queries without requiring ORM configuration. Use it for implementing efficient one-to-many and other relationships between entities.

Documentation

Koan Relationships

Core Principle

Relationships use foreign keys + navigation helpers. Batch load to prevent N+1 queries. No ORM mapping configuration needed.

Relationship Patterns

One-to-Many

public class User : Entity<User>
{
    public string Name { get; set; } = "";

    public Task<List<Todo>> GetTodos(CancellationToken ct = default) =>
        Todo.Query(t => t.UserId == Id, ct);
}

public class Todo : Entity<Todo>
{
    public string UserId { get; set; } = "";

    public Task<User?> GetUser(CancellationToken ct = default) =>
        User.Get(UserId, ct);
}

Preventing N+1 Queries

// ❌ WRONG: N+1 query problem
foreach (var todo in todos)
{
    var user = await todo.GetUser(); // N queries!
}

// ✅ CORRECT: Batch load
var userIds = todos.Select(t => t.UserId).Distinct().ToArray();
var users = await User.Get(userIds);
var userDict = users.Where(u => u != null).ToDictionary(u => u!.Id);

foreach (var todo in todos)
{
    var user = userDict[todo.UserId];
}

Optional Relationships

public class Todo : Entity<Todo>
{
    public string? CategoryId { get; set; }

    public Task<Category?> GetCategory(CancellationToken ct = default) =>
        string.IsNullOrEmpty(CategoryId) ? Task.FromResult<Category?>(null)
            : Category.Get(CategoryId, ct);
}

Hierarchical (Parent-Child)

public class TodoItem : Entity<TodoItem>
{
    public string TodoId { get; set; } = "";
    public int SortOrder { get; set; }

    public Task<Todo?> GetParentTodo(CancellationToken ct = default) =>
        Todo.Get(TodoId, ct);
}

public class Todo : Entity<Todo>
{
    public Task<List<TodoItem>> GetItems(CancellationToken ct = default) =>
        TodoItem.Query(i => i.TodoId == Id, ct)
            .ContinueWith(t => t.Result.OrderBy(i => i.SortOrder).ToList());
}

When This Skill Applies

  • ✅ Complex data relationships
  • ✅ Navigation patterns
  • ✅ Performance optimization (N+1)
  • ✅ Hierarchical data
  • ✅ Optional relationships

Reference Documentation

  • Example Code: .claude/skills/entity-first/examples/entity-relationships.cs
  • Sample: samples/S1.Web/README.md (Relationship demo)

Quick Install

/plugin add https://github.com/sylin-org/koan-framework/tree/main/relationships

Copy and paste this command in Claude Code to install this skill

GitHub 仓库

sylin-org/koan-framework
Path: .claude/skills/relationships

Related Skills

subagent-driven-development

Development

This skill executes implementation plans by dispatching a fresh subagent for each independent task, with code review between tasks. It enables fast iteration while maintaining quality gates through this review process. Use it when working on mostly independent tasks within the same session to ensure continuous progress with built-in quality checks.

View skill

algorithmic-art

Meta

This Claude Skill creates original algorithmic art using p5.js with seeded randomness and interactive parameters. It generates .md files for algorithmic philosophies, plus .html and .js files for interactive generative art implementations. Use it when developers need to create flow fields, particle systems, or other computational art while avoiding copyright issues.

View skill

executing-plans

Design

Use the executing-plans skill when you have a complete implementation plan to execute in controlled batches with review checkpoints. It loads and critically reviews the plan, then executes tasks in small batches (default 3 tasks) while reporting progress between each batch for architect review. This ensures systematic implementation with built-in quality control checkpoints.

View skill

cost-optimization

Other

This Claude Skill helps developers optimize cloud costs through resource rightsizing, tagging strategies, and spending analysis. It provides a framework for reducing cloud expenses and implementing cost governance across AWS, Azure, and GCP. Use it when you need to analyze infrastructure costs, right-size resources, or meet budget constraints.

View skill