Define a Protean projector, a specialized event handler that maintains read-optimized projections (read models) by listening to domain events from one or more aggregates. Projectors are always associated with a projection via projector_for and use the @on decorator (alias for @handle) to process specific event types. Unlike generic event handlers, projectors explicitly target a projection and can listen to multiple stream categories or aggregates. Use when you need to build a read model, main...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Projector?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/proteanhq-projector)
---
name: projector
description: Define a Protean projector, a specialized event handler that maintains read-optimized projections (read models) by listening to domain events from one or more aggregates. Projectors are always associated with a projection via projector_for and use the @on decorator (alias for @handle) to process specific event types. Unlike generic event handlers, projectors explicitly target a projection and can listen to multiple stream categories or aggregates. Use when you need to build a read model, maintain a projection, create a denormalized view, sync query-side data from domain events, build cross-aggregate views, or when the user asks to "create a projector", "add a projection handler", "build a read model", "maintain a query view", "project events into a read model", "create a CQRS read side", or "populate a projection".
license: Apache-2.0
compatibility: Requires Python 3.11+, protean framework
metadata:
author: proteanhq
version: "0.1"
category: element
diagnostic_codes:
- PROJECTOR_HANDLES_ORPHANED_EVENT
---
# Projector
A projector keeps one projection up to date: it listens to domain events and writes the projection's records. This skill defines projectors: event sources, options and redelivery.
## How projectors differ from event handlers
| Aspect | Event Handler | Projector |
|--------|--------------|-----------|
| **Purpose** | Orchestrate side effects (notifications, syncing) | Maintain read-optimized projections |
| **Association** | `part_of=Aggregate` | `projector_for=Projection` |
| **Decorator** | `@handle(EventClass)` | `@on(EventClass)` (alias for `@handle`) |
| **Target** | Updates aggregates | Updates projections (read models) |
| **Event source** | `stream_category` | `aggregates` or `stream_categories` |
| **Import** | `from protean import handle` | `from protean.core.projector import on` |
## Basic structure
```python
from protean import Domain
from protean.core.projector import on
from protean.fields import Identifier, Integer, String
domain = Domain()
domain.config["event_processing"] = "sync"
@domain.event(part_of="Product")
class ProductAdded:
product_id: Identifier(required=True)
name: String(required=True)
stock_quantity: Integer(required=True)
@domain.aggregate
class Product:
name: String(required=True)
stock_quantity: Integer(default=0)
@classmethod
def create(cls, name, stock_quantity=0):
product = cls(name=name, stock_quantity=stock_quantity)
product.raise_(ProductAdded(
product_id=product.id, name=name, stock_quantity=stock_quantity
))
return product
@domain.projection
class ProductInventory:
product_id: Identifier(identifier=True, required=True)
name: String(required=True)
stock_quantity: Integer(default=0)
@domain.projector(projector_for=ProductInventory, aggregates=[Product])
class ProductInventoryProjector:
@on(ProductAdded)
def on_product_added(self, event: ProductAdded):
repo = domain.repository_for(ProductInventory)
inventory = ProductInventory(
product_id=event.product_id,
name=event.name,
stock_quantity=event.stock_quantity,
)
repo.add(inventory)
```
## Key rules
1. **projector_for required**: Projector must be associated with a projection: `@domain.projector(projector_for=MyProjection, ...)`
2. **aggregates or stream_categories required**: Must specify event source: `aggregates=[Product]` or `stream_categories=[Product.meta_.stream_category]`. A stream category is domain-qualified (`<domain>::<aggregate>`, such as `inventory::product`). A bare `"product"` gets the domain prefix, but it keeps its value when the aggregate is renamed or sets its own category. Derive it from the aggregate
3. **Use @on decorator**: Each handler method is decorated with `@on(EventClass)`, imported with `from protean.core.projector import on` (not from protean directly)
4. **Handler methods take self and event**: Signature: `def method_name(self, event: EventClass)`
5. **No return values**: Projector methods do NOT return values (CQRS pattern)
6. **Implicit UnitOfWork**: Each handler method runs within a UnitOfWork context automatically
7. **Multiple projectors per event**: Different projectors can process the same event into different projections
8. **Projections reject References and Associations**: `Reference` and Associations (`HasOne`/`HasMany`) are not allowed; basic field types and `ValueObject` fields are allowed (a `ValueObject` is stored as flattened shadow fields)
9. **Events can be redelivered**: Write each handler so a repeat is safe, or set `idempotent=True` (see Projector options)
## Projector options
| Option | Purpose | Required |
|--------|---------|----------|
| `projector_for` | The projection class this projector maintains | Yes |
| `aggregates` | List of aggregate classes whose events to listen to | Yes (unless stream_categories provided) |
| `stream_categories` | List of stream category names to listen to | Yes (unless aggregates provided) |
| `retries` | Max retry attempts on transient exceptions; overrides `server.transient_retry` | No |
| `backoff` | Retry delay strategy: `"exponential"`, `"linear"`, or `"fixed"` | No |
| `retry_exceptions` | Exception types (classes or dotted paths) treated as transient for retry | No |
| `sequential_by` | Field name on every handled event; events in the same stream category that share its value are processed one at a time (needs a partitioning broker and a stream subscription) | No |
| `subscription_type` | `"stream"` or `"event_store"` | No |
| `subscription_profile` | `"production"`, `"fast"`, `"batch"`, `"debug"`, `"projection"` | No |
| `subscription_config` | Custom config dict (messages_per_tick, max_retries, etc.) | No |
| `idempotent` | When `True`, each handler records a delivery marker in the same UnitOfWork as its write, so a redelivered event applies exactly once on a relational provider. On the in-memory provider the marker only skips a sequential redelivery; it is neither atomic nor concurrency-safe. For a cache-backed projection the option is a no-op, so the handler must write an idempotent upsert | No |
| `suppress_checks` | Diagnostic codes to suppress for this projector | No |
## Quick example: Multiple events in one projector
```python
@domain.event(part_of="Product")
class StockAdjusted:
product_id: Identifier(required=True)
new_stock_quantity: Integer(required=True)
@domain.projector(projector_for=ProductInventory, aggregates=[Product])
class ProductInventoryProjector:
@on(ProductAdded)
def on_product_added(self, event: ProductAdded):
repo = domain.repository_for(ProductInventory)
inventory = ProductInventory(
product_id=event.product_id, name=event.name,
stock_quantity=event.stock_quantity,
)
repo.add(inventory)
@on(StockAdjusted)
def on_stock_adjusted(self, event: StockAdjusted):
repo = domain.repository_for(ProductInventory)
inventory = repo.get(event.product_id)
inventory.stock_quantity = event.new_stock_quantity
repo.add(inventory)
```
## Quick example: Cross-aggregate projector
```python
from protean.fields import Decimal
@domain.aggregate
class User:
name: String(required=True)
@domain.aggregate
class Transaction:
user_id: Identifier(required=True)
amount: Decimal(precision=19, scale=4, required=True)
@domain.event(part_of=User)
class Registered:
user_id: Identifier(required=True)
name: String(required=True)
@domain.event(part_of=Transaction)
class Transacted:
user_id: Identifier(required=True)
amount: Decimal(precision=19, scale=4, required=True)
@domain.projection
class Balances:
user_id: Identifier(identifier=True, required=True)
name: String()
balance: Decimal(precision=19, scale=4, default=0)
@domain.projector(
projector_for=Balances,
aggregates=[User, Transaction],
)
class TransactionProjector:
@on(Registered)
def on_registered(self, event: Registered):
balance = Balances(user_id=event.user_id, name=event.name, balance=0)
domain.repository_for(Balances).add(balance)
@on(Transacted)
def on_transacted(self, event: Transacted):
balance = domain.repository_for(Balances).get(event.user_id)
balance.balance += event.amount
domain.repository_for(Balances).add(balance)
```
A redelivered `Transacted` adds the amount twice here, and a redelivered `Registered` fails because the record exists. Writing `on_registered` as an upsert (get the record, create it only if missing) fixes the second case. An upsert does not fix the first, because adding to a balance is not safe to repeat. Set `idempotent=True` on the projector (see the options table below) to have the framework skip an event it has already handled.
## Quick example: Multiple projectors for same event
```python
@domain.projection
class ProductCatalog:
product_id: Identifier(identifier=True, required=True)
name: String(required=True)
@domain.projector(projector_for=ProductInventory, aggregates=[Product])
class ProductInventoryProjector:
@on(ProductAdded)
def on_product_added(self, event: ProductAdded):
# Populate detailed inventory projection
...
@domain.projector(projector_for=ProductCatalog, aggregates=[Product])
class ProductCatalogProjector:
@on(ProductAdded)
def on_product_added(self, event: ProductAdded):
# Populate simplified catalog projection
...
```
## Quick example: Using stream_categories
```python
@domain.projection
class SystemMetrics:
metric: String(identifier=True)
count: Integer(default=0)
@domain.projector(
projector_for=SystemMetrics,
stream_categories=[
User.meta_.stream_category, # "<domain>::user"
Transaction.meta_.stream_category, # "<domain>::transaction"
],
)
class SystemMetricsProjector:
@on(Registered)
def on_registered(self, event):
...
```
Read the category from `meta_.stream_category` after the aggregate is registered. Registration prefixes it with the domain name.
## Common mistakes
### Using @handle instead of @on
```python
from protean import handle # Wrong import for projectors
@domain.projector(projector_for=ProductInventory, aggregates=[Product])
class MyProjector:
@handle(ProductAdded) # Works but not idiomatic
def on_product_added(self, event):
...
```
Instead: Use `@on` from `protean.core.projector`. It is an alias for `@handle` and reads better in a projector.
### Handling an event the domain never registers
```python
# fragment
@domain.projector(projector_for=ProductInventory, aggregates=[Product])
class MyProjector:
@on(ProductRenamed) # Wrong! ProductRenamed was renamed/removed
def on_product_renamed(self, event):
...
```
A projector `@on` handler wired to an event the domain does not register can
never be dispatched, usually a stale reference after a rename or removal.
`check` reports this as `PROJECTOR_HANDLES_ORPHANED_EVENT`. Register the
event, or remove the handler for the orphaned type.
### Projection not registered with domain
```python
# fragment
class MyProjection: # Wrong! Not registered with domain
product_id: Identifier(identifier=True)
@domain.projector(projector_for=MyProjection, aggregates=[Product])
class MyProjector:
pass
```
Instead: Register projection with `@domain.projection` decorator
## Detailed references
### Core Concepts
- [Single-Aggregate Projector](references/single-aggregate.md): Projector that listens to one aggregate's events
- [Cross-Aggregate Projector](references/cross-aggregate.md): Projector combining events from multiple aggregates
- [Multiple Projectors](references/multiple-projectors.md): Multiple projectors handling the same events
- [Error Handling](references/error-handling.md): Error handling patterns for projectors
- [Anti-patterns](references/anti-patterns.md): Common mistakes and how to avoid them
### Complete Examples
- [Single-Aggregate Projector](assets/projector_single_aggregate.py): Basic projector with one aggregate
- [Multiple Events Projector](assets/projector_multiple_events.py): Projector handling create and update events
- [Cross-Aggregate Projector](assets/projector_cross_aggregate.py): Projector combining data from multiple aggregates
- [Multiple Projectors](assets/projector_multiple_projectors.py): Two projectors maintaining different projections
- [Error Handling](assets/projector_error_handling.py): Error handling with handle_error classmethod
### Related Skills
- `event`: Events are the input to projectors
- `aggregate`: Aggregates produce the events that projectors consume
- `event-handler`: Generic event handlers (compare/contrast with projectors)
- `projection`: Projections are the output that projectors maintain
- [CQRS](https://docs.proteanhq.com/concepts/architecture/cqrs/): Projectors implement the query side of CQRS
## Verify your work
- [Verify with check](../../references/verify-with-check.md): run `check` and resolve what it reports