У меня есть отношение n: m с пользовательской таблицей соединения в TYPEORM.
Entity1
@Entity({ name: 'users' })
export class User extends BaseModel {
@PrimaryGeneratedColumn()
id!: number;
@Column({ type: 'varchar', length: 50 })
@IsNotEmpty()
username!: string;
@Column({ unique: true, type: 'varchar', length: 50 })
@IsEmail()
@IsNotEmpty()
email!: string;
@CreateDateColumn({ type: 'timestamp' })
createdAt!: Date;
@UpdateDateColumn({ type: 'timestamp' })
updatedAt!: Date;
@DeleteDateColumn({ type: 'timestamp' })
deletedAt!: Date;
@OneToMany(
(type) => UsersGameGroup,
(userGameGroup) => userGameGroup.user,
{ cascade: true }
)
usersGameGroups!: UsersGameGroup[];
}
Entity2
@Entity({ name: 'game_groups' })
export class GameGroup extends BaseModel {
@PrimaryGeneratedColumn()
id!: number;
@Column()
title!: string;
@OneToMany(
(type) => UsersGameGroup,
(userGameGroup) => userGameGroup.gameGroup,
{ cascade: true }
)
usersGameGroups!: UsersGameGroup[];
}
Entity3 a Таблица соединения
@Entity({ name: 'users_game_groups' })
export class UsersGameGroup extends BaseModel {
@PrimaryGeneratedColumn()
id!: number;
@Column({ type: 'int' })
userId!: number;
@Column({ type: 'int' })
gameGroupId!: number;
@ManyToOne(
(type) => User,
(user) => user.usersGameGroups,
{ onDelete: 'CASCADE' }
)
user!: User;
@ManyToOne(
(type) => GameGroup,
(gameGroup) => gameGroup.usersGameGroups,
{ onDelete: 'CASCADE' }
)
gameGroup!: GameGroup;
}
и я запрашиваю gameGroup
, чтобы получить пользователей.
const gg = await GameGroup.findOneOrFail(gameGroupID, {
select: ['id', 'title'],
relations: ['usersGameGroups', 'usersGameGroups.user']
});
const { id, title, createdAt, updatedAt, usersGameGroups } = gg;
но проблема здесь в том, что он вернет все столбцы пользователя. Все, что я хочу, это username
.
возвращаемый образец:
{
"meta": {},
"payload": {
"gameGroup": {
"id": 2,
"title": "game2",
"users": {
"id": 2,
"title": "game2",
"usersGameGroups": [
{
"id": 2, <-this too I don't need this but whatever
"userId": 1, <-this too I don't need this but whatever
"gameGroupId": 2, <-this too I don't need this but whatever
"createdAt": "2020-04-09T00:11:39.000Z", <-this too I don't need this but whatever
"updatedAt": null, <-this too I don't need this but whatever
"deletedAt": null, <-this too I don't need this but whatever
"user": {
"id": 1,
"username": "new1",
"email": "new1@gmail.com", <- I just need this
"createdAt": "2020-04-09T00:09:45.000Z",
"updatedAt": "2020-04-09T00:10:55.000Z",
"deletedAt": null
}
},
{
"id": 3, <-this too I don't need this but whatever
"userId": 2, <-this too I don't need this but whatever
"gameGroupId": 2, <-this too I don't need this but whatever
"createdAt": "2020-04-09T00:12:10.000Z", <-this too I don't need this but whatever
"updatedAt": null, <-this too I don't need this but whatever
"deletedAt": null, <-this too I don't need this but whatever
"user": {
"id": 2,
"username": "new2", <- I just need this
"email": "new2@gmail.com",
"createdAt": "2020-04-09T00:09:51.000Z",
"updatedAt": null,
"deletedAt": null
}
}
]
}
}
}
}
И если я сделаю запрос, вот так. Я включаю user
и его имя пользователя, например user.username
relations: ['usersGameGroups', 'usersGameGroups.user', 'user', 'user.username']
Я получаю сообщение об ошибке.
"Relation \"user\" was not found, please check if it is correct and really exist in your entity."
В raw SQL запрос выглядит примерно так .
SELECT
u.username
FROM
users u
JOIN
users_game_groups ugg
ON ugg.userId = u.id
JOIN
game_groups gg
ON gg.id = ugg.gameGroupId
WHERE gg.id = 2;
Я ожидаю такой ответ JSON.
"gameGroup": {
"id": 2,
"title": "game2",
"users": {
"id": 2,
"title": "game2",
"usersGameGroups": [
{
"user": {
"username": "new1",
}
},
"user": {
"username": "new2",
}
}
]
}
}
Спасибо! <3 </p>