amirsarfar / dyna-base
一个无头CMS,旨在创建动态定义的数据模型和API端点
Requires
- illuminate/support: ~7|~8
- web64/laravel-cli-colors: ^0.20.0
Requires (Dev)
- orchestra/testbench: ~5|~6
- phpunit/phpunit: ~9.0
This package is auto-updated.
Last update: 2024-01-29 03:43:36 UTC
README
关于Dyna
Dyna背后的理念是提供动态API创建API。这意味着您可以通过向某些API发送请求来从您的前端应用程序构建API。
您只需在Laravel应用程序中安装此软件包并运行其迁移即可!
数据库模式
我们的目标是摆脱数据库迁移和关系。为了实现这一点,我们提出了一个想法,即使用JSON对象定义模型(以前的迁移)。
本项目中有5个主要表:Types、Posts、Relations、Metas和LargeMetas。
Types表负责存储帖子类型的定义(与迁移相同)。它有一个唯一键、一个标题和一个JSON定义列。
Posts表存储id和type_id,这定义了它是哪种帖子以及属于哪种类型。
Metas表具有id、post_id、key和value,这定义了它属于哪个帖子,它是帖子哪个属性(key)以及其值。
LargeMetas表与Metas表相同,但其value列是longText而不是text,以存储更大的数据块。
Relations表有parent_key、child_key、parent_id、child_id,这定义了哪个帖子/帖子具有哪些帖子/帖子的父/子关系以及它所属的帖子属性(keys)。
推荐方法
创建您的帖子类型
您需要向 /api/dyna/v1/types 发送POST请求以创建类型。此端点接收三个参数
- key : 它是一个用于引用类型的唯一字符串。
- title : 它是一个字符串。
- definition : 它是类型属性的JSON数组。每个属性都是一个JSON对象。
类型的定义
每个属性至少包含两个键值对: name 和 type。name键的值被引用为该类型帖子的属性。type键的值决定了为该属性存储的数据类型。
每个属性还可以选择性地包含一个键,即 options,options键有一个对象值,描述了属性的扩展行为。以下是一些不同类型定义对象的示例。
支持的属性类型
- text
{ "name" : "<anything>", "type" : "text", "options" : { // TODO .... } }
- relation
{ "name" : "<anything>", "type" : "relation", "options" : { "type_id" : "<id of related type / if external table it should be 0>", "type_key" : "<key of related type / if external table it should be the table's name>", "count" : "<one/many>", "relation" : "<child/parent>" } }
类型创建示例
例如,如果您有一个blog_post类型和一个blog_post_comment类型,那么您发送到创建类型API(POST到 /api/dyna/v1/types)的数据可能如下所示(如果您正在复制粘贴,请记住删除以下JSON中的注释)
对于blog_post类型
{ "key" : "blog_post", "title" : "Blog Post", "definition" : [ { "name" : "title", "type" : "text" }, { "name" : "comments", "type" : "relation", "options" : { "type_id" : "2", "type_key" : "blog_post_comment", "count" : "many", // a post has many comments "relation" : "child" // comments are children of the post } }, { "name" : "author", "type" : "relation", "options" : { "type_id" : "0", // it is an external relations (different table from posts) "type_key" : "users", // table of the related entity "count" : "one", // a post has one author "relation" : "parent" // author is parent of the post } } ] }
对于blog_post_comment类型
{ "key" : "blog_post_comment", "title" : "Blog Post Comment", "definition" : [ { "name" : "content", "type" : "text" }, { "name" : "post", "type" : "relation", "options" : { "type_id" : "1", "type_key" : "blog_post", "count" : "one", // a comment belongs to one post "relation" : "parent" // post is parent of comments } }, { "name" : "author", "type" : "relation", "options" : { "type_id" : "0", // it is an external relations (different table from posts) "type_key" : "users", // table of the related entity "count" : "one", // a comment has one author "relation" : "parent" // author is parent of the comment } } ] }
类型的动态端点
在完成上述请求后,您将可以访问基于类型表数据动态创建的新API。(您可以通过运行 php artisan route:list 来检查它们)
本例的新API路由如下
博客文章简单操作(这些操作仅包含非关系属性)
GET /api/dyna/v1/blog_post # to get all of blog posts POST /api/dyna/v1/blog_post # to store a blog post GET /api/dyna/v1/blog_post/{blog_post} # to get a blog post by id POST /api/dyna/v1/blog_post/{blog_post} # to update a blog post DELETE /api/dyna/v1/blog_post/{blog_post} # to delete a blog post
博客文章内部关系操作
GET /api/dyna/v1/blog_post/{blog_post}/comments # to get all comments of a post
GET /api/dyna/v1/blog_post/{blog_post}/comments/{comments} # to get one comment of a post
POST /api/dyna/v1/blog_post/{blog_post}/add-comments # to add comments to the post (gets array of comments ids to add)
POST /api/dyna/v1/blog_post/{blog_post}/remove-comments # to remove comments of the post (gets array of comments ids to remove)
POST /api/dyna/v1/blog_post/{blog_post}/sync-comments # to sync comments of the post (gets array of comments ids to sync)
博客文章外部关系操作:尚未实现!
同样适用于博客文章评论类型。
安装
只需运行
composer require amirsarfar/dyna-base
然后运行迁移
php artisan migrate
待办事项
- 为文本属性添加选项
- 添加更多属性类型(数字、文件等)
- 为请求添加更多验证
- 为操作添加授权
- 添加外部关系路由