/protean:repositoryfrom protean
Defines a Protean repository for persisting and loading DDD aggregates. Provides a default repository automatically; custom repositories add domain-specific queries or database optimizations.
Ships in protean. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Repositories are the gateway between your domain model and the persistence layer. In DDD, repositories represent a collection of aggregates: you add to the collection and get from it, without exposing database internals to the domain.
Protean provides a default repository for every aggregate automatically. You only need a custom repository when you want to add custom query methods or database-specific optimizations.
The examples on this page use this aggregate and these commands. The custom
repositories below filter on status, customer_id and assignee, so the
aggregate declares an index led by each of them:
@domain.aggregate(
indexes=[Index("status"), Index("customer_id"), Index("assignee")]
)
class Order:
order_id: Identifier(identifier=True)
customer_id: Identifier()
status: String(max_length=20, default="draft")
assignee: String(max_length=80)
items = HasMany("LineItem")
def place(self):
self.status = "placed"
def cancel(self):
self.status = "cancelled"
@domain.entity(part_of=Order)
class LineItem:
product_id: String(required=True, max_length=50)
quantity: Integer(default=1)
@domain.command(part_of=Order)
class PlaceOrder:
order_id: Identifier(identifier=True)
customer_id: Identifier(required=True)
@domain.command(part_of=Order)
class CancelOrder:
order_id: Identifier(identifier=True)
No repository class is needed. Protean generates one, and you use it directly:
domain.init(traverse=False)
with domain.domain_context():
repo = domain.repository_for(Order)
repo.add(Order(order_id="ORD-001", customer_id="CUST-1"))
order = repo.get("ORD-001")
@domain.repository(part_of=Order)
class OrderRepository:
def find_placed_orders(self):
return self.query.filter(status="placed").all()
def find_by_customer(self, customer_id):
return self.query.filter(customer_id=customer_id).all()
part_of with the aggregate class, @domain.repository(part_of=Order), or with a string reference, @domain.repository(part_of="Order"). A string resolves at init, so the aggregate may be defined after the repositoryadd() and get() for free; only write a custom repository when you need custom queriesadd() for both create and update: The repository's add() method handles both new and modified aggregates (collection semantics)get() to load by identifier: Loads the aggregate with all its children from the persistence storeself.query for filtering, sorting, and paging. self.find_by() loads one aggregate by its field values, self.find() runs a composable Q expression and returns one ResultSet page of matches, capped at the aggregate's limit (100 by default), with total carrying the full count. self.exists() returns a bool. Below these helpers sits a private data access object, kept for infrastructure work such as hard deletion and test teardown. Custom Queries names it and says when to use itdatabase option to lock a repository to a specific database type (default is "ALL")add() and get() from BaseRepository; only add new methodsEvery field a query method filters on needs an index. The identifier field and
any field marked unique=True already have one. For any other field, add an
Index; without one, check reports UNINDEXED_FILTER_PATH. Index String fields, not Text: an index on
a Text field makes check report UNBOUNDED_INDEXED_STRING:
@domain.aggregate(
indexes=[Index("category"), Index("is_active"), Index("price")]
)
class Product:
product_id: Identifier(identifier=True)
name: String(required=True, max_length=200)
category: String(max_length=50)
price: Decimal(precision=19, scale=4)
is_active: Boolean(default=True)
@domain.repository(part_of=Product)
class ProductRepository:
def find_by_category(self, category):
return self.query.filter(category=category).all()
def find_active(self):
return self.query.filter(is_active=True).all()
def find_affordable(self, max_price):
return self.query.filter(price__lte=max_price).all()
When you only need how many rows match, use count(). It issues a flat
COUNT without materializing entities, so it is cheaper than len(query.all()):
@domain.repository(part_of=Order)
class OrderRepository:
def open_count(self) -> int:
return self.query.filter(status="open").count()
Filter on whether a field is set with the isnull lookup:
# fragment
# Orders with no assignee (assignee IS NULL)
self.query.filter(assignee__isnull=True).all().items
# Orders that have an assignee (assignee IS NOT NULL)
self.query.filter(assignee__isnull=False).all().items
When you need the page of items but not the total match count, pass
with_total=False so the adapter can skip the separate count round-trip:
# fragment
items = self.query.filter(status="open").all(with_total=False).items
# fragment
# Wrong! Never persist entities directly
@domain.repository(part_of=LineItem) # LineItem is an entity, not an aggregate
class LineItemRepository:
pass
Instead: Persist at the aggregate level; child entities are synced automatically (Key rule 9).
# fragment
@handle(PlaceOrder)
def handle_place_order(self, command):
with UnitOfWork(): # Unnecessary! Handler has implicit UoW
order = Order(order_id=command.order_id, customer_id=command.customer_id)
domain.repository_for(Order).add(order)
Instead: Let the handler's own Unit of Work do it. A Unit of Work started inside
another one joins it, so the inner with UnitOfWork() opens no transaction of its
own: its commit and rollback defer to the handler's.
count(), all(with_total=False), and the isnull lookupaggregate: Repositories persist aggregatescommand-handler: Command handlers use repositories to load and persist aggregatesevent-handler: Event handlers load and persist their own aggregate through its repositoryentity: Entities enclosed within aggregates are persisted automatically via repositoriescheck and resolve what it reportsassets/repository_counting_and_nulls.py (opens in a new tab)assets/repository_custom.py (opens in a new tab)assets/repository_default.py (opens in a new tab)assets/repository_with_children.py (opens in a new tab)assets/repository_with_database.py (opens in a new tab)assets/repository_with_uow.py (opens in a new tab)references/anti-patterns.md (opens in a new tab)references/custom-queries.md (opens in a new tab)references/database-specific.md (opens in a new tab)references/default-repository.md (opens in a new tab)references/unit-of-work.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub proteanhq/protean --plugin protean39 skills in this plugin
/add-commandAdd a complete command flow to an existing aggregate. It creates a command class, a FastAPI API endpoint, and a command handler with repository logic. This is the primary workflow for adding write operations to a Protean domain. Use when the user wants to "add a command", "add an action", "add an operation", "expose a write endpoint", "create a command flow", "wire up a POST endpoint", "add a new operation to an aggregate", or describes an action like "I want users to be able to [verb] a [thing]" (e.g., "place an order", "register a user", "cancel a reservation"). This workflow composes the command, command-handler, and api-endpoint element skills./add-domain-service-flowBuild a domain service flow in Protean for business logic that spans two or more aggregates. Domain services are stateless, encapsulate cross-aggregate business rules, and run pre/post invariants for validation. They are used when business logic doesn't naturally belong to a single aggregate. Wire them into command handlers or application services for persistence. Use when the user asks to "add a domain service", "implement cross-aggregate logic", "coordinate multiple aggregates", "add a business operation spanning aggregates", "validate across aggregates", or when they describe scenarios like "placing an order checks inventory and reserves stock", "transferring funds between accounts", "scheduling involves both resource and calendar".Other plugins' skills for the same job