react-admin dataprovider 指定一个自定义字段作为主键 - 一个未命名为“id”的字段

发布于 2025-01-12 22:07:25 字数 247 浏览 1 评论 0 原文

我在react-admin ra-data-hasura中使用hasura数据提供程序(但我确信它或多或少与ra-data-graphql相同) )

我的实体的主键的名称与 id 不同 是否可以将未命名的字段设置为主要字段id 例如:MyEntityId

是否可以按资源指定它(因为每个表都可以有自己的名称模式) )或全局

I'm using the hasura data provider in react-admin ra-data-hasura (but i'm sure its more or less the same thing of ra-data-graphql)

My entity has their primary keys that are named differently from id
Is it possible to set a field as primary that is not named id Eg: MyEntityId

Is it possible to specifiy it resource by resource (because each table could have its own name pattern) or globally

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凤舞天涯 2025-01-19 22:07:25

亚历克斯,
您在文档中找到了答案: https://marmelab.com/react-admin/FAQ.html#can-i-have-custom-identifiersprimary-keys-for-my-resources

你必须解决这是在 dataProvider 级别。
您必须实现自己的 dataProvider 的方法,将您的 MyEntityId 字段映射到react-admin 所需的 id 字段。

例如(实现getList方法):

const myDataProvider = {
    getList: (resource, params) => {
        return fetch(API_URL)
               .then(({ json }) => ({
                    data: json.map((record) => ({ id: record.MyEntityId, ...record })),
                    total: parseInt(json.total, 10),
                })
        );
    }
   //...
}
json.map(record => ({"id": record.userID, ...record})),

Alex,
You have that answer in the docs: https://marmelab.com/react-admin/FAQ.html#can-i-have-custom-identifiersprimary-keys-for-my-resources

you have to resolve this at the dataProvider level.
You have to implement your own dataProvider's methods that mapped your MyEntityId field to the id field required by react-admin.

For example (implementing getList method):

const myDataProvider = {
    getList: (resource, params) => {
        return fetch(API_URL)
               .then(({ json }) => ({
                    data: json.map((record) => ({ id: record.MyEntityId, ...record })),
                    total: parseInt(json.total, 10),
                })
        );
    }
   //...
}
json.map(record => ({"id": record.userID, ...record})),
郁金香雨 2025-01-19 22:07:25

是的,您可以自定义缓存 IDtypePolicies 选项中的每种类型使用 keyFields href="https://www.apollographql.com/docs/react/caching/cache-configuration/#configuration-options" rel="nofollow noreferrer">InMemoryCache 构造函数

const client = new ApolloClient({
  ...
  cache: new InMemoryCache({
    typePolicies: {
      MyEntity1: {
        keyFields: ["MyEntityId"],
      },
      MyEntity2: {
        keyFields: ["MyEntityId"],
      },
      ...
    }
  }),
  ...
})

buildHasuraProvider({ client });

Yes, you can customize the cache IDs using the keyFields for each type in the typePolicies option of the InMemoryCache constructor:

const client = new ApolloClient({
  ...
  cache: new InMemoryCache({
    typePolicies: {
      MyEntity1: {
        keyFields: ["MyEntityId"],
      },
      MyEntity2: {
        keyFields: ["MyEntityId"],
      },
      ...
    }
  }),
  ...
})

buildHasuraProvider({ client });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文