prooph / http-middleware

PSR compatible middleware to integrate prooph with a middleware dispatcher
http://getprooph.org/
BSD 3-Clause "New" or "Revised" License
11 stars 4 forks source link

Support for queries on standard projections #4

Open basz opened 6 years ago

basz commented 6 years ago

From @basz on March 16, 2017 15:37

on chat

I'm thinking bout updating the middleware component to add support for queries on standard projections. Would that be useful?

sounds good

It is possible to query standard projections. One QueryStandardProjection query could do it, but we would loose out on permissions etc.. so... quick and dirty

trait StandardQueryTrait {
 public function streamName(): string {
    return $this->metadata()['stream_name'];
 }

 public function result($state) {
   return $state;
  }
}
abstract class QueryStandardProjection extends Messaging\Query implements Messaging\PayloadConstructable  {
   use Messaging\StandardQueryTrait;
}

class StandardQueryHandler {
    private $projectionManager;

    public function __construct(ProjectionManager $projectionManager)
    {
        $this-> projectionManager = $projectionManager;
    }

    public function onEvent(QueryStandardProjection $query, Deferred $deferred)
    {
        // use projectionManager to retrieve state of $query->streamName()

      $deferred->resolve($query->makeResult($state));
    }
}

class MyQuery extends QueryStandardProjection {
   public function result($state) {
     return ValueObject::fromArray($state);
  }
}

routes

        [
            'name'            => 'query::something',
            'path'            => '/path',
            'middleware'      => [
                StandardQueryMiddleware::class,
            ],
            'allowed_methods' => ['OPTION', 'GET'],
            'options'         => [
                'values' => [
                    StandardQueryMiddleware::NAME_ATTRIBUTE => MyQuery::class,
                    StandardQueryMiddleware::STREAM_NAME => 'my_projection',
                ],
            ],
        ],

Which add a StandardQueryMiddleware.

It's just an rough idea... Looking for input

Copied from original issue: prooph/psr7-middleware#22

basz commented 6 years ago

@prolic @sandrokeil @codeliner thoughts?

I now understand Query's as used in the standard projections are not traditional queries but rather projections without persisted state.... So that might skew this a little... Thought I needed to point that out...

basz commented 6 years ago

From @prolic on March 29, 2017 3:55

I don't think it makes sense to have this as middleware. An event store query can use some time, it's more for answering some questions that arise from the business, where someone wants to know some stats f.e. and he is happy, when you give him the result the next few days. There is no need to have this exposed through the Api I think.