使用 Knex 在 Postgres 的 jsonb 列中搜索内部数组

问题描述 投票:0回答:0

我的表有数据列,其中包含具有以下结构的 jsonb 值。

Table: item_details

id: uuid,
data: jsonb

Item = {
    itemNumber: 73463,
    itemName: pqr,
    description: [
      {
      id: abc,
      type: xyz,
      units: 6
      },
      {
      id: def,
      type: stu,
      units: 9
      }
    ]
 }

当我收到一个描述对象时,我需要检查它是否存在于描述数组中。我正在将 Postgres 数据库与打字稿和 Knex 一起使用。我尝试了很多方法,但都没有用。

现在在我尝试过的所有方法中,下面的运行没有任何错误,但它返回未定义。

async findByDescription (description: Description) {
    const query = this.client<ItemRow>('item_details')
      .withSchema('item_schema')
      .select()
      .whereRaw('data->>? = ?', ['description' , description])
    const result = await query
    return result
  }

样本数据

    id                    data
x-y-z             {
                  itemNumber: 73463,
                  itemName: Train,
                  description: [
                    {
                    id: 263761,
                    type: Superfast,
                    units: 6
                    },
                    {
                    id: 345467,
                    type: Bullet,
                    units: 9
                    }
                  ]
                }

a-b-c           {
                  itemNumber: 83483,
                  itemName: pqr,
                  description: [
                    {
                      id: 263761,
                      type: Superfast,
                      units: 6
                      },
                      {
                      id: 445787,
                      type: Normal,
                      units: 18
                      }
                  ]
                }

我有一个描述对象

{
     id: 263761,
     type: Superfast,
     units: 6
   }

现在我需要在 data->description 数组中搜索这个对象。如果找到匹配项,则应返回包含该匹配项的行。在上面的例子中,它将是带有 id x-y-z 和 a-b-c

的记录
typescript postgresql knex.js
© www.soinside.com 2019 - 2024. All rights reserved.