【问题标题】:Typeorm jsonb array querytypeorm jsonb数组查询
【发布时间】:2021-01-29 05:46:44
【问题描述】:

我正在尝试查询 jsonb 数组。

列中的样本数据

[{"entityId": "1ebi", "entityType": "user", "mode": "editor"}, {"entityId": "F13t", "entityType": "team", "mode": "viewer"}]

实体定义

@Column({ name: 'details', type: 'jsonb' })
public details: {entityId: string, entityType: string, mode: string}[]; 

此查询似乎不起作用。我在看到这个 StackOverflow post 后尝试了这个。但我的数据是 JSON 数组。

 this.workbookPermissionRepository
      .createQueryBuilder('wp')
      .select()
      .where('wp.details ::jsonb @> :details', {
        details: {
          entityType: IPermissionEntityTypes.USER,
          entityId: user.slug,
          mode: Not(IPermissionSharingModes.NO_ACCESS),
        },
      })
      .printSql()
      .getMany();

我在 TypeOrm documentation 中没有找到任何东西。欢迎提出任何建议。

【问题讨论】:

    标签: postgresql nestjs jsonb typeorm


    【解决方案1】:

    查询 json 数组时 Postgres 的语法要求将查询的右侧括在括号中。在这种情况下,请尝试将您的查询包装在括号中,然后将其字符串化,如下所示:

     this.workbookPermissionRepository
      .createQueryBuilder('wp')
      .select()
      .where('wp.details ::jsonb @> :details', {
        details: JSON.stringify([{
          entityType: IPermissionEntityTypes.USER,
          entityId: user.slug,
          mode: Not(IPermissionSharingModes.NO_ACCESS),
        }])
      })
      .printSql()
      .getMany();
    

    这是一篇类似的帖子,可能有助于解释底层 Postgres 查询:
    Query for array elements inside JSON type

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 2019-07-02
      • 2022-01-22
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2019-11-29
      相关资源
      最近更新 更多