2017 © Pedro Peláez
 

library laravel-datastore

Laravel data store framework.

image

czim/laravel-datastore

Laravel data store framework.

  • Wednesday, June 27, 2018
  • by czim
  • Repository
  • 2 Watchers
  • 4 Stars
  • 351 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 9 Versions
  • 24 % Grown

The README.md

Latest Version on Packagist ![Software License][ico-license] Build Status Coverage Status, (*1)

Laravel Datastore

Basic datastore framework for building APIs., (*2)

This is intended to be combined with a (JSON-API) transformer/serialization layer., (*3)

This approach will allow you to separate responsibilities between serialization and transformation (the API representation layer) and data access abstraction., (*4)

Disclaimer

Currently a WIP under heavy development., (*5)

Version Compatibility

Laravel Package
5.4 - 5.8 1.1
6.0 2.0+
7.0 2.1

Installation

Via Composer, (*6)

``` bash $ composer require czim/laravel-datastore, (*7)


Add the `DataStoreServiceProvider` to your `config/app.php`: ``` php Czim\DataStore\Providers\DataStoreServiceProvider::class,

Publish the configuration file., (*8)

``` bash $ php artisan vendor:publish, (*9)


### Filtering If you intend to make use of the (default) filtering functionality of this package, you should add the [czim/laravel-filter](https://github.com/czim/laravel-filter) dependency: ```bash $ composer require czim/laravel-filter

Documentation

This data store package is split up, responsibility-wise, into two layers: the resource adapter and the data store itself., (*10)

A data store is responsible for retrieving and manipulating data. The resource adapter is an interface layer between the data store and the incoming and outgoing data (which can be JSON-API, or any custom transformation/formatting layer that you choose to implement)., (*11)

Data Stores

Available data stores:, (*12)

  • \Czim\DataStore\Stores\EloquentDataStore Simple Model data store., (*13)

  • \Czim\DataStore\Stores\EloquentDataStore Store to use if you have a repository (Czim\Repository\Contracts\BaseRepositoryInterface) available., (*14)

Resource Adapter

This package only provides a resource adapter set-up for JSON-API out of the box, expecting you to use czim/laravel-jsonapi. For any other implementation, you're encouraged to write your own adapter. This package has been designed to make it easy to swap out (custom) implementations, provided some familiarity with Laravel's container and provisioning., (*15)

Retrieval Context

The context for retrieving information (filters, sorting, pagination) is defined in interfaces. A RequestContext object may be filled with data in any way, and then passed into the data store to restrict or sort the results. No specific implementation is assumed for this., (*16)

Includes

By default, the resource (adapter) and client input determine the includes that will be used for eager loading. Eager loading is then performed on the basis of simple string relation names as with() parameters., (*17)

For more flexibility, it is possible to configure include decorators to further control eager loading. To make use of this:, (*18)

  1. Write an implementation of Czim\DataStore\Contracts\Stores\Includes\IncludeDecoratorInterface.
  2. Configure this class in the datastore.php configuration file:
    • Either as the default include decorator, in datastore.include.decorator.default,
    • or mapped for a specific class under datastore.include.decorator.model-map.<your model class>.

The decorate() method on the decorator will be fed a resolved array of dot-notated include strings, that can be manipulated and returned as desired., (*19)

Example:, (*20)

use Czim\DataStore\Contracts\Stores\Includes\IncludeDecoratorInterface;
use Illuminate\Database\Eloquent\Model;

class CustomIncludeDecorator implements IncludeDecoratorInterface
{
    public function setModel(Model $model)
    {
        // Ignore or store and use the model as desired.
    }

    public function decorate(array $includes, $many = false)
    {
        // Replace a specific include with a closure to eager load with specific columns.
        if (in_array('someRelation', $includes)) {
            $includes = array_diff($includes, ['someRelation']);
            $includes['someRelation'] = function ($query) {
                return $query->select(['id', 'title']);
            };
        }

        // Never eager load a specific relation.
        $includes = array_diff($includes, ['neverEagerLoadThis.relation']);

        // Always eager load some specific relation.
        $includes[] = 'translations';

        return $includes;
    }
}

Contributing

Please see CONTRIBUTING for details., (*21)

Credits

License

The MIT License (MIT). Please see License File for more information., (*22)

The Versions