crucialdigital/metamorph

数据模型管理包

1.0.2 2024-04-29 08:54 UTC

This package is auto-updated.

Last update: 2024-09-28 08:17:13 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Metamorph 是一个 Laravel 扩展包,它实现了基于 mongodb 的数据模型系统。此扩展包为 API 开发提供了一套强大的动态模型管理系统。

在继续之前,请注意此扩展包旨在与 Laravel 和 Mongodb 一起用于 API 开发

目录

安装

您可以通过 composer 安装此扩展包

composer require crucialdigital/metamorph

您现在必须使用以下命令发布配置文件

php artisan vendor:publish --tag="metamorph-config"

使用方法

创建模型仓库和数据模型

使用 artisan 命令创建您的数据模型文件

php artisan metamorph:make-model Post -R

此命令将创建三个文件

  • Eloquent 模型文件
app/Models/Post.php

该 Laravel Eloquent 模型扩展自 CrucialDigital\Metamorph\BaseModel.php 类。您可以使用 Laravel 语法 创建模型并扩展 CrucialDigital\Metamorph\BaseModel.php。请实现 label()search() 方法,分别用于标记资源表单和定义查询的字段,默认搜索 term

php artisan make:model Post
  • 模型仓库文件
app/Repositories/PostRepositories.php

该仓库类负责创建模型查询构建器。您还可以使用 artisan 命令创建仓库

php artisan metamorph:make-repository PostRepository --model=Post
  • 数据模型表单文件
database/models/post.json

该 json 文件描述了处理模型的所有输入的表单。json 文件结构如下

{
    "name": "Post form",
    "ref": "post",
    "entity": "post",
    "readOnly": true,
    "inputs": [
        {
            "field": "name",
            "type": "text",
            "name": "Name",
            "description": "Name of the role",
            "placeholder": "Enter the name of the role",
            "required": true,
            "readOnly": true
        },
        {
            "field": "created_by",
            "type": "resource",
            "entity": "user",
            "name": "Create by",
            "description": "Role create by",
            "placeholder": "Select user",
            "required": false,
            "hidden": true,
            "readOnly": true
        }
    ],
    "columns": [
        "name",
        "user.name"
    ]
}

其中必填字段为 entityinputs

inputs 的每个条目都必须至少包含以下内容

* field: The input field
* name: The label of the input
* type: The input type in list below
    * text
    * number
    * tel
    * email
    * date
    * datetime
    * radio
    * boolean
    * select
    * textarea
    * url
    * selectresource
    * resource
    * geopoint

对于类型为 select 的输入,必须提供 options,它是一个包含 labelvalue 的对象数组

对于类型为 selectresourceresource 的输入,必须提供 entity 字段。该实体必须与应用程序中该模型的实体唯一

其他字段包括

  • required: 布尔值
  • hidden: 布尔值
  • readOnly: 布尔值
  • rules: 字符串(Laravel 请求规则管道分隔)
  • description: 字符串
  • placeholder: 字符串
  • min: 整数
  • max: 整数
  • unique: 布尔值
  • filters: 见下表

您可以为输入添加任何其他字段,您可以在前端应用程序中使用这些字段

将数据模型配置到 metamorph 配置文件中

要配置 metamorph 如何将模型映射到仓库、数据模型表单、控制器和路由,您必须在 metamorph 配置文件中分别指定模型和仓库部分的 Eloquent 模型和模型仓库。

示例

// config/metamorph.php
[
    ....
    'repositories' => [
         'post' => \App\Repositories\PostRepository::class,
         'user' => \App\Repositories\UserRepository::class
    ],
    'models' => [
         'post' => \App\Models\Post::class,
         'user' => \App\Models\User::class
    ]
    ...
]

运行您的数据模型

在您的 .json 文件中创建数据模型后,您必须使用 artisan 命令将其持久化。

php artisan metamorph:models

此 artisan 命令将数据模型持久化到数据库中。每次您在 database\models 中修改 .json 文件时,都应使用此命令更新数据。您可以使用 --name 参数指定 .json 文件的名称

请考虑在配置 MongoDB 数据库连接之前

进行 API 请求

Metamorph 提供了创建、读取、更新和删除的各种端点。可用的端点包括


模型条目列表请求参数


注意
field:过滤器的值可以是嵌套关系字段,例如 comments.user._id
协调器:表示使用 where(...) / orWhere(...) 的 andor 之一
分组:用于将过滤条件分组到子查询中;值必须以 and_or_ 之一开头

高级

全局中间件

要在所有 metamorph 路由中定义全局中间件,在 metamorph 配置文件 config/metamorph.php 中,将 middlewares 数组填充为您的中间件

//config/metamorph.php
...
'middlewares' => ['auth:sanctum', 'verified'],
...

如果您使用 Laravel Sanctum 进行身份验证,请务必添加中间件 auth:sanctum 以避免与 Metamorph 授权系统 发生问题

模型中间件

除了全局中间件之外,您无法在 metamorph 配置文件 config/metamorph.php 中为每个模型路由以及每个控制器动作定义单独的中间件,请将 model_middlewares 数组填充为您的中间件

//config/metamorph.php
...
'model_middlewares' => [
    'post' => [
        'App\Http\Middleware\EnsureUserIsOwner::class' => '*', //Protect all CRUD action with the middleware for posts
        'isOwner' => ['destroy', 'update'] //Prevent non owner from deleting and updating posts
    ]
],
...

策略

要使用策略授权模型控制器动作,在 metamorph 配置文件 config/metamorph.php 中,将 policies 数组填充与您的模型关联的策略动作

//config/metamorph.php
...
'policies' => [
    'post' => ['viewany', 'view', 'create', 'update', 'delete'],
    'user' => ['viewany', 'view', 'create', 'update', 'delete'],
    ...
 ],
...

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献指南

安全漏洞

有关如何报告安全漏洞,请参阅 我们的安全策略

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件