将嵌套数组转换为嵌套对象

格伦·马特斯(Glenn Mateus)

我正在使用typeorm从DB获取数据,我想列出所有问题以及每个问题的最新状态。

我的数据库有2个表:Issues和IssueStatus进入IssueStatus我将保存每个状态更改以及有关如何解决Issue的注释。

我的代码:

var issues = await issuesRepository
    .createQueryBuilder('issue')
    .select(['issue', 'history.status'])
    .innerJoin('issue.history', 'history')
    .leftJoin(
      'issue.history',
      'history2',
      'history.date < history2.date OR (history.date = history2.date AND history.id < history2.id)'
    )
    .where('history2.id IS NULL')
    .getMany();

我得到这个结果:

{
    "id": "5ff86c81-a202-4211-84f4-afe2d5c0fc0d",
    "cod": 1,
    "opendate": "2020-12-08T13:18:55.683Z",
    "closedate": null,
    "type": "systems",
    "subtype": "avance",
    "description": "first test",
    "mailto": "[email protected]",
    "hostname": "dskcwbti02",
    "username": "glenn.veloso",
    "solution": null,
    "supportby": null,
    "history": [
      {
        "status": 0
      }
    ]
  }

但是我想要这个:

{
    "id": "5ff86c81-a202-4211-84f4-afe2d5c0fc0d",
    "cod": 1,
    "opendate": "2020-12-08T13:18:55.683Z",
    "closedate": null,
    "type": "systems",
    "subtype": "avance",
    "description": "first test",
    "mailto": "[email protected]",
    "hostname": "dskcwbti02",
    "username": "glenn.veloso",
    "solution": null,
    "supportby": null,
    "status": 0
  }
艺术奥尔尚斯基

您可以像下面这样进行创建,但是您需要使用别名覆盖所有必填字段select

var issues = await issuesRepository
    .createQueryBuilder('issue')
    .select(['issue', 'history.status AS status']) // <- changes here
    .innerJoin('issue.history', 'history')
    .leftJoin(
      'issue.history',
      'history2',
      'history.date < history2.date OR (history.date = history2.date AND history.id < history2.id)'
    )
    .where('history2.id IS NULL')
    .getRawMany(); // <- changes here

我的意思是

您将需要使用中的别名覆盖所有必填字段 select

.select([
  'issue.id AS id',
  'issue.cod AS cod',
  'issue.opendate AS opendate',
...
...
...
  'issue.solution AS solution',
  'issue.supportby AS supportby',
  'history.status AS status'
])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章