czim/laravel-datastore

此包已被废弃且不再维护。没有建议的替代包。

Laravel数据存储框架。

2.1.2 2020-05-06 13:35 UTC

This package is auto-updated.

Last update: 2021-05-06 16:01:22 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status

Laravel Datastore

用于构建API的基本数据存储框架。

这旨在与(JSON-API)转换/序列化层结合使用。

这种方法允许您在序列化和转换(API表示层)以及数据访问抽象之间分离职责。

免责声明

目前处于重开发中的WIP状态。

版本兼容性

Laravel
5.4 - 5.8 1.1
6.0 2.0+
7.0 2.1

安装

通过Composer

$ composer require czim/laravel-datastore

DataStoreServiceProvider添加到您的config/app.php

Czim\DataStore\Providers\DataStoreServiceProvider::class,

发布配置文件。

$ php artisan vendor:publish

过滤

如果您打算使用此包的(默认)过滤功能,您应该添加czim/laravel-filter依赖项

$ composer require czim/laravel-filter

文档

从职责角度来说,此数据存储包分为两层:资源适配器和数据存储本身。

数据存储负责检索和操作数据。资源适配器是数据存储与入站和出站数据(可以是JSON-API,或您选择实现的任何自定义转换/格式化层)之间的接口层。

数据存储

可用数据存储

  • \Czim\DataStore\Stores\EloquentDataStore 简单模型数据存储。

  • \Czim\DataStore\Stores\EloquentDataStore 如果您有可用的存储库(Czim\Repository\Contracts\BaseRepositoryInterface),则应使用此存储。

资源适配器

此包默认仅提供JSON-API的资源适配器设置,并期望您使用czim/laravel-jsonapi。对于任何其他实现,您被鼓励编写自己的适配器。此包已设计为易于更换(自定义)实现,前提是对Laravel的容器和配置有一定了解。

检索上下文

检索信息的上下文(过滤、排序、分页)在接口中定义。可以以任何方式填充RequestContext对象,并将其传递给数据存储以限制或排序结果。对此没有特定的实现假设。

包含

默认情况下,资源(适配器)和客户端输入确定将用于预加载的包含。然后根据简单的字符串关系名称作为with()参数进行预加载。

为了提高灵活性,可以配置包含装饰器以进一步控制预加载。为了使用这个

  1. 编写一个实现 Czim\DataStore\Contracts\Stores\Includes\IncludeDecoratorInterface 的类。
  2. datastore.php 配置文件中配置此类
    • 要么作为默认的包含装饰器,在 datastore.include.decorator.default 下,
    • 或者为特定类映射,在 datastore.include.decorator.model-map.<你的模型类> 下。

装饰器上的 decorate() 方法将接收一个由点分隔的包含字符串数组,可以进行操作并按需返回。

示例

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 获取详细信息。

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 获取更多信息。