koan-relationships

Eliminate N+1 query problems in Koan Framework using foreign keys and batch loading.

4|3|Updated Aug 18, 2025
One-click install
npx skills add https://github.com/sylin-org/koan-framework --skill koan-relationships
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: koan-relationships
Source: https://github.com/sylin-org/koan-framework/tree/main/.claude/skills/relationships
Command: npx skills add https://github.com/sylin-org/koan-framework --skill koan-relationships

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Managing relationships between entities in traditional ORMs often involves complex mapping configurations and can lead to inefficient N+1 query problems. This Skill simplifies relationship handling in Koan Framework using foreign keys and efficient batch loading.

Core Features & Use Cases

  • Foreign Key-Based Relationships: Define one-to-many, many-to-one, and optional relationships using simple foreign key properties.
  • Navigation Helpers: Add convenient methods directly on entities (e.g., user.GetTodos(), todo.GetUser()) to traverse relationships.
  • Prevent N+1 Queries: Implement efficient batch loading patterns to fetch related entities in a single query, dramatically improving performance for collections.
  • Hierarchical Data: Easily model parent-child relationships and retrieve nested data structures.
  • Use Case: Define a User with many Todo items. Instead of fetching each user's todos individually (N+1), batch load all user IDs, then fetch all associated todos in one optimized query.

Quick Start

To define a one-to-many relationship between User and Todo: public class User : Entity<User> { public string Name { get; set; } = ""; public Task<List<Todo>> GetTodos() => Todo.Query(t => t.UserId == Id); } public class Todo : Entity<Todo> { public string UserId { get; set; } = ""; public Task<User?> GetUser() => User.Get(UserId); } To prevent N+1 queries when loading multiple todos and their users: var userIds = todos.Select(t => t.UserId).Distinct().ToArray(); var users = await User.Get(userIds); // Single query

Frequently Asked Questions about koan-relationships

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent N+1 queries when loading related entities in a relational database?

N+1 queries occur when you fetch a parent entity then query for each child individually. Batch loading solves this by fetching all related entities in a single query using foreign keys, dramatically reducing database round trips and improving performance.

How do I define one-to-many relationships between entities without complex ORM configuration?

Define one-to-many relationships using foreign key properties on the child entity and navigation helpers—methods that query related records by matching foreign keys. Add a GetTodos() method on User that queries todos where UserId matches.

Can I model hierarchical parent-child data structures with foreign keys?

Yes. Hierarchical data uses the same foreign key pattern: add a parent ID property to the child entity and create navigation helpers to traverse up and down the tree, fetching nested structures efficiently through batch loading.

What's the difference between fetching related entities one-at-a-time versus batch loading?

One-at-a-time fetching executes a query per entity, creating N+1 overhead. Batch loading collects all IDs, queries once to retrieve all related records, then associates them locally—eliminating redundant database calls and improving latency.

Do I need an ORM mapping configuration to manage entity relationships?

No. Foreign key-based relationships with navigation helpers work without ORM configuration. You define the relationship structure directly on entities using foreign key properties and query methods, keeping setup minimal.