通过JSONB数据类型的订购,并续集字面

发布于 2025-01-22 17:14:42 字数 1384 浏览 2 评论 0原文

目前,我正在使用此结构查询所有项目:

const filteredItems = await allItems.findAll({
         where: conditions,
         include: associations,
         order: sortingCriteria,
         limit: limit,
         offset: offset,
      });

sortingcriteria当前具有以下结构:

const sortingCriteria = [
      ["price", "ASC NULLS LAST"],
      ["created_at", "DESC NULLS LAST"],
    ]

price and create_at <<代码> Allitems 表。在同一张表中,我有另一个名为详细信息具有 JSONB 每项。

我需要添加一个额外的排序标准,因此我按照JSONB中可以找到的一些特定数据来订购以下结构:

details = { 
            "sizes": {
               "height": 10,
               "width": 20,
            },
            "capacities": {
               "volume": 200,
               "weight": 2,
            }
          }

假设我想通过卷订购所有项目(以及pricecreate_at_at,我已经拥有)。我应该如何将其包括在sortingcriteria

使用JSONB代替嵌套表和连接的原因是表演。我已经尝试过,并且在一张桌子上所有的东西,对于巨大的桌子来说,性能大大提高。我将限制offset用于分页目的。我正在使用续集6.6.2。

I'm currently using this structure to query for all the items:

const filteredItems = await allItems.findAll({
         where: conditions,
         include: associations,
         order: sortingCriteria,
         limit: limit,
         offset: offset,
      });

The sortingCriteria currently has this structure:

const sortingCriteria = [
      ["price", "ASC NULLS LAST"],
      ["created_at", "DESC NULLS LAST"],
    ]

Being price and created_at fields of the allItems table. In the same table I have another field called details that has a JSONB per item.

And I need to add an extra sorting criteria, so I order by some specific data that can be found in that JSONB, with this structure:

details = { 
            "sizes": {
               "height": 10,
               "width": 20,
            },
            "capacities": {
               "volume": 200,
               "weight": 2,
            }
          }

Let's say I want to order by volume all the items (and also by price and created_at, as I already have). How should I include that in sortingCriteria ?

The reason of using a JSONB instead of nested tables and JOINs is performace. I have already tried and, having everything in one table, for huge tables, the performance increases tremendously. I use limit and offset for pagination purposes. I'm using Sequelize version 6.6.2.

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

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

发布评论

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

评论(1

和影子一齐双人舞 2025-01-29 17:14:42

对于order,用于嵌套的JSONB列,您必须使用sequelize.literal使用- &gt;&gt;&gt ;操作员。

order: [
  [sequelize.literal('details->>\'capacities\'->>\'volume\''), 'ASC']
]

希望它有帮助

For the order for the nested JSONb column, you'll have to use the sequelize.literal method with the ->> operator.

order: [
  [sequelize.literal('details->>\'capacities\'->>\'volume\''), 'ASC']
]

Hope it helps

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