Skip to content

Repository files navigation

Read Model

CIPackagist VersionGitHub license

A domain-first Read Model library that provides a uniform API for querying read-only data from multiple backends. Define your query logic once; plug in Doctrine ORM, raw SQL, JSON-RPC APIs, or Elasticsearch — or use the built-in in-memory implementation for fast, dependency-free tests.

Installation

composer require kraz/read-model

Additional packages

Install any of the following packages which matches your infrastructure requirements:

PackageDescription
kraz/read-model-doctrineDoctrine ORM & raw SQL backend
kraz/read-model-json-rpcJSON-RPC 2.0 API backend (expects read-model exposed as API)
kraz/read-model-elastic-searchElasticsearch backend

Documentation

Full documentation lives in the docs/ directory:

Example usage

Raw SQL

useKraz\ReadModel\ReadDataProviderInterface;
useKraz\ReadModelDoctrine\DataSource;
useKraz\ReadModelDoctrine\DataSourceBuilder;
useKraz\ReadModelDoctrine\RawQueryReadDataProvider;
class ProductsReadModel implements ReadDataProviderInterface
{
use RawQueryReadDataProvider;
publicfunction__construct(privatereadonlyConnection$connection) {}
protectedfunctioncreateDataSource(): DataSource
{
returnnewDataSourceBuilder()
->withData(<<<'SQL' SELECT r.* FROM ( SELECT p.id, p.name, p.price, c.name AS category FROM product p JOIN category c ON c.id = p.category_id WHERE p.deleted_at IS NULL ) r /*#WHERE#*/ /*#ORDERBY_B#*/ORDER BY r.id ASC/*#ORDERBY_E#*/
SQL)
->create($this->connection);
} }

The /*#WHERE#*/ and /*#ORDERBY#*/ markers are replaced automatically with generated SQL when filters and sorts are applied. When nothing is applied they are removed cleanly.

ORM QueryBuilder

useKraz\ReadModelDoctrine\DoctrineReadDataProvider;
class ProductsReadModel implements ReadDataProviderInterface
{
use DoctrineReadDataProvider;
publicfunction__construct(privateEntityManagerInterface$em) {}
protectedfunctioncreateDataSource(): DataSource
{
$qb = $this->em->createQueryBuilder()
->select('r.id, r.name, r.price')
->from(Product::class, 'r');
returnnewDataSource($qb);
}
}

Querying

Both styles share the same query API:

// Filters and sorting$products = $readModel
->withQueryExpression(
$readModel->qry()
->andWhere($readModel->expr()->greaterThan('price', 10))
->andWhere($readModel->expr()->equalTo('category', 'tools'))
->sortBy('name', SortExpression::DIR_ASC)
)
->withPagination(page: 1, itemsPerPage: 20)
->getResult();
// $result->data — items on this page// $result->page — current page// $result->total — total matching rows

The query expression is serializable and can be stored and/or transferred as needed.

Acknowledgements

The library uses source code from Doctrine Collections which was modified to meet the required behavior.

License

This library is licensed under the MIT License. See the LICENSE file for details.

About

Read Model Domain implementation

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages