支持资源嵌套 [英] Support for resource nesting

查看:26
本文介绍了支持资源嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否可以配置 DataProvider/Resource/List 来支持 api/users/1/roles 等 REST url?

对于 RESTful API,获取某些父实体的子实体是非常常见的用例,但我无法弄清楚如何设置 React Admin 并实现这一点.我在 OData 规范后端使用自定义 DataProvider 构建.

我知道我可以通过过滤请求获取特定用户的角色 api/roles?filter={userId: 1} 或类似的东西,但我的问题是我的用户和角色是在多对多关系中,因此关系引用存储在数据透视表中.换句话说,我在角色表中没有关于用户的参考,所以我无法过滤它们.

我是否在监督某事,还是有一些我根本没有看到的方法?

REST API 内置于 OData 规范中,它支持与经典数据透视表(或中间)表的多对多关系.此表未在 API 中公开,但在上述 URL 中使用.所以我不能直接访问它作为资源.

用户 - 角色关系的架构看起来也很标准.

|----------||-----------||--------||用户 ||User_Role ||角色 ||----------||-----------||--------||编号 |-\ |身份证 |/-|身份证 ||登录 |\-|用户 ID |/|姓名 ||密码 ||角色 ID |-/|代码 ||----------||-----------||--------|

解决方案

TL;DR: 默认情况下,React Admin 不支持嵌套资源,您必须

为了支持嵌套资源,您必须编写自己的数据提供程序.

嵌套资源支持是一个循环 功能 请求,但当时核心团队不想处理这种工作量.

我强烈建议集结你的力量并编写一个外部数据提供者并像ra-data-odata提供者一样发布它.这将是一个很好的补充,我们将很荣幸为您提供该外部软件包.

I am wondering, is it possible to configure DataProvider/Resource/List to support REST urls like api/users/1/roles?

For RESTful API it is very common use case to get childs of certain parent entity, but I cant figure it how to setup React Admin and achieve this. I am using custom DataProvider build on OData spec backend.

I understand that I can get roles of certain user by filtered request on api/roles?filter={userId: 1} or something like that, but my issue is that my users and roles are in many-to-many relation so relation references are stored in pivot table. In other words, I dont have reference on user in roles table so I cant filter them.

Am I overseeing something or is there some approach which I simply dont see?

EDIT: REST API is built in OData spec and it supports many-to-many relations with classic pivot (or intermediate) table. This table is not exposed in API, but is utilized in urls like the one above. So I cant directly access it as a resource.

Schema for User - Role relations looks pretty standard too.

|----------|    |-----------|     |--------|
| USER     |    | User_Role |     | Role   |
|----------|    |-----------|     |--------|
| Id       |-\  | Id        |   /-| Id     |
| Login    |  \-| UserId    |  /  | Name   |
| Password |    | RoleId    |-/   | Code   |
|----------|    |-----------|     |--------|

解决方案

TL;DR: By default, React Admin doesn't support nested resource, you have to write a custom data provider.

This question was answered on a past issue: maremelab/react-admin#261

Detailed Answer

The default data provider in React Admin is ra-data-simple-rest.

As explained on its documentation, this library doesn't support nested resources since it only use the resource name and the resource ID to build a resource URL:

In order to support nested resources, you have to write your own data provider.

Nested resources support is a recurrent feature request but, at the time, the core team don't want to handle this load of work.

I strongly suggest to gather your forces and write an external data provider and publish it like ra-data-odata provider. It would be a great addition and we will honored to help you with that external package.

这篇关于支持资源嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆