我正在使用 typeorm 和 MySQL。我的实体存储不同类型的运动数据,例如 Football、Athletics->100m、200m、400m。
在 get 调用中,我希望按 gameType 分组数据,即田径或足球,并希望创建嵌套 JSON,如下所示
JSON 格式: {
"footBall":[
{"location":"use",gameType:"footbal", "goals":2,"result":"won"},
{"location":"use",gameType:"footbal", "goals":1,"result":"draw"}
],
"athletics":[
{"location":"poland",gameType:"athletics", gameSubType:"100m", "rank":2,"result":"first place"},
{"location":"ireland",gameType:"athletics", gameSubType:"200m", "rank":1,"result":"second place"}
]
}
我的实体类是这样的:
@Entity('competition')
export class CompetitionEntity {
@PrimaryGeneratedColumn()
id:Number
@Column({ type: 'timestamp', default: () => "CURRENT_TIMESTAMP"})
date:Date
@Column()
location:String
@Column()
rank:Number
@Column()
eventType: string
@Column()
minutes: number
@Column()
seconds: number
@Column()
miliseconds: number
@Column()
gameType: string //it can take values like athletics or football or baseball etc
@Column()
gameSubType: string //it can take value like 100m, 200m, relay etc
}
在服务中,我编写了这样的代码来获取竞争表中的所有数据
const qb = await getRepository(CompetitionEntity)
.createQueryBuilder('competition')
const competitions = await qb.getMany();
获取结果表单数据库后,我想按游戏类型列分组数据并将其作为嵌套 json 发送。
请指导我
谢谢你们