Search code examples
.netcqrsevent-sourcingeventstoredb

EventStore Subscribing to a stream for a category


I have started creating a test application in .Net, that uses EventStore by Greg Young as the backing store for CQRS/ES.

In order to make it easy to load up a full aggregate, I save to a stream with a name of "agg-123". For example for a product aggregate with an id of 553, there would be a stream named "product-553". And then same for an "Order" aggregate, the stream would be named "order-123".

From the rehydrating and saving of events this works well.

I am now attempting to create a listener that will listen to certain streams to then populate a query database. The subscribe methods that I see can only seem to subscribe to "order-123", or "all". I can't see how I would subscribe to "product-" or "order-", or both.

I imagine that either

  • I have missed the point with stream names, and have named them wrong
  • Have missed a way to choose it, something like "product-*"
  • It is expected to subscribe to "all" and filter out what you aren't interested in, although this gives the problem that it also sends all of the "stats" events

Anyone any advice?


Solution

  • In your projections you can use fromCategory(). EventStore categorises each stream by the name of the stream up to the first '-'. So your 'order-123' and 'order-456' streams are both in the 'order' category.

    So you can do something like:

    fromCategory('order')
      .whenAny(function(s,e) {
        linkTo('orders',e);
      });
    

    This will project all order related events from all order aggregates into a single 'orders' stream that you can subscribe.

    You'll need to ensure that the category projections are running. (It's been a while since I've used EventStore, it was back when projections were in beta and weren't enabled by default, not sure if things are the same in the latest versions)