sourcing()

The primary entry point for creating and retrieving projection/aggregation instances.

Signature

sub sourcing(Sourcing::Projection:U $proj, *%ids) is export

Parameters

ParameterTypeDescription
$projSourcing::Projection:UThe projection or aggregation type (type object, not instance)
*%idsNamed argumentsValues for the projection's ID attributes (e.g., :id(42))

Returns

A fresh instance of the projection/aggregation with all events from the store applied. Each call creates a new instance — there is no instance caching.

How It Works

  1. Retrieves all events for the given identity from the store via get-events-after: -1
  2. Creates a fresh instance with the provided IDs and events
  3. Sets __current-version__ to the total number of events minus one
  4. Stores updated cached data via the plugin
  5. Returns the new instance

Example

# Create a new aggregation instance
my $account = sourcing BankAccount, :account-id(1);

# Create a projection instance
my $view = sourcing AccountView, :account-id(1);

# Multiple IDs
my $order = sourcing OrderLine, :order-id(1), :line-id(3);

Important Notes

Always Creates Fresh Instances

Each call to sourcing() creates a new instance and replays all events. It does not return cached instances. This ensures you always get a clean, consistent snapshot of the event stream.

See Also