有没有办法通过使用 Drupal json-api include 来降低前端的时间复杂度?

发布于 2025-01-09 07:46:13 字数 1816 浏览 0 评论 0原文

我目前正在处理 Drupal json-api 模块 并注意到输出的结构通过迫使前端开发人员重新格式化提供给他们的 json 输出,在前端强制出现 O(n^2) 时间复杂度问题,以便附件可以给我吗与其所属实体相同的对象。

示例

假设我列出了一堆类别及其要在前端使用的缩略图。 json 输出通常看起来像这样:

普通类别 json 结构

[
   {
      "uid":123,
      "category_name":"cars",
      "slug":"cars",
      "thumbnail":"example.com/cars.jpg"
   },
   {
      "uid":124,
      "category_name":"sports",
      "slug":"sports",
      "thumbnail":"example.com/sports.jpg"
   }
]

对于 drupal,缩略图似乎与创建 O(n^2) 的数据分开。例如:

我使用此端点发出 get 请求:

example.com/jsonapi/taxonomy_term/genre?fields[taxonomy_term--genre]=name,path,field_genre_image,vid&include=field_genre_image

结构从 drupal json api 模块返回的数据将类似于以下内容:

伪代码以提高可读性

{
   "data":[
      {
         "uid":123,
         "category_name":"cars",
         "slug":"cars",
         "relationships":{
            "thumbnail":{
               "id":123
            }
         }
      },
      {
         "uid":124,
         "category_name":"sports",
         "slug":"sports",
         "relationships":{
            "thumbnail":{
               "id":124
            }
         }
      }
   ],
   "included":[
      {
         "type":"file",
         "id":123,
         "path":"example.com/cars.jpg"
      },
      {
         "type":"file",
         "id":124,
         "path":"example.com/sports.jpg"
      }
   ]
}

drupal 输出的问题是我必须循环遍历数据,然后在数据循环中循环遍历包含并将每个缩略图附加到类别,从而导致前端的 O(n^2) 。

有没有一种方法可以让前端使用 drupal json 模块请求一个类别,以便像上面的普通 json 输出一样包含类别中的缩略图,而无需在前端重构 json api?

请注意,我不是 Drupal 开发人员,因此我可能使用的术语将被关闭。

I'm currently working with an output from the Drupal json-api module and have noticed that the structure of an output forces an O(n^2) time complexity issue on the front by forcing the front end developers to reformat the json output given to them so an attachment can me in the same object as the entity it belongs to.

Example

So let's say I'm listing a bunch of categories with their thumbnails to be used on the front end. What a json output would normally look like for that is something like:

Normal category json structure

[
   {
      "uid":123,
      "category_name":"cars",
      "slug":"cars",
      "thumbnail":"example.com/cars.jpg"
   },
   {
      "uid":124,
      "category_name":"sports",
      "slug":"sports",
      "thumbnail":"example.com/sports.jpg"
   }
]

With drupal it seems that thumbnails are in their own includes separate from data creating an O(n^2). For example:

I make a get request using this endpoint:

example.com/jsonapi/taxonomy_term/genre?fields[taxonomy_term--genre]=name,path,field_genre_image,vid&include=field_genre_image

The structure of the data returned from the drupal json api module is going to be similar to this:

pseudo code for better readability

{
   "data":[
      {
         "uid":123,
         "category_name":"cars",
         "slug":"cars",
         "relationships":{
            "thumbnail":{
               "id":123
            }
         }
      },
      {
         "uid":124,
         "category_name":"sports",
         "slug":"sports",
         "relationships":{
            "thumbnail":{
               "id":124
            }
         }
      }
   ],
   "included":[
      {
         "type":"file",
         "id":123,
         "path":"example.com/cars.jpg"
      },
      {
         "type":"file",
         "id":124,
         "path":"example.com/sports.jpg"
      }
   ]
}

The problem with the drupal output is that I have to loop through the data and then in the data loop loop through the includes and attach each thumbnail to the category causing an O(n^2) on the frontend.

Is there a way for the frontend to request a category using the drupal json module to contain the thumbnail in the category like the normal json output above without having to restructure the json api on the frontend?

Please note I am not a drupal developer so the terminology I might use will be off.

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

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

发布评论

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

评论(1

孤君无依 2025-01-16 07:46:13

JSON:API可以输出一个实体列表并包含另一个实体列表(可以有不同的类型)。每个实体都有 UUID,因此,如果您将索引应用于它们的 UUID,则访问它们可以是 O(logn) 甚至 O(0)

因此,您将有一个循环来解析每个包含的实体并存储和索引它们(如 SQLite ),或者简单地循环所有包含的实体,通过 UUID 构建 1 个数组键,值是实体的对象

JSON:API can output a list of entities and includes another list of entities (can have different types). each entity has UUID, so, accessing them can be O(logn) or even O(0) if you apply index to their UUID

So, you would have one loop to parse each of the included entity and store and index them (like SQLite), or simply loop over all included entities, build 1 array key by UUID and value is object of an entity

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