Skip to content

Latest commit

 

History

History

README.md

Read Model Documentation

The kraz/read-model library family provides a uniform API for querying read-only data from multiple backends: in-memory collections/arrays, Doctrine ORM/raw SQL, JSON-RPC APIs, and Elasticsearch. You write your query logic once against a common interface, then plug in any backend — including an in-memory one for fast, side-effect-free testing.

Packages

Package Description Install
kraz/read-model Core interfaces + in-memory implementation composer require kraz/read-model
kraz/read-model-doctrine Doctrine ORM & raw SQL backend composer require kraz/read-model-doctrine
kraz/read-model-json-rpc JSON-RPC 2.0 API backend (expects read-model exposed as API) composer require kraz/read-model-json-rpc
kraz/read-model-elastic-search Elasticsearch backend composer require kraz/read-model-elastic-search

Contents

Quick Example

// Define your read model once
class ProductsReadModel implements ReadDataProviderInterface
{
    use DoctrineReadDataProvider;

    public function __construct(private EntityManagerInterface $em) {}

    protected function createDataSource(): DataSource
    {
        $qb = $this->em->createQueryBuilder()
            ->select('r')
            ->from(Product::class, 'r');

        return new DataSource($qb);
    }
}

// Query it
$products = $readModel
    ->withQueryExpression(
        QueryExpression::create()
            ->andWhere(FilterExpression::create()->greaterThan('price', 10))
            ->sortBy('name', SortExpression::DIR_ASC)
    )
    ->withPagination(page: 1, itemsPerPage: 20)
    ->data();

// In tests, replace the whole read model with an in-memory stub
$readModel = new DataSource([
    ['id' => 1, 'name' => 'Widget', 'price' => 15],
    ['id' => 2, 'name' => 'Gadget', 'price' => 5],
]);