TypeOrm - 自定义多对多不拉关系数据

rt_*_*rt_ 2 typeorm

因为我不想使用级联来更新连接表并且我想要自定义列,所以我创建了一个自定义的多对多关系。但是,当我查询关系时,它仅提供连接表中的值,而不提取关系数据。

用户

@Entity('user')
export class User {
  @PrimaryColumn()
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.user
  )
  following: UserArtistFollowing[];
}
Run Code Online (Sandbox Code Playgroud)

艺术家


@Entity('artist')
export class Artist {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.artist
  )
  usersFollowing: UserArtistFollowing[];

}
Run Code Online (Sandbox Code Playgroud)

用户艺术家正在关注


@Entity('userArtistFollowing')
export class UserArtistFollowing {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  userId!: string;

  @Column()
  artistId!: string;

  @ManyToOne(
    () => User,
    (user) => user.following
  )
  user!: User;

  @ManyToOne(
    () => Artist,
    (artist) => artist.usersFollowing
  )
  artist!: Artist;

  @CreateDateColumn()
  createdAt!: Date;

  @UpdateDateColumn()
  updatedAt!: Date;
}
Run Code Online (Sandbox Code Playgroud)

询问

    const user = await getManager()
      .getRepository(Models.User)
      .findOne({
        where: { id: id },
        relations: ['following'],
      });
Run Code Online (Sandbox Code Playgroud)

因此该查询仅提供 id、userId、artistId,但不提供 Artist 对象数组(这是我需要的数据)。

想法?

rt_*_*rt_ 8

经过进一步测试,结果表明您可以使用 find -> 关系来处理这些类型的自定义多对多关系。您只需为嵌套关系指定点分符号的关系。

      const user = await getManager()
        .getRepository(Models.User)
        .findOne({
          where: { id },
          relations: [
// you have to specify the join table first (following) to retrieve the columns in the join table
            'following',
// then you use dotted notation to get the relation from the join table
            'following.artist',
// another example of a deeply nested relation
            'favourites',
            'favourites.song',
            'favourites.song.supportingArtists',
            'favourites.song.supportingArtists.artist',
          ],
        });

Run Code Online (Sandbox Code Playgroud)

您还可以将 join 与嵌套的 leftJoinAndSelect 一起使用,但它更乏味。

    const user = await getManager()
      .getRepository(Models.User)
      .findOne({
        where: { id },
        join: {
          alias: 'user',
          leftJoinAndSelect: {
            following: 'user.following',
            artists: 'following.artist',
          },
        },
      });
Run Code Online (Sandbox Code Playgroud)

这是更新的实体

用户艺术家正在关注

@Entity('userArtistFollowing')
export class UserArtistFollowing {
  @PrimaryColumn()
  userId: string;

  @PrimaryColumn()
  artistId: string;

  @ManyToOne(
    () => User,
    (user) => user.following
  )
  user!: User;

  @ManyToOne(
    () => Artist,
    (artist) => artist.usersFollowing
  )
  artist!: Artist;

  @CreateDateColumn()
  createdAt!: Date;

  @UpdateDateColumn()
  updatedAt!: Date;
}

Run Code Online (Sandbox Code Playgroud)

艺术家

@Entity('artist')
export class Artist {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.artist
  )
  usersFollowing: UserArtistFollowing[];
}

Run Code Online (Sandbox Code Playgroud)

用户

@Entity('user')
export class User {
  @PrimaryColumn()
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.user
  )
  following: UserArtistFollowing[];
}

Run Code Online (Sandbox Code Playgroud)